Junior Dev Junior Dev - 7 months ago 24
SQL Question

Using a temporary variable once in Oracle SQL Developer

I have a requirement for a query. It needs to select every number from a list that IS NOT present in a column. Currently, I have this working fine. This query returns every number between

1833
and
2000
that is not present in the
ATTR
table.

SELECT LEVEL + 1833
FROM DUAL
CONNECT BY LEVEL <= (2000 - 1833)
MINUS
SELECT ID_TX
FROM ATTR
WHERE ID_TX BETWEEN 1834 AND 2000;


What I want to do is make this as user-friendly as possible. To do that, I can enter two variables, a
STARTING_ID
and
LIST_LENGTH
. Now my query looks like this.

SELECT LEVEL + &STARTING_ID
FROM DUAL
CONNECT BY LEVEL <= &LIST_LENGTH
MINUS
SELECT ID_TX
FROM ATTR
WHERE ID_TX BETWEEN &STARTING_ID AND &STARTING_ID + &LIST_LENGTH;


At first, I was using
&&
, but then I could only use this query once.
UNDEFINE
couldn't be placed in the code block, and wasn't cleaning my variables anyway. Now my issue is that it considers each
&
variable to be different, so it's making the user enter 5 variables instead of 2.

How do I make it where I'm still using temporary variables (with or without the popup to enter the variable), but the person running the query only has to enter two values
1833
and
67
?

Answer

How about using : as prompt ?

SELECT LEVEL + :STARTING_ID
FROM   DUAL
CONNECT BY LEVEL <= :LIST_LENGTH
MINUS
SELECT ID_TX
FROM   ATTR
WHERE  ID_TX BETWEEN :STARTING_ID AND :STARTING_ID + :LIST_LENGTH;

This employs the concept of bind variables. Thus, user could enter the necessary values and proceed.

enter image description here

enter image description here