Using a temporary variable once in Oracle SQL Developer - sql

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?

A bit of a fudge but if you want the prompt for substitution variables then you can use bind variables but just populate them using substitution variables like this:
(Run it as a script using F5 and not as a statement using Ctrl+Enter)
VARIABLE list_length NUMBER;
VARIABLE start_value NUMBER;
BEGIN
:list_length := &ll;
:start_value := &sv;
END;
/
SELECT LEVEL + :start_value
FROM DUAL
CONNECT BY LEVEL <= :list_length
MINUS
SELECT ID_TX
FROM ATTR
WHERE ID_TX BETWEEN :start_value + 1 AND :start_value + :list_length;
Otherwise, just use bind variables (i.e. the query at the bottom of the script).

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.

Try to use '&&' instead of one '&'.
If a single ampersand prefix is used with an undefined variable, the value you enter at the prompt is not stored.So once the alue is substituted,variable is discarded and remains undefined. If the variable is referenced twice, even in the same statement, then you are prompted twice.
If you use '&&', the value is stored and hence you will be prompted only one.

Related

How to enable multiple variables to be searched when having a Colon search box

Unable to make multiple search with = :variable.
I have a Select statement where I want to pull data, to do it in the most efficient way I have many places to gather the data from therefore I want null unless otherwise stated.
SELECT * FROM table WHERE (
(:nameColmn is null or nameColmn = :nameColmn)
AND
(:nameColmn2 is null or nameColmn2 = :nameColmn2)
AND
(:nameColmn3 is null or nameColmn3 = :nameColmn3)
)ORDER BY variable desc
there are 9 in the one I actually have, however it's the same.
A box prompts up where I can fill my data unless nothing is filled it stays blank (null)
I need to be able to add more then 1 variable, such as when doing
( nameColmn IN ('xyz','xyy','xyx'))
However this does not prompt a box. as they are set.
If you want to use Bind variable with multiple values, it can be passed as CSV string and split into rows.
SELECT * FROM yourtable
WHERE namecolmn IN ( SELECT regexp_substr(:vals,'[^,]+',1,level)
FROM dual CONNECT BY
regexp_substr(:vals,'[^,]+',1,level) IS NOT NULL
)
Note that the argument to this (:vals) should be xyz,xyy,xyx, i.e there can't be quotes in between.
Another option is to use Substitution variable (&vals), which will also prompt you for input and you can pass 'xyz','xyy','xyx' to get the desired result.
SET VERIFY OFF --diables substitution output.
SELECT * FROM yourtable
WHERE namecolmn IN (&vals);

How to replace where clause dynamically in query (BIRT)?

In my report query I have a where clause that needs to be replaced dynamically based on the data chosen in the front end.
The query is something like :
where ?=?
I already have a code to replace the value - I created report parameter and linked to the value ? in the query.
Example:
where name=?
Any value of name that comes from front end replaces the ? in the where clause - this works fine.
But now I need to replace the entire clause (where ?=?). Should I create two parameters and link them to both the '?' ?
No, unfortunately most database engines do not allow to use a query parameter for handling a dynamic column name. This is for security considerations.
So you need to keep an arbitrary column name in the query:
where name=?
And then in "beforeOpen" script of the dataset replace 'name' with a report parameter value:
this.queryText=this.queryText.replace("name",params["myparameter"].value);
To prevent SQLIA i recommend to test the value of the parameter in this script. There are many ways to do this but a white list is the strongest test, for example:
var column=params["myparameter"].value;
if (column=="name" || column=="id" || column=="account" || column=="mycolumnname"){
this.queryText=this.queryText.replace("name",column);
}
In addition to Dominique's answer and your comment, then you'll just need a slightly more advanced logic.
For example, you could name your dynamic column-name-value pairs (column1, value1), (column2, value2) and so on. In the static text of the query, make sure to have bind variables for value1, value2 and so on (for example, with Oracle SQL, using the syntax
with params as (
select :value1 as value1,
:value2 as value2 ...
from dual
)
select ...
from params, my_table
where 1=1
and ... static conditions....
Then, in the beforeOpen script, append conditions to the query text in a loop as needed (the loop left as an exercise to the reader, and don't forget checking the column names for security reasons!):
this.queryText += " and " + column_name[i] + "= params.value" + i;
This way you can still use bind variables for the comparison values.

Printing characters one by one from a string(VARCHAR2) oracle sql without using plsql and also without using dual

I am learning SQL using ORACLE 11g. How to print a string(comes from a SELECT query), character by character in ORACLE SQL, without using dual and also without using PLSQL? Here is the sample string:
'MANOJ'
and the output should be like this:
M
A
N
O
J
I tried using LEVEL,CONNECT BY but they are not working in isqlplus. Please help!
It is a simple use of SUBSTR and CONNECT BY LEVEL. Have a look at How to split string into rows.
For example,
SQL> SELECT SUBSTR('MANOJ', level, 1) str
2 FROM dual
3 CONNECT BY LEVEL <= LENGTH('MANOJ')
4 /
S
-
M
A
N
O
J
SQL>
Not sure what you mean by "not using DUAL table", but the dual table above is just used to create the sample data for demonstration. In your case, you could use the column name instead of hard-coding the value, and you could use a sub-query in place of the dual table if your value is a result of a sub-query.

ORACLE substitute variable in IN statement

I have simple query like
SELECT * FROM temp t WHERE t.id IN (:IDs)
When executed, it prompts me (Oracle SQL Developer) for entering value if IDs variable.
When I enter for example 169, everything runs smoothly, but when I try to enter multiple IDs, like 169,170,171, I get error Invalid Number even while putting it into ''.
I'm used to working with MS SQL and MySQL, so this is little confusing to me.
Anyone any suggestions.
The problem is the varying-IN list. In SQL Developer, when you are prompted to enter the value for the bind variable, you are simple passing it as 169,170,171 which it is not considering as a set of values.
What you could do is, have multiple binds -
SELECT * FROM temp t WHERE t.id IN (:ID1, :ID2)
When prompted, enter value for each bind.
UPDATE Alright, if the above solution looks ugly, then I would prefer the below solution -
WITH DATA AS
(SELECT to_number(trim(regexp_substr(:ids, '[^,]+', 1, LEVEL))) ids
FROM dual
CONNECT BY instr(:ids, ',', 1, LEVEL - 1) > 0
)
SELECT * FROM temp t WHERE it.d IN
(SELECT ids FROM data
)
/
If you put them into ", you get error. Oracle doesn't accept ". You should use just numbers without ".
i.e: (169,170,171,...)
You can define a substitution variable as an array like so:
define IDS = (169,170,171);
and then use it like so:
SELECT * FROM temp t WHERE t.id IN &IDS;

Generate range of strings

Hi, everyone.
I need you help with this problem.
I need to create a bunch of serial numbers in one of my tables and for that I want to use stored procedure. So I want to pass FirstSN and LastSN as parameters to the SP and it inserts N records into my table. A serial number consists of prefix and an incremental part.
For example, I send SN0001 as FirstSN and SN0100 as LastSN and it should insert the following:
SN0001
SN0002
SN0003
...
SN0099
SN0100
How can I do that without using loops?
P.s. I am using oracle 11.2.0
select 'SN' || lpad(lvl, length('100')+1, '0') from (select level lvl from dual connect by level <= 100);