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) : '
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With