Cursor Vs Select statement
Hi,
Please let me know what is the difference between using OPEN CURSOR and SELECT statement WITH PACAKGE additon.
Praneeth
Hi,
http://wiki.sdn.sap.com/wiki/display/ABAP/SELECTStatementsandCURSORstatement-Performance+Analysis.
and also check
http://help.sap.com/saphelp_46c/helpdata/en/fc/eb3b23358411d1829f0000e829fbfe/content.htm
cheers,
Bhavana
Similar Messages
-
Using Cursor in Select statements? How to do this?
I am getting an error whilt passing a cursor to a select clause:
SELECT dbms_xmlquery.getXML('select deptno, dname, '||
'cursor(select empno, ename, sal from emp e where e.deptno = d.deptno) employees '||
'from dept d where d.deptno in (10, 20)')
FROM dual;
DBMS_XMLQUERY.GETXML('SELECTDEPTNO'||'CURSORIS(SELECTEMPNOFROMEMPEWHEREE.DEPT=D.
<?xml version = '1.0'?>
<ERROR>oracle.xml.sql.OracleXMLSQLException: ORA-00923
: FROM keyword not found where expected
</ERROR
THIS IS DUE TO THE CURSOR AND ITS FROM STATEMENT?
CAN ANY ONE PLEASE GUIDE AS TO HOW TO USE CURSORS IN A SELECT STATEMENT PLEASE?Another duplicate thread. See my response Select CLAUSE error using CURSORS & XSU.Please SEE..
Cheers, APC -
Cursor - dynamic select statement?
Hello!
I am passing a string into a function, and my string is a sql select statement. Is it possible to create a cursor using the string that is passed to the function?
Ex:
create or replace function "GETCOUNT"
(sqlstring in VARCHAR2)
return NUMBER
is
rec_count NUMBER; --I eventually want to return a count of the rows returned from my sqlstring
cursor c1 is sqlstring <--this is what I'd like to do...but how?
begin
... -- I haven't gotten to this point yet
end;
The error I get says 'Encountered the symbol "sqlstring" when expecting on of the following: ( select '
Thanks in advance!!Something like this ->
create or replace procedure gen_cur(str in varchar2,rc in out sys_refcursor)
is
str1 varchar2(500);
begin
str1 := 'select empno,ename,hiredate,sal
from emp
where '||str;
open rc for str1;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
variable b refcursor
declare
src varchar2(300);
begin
src:= 'sal between 2000 and 7000';
gen_cur(src,:b);
exception
when others then
dbms_output.put_line(sqlerrm);
end;
print bRegards.
Satyaki De. -
Ref cursor in select statement
I am not sure this is the right forum, but I got no reply from the OTN Sample.
How do I call a function from a select
statement ?
The function returns a ref cursor.
Here is the function definition:
FUNCTION get_priority_code_list
(p_language_id IN VARCHAR2)
RETURN RefCur
IS
rcur RefCur;
BEGIN
OPEN rcur FOR SELECT
priority_code "priority_code_menu",
priority_code_desc "priority_code_menu_desc"
FROM
prio_desc
WHERE
language_id = p_language_id;
RETURN rcur;
END;
I would like to wrap a SELECT statement
around the function and have the same result
as running the SELECT statement directly.
Of course it does not seem logical here to
use a function, but in fact I want the
function to do more than just a select....
I tried the syntax
SELECT package.function(param) FROM dual;
but that does not work...
or
SELECT * FROM package.function(param);
but that does not work either....
any idea ?
Tks.
nullI have a stored function that returns a ref cursor and I want to use it in select
I tried the following code
for example if the ref cursor returned rows of varchar2
I will define an object
CREATE TYPE myType as OBJECT( doc_type VARCHAR2(256));
created a type as a table of myType
create or replace type myTable as table of myType;
If I write a select statement like below
select * from TABLE(CAST(function_name() AS myTable))
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CURSER
I get this error
Can anyone give any clue why this does throw an error or if there is a second way to do this, -
Cursor Expression Select Statement
SELECT DNAME, CURSOR(SELECT SAL,COMM FROM EMP E WHERE E.DEPTNO = D.DEPTNO) FROM DEPT D
DNAME CURSOR(SELECTSAL,COM
ACCOUNTING CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
2450
5000
1300
RESEARCH CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
800
2975
3000
1100
3000
SALES CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
1600 300
1250 500
1250 1400
2850
1500 0
950
6 rows selected.
OPERATIONS CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
no rows selected
Can we get rid of "CURSOR STATEMENT : 2"(Also I don't know what is its
significance there) from the output so that the output is in a readable format.
Thanks,
Neeraj GoelI want Data in the Format Like
deptno cnt1
cnt2
cntN
deptno cnt1
cntM
when you have a parent table (dept) and you sometimes
want to see the child rows (emp) but "on demand". For example, you fetch
"select deptno, cursor( from emp ) from dept" and the user says "hmmm, I'd like
to see the emps in deptno 20" -- great, you just fetch that cursor and display
them -- you didn't have to bring everyone else back with that query.
Also useful when you have more then 1:M relationship in a query
Now you can get a result set like:
deptno Sal Comm
Sal Comm
Sal
3 Sal, 2 Comm items and one deptno -- in one "row"
Thats why i used cursor Expression.
Thanks
Neeraj Goel -
CURSOR Inside select statement
Hi,
I am trying to generate reports in XML Publisher using PL/SQL Procedure. Following is my procedure:
PROCEDURE XXDL_PO_VEN_SUMMARY_BY_VEN (ERR_BUFF VARCHAR2,
RETCODE VARCHAR2,
in_vendor_number IN VARCHAR2,
in_start_date IN VARCHAR2,
in_end_date IN VARCHAR2
IS
SQL_STMT VARCHAR2(15000);
result CLOB;
in_vendor_summary_from_date DATE;
in_vendor_summary_to_date DATE;
CURSOR_WHERE_CLAUSE VARCHAR2(1000);
BEGIN
in_vendor_summary_from_date := TO_DATE(in_start_date,'YYYY/MM/DD HH24:MI:SS');
in_vendor_summary_to_date := TO_DATE(in_end_date,'YYYY/MM/DD HH24:MI:SS');
--DBMS_OUTPUT.PUT_LINe('start date object'||in_vendor_summary_from_date);
--DBMS_OUTPUT.PUT_LINe('end date object'||in_vendor_summary_to_date);
IF in_start_date IS NOT NULL AND LENGTH(in_start_date) > 0 AND in_end_date IS NOT NULL AND LENGTH(in_end_date) > 0 THEN
CURSOR_WHERE_CLAUSE := 'AND TO_DATE(CONCAT(CONCAT(SUMMARY_MONTH, ''/''), SUMMARY_YEAR),''MM/YYYY'') BETWEEN'
||' '''||in_vendor_summary_from_date||''' AND '''||in_vendor_summary_to_date||'''';
END IF;
IF in_vendor_number IS NOT NULL AND LENGTH(in_vendor_number)>0 THEN
CURSOR_WHERE_CLAUSE := CURSOR_WHERE_CLAUSE || ' AND UPPER(VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
END IF;
--DBMS_OUTPUT.PUT_LINe('cursor where clause : '||CURSOR_WHERE_CLAUSE );
SQL_STMT := 'SELECT VS.VENDOR_NUM,' ||
' VS.VENDOR_NAME,' ||
' VS.SUMMARY_MONTH,' ||
' VS.SUMMARY_YEAR,' ||
' VS.ORD_TOTAL_CNT,' ||
' VS.ORD_DOLLAR_AMT,'||
' VS.RPO_TOTAL_CNT,' ||
' VS.RPO_DOLLAR_AMT,'||
' VS.CWA_TOTAL_CNT,' ||
' VS.CWA_DOLLAR_AMT,'||
' VS.STD_TOTAL_CNT,' ||
' VS.STD_DOLLAR_AMT,'||
' VS.BLM_TOTAL_CNT,' ||
' VS.BLM_DOLLAR_AMT,'||
' VS.INVOICE_TOTAL_CNT,' ||
' VS.INVOICE_DOLLAR_AMT, ' ||
' CURSOR (SELECT nvl(SUM( nvl(VS1.ORD_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS1 '||
' ) TOTAL_ORDERS, ' ||
' CURSOR(SELECT nvl(SUM(nvl(VS2.ORD_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS2 '||
' ) TOTAL_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS3.RPO_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS3 '||
' ) RPO_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS4.RPO_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS4 '||
' ) RPO_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS5.CWA_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS5 '||
' ) CWA_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS6.CWA_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS6 '||
' ) CWA_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS7.STD_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS7 '||
' ) STD_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS8.STD_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS8 '||
' ) STD_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS9.BLM_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS9 '||
' ) BLM_ORDERS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS10.BLM_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS10 '||
' ) BLM_DOLLARS,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS11.INVOICE_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS11 '||
' ) TOTAL_INVOICES,' ||
' CURSOR(SELECT nvl(SUM(nvl(VS12.INVOICE_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS12 '||
' ) INVOICE_DOLLARS, ' ||
' CURSOR(SELECT nvl(SUM(nvl(V1.RPO_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V1.RPO_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V1.RPO_TOTAL_CNT,0))) FROM '|| ' STANPRS.VENDOR_SUMMARY V1 ) RPO_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V2.RPO_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V2.RPO_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V2.RPO_DOLLAR_AMT,0))) '|| 'FROM STANPRS.VENDOR_SUMMARY V2 ) RPO_DOLLARS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V3.CWA_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V3.CWA_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V3.CWA_TOTAL_CNT,0))) FROM '|| 'STANPRS.VENDOR_SUMMARY V3 ) CWA_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V4.CWA_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V4.CWA_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V4.CWA_DOLLAR_AMT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V4 ) CWA_DOLLARS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V5.STD_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V5.STD_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V5.STD_TOTAL_CNT,0))) FROM '|| 'STANPRS.VENDOR_SUMMARY V5 ) STD_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V6.STD_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V6.STD_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V6.STD_DOLLAR_AMT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V6 ) STD_DOLLARS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V7.BLM_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V7.BLM_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V7.BLM_TOTAL_CNT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V7 ) BLM_ORDERS_PER,' ||
' CURSOR(SELECT NVL(SUM(nvl(V8.BLM_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V8.BLM_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V8.BLM_DOLLAR_AMT,0))) '||
'FROM STANPRS.VENDOR_SUMMARY V8 ) BLM_DOLLARS_PER,' ||
'CURSOR(SELECT NVL(SUM(nvl(V9.INVOICE_TOTAL_CNT,0)),0)*100 / '||
'decode(SUM(nvl(V9.INVOICE_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V9.INVOICE_TOTAL_CNT,0))) FROM '||
'STANPRS.VENDOR_SUMMARY V9 ) TOTAL_INVOICES_PER, ' ||
'CURSOR(SELECT NVL(SUM(nvl(V10.INVOICE_DOLLAR_AMT,0)),0)*100/ decode(SUM(nvl(V10.INVOICE_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V10.INVOICE_DOLLAR_AMT,0))) FROM STANPRS.VENDOR_SUMMARY V10 ) INVOICE_DOLLARS_PER' ||
' FROM STANPRS.VENDOR_SUMMARY VS' ||
' WHERE';
--DBMS_OUTPUT.PUT_LINE('STANPRS.VENDOR_SUMMARY V10 WHERE V10.VENDOR_NUM = VS.VENDOR_NUM ') INVOICE_DOLLARS_PER');
IF in_start_date IS NOT NULL AND LENGTH(in_start_date) > 0 AND in_end_date IS NOT NULL AND LENGTH(in_end_date) > 0 THEN
SQL_STMT := SQL_STMT || ' TO_DATE(CONCAT(CONCAT(VS.SUMMARY_MONTH, ''/''), VS.SUMMARY_YEAR),''MM/YYYY'') BETWEEN '
||' '''||in_vendor_summary_from_date||''' AND '''||in_vendor_summary_to_date||'''';
END IF;
IF in_vendor_number IS NOT NULL AND LENGTH(in_vendor_number)>0 THEN
IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
SQL_STMT := SQL_STMT || ' UPPER(VS.VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
ELSE
SQL_STMT := SQL_STMT || ' AND UPPER(VS.VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
END IF;
END IF;
IF in_vendor_name IS NOT NULL AND LENGTH(in_vendor_name)>0 THEN
IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
SQL_STMT := SQL_STMT || ' UPPER(VS.VENDOR_NAME) LIKE UPPER('''||in_vendor_name||'%'')';
ELSE
SQL_STMT := SQL_STMT || ' AND UPPER(VS.VENDOR_NAME) LIKE UPPER('''||in_vendor_name||'%'')';
END IF;
END IF;
IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
SQL_STMT := SQL_STMT || ' VS.VENDOR_NUM = '''||in_vendor_number||'''';
END IF; */
SQL_STMT := SQL_STMT || ' ORDER BY VS.VENDOR_NUM, VS.VENDOR_NAME ASC';
--insert into XX_ERROR VALUES('Spriha',SQL_STMT);
SELECT DBMS_XMLGEN.GETXML(SQL_STMT) INTO result FROM DUAL;
FND_FILE.PUT_LINE(FND_FILE.OUTPUT, result);
EXCEPTION
WHEN OTHERS THEN
FND_FILE.PUT_LINE(FND_FILE.OUTPUT, SQLERRM);
END XXDL_PO_VEN_SUMMARY_BY_VEN ;
Now u can see in this Procedure that i have created so many cursors. i.e. for one function i have created one cursor. at present thesecursors are working fine. but the time i will put the where clause inside the cursors this will throw an ERROR ' ORA-01460 Unimplemented or Unreasonable Conversion Request'. Also these cursors are working with where clause but they are not working together. i.e when i will remove some cursors then i am not getting any error but at present i.e. without any where clause they all r working fine. Seriously i am not getting anything that what is happening. Can anyone help me? waiting for the reply....
Thanks
SprihaThis is pretty much a poster for ugly code.
Simplify this down to the bare minimum and debug it. Then add back the rest of the functionality.
But I am at a loss, looking at this code, as to why any cursor is either desirable or required. -
Select statement from "v_filename_txt" inside cursor
Hi,
I've got a store procedure procedure that create parametric tables to upload from csv files. I have collect into v_filename_txt variable the parametric table name where I would like to make some select.
I have write the following code but I can't use a variable into a select statement :
select field
FROM TABLE '''||v_filename_txt||''' ;
Does anybody know how to include the precedent statement into a cursor to retrive recods ?
Paolo.You could rcreate an external table. Then select the values from this external table.
You can switch the source of the external table to a new file with the
alter table myExtTable location ('mySecondCsvFIle.csv');
command.
Of cause this works only when all the files have the same structure. But the Idea is: mimic the structure of the csv file sin the database with a external table command. Then you can read the values and move them to some other table.
Insert into myParameterTable select * from myExtTable; -
Number of records in cursor select statement
hi all,
with the cursor i am selecting set of select statement and writing into the .txt file.
its working file, but it should not open the file if the cursor returns nothing.
how to check for the number of records return by the select statement in the cursor.
pls help me.
Thanks..Hi,
You can use a Cursor for loop (you'll not enter in the loop if the SELECT is retrieving no rows):
begin
for recs in(select * from whatever) loop
null; -- Produce your file
end loop;
end;You can also use a bulk collect and check "yourcollection.count" attribute ... (but don't use it if you are processing a lot of records!) -
Using ref cursor in "in clause" in select statement
Hi,
Is there any way can we use the ref cursor in the in condition of a select statement.
Regards,
Venkat.
Edited by: ramanamadhav on Aug 23, 2011 11:14 AMramanamadhav wrote:
I'm sorry if I post in confusing way. I will give an example. Just see the psudo code here.
declare
rf_cur sys_refcursor;
begin
pr_test(empno,rf_cur);
-- rf_cur returning emp names.
select * from emp
where empname in (ref_cusor results);-- here i want to consume my ref cursor result in the in conditions.
end;
Thanks &Regards,
Venkat.No you can't do that. A ref cursor is not a set of results as you believe.
Take a read of this article...
{thread:id=886365} -
Use of cursors insted of select statements
could any one please explain what is the advantage of using cursors instead of simple select statements
thanks
sibyA benefit to using an explicit cursor rather than a select statement, is for the NO_DATA_FOUND exception. Its kind of like a free IF statment. IF no data is found, then stop.
if you write a select statement, and no data is returned, you SHOULD code for the NO_DATA_FOUND exception. Often people say, "i'll ALWAYS get a row returned". but you should always cover your code "just in case". so you must code an exception...
declare
v_var varchar2(1);
procedure do_something(p_parm varchar2) is
begin
null;
end do_something;
procedure log_error is
begin
null;
end log_error;
begin <<main>>
do_something('x');
begin <<selectblock>>
select dummy
into v_var
from dual
where dummy = 'a';
do_something(v_var);
exception
when no_data_found then
log_error;
end selectblock;
do_something (v_var||'abc');
end main;
if you use an explicit cursor instead, you don't need to code for the NO_DATA_FOUND. If an explicit cursor opens and finds no rows, there are simply no rows. of course, you don't need a loop if you expect only 1 row returned under normal circumstances.
BTW, don' forget that SQL%ROWCOUNT and your_cursor%ROWCOUNT are not initialized. There is a null, until a row is successfully fetched. therefore if no rows are returned at all, %ROWCOUNT is NULL.
declare
v_var varchar2(1);
cursor my_cur is
select dummy
from dual
where dummy = 'a';
procedure do_something(p_parm varchar2) is
begin
null;
end do_something;
procedure log_error is
begin
null;
end log_error;
begin << main>>
for cur_rec in my_cur loop
dbms_output.put_line('inside');
begin <<loop_block>>
if nvl(my_cur%rowcount,0) > 1 then
do_something(cur_rec.dummy);
else
log_error;
end if;
end loop_block;
end loop;
end main;
/ -
Insert select statement or insert in cursor
hi all,
i need a performance compare for: insert operation in cursor one by one and select statement insert,
for example:
1. insert into TableA (ColumA,ColumnB) select A, B from TableB;
2. cursor my_cur is select A, B from TableB
for my_rec in my_cur loop
insert into TableA (ColumA,ColumnB) values (my_rec.A, my_rec.B);
end loop;
also "bulk collect into" can be used.
Which one has a better performance?
Thanks for your help,
kadriyeWhat's stopping you from making 100,000 rows of test data and trying it for yourself?
Edit: I was bored enough to do it myself.
Starting insert as select 22-JUL-08 11.43.19.544000000 +01:00
finished insert as select. 22-JUL-08 11.43.19.825000000 +01:00
starting cursor loop 22-JUL-08 11.43.21.497000000 +01:00
finished cursor loop 22-JUL-08 11.43.35.185000000 +01:00
The two second gap between the two is for the delete.
Message was edited by:
Dave Hemming -
Function returning cursor to be used in select statement
I have written a function which returns a cursor. I want to use this in a select statement to display rows. How to do this.
Eg : function - test_fn return sys_refcursor. (returns empno, empname, deptname)
select test_fn from dual
This should return
empno empname deptno
1 name1 dept1
2 name 2 dept2
....Try This::
SCOTT@xe_144>variable c refcursor
SCOTT@xe_144>CREATE OR REPLACE FUNCTION get_nm
2 RETURN sys_refcursor
3 AS
4
5 c sys_refcursor;
6 BEGIN
7
8 open c for 'SELECT ENAME , SAL FROM EMP';
9
10 RETURN c;
11 END;
12 /
Function created.
SCOTT@xe_144>select get_nm into :c from dual;
GET_NM
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
ENAME SAL
SMITH 800
ALLEN 1600
WARD 1250
JONES 2975
BLAKE 2850
CLARK 2450
KING 3000
AKASH2 5000
TURNER 1500
ADAMS 1100
JAMES 950
MILLER 1300
ABC 2000
13 rows selected.Let me know if its works for you. -
PL/SQL cursors vs. SQL*plus Select statement
Hi folks, hope you're doing well,
Here is a question that kept me wondering:
Why would I use cursors when i can achieve the same thing with a SQL+ Select statement which is much easier to formulate than a cursor (e.g. you need no declaration, loops etc)?.
Thanks so much,
-aThere is no such thing as a SQL*Plus SELECT statement. The SELECT command is part of the SQL Language - not part of the SQL*Plus (very limited small vocabulary) macro language.
All SQL SELECTs (from client languages) winds up in the SQL Engine as SQL cursors. A SQL Cursor is basically the:
- SQL source code
- SQL "compiled" code (instructions on how to fetch the rows)
On the client side, client cursors (not to be confused with SQL cursors) are used. A client cursor is created in the client language when it makes SQL calls via the database client driver (called the OCI/Oracle Call Interface for Oracle clients).
Typically this is what a client does. It makes a connection to the database and gets a database handler in return. The database handler is the "communication channel" from the client to the db. In Oracle, the database handler in the client refers to the Oracle session (for that client) on db server.
A SQL statement (source code) is used by the client. This can be a SELECT statement you type in at the SQL*Plus command line. It can be a SELECT statement for the PL/SQL cursor command in a stored procedure.
The client creates a SQL handle for this SQL statement, by calling the Oracle client driver. Note that this SQL handle is a client handle - a client cursor for that SQL statement.
E.g.
a) sqlHandle = CreateSQL( databaseHandle, 'SELECT ... FROM ...')
b) sqlHandle.Parse
c) sqlHandle.Execute
After the SQL handle (client cursor) has been executed, the client can fetch rows from it.
This is what SQL*Plus does automatically for you, without you having to write the code to do it. SQL*Plus CONNECT command create a database connection handle. You enter a SELECT statement and SQL*Plus creates a SQL handle (client cursor), executes it, fetches from it, displays the rows, and closes the SQL handle when done.
The same applies to PL/SQL. You can use a SELECT statement just like that in PL/SQL. E.g.
declare
i integer;
begin
select count(*) into i from emp where deptid = 123;
end;This is called an implicit cursor. PL/SQL creates (just like SQL*Plus) an implicit client cursor. It creates and disposes of that client SQL handle for you - you do not need to do it.
Or, you can create an explicit cursor. E.g. declare
cursor c is select count(*) from emp where deptid = 123;
i integer;
begin
open c;
fetch c into i;
close c;
end;The question as to when to use implicit (client) cursors versus explicit (client) cursors depends on your requirements. Do you need to cycle through the results of the SQL? Etc.
And keep in mind that in either case, the SQL Engine creates a SQL cursor anyway on its side. -
Cursor - Suggestions for Dynamic select statements
Hey,
Am trying to define a cursor like this -
cursor c1 is
select table_name from dba_tables INTERSECT select table_name from dba_tables@SOME_DBLINKMy need is to pass this dblink as IN parameter to the procedure and use it in the select statement for cursor. How can I do this?
Any suggestion is highly appreciated. Thanks!Well that was meant to be my point. If you had two, you wouldn't (I hope) call the second one "c2" - you would be forced to think about what it represented, and name it "c_order_history" or something. Sticking "1" on the end does not make an extensible naming convention for cursors any more than it does for variables, procedures, tables or anything else, and so the "1" in "c1" is redundant because there will never be a c2.
-
How to create a cursor on a union select statement?
Hi,
Using Oracle 10g RAC +ASM ( RELEASE 1002000300)
What are is the proper way to create a cursor on a union select statement?
Is it possible?
code lines, Results in PLS-00201 error: sT := crsR.STATUS; sS := crsR.TIME;
Procedure listed below:
CREATE OR REPLACE PROCEDURE BUILD_SUMMARY IS
CURSOR csrO IS
SELECT
STATUS,
TIME
FROM (
SELECT
SUBSTR(DESCRIPTION,1,50) STATUS,
TO_CHAR(TIMESTAMP,'MM/DD/YY hh12:mi:ss') TIME
FROM GLOBALSALES.CUBE_STATUS
UNION ALL
SELECT
' TOTAL BUILD TIME',
TO_CHAR(TO_DATE('00:00:00','HH24:MI:SS') +(MAX(TIMESTAMP) - MIN(TIMESTAMP)), 'HH24:MI:SS')
FROM GLOBALSALES.CUBE_STATUS);
csrR csrO%ROWTYPE;
sT LONG :='';
sS LONG :='';
BEGIN
FOR csrR IN csrO
LOOP
sT := crsR.STATUS;
sS := crsR.TIME;
DBMS_OUTPUT.PUT_LINE(sT || ' ' || sS);
END LOOP;
END;
/csrR csrO%ROWTYPE;
Declares the csrR correct? Cursor Record of Cursor Object Type?That line declares one variable CSRR with your column structure described by the cursor (CSRO) type you declared above it. It means the CSRR variable has the columns you had in the SELECT statement in that cursor.
When you run a cursor as you did you don't need to declare the record variable. Try this:
CREATE OR REPLACE PROCEDURE build_summary IS
CURSOR csro IS
SELECT status, TIME
FROM (SELECT substr(description, 1, 50) status, to_char(TIMESTAMP, 'MM/DD/YY hh12:mi:ss') TIME
FROM globalsales.cube_status
UNION ALL
SELECT ' TOTAL BUILD TIME',
to_char(to_date('00:00:00', 'HH24:MI:SS') + (MAX(TIMESTAMP) - MIN(TIMESTAMP)),
'HH24:MI:SS')
FROM globalsales.cube_status);
--csrr csro%ROWTYPE;
st LONG := '';
ss LONG := '';
BEGIN
FOR csrr IN csro
LOOP
st := csrr.status;
ss := csrr.time;
dbms_output.put_line(st || ' ' || ss);
END LOOP;
END;Also why are you using LONG? It's a deprecated data type and Oracle recommends against using it. Either pick varchar2(4000) or CLOB.
DBMS_OUTPUT.PUT_LINE will implicitly convert that LONG data type anyway.
Maybe you are looking for
-
Getting IP address for 2nd hand HP LaserJet 6MP?
We don't have the user manual, because the printer is 2nd hand and did not come with one. I had read that the IP address might be on the back, but there are no IP-like numbers there. We are trying to get this printer running with an old clamshell iBo
-
Background Image In Table Cell
I'm not sure what I'm doing wrong or missing here. This is the first time that I'm using an image as a background. I've made a table with a 100% width with 3 colums and 3 rows. The left and right table cells have been merged and the 3 middle rows are
-
We are planning to migrate our existing environment to SQL 2012. Existing Environment: SERVER1 OS: Windows Server 2003 Standard Edition SQL Server 2005 Standard Edition 2 Node Active/Passive Clustering With Transactional Replication Number of Databas
-
What is behavior for cookie-http-only?
I noticed cookie-http-only property available in 9.2 and also 10.3 but what exactly does enabling this do? The documentation isn't very clear. "Specifies whether HttpOnly cookies are enabled. When this element is set to true, all session cookies woul
-
Two videos in the same MediaView
Hi, ill try to explain my problem: i have a code to make a player with 3 diferent videos. The problem is that i dont know how to change from one video to another. I have a "next" button, and in that button i do this: player.stop();