This question already has answers here:
PL/SQL - Use "List" Variable in Where In Clause
(3 answers)
How to load a large number of strings to match with oracle database?
(3 answers)
Closed 10 months ago.
EDIT: The question is how to query all row with input stringarray variable, NOT how to delarce/assign stringarray variable.
I'm newbie oracle user.
Table_example
ID
Name
1
A
2
B
3
C
4
D
Query
select * from Table_example
where id in ($var);
$var = '1','2' => query 2 rows.
$var = ...? => list all row. ($var input from textbox, please don't suggest $var = select id from Table_example)
I tried:
select * from Table_example
where (
id in ($var)
or
$var = 'all'
);
$var = 'all' => list all row OK
$var = '1','2' => got ORA-00920 error, cannot query.
Please help me how to bypass stringarray variable to list all row, or how to check if stringarray variable ($var) is null or $var = 'all'. (please don't use function or procedure, just query).
Thanks and best regard.
Loc Nguyen.
Related
This question already has answers here:
SQL IN Clause 1000 item limit
(5 answers)
How to pass values to IN operator dynamically?
(1 answer)
PL/SQL - Use "List" Variable in Where In Clause
(3 answers)
how to select a list of 10,000 unique ids from dual in oracle SQL
(4 answers)
Closed 2 years ago.
I am trying to build an SQL query with around 100.000 given values.
For example:
Given values = [1, 2, 4, 5, 30, ...]
And I want to select all elements with an ID matching one of the elements.
I tried it like this:
SELECT x FROM y WHERE
someOtherColumn = 'test'
AND (
id = 1
OR id = 2
OR id = 4
OR id = 5
OR id = 30
-- ...
);
And like this:
SELECT x FROM y WHERE
someOtherColumn = 'test'
AND (
id IN (1, 2, 4, 5, 30, ...) --- 1000 values per IN clause
OR id IN (...)
-- ...
);
Both give me the same Error:
ORA-00913: Zu viele Werte
Is there another way to do this?
This is not a 1000 Limit in IN clause issue!
Where do the values for your IN clause originate? Load your values into a reference table, then select that in your IN clause.
create ref_table (ref_value varchar2(20);
insert into ref_table ('1');
insert into ref_table ('2');
SELECT x FROM y WHERE
someOtherColumn = 'test'
AND (
id IN (select ref_value from ref_table);
This question already has answers here:
Oracle select most recent date record
(4 answers)
Closed 5 years ago.
I want to select the latest record from a table as based on a date field (crtn_dt). The query below does not work. Does anyone have an idea how it should be fixed?
select * from parcels
order by crtn_dt desc
where rownum = 1
You'd need to order data in the subquery and filter them in an outer query.
select *
from (
select *
from parcels
order by crtn_dt desc
)
where rownum = 1
order by clause is among last operations to perform.
What your query does, apart from being semantically incorrect, it returns one (thanks to rownum = 1 predicate) arbitrary row, and then applies order by clause to that one row.
I am facing an error while incorporating an Oracle query in a SSRS report.
The below mentioned query is supposed to give output from a table TABLE1 on the basis of values selected for twofilters for VAR1 and VAR2. VAR1 must be selected by the user. VAR2(i.e. a date field) by default will show all the results. VAR2 will show results for a particular date if that is selected by the user.
Plesae note that this query runs just fine in Oracle. However, when I execute this code in SSRS, it throws the below mentioned error: "ORA-00920: invalid relational operator"
Would really appreciate any help here.
Select Distinct
VAR1,
VAR2,
AMT_FIELD
From TABLE1 SPF
Where VAR1 = (:prm1)
And (CASE
WHEN ((:PRM2) IS NOT NULL AND VAR2 = (:PRM2)) THEN 1
WHEN ((:PRM2) IS NULL AND VAR2 >= (SELECT min(VAR2) FROM TABLE1 NI Where NI.VAR1 = (:prm1))) THEN 1
ELSE 0
END) = 1 ;
Thanks
This question already has answers here:
Working with dates in Oracle SQL
(3 answers)
Closed 6 years ago.
I am very new to using PL/SQL and I have created a procedure but I cannot figure out the correct syntax in order to get the current month and year's data and another cursor to get the data from exactly 1 year prior:
create or replace procedure data(acc integer, month integer, year integer)
as
Cursor c1 is
select usage
from bill
where account =acc_num and to_char(BILL_DATE, 'MM-YYYY') = 'month-year';
Cursor c3 is
select usage
from bill
where account =acc_num and
to_char(BILL_DATE, 'MM-YYYY') = 'month-year' - 1;
** I do understand this is only part of the code, but I believe my logic is almost complete for finding the data I want. Using PLSQL
I think you are looking for something like this:
select usage
from bill
where account = in_account and
extract(year from bill_date) = in_year and
extract(month from bill_date) = in_month;
If you want to compare the year and month (which are passed in as integers), just extract those attributes from the date.
If you are learning PL/SQL, learn to name your parameters and arguments so you can distinguish them from columns:
create or replace procedure data (
in_account integer,
in_month integer,
in_year integer
) as
begin
. . .
(And "data" is a very curious name for a stored procedure. I would expect a verb in the name.)
What is the difference between these two variable declarations?
1: num number:='&&num';
2: variable num1 number;
Since in both cases I can reference num by using &num or &&num in other files also,
and in the case of bind variables :num1.
Moreover I have one more confusion: whether any of the below statements differ somehow, are they both valid and do they mean the same thing?
1: variable num1 number;
2: var num1 number;
You appear to have some confusion about the differences between bind variables in Oracle and substitution variables in SQL*Plus.
Let's start with substitution variables. Substitution variables are unique to SQL*Plus and are not part of the database. They won't work if you try to use them with JDBC, for example.
Substitution variables can only hold a piece of text. If SQL*Plus encounters a substitution variable in a line of input, it will replace the variable with its text contents:
SQL> define subvar=X
SQL> select * from dual where dummy = &subvar;
old 1: select * from dual where dummy = &subvar
new 1: select * from dual where dummy = X
select * from dual where dummy = X
*
ERROR at line 1:
ORA-00904: "X": invalid identifier
Note that SQL*Plus replaced our substitution variable with its text value with no regard for whether it gave us valid SQL. In the example above, we omitted the single quotes around &subvar and it gave us invalid SQL, so we got an error.
The lines beginning old and new show us the line we entered before and after SQL*Plus applied the substitution variables. The new line is the line the database tried to run.
You can enable or disable the display of the old and new lines using SET VERIFY ON and SET VERIFY OFF. You can also turn the replacement of substitution variables on or off by using SET DEFINE ON and SET DEFINE OFF.
If we want to run the above query using the substitution variable, we must put quotes around it:
SQL> select * from dual where dummy = '&subvar';
old 1: select * from dual where dummy = '&subvar'
new 1: select * from dual where dummy = 'X'
D
-
X
If &subvar happens to contain a string that was a valid number (e.g. 5), then we can get away without using the quotes, but that's only because taking out the text &subvar and replacing it with the text 5 happens to give us valid SQL.
For example, suppose we have a table called test with the following data in it:
A
----------
1
2
3
4
5
Then we can do
SQL> define subvar=5
SQL> select * from test where a = &subvar;
old 1: select * from test where a = &subvar
new 1: select * from test where a = 5
A
----------
5
Bind variables, on the other hand, have types. They are not simple text values. Their values are sent to the database, and the database can also set their values.
SQL> variable bindvar varchar2(1);
SQL> exec :bindvar := 'X';
PL/SQL procedure successfully completed.
You don't put quotes around a bind variable when you want to use it:
SQL> select * from dual where dummy = :bindvar;
D
-
X
SQL> select * from dual where dummy = ':bindvar';
no rows selected
In the second example above, we got no rows returned because the DUAL table has no rows with the DUMMY column containing the text :bindvar.
You'll get an error if you attempt to assign a value of the wrong type to a bind variable:
SQL> variable bindvar number;
SQL> exec :bindvar := 'X';
BEGIN :bindvar := 'X'; END;
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 1
Bind variables are a standard part of the database, and you can use them with JDBC or whichever method of connecting to the database you choose.
Finally, variable num1 number and var num1 number both mean the same thing. They both define a bind variable num1 of type number. var is just an abbreviation for variable.