I have a query roughly like this:
select *
from A_TABLE
where A_COLUMN = '&aVariable'
union
select *
from A_TABLE
where B_COLUMN = '&aVariable';
But when I run it, SQL Developer prompts me for the variable twice, even though it's the same variable.
If there's a way to make it prompt only once for a variable that is used twice, how do I do it?
I do not want to use a script, it must be a single executable query.
You can bypass the prompts for values associated with substitution variables by passing values to parameters in a command file through the START command. You do this by placing an ampersand (&) followed by a numeral in the command file in place of a substitution variable.
Use the double-ampersand (&&) if you want to reuse the variable value vvithout prompting the user each time.
This happens because when you define variables this way, the value is not stored anywhere. The variable is just substituted by the value and the value is discarded, so if the variable appears again, SQL Developer will prompt for a value again.
You can use substitution variable in select statement, order by clause or where condition. Select employee_id,First_name,job_id from Employees where employee_id=&Emp_no; The above query will ask input for Employee_id in box format. User needs to give employee_id to fetch the data.
As I was forming this post, I figured out how to do it:
:a_var
select *
from A_TABLE
where A_COLUMN = :a_var
union
select *
from A_TABLE
where B_COLUMN = :a_var;
SQL Developer will then prompt for a bind variable, you can enter it and hit apply.
select *
from A_TABLE
where A_COLUMN = '&aVariable'
union
select *
from A_TABLE
where B_COLUMN = '&&aVariable';
Notice how the 2nd (and subsequent) variables will use double-ampersand (&&)
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