This question already has answers here:
Create user from string variables in a PL/SQL block
(1 answer)
Why can't I write DDL directly after a PLSQL anonymous block?
(1 answer)
Closed 7 years ago.
What's going on here?
DECLARE
V_COUNT NUMBER(10) := 0;
BEGIN
SELECT COUNT(*) INTO V_COUNT FROM USER_VIEWS WHERE VIEW_NAME = 'DBO$EVT_APP';
IF V_COUNT > 0 THEN
DROP VIEW DBO$EVT_APP;
END IF;
END;
I'm getting the following error:
Error report:
ORA-06550: line 9, column 5:
PLS-00103: Encountered the symbol "DROP" when expecting one of the following: ...
This looks like valid SQL to me. This is my exact code, pasted.
DECLARE
V_COUNT NUMBER(10) := 0;
BEGIN
SELECT COUNT(*) INTO V_COUNT FROM USER_VIEWS WHERE VIEW_NAME = 'DBO$EVT_APP';
IF V_COUNT > 0 THEN
execute immediate 'DROP VIEW DBO$EVT_APP';
END IF;
END;
Related
Is there any way to assign a select query to local variable in PL/SQL other than select into statement?. Because select into throwing null value exception if the select query returns null value. Thanks
It would be helpful to post your code, but here is an example that should show the behavior you need. Assume there is a table called courses_tbl:
declare
cnumber number := NULL;
CURSOR c1
IS
SELECT course_number
FROM courses_tbl
WHERE course_name = 'XYZ';
BEGIN
open c1;
fetch c1 into cnumber;
if c1%notfound then
-- Do something here if you care about not found.
cnumber := 999; -- whatever
end if;
you can read about cursor attributes here
Seems that you need to use the exception handling as follows:
... -- Your procedure other code
BEGIN
SELECT <COLUMN_NAME> INTO <YOUR_VARIABLE>
FROM .....
WHERE ....
EXCEPTION WHEN NO DATA FOUND THEN
<YOUR_VARIABLE> := NULL;
END;
... -- Your procedure other code
You can use EXECUTE IMMEDIATE...INTO...:
DECLARE
nCnumber NUMBER;
BEGIN
EXECUTE IMMEDIATE 'SELECT CNUMBER FROM COURSES_TBL WHERE CNUMBER = 1'
INTO nCnumber;
DBMS_OUTPUT.PUT_LINE('SELECT #1 : nCnumber = ' || nCnumber);
nCnumber := NULL;
BEGIN
EXECUTE IMMEDIATE 'SELECT CNUMBER FROM COURSES_TBL WHERE CNUMBER = 100'
INTO nCnumber;
DBMS_OUTPUT.PUT_LINE('SELECT #2 : nCnumber = ' || nCnumber);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('SELECT #2 : NO DATA FOUND');
END;
END;
db<>fiddle here
You've seen how to do it using a cursor or exception handling section (which is - in my opinion - the right way to do it). However, as we're discussing, here's yet another option - an aggregate function. It won't return NO_DATA_FOUND but NULL.
This is what you have now:
SQL> declare
2 l_job emp.job%type;
3 begin
4 select job
5 into l_job
6 from emp
7 where ename = 'Does not exist';
8 end;
9 /
declare
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 4
This is what you might do:
SQL> declare
2 l_job emp.job%type;
3 begin
4 select max(job)
5 into l_job
6 from emp
7 where ename = 'Does not exist';
8 end;
9 /
PL/SQL procedure successfully completed.
SQL>
This question already has answers here:
Using variables in PLSQL SELECT statement
(2 answers)
Closed 5 years ago.
What's the basic syntax for a pl/sql variable? I'm trying to add a variable in my select statement.
My query:
DECLARE
V_id number := 251 ;
BEGIN
SELECT *
FROM client
where 1=1
and clientid = V_id;
END;
Error:
PLS-00428 an INTO clause is expected in this Select statement
This example may work for you. Please check it out
DECLARE
V_Num NUMBER(15,0) := 150;
V_Col VARCHAR2(50);
BEGIN
SELECT col1 into V_col FROM tableA where id = V_num;
END ;
This question already has answers here:
SELECT from table with Varying IN list in WHERE clause
(2 answers)
Closed 5 years ago.
I am passing an input value as 'MI,NOKIA,APPLE' to a procedure. This input value has to be passed to the query in where condition.
For example:
create or replace procedure abc123(p_name varchar2)
is
v_val number;
begin
dbms_output.put_line ('p_name: '||p_name );
Select 1
Into v_val
from MYTABLE
where Model in p_name;
dbms_output.put_line ('v_val: '||v_val );
end;
This is not working. How do I pass the multiple input values as single parameter?
Use paranthesis after IN operator :
Select 1 Into v_val from MYTABLE where Model in (p_name);
try below code,
create or replace procedure proc123 (p_param VARCHAR2)
IS
TYPE cur_typ IS REF CURSOR;
c cur_typ;
v_query VARCHAR2(200) := 'SELECT * '||
'FROM (SELECT ''Model1'' model FROM dual '||
'union all '||
'SELECT ''Model2'' FROM dual '||
'union all '||
'SELECT ''Model3'' FROM dual)';
v_model VARCHAR2(20);
BEGIN
v_query := v_query||' WHERE INSTR('''||p_param||''',model) > 0';
dbms_output.put_line(v_query);
OPEN c FOR v_query;
LOOP
FETCH c INTO v_model;
EXIT WHEN c%NOTFOUND;
dbms_output.put_line(v_model);
END LOOP;
CLOSE c;
END;
/
--set your dbms_output on
begin
proc123('Model1, Model2');
end;
/
if you want to pass the parameter in a query, then you can use the INSTR function
Select 1
from MYTABLE
where INSTR(p_name, model) > 0;
DECLARE
rec_count integer default 0;
str varchar(100);
BEGIN
str := 'select count(*) into ' || rec_count ||' from emp_table';
EXECUTE IMMEDIATE (str);
dbms_output.put_line(rec_count);
END;
I cannot get count in rec_count valibale. I received ORA-00905 message.
I have to add these lines in my script so sql statment should be like this.
I would pass trip name to this script and it would return count of that table.
thanks in advance.
you must use like below
DECLARE
rec_count integer default 0;
str varchar(100);
BEGIN
str := 'select count(*) from emp_table';
EXECUTE IMMEDIATE str into rec_count;
dbms_output.put_line(rec_count);
END;
the into clause must be use after execute immediate. refer here http://docs.oracle.com/cd/B12037_01/appdev.101/b10807/13_elems017.htm for more information
The correct way is EXECUTE IMMEDIATE str into rec_count;. the INTO clause must be used while you do EXECUTE IMMEDIATE.
SQL> set serveroutput on
SQL> DECLARE
2 rec_count integer default 0;
3 str varchar(100);
4 BEGIN
5 str := 'select count(*) from emp';
6 EXECUTE IMMEDIATE str into rec_count;
7 dbms_output.put_line(rec_count);
8 END;
9 /
14
PL/SQL procedure successfully completed.
SQL>
The following PL/SQL block works:
DECLARE
r TABLE1%ROWTYPE;
BEGIN
SELECT * INTO r FROM TABLE1 SAMPLE(1) WHERE ROWNUM = 1;
END;
However, when I try to replace the literal with a variable within the SAMPLE clause, Oracle returns a syntax error:
DECLARE
s NUMBER;
r TABLE1%ROWTYPE;
BEGIN
s := 1;
SELECT * INTO r FROM TABLE1 SAMPLE(s) WHERE ROWNUM = 1;
END;
ORA-06550: line 6, column 39:
PL/SQL: ORA-00933: SQL command not properly ended
What am I doing wrong?
I'm using Oracle 10 and SQL Developer.
(These are simplified examples. What I'm actually trying to do in practice is to optimize the selection of random row, where SAMPLE percentage would be calculated dynamically, based on the current number of rows in the table. So I can't use literal, I need a variable to assign the result of the calculation.)
The SAMPLE synthax requires a numeral. You could use dynamic SQL to build a dynamic query, for example with a ref cursor:
SQL> CREATE TABLE table1 AS
2 SELECT ROWNUM ID, rpad(ROWNUM, 10, 'x') DATA
3 FROM dual CONNECT BY LEVEL <= 1000;
Table created
SQL> DECLARE
2 l_cur SYS_REFCURSOR;
3 l_row table1%ROWTYPE;
4 l_pct NUMBER := 50;
5 BEGIN
6 OPEN l_cur
7 FOR 'SELECT * FROM table1 SAMPLE('||l_pct||') WHERE rownum = 1';
8 LOOP
9 FETCH l_cur INTO l_row;
10 EXIT WHEN l_cur%NOTFOUND;
11 dbms_output.put_line(l_row.id);
12 END LOOP;
13 END;
14 /
3
PL/SQL procedure successfully completed