Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept only restricted characters using accept in oracle

Tags:

sql

oracle

i need to accept only Y/N for is_master variable declare below.

For example if i use the below statement in SQL script the user can enter any character.

I want to restrict user to enter only y Y n or N

Accept is_master prompt ' Is the user a Master user (Y/N)  : '
like image 203
ponds Avatar asked Oct 21 '10 06:10

ponds


People also ask

How do you handle special characters in Oracle query?

Answer: Oracle handles special characters with the ESCAPE clause, and the most common ESCAPE is for the wildcard percent sign (%), and the underscore (_). For handling quotes within a character query, you must add two quotes for each one that is desired.

How do I escape special characters in Oracle SQL query?

Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.


2 Answers

Inspired by an earlier answer from Tony Andrews, if you have an external script called accept_y_n.sql:

accept answer prompt '&question (Y/N): ' format A1

set verify off
set termout off

column script new_value v_script
select case
    when '&answer' in ('Y','N') then ''
    else 'accept_y_n'
    end as script
from dual;

set termout on

@&v_script.

... then from a real script you can loop to get the response in the required format:

define question='Is the user a Master user'
@accept_y_n
define is_master=&answer

select '&is_master' as is_master from dual;

It'll keep prompting until you get a Y or an N, and you can use the response later.

Is the user a Master user (Y/N): A
Is the user a Master user (Y/N): 1
Is the user a Master user (Y/N):
Is the user a Master user (Y/N): Y

I
-
Y
like image 83
Alex Poole Avatar answered Sep 24 '22 19:09

Alex Poole


You can use the format keyword after accept to determine the format the user needs to follow as it is defined here. However I do not see a Y/N-like format among format models. With A1 format you restrict the input to be only 1 character.

According to this you can do something with the formats of change keyword but I have no experience with it.

like image 32
rics Avatar answered Sep 23 '22 19:09

rics