Select the query within the PL/SQL block.
Hello Experts,I'm just a beginner with PL/SQL.
If I write a select query of client like fire against a database and SQL dev, it gives me result.
For example: select * from employee;
Now suppose that when I use the same query within a PL/SQL block:
Declare
Start
Select * from employee;
end;
/
It gives error during execution, by mentioning that an INTO should etc...
I have my doubts here:
1. is it impossible to use a simple select statement within a PL/SQL block (If yes why?)
I know it's a very basic question, I tried to search it on the forum but could not find the thread, please redirect me to the link if it is already the answer.
Please read this first. may ask why?
http://docs.Oracle.com/CD/B28359_01/AppDev.111/b28370/TOC.htm
Tags: Database
Similar Questions
-
Open the pl/sql block pop title request page
Hi all
Can someone show me the correct syntax for opening a page of the application in a window popup using
of htp.p of pl/sql with (parameters of the page.) or local variables of the pl/sql block?
And also how can force you a refresh of the page parent once the pop-up was closed.
Any help would be appreciated.
Kind regards
CleoHello
To generate a link to a pop-up window on your parent page:
htp.p(' <a href="javascript:callPopup('||:P1_ID|| ')" > ');
And add this in the header html on your parent page:
<script type="text/javascript" > function callPopup(recordId){ var url; url = 'f?p=&APP_ID.:POPUPPAGEALIAS:&APP_SESSION.::::P_RECORD_ID:' + recordId; w = open(url,"textarea","Scrollbars=1,resizable=1,width=800,height=600"); if (w.opener == null) w.opener = self; w.focus(); } </script >
And to refresh the page parent after the closure of the pop-up window, add to the html on your popup page header:
<BODY onUnload="javascript: window.opener.location.reload(true);">
-
Why do I init VARCHAR to a SELECT in a Pro * C PL/SQL block?
Hello
I use the PL/SQL block in a compiled and Embedded C Programm with the PRO * C compile. Oracle 10 g 2.
When I select... In a liaison VARCHAR variable, I have to initialize the variable before, otherwise I get an ORA-1458.
Same problem with assignments to VARCHAR variables.
Question: Why do I need to initialize the variable?
TNX for your help!
The following test program shows my question:
#include < stdio.h >
#include < string.h >
#include "oraca.h".
#include 'sqlca.h.
#define USER 'scott '.
#define PASSWORD 'tiger '.
int main()
{
EXEC SQL BEGIN DECLARE SECTION;
char * db_user = USER;
char * db_passw = password;
Sysdate_str VARCHAR [64];
EXEC SQL END DECLARE SECTION;
EXEC SQL CONNECT: db_user identified by: db_passw;
/ * This works * /.
sysdate_str. Len = 1000; / * invalid length * /.
EXEC SQL SELECT
TO_CHAR (sysdate, 'DD hh24:mi:ss')
in: sysdate_str
of the DOUBLE
;
printf ("sqlca.sqlcode %d\n", sqlca.sqlcode);
/ * the following code does not work, sqlcode = - 1458 * /.
/ * 01458, 00000, "length invalid inside variable character string * /.
sysdate_str. Len = 1000; / * invalid length * /.
EXEC SQL EXECUTE
BEGIN
Select to_char (SYSDATE, 'DD hh24:mi:ss')
in: sysdate_str
Double;
END;
END-EXEC;
printf ("sqlca.sqlcode %d\n", sqlca.sqlcode);
/ * the following code does not work, sqlcode = - 1458 * /.
/ * 01458, 00000, "length invalid inside variable character string * /.
sysdate_str. Len = 1000; / * invalid length * /.
EXEC SQL EXECUTE
BEGIN
: sysdate_str: = to_char (SYSDATE, "DD hh24:mi:ss");
END;
END-EXEC;
printf ("sqlca.sqlcode %d\n", sqlca.sqlcode);
Return (0);
}
Published by: jjaeckel on May 5, 2010 20:37Hello
This is the behavior documented by my understanding.
Pro * Guide C/C++ programmer, Chapter 7
Host
Note:
To use VARCHAR, MASSILIA, or STRING types as output in PL/SQL blocks host variables, you must initialize the length before entering the block. Set the length to the declared (maximum) length of the VARCHAR, MASSILIA or STRING type.http://download.Oracle.com/docs/CD/B19306_01/AppDev.102/b14407/pc_07pls.htm#i2336
Kind regards
Mark
-
to change the pl/sql block to get the value of the user
In this code rather than affect the value of the SID in the block, I want to get the sid of the user.
Help, please.
Oracle Database 10 g Express Edition Release 10.2.0.1.0 - product PL/SQL Release 10.2.0.1.0 - Production CORE 10.2.0.1.0 Production AMT for 32-bit Windows: Version 10.2.0.1.0 - Production NLSRTL Version 10.2.0.1.0 - Production declare
v_sname t1.sname%type;
v_issue_date t3.issue_date%type;
v_book_title t2.book_title%type;
v_book_id t2.book_id%type;
cursor c1 is
Select t1.sname, t3.issue_date, t2.book_title, book_id
join T1 T3
using (SID)
Join t2
using (book_id)
where sid = 1;
Start
Open c1
loop
dbms_output.put_line (v_sname |) » ' || v_issue_date | » ' || v_book_title | ' ' || v_book_id);
extract the c1 in v_sname, v_issue_date, v_book_title, v_book_id;
When exit c1% notfound;
end loop;
Close c1;
end;
/
Thank you
If I set up a script that I want to pass a value, I just a configuration variable and name it and use it very similar to a procedure or function. Makes it easy to implement a procedure/function, or to convert a function/procedure in a PL/SQL block for test/debugging with relatively few changes to the code inside:
declare in_sid number := to_number('&1'); v_sname t1.sname%type; v_issue_date t3.issue_date%type; v_book_title t2.book_title%type; v_book_id t2.book_id%type; cursor c1 is select t1.sname, t3.issue_date, t2.book_title, book_id from t1 join t3 using(sid) join t2 using(book_id) where sid=in_sid; begin open c1 loop dbms_output.put_line( v_sname ||' ' || v_issue_date ||' ' || v_book_title || ' ' || v_book_id); fetch c1 into v_sname, v_issue_date, v_book_title, v_book_id; exit when c1%notfound; end loop; close c1; end; /
-
Ignore the error and continue the pl/sql block
Dear all,
Please help me to fix the script below.
I had an obligation to write a pl/sql that will handle the update instructions.
For example:
A table that contains 10 rows of data. Through pl/SQL block, I update the 10 rows (or a smaller number of lines).
Now, the scenario is by updating registration / line 4, oracle has met an error and then oracle leave pl/sql block and cancel the transaction.
My requirement here is, by updating the records, if oracle encounters an error, it must raise an error or ignore the record and continue to process records. Oracle wouldn't pl/sql block.
We are using oracle 10g.
Thank you
KODSAs has already been suggested, you might check DML ERROR LOGGING
-
How to execute the pl/sql block from a file
Hi all
can someone tell me how to execute a pl/sql block to a file.it does contain no procedures.it is of the form
-Start
-says
-end;
ThanxOk.. If the time points? :)
See you soon!
Bobin -
My first while loop inside the PL/SQL block does not, please help
Hello
I'm new to PL/sql and struck PL SQL blocks, please help solve this problem.
declare
constant v_A number: = 10;
constant number j: = 3;
BEGIN
WHILE j < v_A
LOOP
DBMS_OUTPUT. Put_line ('Hai');
END LOOP;
END;
Please help as how to solve this problem.
Thanks in advance.You cannot declare j as in 'permanent' If you want to increment its value. better go to another variable if you intentionally want constant j.
Something like this:
declare v_A number constant:=10 ; j number constant := 3 ; i number; BEGIN i := j; WHILE i < v_A LOOP i := i + 1; DBMS_OUTPUT.PUT_LINE('Hai'); END LOOP; END;
... Vivek
Published by: Vivek 21 Sep, 2010 02:54
-
A question about the input within the PL/SQL block values
Hi all
I would be grateful if you could kindly help me with this issue well.
Consider the following code.
This program reads the three input values and print them. However, I have noticed that if instead of writingDECLARE myvar1 NUMBER; myvar2 NUMBER; myvar3 NUMBER; BEGIN myvar1 := &1; myvar2 := &2; myvar3 := &3; DBMS_OUTPUT.put_line('myvar1 = ' || myvar1); DBMS_OUTPUT.put_line('myvar2 = ' || myvar2); DBMS_OUTPUT.put_line('myvar3 = ' || myvar3); END; /
I am writingmyvar1 := &1; myvar2 := &2; myvar3 := &3;
The program will have the same result. I would like to know if there is a semantic difference between the two syntax,myvar1 := '&1'; myvar2 := '&2'; myvar3 := '&3';
in other words, there is no difference between for example & myvar1 and "myvar1?
Thanks in advance,
Dariyoosh& 1 would be a NUMBER
' & 1' would be a TANK, that would be implicitly converted to a NUMBER. -
Error in the pl/sql block using associative arrays
Hello
I tried the following block of code using associative arrays.
and the error isDECLARE TYPE NumTab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; CURSOR c1 IS SELECT empno FROM emp; empnos NumTab; rows NATURAL := 10; BEGIN OPEN c1; FOR i in empnos.first..empnos.last LOOP /* The following statement fetches 10 rows (or less). */ FETCH c1 BULK COLLECT INTO empnos LIMIT rows; EXIT WHEN c1%NOTFOUND; DBMS_OUTPUT.PUT_LINE ( empnos.next(i)); END LOOP; CLOSE c1; END;
could you please let me know where I am going wrongDECLARE * ERROR at line 1: ORA-06502: PL/SQL: numeric or value error ORA-06512: at line 8
and please guide me where we use these associative arrays.
Thank youTry this
DECLARE TYPE NumTab IS TABLE OF emp%rowtype INDEX BY PLS_INTEGER; CURSOR c1 IS SELECT * FROM emp; empnos NumTab; rows INTEGER := 10; BEGIN OPEN C1; LOOP FETCH c1 BULK COLLECT INTO empnos LIMIT rows; EXIT WHEN c1%NOTFOUND; FOR i IN 1..empnos.count LOOP DBMS_OUTPUT.PUT_LINE(empnos(i).empno || '/' || empnos(i).ename); END LOOP; END LOOP; END;
-
Popup 'Select the file path' block 'Open the file' vi
When popup 'Select File Path' by clicking on the "browser button", vi "Open file" will be blocked.
I found when I tried to select a file in popup "Select path", the newspaper system stop recording.
Is this true?
Any suggestion? (This will help if the vi for the syslog for the readmission of the value). I need the newspaper to keep on going even when someone clicks and the path of the file browser popup.
Thank you.
-
What's not in the pl/sql block
Hi, I am unable to pass the parameter of a cursor to other cursors.
create table temp3 as select * from dept; create table temp4 as select * from emp; create or replace procedure xx_rep is cursor xx_dep_cur is select deptno from dept; cursor xx_emp_cur(p_dept number) is select sal,comm from temp4 where deptno =p_dept; TYPE idt_cur_dep2 IS TABLE OF xx_dep_cur%ROWTYPE INDEX BY PLS_INTEGER; t_idt_cur_dep2 idt_cur_dep2; TYPE idt_cur_emp2 IS TABLE OF xx_emp_cur(p_dept number)%ROWTYPE INDEX BY PLS_INTEGER; t_idt_cur_emp2 idt_cur_emp2; BEGIN OPEN xx_dep_cur; LOOP FETCH xx_dep_cur BULK COLLECT INTO t_idt_cur_dep2 LIMIT 1000; EXIT WHEN t_idt_cur_dep2.count<=0; FOR indx in t_idt_cur_dep2.first .. t_idt_cur_dep2.last LOOP DELETE from temp3 where deptno=t_idt_cur_dep2(indx).deptno; --main update BEGIN OPEN xx_emp_cur(t_idt_cur_dep2(indx).deptno); LOOP FETCH xx_emp_cur BULK COLLECT INTO t_idt_cur_emp2 LIMIT 1000; EXIT WHEN t_idt_cur_emp2.count<=0; FOR indx in t_idt_cur_emp2.first .. t_idt_cur_emp2.last LOOP UPDATE temp4 set comm=10 , sal =100 where deptno=t_idt_cur_dep2(indx).deptno; END LOOP; END LOOP; close xx_emp_cur(t_idt_cur_dep2(indx).deptno); EXCEPTION WHEN OTHERS THEN dbms_output.put_line('Exception' || SQLCODE||' '|| SQLERRM); END; --end of main update END LOOP; END LOOP; COMMIT; Close xx_dep_cur; EXCEPTION WHEN OTHERS THEN dbms_output.put_line('Exception'|| SQLCODE||' '|| SQLERRM); END;
You have syntax errors and when you set the couple noticed that you will find a little more.
First place a
/
display errors;at the end of your code. This will display your mistakes you don't have query user_errors. When you close a cursor set you just use the name of cursor with no parameters, or the parameter list appears in the reference cursor in the Type statement on line 13.
The rest is up to you.
HTH - Mark D Powell.
-
selection of the results aggregated into a PL/SQL block gives me ORA-00934
Hello
I have a simple table of objects on the map that were inclusive. That is to say:
I can query the number of entries for a parent entity and the use of aggregate functions calculate the framework encompassing total for the parent entity:CREATE TABLE tbl ( id NUMBER(11,0), -- Id of this record parentId NUMBER(11,0), -- Id from some parent table bb_sw_lat FLOAT, -- SW bounding box corner - Latitude bb_sw_lng FLOAT, -- SW bounding box corner - Longitude bb_ne_lat FLOAT, -- NE bounding box corner - Latitude bb_ne_lng FLOAT -- NE bounding box corner - Longitude ); INSERT INTO tbl VALUES (1, 1, 10, 10, 20, 20); INSERT INTO tbl values (2, 1, 5, 5, 35, 35); INSERT INTO tbl values (3, 1, -5, -20, 0, 0); INSERT INTO tbl VALUES (4, 2, 9, 9, 10, 10); INSERT INTO tbl values (5, 2, 1, 1, 1, 1);
but the same select cannot be put to the PL/SQL block:SELECT count(*), NVL(MIN(bb_sw_lat),0), NVL(MIN(bb_sw_lng),0), NVL(MAX(bb_ne_lat),0), NVL(MAX(bb_ne_lng),0) FROM tbl WHERE parentId=1;
can someone please explain to me why and what to do with it? Examples are executable as they are, it should be fairly easy to reproduce.DECLARE cnt NUMBER; sw_lat FLOAT; sw_lng FLOAT; ne_lat FLOAT; ne_lng FLOAT; BEGIN SELECT count(*) INTO cnt, NVL(MIN(bb_sw_lat),0) INTO sw_lat, -- ORA-00934: group function is not allowed here NVL(MIN(bb_sw_lng),0) INTO sw_lng, NVL(MAX(bb_ne_lat),0) INTO ne_lat, NVL(MAX(bb_ne_lng),0) INTO ne_lng FROM tbl WHERE parentId=1; END;
Thank you very much!
Kuba
Published by: user3620664 on May 14, 2009 02:14 added formattingwithout testing, just an IN
DECLARE cnt NUMBER; sw_lat FLOAT; sw_lng FLOAT; ne_lat FLOAT; ne_lng FLOAT; BEGIN SELECT count(*), NVL(MIN(bb_sw_lat),0), NVL(MIN(bb_sw_lng),0), NVL(MAX(bb_ne_lat),0), NVL(MAX(bb_ne_lng),0) INTO cnt, sw_lat, sw_lng, ne_lat, ne_lng FROM tbl WHERE parentId=1; END;
Published by: Alex Nuijten on May 14, 2009 11:19
-
How to truncate the table in a pl/sql block
Hello guys, I'm trying to truncate a table that includes data using a pl/sql block, so if the table has even a single row then the truncate should run. to do this, I wrote the following code
declare
number num.
Start
Select count (*) in the num from table_name;
If num > 0 then
truncate table table_name;
end if;
end;
Now, when I execute the pl/sql block, I get the following error.
PLS-00103: encountered the symbol "TABLE" when the expected in the following way:
When I run the truncate without pl/sql command, it runs very well.
Can someone please where I'm wrong.
Thank you
Published by: 969224 on April 23, 2013 08:08969224 wrote:
Hello guys, I'm trying to truncate a table that includes data using a pl/sql block, so if the table has even a single row then the truncate should run. to do this, I wrote the following codedeclare
number num.
Start
Select count (*) in the num from table_name;
If num > 0 then
truncate table table_name;
end if;
end;Now, when I execute the pl/sql block, I get the following error.
PLS-00103: encountered the symbol "TABLE" when the expected in the following way:
When I run the truncate without pl/sql command, it runs very well.
Can someone please where I'm wrong.
Thank you
Published by: 969224 on April 23, 2013 08:08
DDL is not allowed directly within PL/SQL procedures.
You must (ab) use EXECUTE IMMEDIATE issuing the DDL
-
SQL query to avoid the PL/SQL
Hello
I'm doing below pl/sql block in a single stm SQL, let me know how could achieve us.
Here I use SELECT * FROM TEMP_ORDER WHERE DB_NAME = V_DBNAME which runs a cursor to get the separate database names.
This V_DBNAME could be avoided? and put everything in a single SQL?
(The PL/SQL block is not running properly because several rows are returned) I would be interested in the SQL solution.
create table TEMP_ORDER ( ORD1 VARCHAR2(25), ORD2 VARCHAR2(25), DB_NAME VARCHAR2(25) ) INSERT INTO TEMP_ORDER(ORD1,ORD2,DB_NAME) VALUES('A1','FIRST ORDER','DB1'); INSERT INTO TEMP_ORDER(ORD1,ORD2,DB_NAME) VALUES('A2','SECOND ORDER','DB2'); INSERT INTO TEMP_ORDER(ORD1,ORD2,DB_NAME) VALUES('A3','THIRD ORDER','DB1'); create table TEMP_PAYMENT ( PAY1 VARCHAR2(25), PAY2 VARCHAR2(25), DB_NAME VARCHAR2(25) ) INSERT INTO TEMP_PAYMENT(PAY1,PAY2,DB_NAME) VALUES('A1','FIRST PAYMENT','DB1'); INSERT INTO TEMP_PAYMENT(PAY1,PAY2,DB_NAME) VALUES('A2','SECOND PAYMENT','DB2'); INSERT INTO TEMP_PAYMENT(PAY1,PAY2,DB_NAME) VALUES('A3','THIRD PAYMENT','DB1'); create table TEMP_DELIVER ( DEL1 VARCHAR2(25), DEL2 VARCHAR2(25), DB_NAME VARCHAR2(25) ) INSERT INTO TEMP_DELIVER(DEL1,DEL2,DB_NAME) VALUES('A1','FIRST DELIVER','DB1'); INSERT INTO TEMP_DELIVER(DEL1,DEL2,DB_NAME) VALUES('A2','SECOND DELIVER','DB2'); INSERT INTO TEMP_DELIVER(DEL1,DEL2,DB_NAME) VALUES('A3','THIRD DELIVER','DB1'); --------------------------------- DECLARE V_DBNAME TEMP_ORDER.DB_NAME%TYPE; V_ORDER_NAME TEMP_ORDER.ORD2%TYPE; V_PAYMENT_NAME TEMP_PAYMENT.PAY2%TYPE; V_DELIVER_NAME TEMP_DELIVER.DEL2%TYPE; CURSOR DBCURSOR IS SELECT UNIQUE DB_NAME from TEMP_ORDER; BEGIN IF NOT DBCURSOR%ISOPEN THEN OPEN DBCURSOR; END IF; LOOP FETCH DBCURSOR INTO V_DBNAME; EXIT WHEN DBCURSOR%NOTFOUND; SELECT A.ORD2, B.PAY2, C.DEL2 INTO V_ORDER_NAME,V_PAYMENT_NAME,V_DELIVER_NAME FROM (SELECT * FROM TEMP_ORDER WHERE DB_NAME = V_DBNAME)A, (SELECT * FROM TEMP_PAYMENT WHERE DB_NAME = V_DBNAME)B, (SELECT * FROM TEMP_DELIVER WHERE DB_NAME = V_DBNAME)C WHERE A.ORD1 = B.PAY1 AND B.PAY1 = C.DEL1; DBMS_OUTPUT.put_line(V_ORDER_NAME || ':' || V_PAYMENT_NAME || ':' || V_DELIVER_NAME); END LOOP; CLOSE DBCURSOR; END;
Hello
Do you mean something like this?
SELECT a.db_name, a.ord2 || ':' || b.pay2 || ':' || c.del2 string FROM TEMP_ORDER A, TEMP_PAYMENT B, TEMP_DELIVER C WHERE A.ORD1 = B.PAY1 AND B.PAY1 = C.DEL1 AND a.db_name = b.db_name AND b.db_name = c.db_name; DB_NAME STRING ------- ------------------------------------------ DB1 FIRST ORDER:FIRST PAYMENT:FIRST DELIVER DB2 SECOND ORDER:SECOND PAYMENT:SECOND DELIVER DB1 THIRD ORDER:THIRD PAYMENT:THIRD DELIVER
I hope this helps.
Kind regards.
-
What is the problem with this PL/SQL block
declare
v_count integer: = 0;
Start
-immediate ' select count (1) in v_count of | "abc". '.table1 ';
run immediately ' select count (1) in abc.table1 v_count;
dbms_output.put_line ('v_count: ' | v_count);
end;
When I execute the pl/sql block above, I get the following error:
ORA-00905: lack of keyword
ORA-06512: at line 6
What I am doing wrong?
When I use SELECT COUNT (1) INTO v_count FROM abc.table1; (instead of the dynamic sql), it works.
I know I can't use run immediately to SELECT, but I need to make the dynamic SQL because table1 will be developed according to several drawings.
Would appreciate an explanation of the error above, or alternate solution (s) SQL dynamic.
Thank youDECLARE v_count INTEGER := 0; BEGIN -- execute immediate 'select count(1) into v_count from ' || 'abc' || '.table1'; EXECUTE IMMEDIATE 'select count(1) from abc.table1' into v_count; dbms_output.put_line('v_count: ' || v_count); END;
TEST
DECLARE v_count INTEGER := 0; BEGIN -- execute immediate 'select count(1) into v_count from ' || 'abc' || '.table1'; EXECUTE IMMEDIATE 'select count(1) from dual ' into v_count; dbms_output.put_line('v_count: ' || v_count); END; / v_count: 1 PL/SQL procedure successfully completed.
HTH
SS
Maybe you are looking for
-
Add Windows XP to a wireless home network
Hi just to add a lapttop XP to a wireless home network. the network icons are shown as they are always posted on vista and windows 7 and is there a centre network for xp so do not know if the connection is correct. In addition, the Dungeon of connect
-
Problems with not being able to send and receive instant messages.
I receive some messages, so cannot send or reveice messages. The system seems to work and then stop.
-
can I install an operating system on my ram?
I went through a lot of posts on how to do that, people just give fancy words in huge paragraphs, that's the only problem I have not been able to address, I mean, Ive got a lot of things in my pc just by reading a few posts. but this, increasing the
-
How can I disable CS5 on computer that I do not have access?
I bought CS5 and its available up to 3 computers. But each of them finally crashed. Does anyone have any suggestions on how I can remove these 2 previous computers of the user of the license? @
-
Interpolation of shape files have?
I want to make a shape tween between a logo built Illustrator (.ai file), which consists of a few vector shapes more white text as the contours and morph a vector unique form with white outlines. I use Flash CS6. When I draw simple shapes in Flash, t