Bind Variables in RefCursor
Hi Experts,
CREATE OR REPLACE PROCEDURE CHECK_BIND_VAR(pmCursor IN OUT SYS_REFCURSOR,pmDeptno IN NUMBER,pmOrderBy IN OUT VARCHAR2)
AS
vSql VARCHAR2(4000);
BEGIN
vSql := 'SELECT EMPNO,ENAME,ROW_NUMBER() OVER(:pmOrderBy) FROM EMP WHERE DEPTNO='||pmDeptno;
OPEN pmCursor FOR vSql USING pmOrderBy;
END CHECK_BIND_VAR;
SET SERVEROUTPUT ON;
DECLARE
vCursor SYS_REFCURSOR;
vDeptNo NUMBER := 20;
vEmpno NUMBER;
vEname EMP.ENAME%TYPE;
vIndex NUMBER;
vOrderBy VARCHAR2(400) := ' ORDER BY DEPTNO ';
BEGIN
CHECK_BIND_VAR(vCursor,vDeptNo,vOrderBy);
LOOP
FETCH vCursor INTO vEmpno,vEname,vIndex;
EXIT WHEN vCursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('EMPNO :-'||vEmpno);
DBMS_OUTPUT.PUT_LINE('ENAME :-'||vEname);
DBMS_OUTPUT.PUT_LINE('INDEX :-'||vIndex);
END LOOP;
END;
Error at line 11
ORA-00907: missing right parenthesis
ORA-06512: at "SCOTT.CHECK_BIND_VAR", line 6
ORA-06512: at line 9What i am doing it wrong...
Thanks,
DharanV
Hi Dharan,
maybe you have a look at the documentation:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch5.htm#i1211948:
A REFCURSOR bind variable is passed as a parameter to a procedure. The parameter has a REF CURSOR type. First, define the type.
CREATE OR REPLACE PACKAGE EmpPack AS
TYPE EmpInfoTyp IS REF CURSOR;
PROCEDURE EmpInfoRpt (emp_cv IN OUT EmpInfoTyp);
END EmpPack;
Next, create the stored procedure containing an OPEN... FOR SELECT statement.
CREATE OR REPLACE PACKAGE BODY EmpPack AS
PROCEDURE EmpInfoRpt (emp_cv IN OUT EmpInfoTyp) AS
BEGIN
OPEN emp_cv FOR SELECT EMPLOYEE_ID, SALARY
FROM EMP_DETAILS_VIEW
WHERE JOB_ID='SA_MAN' ;
END;
END;
Execute the procedure with a SQL*Plus bind variable as the parameter.
VARIABLE cv REFCURSOR
EXECUTE EmpPack.EmpInfoRpt(:cv)
HTH
Matthias
Similar Messages
-
Having issues with bind variable refcursor
Hi,
I have a procedure which returns just the list of employees from the emp table.
while executing this package, it gives me an error
Ex:
sql> variable cur refcursor;
sql> exec emp.getemplist(:cur);
error:
not all variables bound
Please let me know what is wrong here.
Thanks
Manjumanjukn wrote:
package is a simple one..
Exact Oracle version? Works fine on 10.2.0.4.0:
SQL> create or replace package body emp_pkg is
2 PROCEDURE getemplist (result_cursor OUT sys_refcursor)
3 IS
4 begin
5 open result_cursor for select * from emp where deptno=10;
6 end getemplist;
7 end emp_pkg;
8 /
Package body created.
SQL> variable cur refcursor;
SQL> exec emp_pkg.getemplist(:cur);
PL/SQL procedure successfully completed.
SQL> print cur
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
SQL> SY. -
Using a query with bind variable with columns defined as raw
Hi,
We are on Oracle 10.2.0.4 on Solaris 8. I have a table that has 2 columns defined as raw(18). I have a query from the front end that queries these two raw columns and it uses bind vairables. The query has a performance issue that I need to reproduce but my difficulty is that how to test the query in sqlplus using bind variables (the syntax for bind vairables fails for columns with raw datatype).
SQL> DESC TEST
Name Null? Type
ID1 RAW(18)
ID2 RAW(18)
SQL> variable b1 RAW(18);
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | REFCURSOR |
BINARY_FLOAT | BINARY_DOUBLE ] ]
The above is the error I get - i cant declare a variable as raw.
SQL> variable b2 RAW(18);
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | REFCURSOR |
BINARY_FLOAT | BINARY_DOUBLE ] ]
SQL> variable b3 RAW(18);
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | REFCURSOR |
BINARY_FLOAT | BINARY_DOUBLE ] ]
--now the actual query below
SQL> SELECT * FROM TEST WHERE ID1=:B1 AND ID2 BETWEEN :B2 AND :B3;
SP2-0552: Bind variable "B3" not declared.
(this fails due to the errors earlier)Also this is a third party app schema so that we don't have the option of modifying the data type of the columns.
Thanks,
Edited by: orausern on May 10, 2011 11:30 AMTry anonymous PL/SQL block:
declare
b1 RAW(18);
b2 RAW(18);
b3 RAW(18);
begin
b1:=..;
b2:=..;
b3:=..;
SELECT col1, col2, ..
INTO ...
FROM TEST
WHERE ID1=:B1
AND ID2 BETWEEN :B2 AND :B3;
end;
/ -
How to pass a list as bind variable?
How can I pass a list as bind variable in Oracle?
The following query work well in SQL Developer if I set ":prmRegionID=2".
SELECT COUNTRY_ID,
COUNTRY_NAME
FROM HR.COUNTRIES
WHERE REGION_ID IN (:prmRegionID);
The problem is that I can't find how to set ":prmRegionID=2,3".
I know that I can replace ":prmRegionID" by a substitution variable "&prmRegionID". The above query work well with"&prmRegionID=2" and with "&prmRegionID=2,3".
But with this solution, I lost all advantage of using binds variables (hard parse vs soft parse, SQL injection possibility, etc.).
Can some one tell me what is the approach suggest by Oracle on that subject? My developer have work a long time too find how but didn't found any answer yet.
Thank you in advance,
MBBlais wrote:
The problem is that I can't find how to set ":prmRegionID=2,3".Wrong problem. Setting the string bind variable to that means creating a single string that contains the text "+2,3+". THE STRING DOES NOT CONTAIN TWO VALUES.
So the actual problem is that you are using the WRONG data type - you want a data type that can have more than a single string (or numeric) value. Which means that using the string (varchar2) data type is the wrong type - as this only contains a single value.
You need to understand the problem first. If you do not understand the problem, you will not realise or understand the solution too.
What do you want to compare? What does the IN clause do? It deals with, and compares with, a set of values. So it needs a set data type for the bind variable. A set data type enables you to assign multiple values to the bind variable. And use this bind variable for set operations and comparisons in SQL.
Simple example:
SQL> --// create a set data type
SQL> create or replace type TStringSet is table of varchar2(4000);
2 /
Type created.
SQL>
SQL>
SQL> var c refcursor
SQL>
SQL> --// use set as bind variable
SQL> declare
2 names TStringSet;
3 begin
4 --// assign values to set
5 names := new TStringSet('BLAKE','SCOTT','SMITH','KING');
6
7 --// use set as a bind variable for creating ref cursor
8 open :c for
9 'select * from emp where ename in (select column_value from TABLE(:bindvar))'
10 using names;
11 end;
12 /
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 1981/05/01 00:00:00 2850 30
7788 SCOTT ANALYST 7566 1987/04/19 00:00:00 3000 20
7369 SMITH CLERK 7902 1980/12/17 00:00:00 800 20
7839 KING PRESIDENT 1981/11/17 00:00:00 5000 10
SQL>
SQL> --// alternative set comparison
SQL> declare
2 names TStringSet;
3 begin
4 --// assign values to set
5 names := new TStringSet('BLAKE','SCOTT','SMITH','KING');
6
7 --// use set as a bind variable for creating ref cursor
8 open :c for
9 'select * from emp where TStringSet(ename) submultiset of (:bindvar)'
10 using names;
11 end;
12 /
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 1980/12/17 00:00:00 800 20
7698 BLAKE MANAGER 7839 1981/05/01 00:00:00 2850 30
7788 SCOTT ANALYST 7566 1987/04/19 00:00:00 3000 20
7839 KING PRESIDENT 1981/11/17 00:00:00 5000 10
SQL> -
Cursor bind variable syntax err
Any one help me in correcting the syntax, for cursor return.
Oracle version 10g
I am trying to execute the sproc and print the cursor results using bind variable, I am getting he below error
Variable V_REFCUR REFCURSOR
Variable V_REFCUR2 REFCURSOR
variable swp_ret_value NUMBER
exec DELETE_POSITION(0,0,2,'U',:swp_ret_value,:V_REFCUR,:V_REFCUR2)
print swp_ret_value
Print v_refcur
Print v_refcur2
-- output
Error starting at line 5 in command:
exec DELETE_POSITION(0,0,2,'U',:swp_ret_value,:V_REFCUR,:V_REFCUR2)
Error report:
Cursor is closed.
SWP_RET_VALUE
V_REFCUR
V_REFCUR2
------Edited by: NeilCSE on Jan 18, 2011 4:08 AMit is calling another proc
PROCEDURE Assign_outRefcur2 (CurrentCursor IN OUT SYS_REFCURSOR
, v_ref_cur IN OUT SYS_REFCURSOR
, v_ref_cur2 IN OUT SYS_REFCURSOR
AS
InitCursor SYS_REFCURSOR;
BEGIN
IF NOT v_ref_cur%IsOpen THEN v_ref_cur := CurrentCursor;
ELSIF NOT v_ref_cur2%IsOpen THEN v_ref_cur2 := CurrentCursor;
END IF;
CurrentCursor:= InitCursor;
END; -
Using BIND VARIABLES in REF CURSOR(s)
Hello I am having trouble making my pl/sql program work using bind variables.
here is a little snipit from my code:
declare
type ref_type is REF CURSOR;
ref_cursor ref_type;
summation_table varchar2(4000);
begin
summation_table := 'table_sum tsum';
open ref_cursor for
select * from :summation_table
where tsum.revenue = 1000
USING summation_table;
end;
The Error that I get is
"bad bind variable 'summation_table'"
could someone please help? I think the way 'tsum' is used could be a problem, but I don't know.SQL> CREATE TABLE TABLE_SUM(REVENUE NUMBER(10),
2 OTHER NUMBER(10));
Table created.
SQL> INSERT INTO TABLE_SUM VALUES(1000,1);
1 row created.
SQL> INSERT INTO TABLE_SUM VALUES(1000,2);
1 row created.
SQL> variable alpha refcursor
SQL> INSERT INTO TABLE_SUM VALUES(2000,3);
1 row created.
SQL> DECLARE
2 summation_table varchar2(30) := 'table_sum tsum';
3 PROCEDURE MYTEST(p_out out sys_refcursor)
4 IS
5 BEGIN
6 OPEN p_out for 'select * from '||summation_table||
7 ' where tsum.revenue = :val' using 1000;
8 END;
9 BEGIN
10 MYTEST(:alpha);
11 END;
12 /
PL/SQL procedure successfully completed.
SQL> print alpha
REVENUE OTHER
1000 1
1000 2
SQL> -
Version details
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Solaris: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - ProductionBelow is the procedure where I'm using ref cursor
CREATE OR REPLACE PROCEDURE ref_sample
p_account_nbr in varchar2,
p_ref_out out sys_refcursor
IS
BEGIN
OPEN p_ref_out FOR
SELECT account_nbr,status,(
CASE
WHEN status = 'Pending' THEN
req_mail_date
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_mail_date,req_mail_date)
END
)req_mail_date ,
CASE
WHEN status = 'Pending' THEN
NULL
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_user_id,req_user_id)
END
)req_user_id
FROM X_tbl
WHERE account_nbr IN p_account_nbr
AND TRUNC(upload_date) = TRUNC(SYSDATE)
ORDER BY upload_date DESC ;
END;
/My input parameter p_account_nbr looks like ('a1','a2','a3')
Now,after knowing the importance of bind variables I'd like to make use of them in the above ref cursor.
But,here my input parameter is a string of varying length..either I've to go for the approach suggested here
http://asktom.oracle.com/pls/asktom/f?p=100:11:3667281145899708::::P11_QUESTION_ID:110612348061
or
http://www.dba-oracle.com/t_cursor_sharing.htm
I'm not much clear with the first approach,so I'm thinking of to modify my procedure as below
CREATE OR REPLACE PROCEDURE ref_sample
p_account_nbr in varchar2,
p_ref_out out sys_refcursor
IS
BEGIN
alter session set cursor_sharing=force;
OPEN p_ref_out FOR
SELECT account_nbr,status,(
CASE
WHEN status = 'Pending' THEN
req_mail_date
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_mail_date,req_mail_date)
END
)req_mail_date ,
CASE
WHEN status = 'Pending' THEN
NULL
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_user_id,req_user_id)
END
)req_user_id
FROM X_tbl
WHERE account_nbr IN p_account_nbr
AND TRUNC(upload_date) = TRUNC(SYSDATE)
ORDER BY upload_date DESC ;
alter session set cursor_sharing=exact;
END;
/Please let me know if the above modified code is fine or should I use bind variables??Also let me know better approach of both.The correct way to do this is use an array type for the input values as in this example.
SQL> create or replace procedure p
2 (
3 p_values sys.odcivarchar2list,
4 c out sys_refcursor
5 ) as
6 begin
7 open c for
8 select object_name, owner, object_type
9 from all_objects
10 where object_name in (select column_value from table(p_values));
11 end;
12 /
Procedure created.
SQL> var c refcursor
SQL> exec p (sys.odcivarchar2list('DUAL','USER_VIEWS'), :c)
PL/SQL procedure successfully completed.
SQL> print c
OBJECT_NAME OWNER OBJECT_TYPE
DUAL SYS TABLE
DUAL PUBLIC SYNONYM
USER_VIEWS SYS VIEW
USER_VIEWS PUBLIC SYNONYM
SQL> exec p (sys.odcivarchar2list('DUAL','USER_VIEWS','ALL_OBJECTS','ALL_SOURCE'), :c)
PL/SQL procedure successfully completed.
SQL> print c
OBJECT_NAME OWNER OBJECT_TYPE
DUAL SYS TABLE
DUAL PUBLIC SYNONYM
ALL_OBJECTS SYS VIEW
ALL_OBJECTS PUBLIC SYNONYM
USER_VIEWS SYS VIEW
USER_VIEWS PUBLIC SYNONYM
ALL_SOURCE SYS VIEW
ALL_SOURCE PUBLIC SYNONYM
8 rows selected.
SQL>That and other methods are described here.
http://tkyte.blogspot.com/2006/06/varying-in-lists.html
You would not use dynamic SQL. -
SP2-0552: Bind variable not declared error. Any help please?
Hi Experts,
I have a question regarding the error that I am getting: SP2-0552: Bind variable "V_COUNT_TOT_BAL" not declared.
I have 'out' parameters declared in my procedure and executing the same from sql script as shown below:
set ver off
set serverout on
set linesize 8000
Declare
Variable v_count_dtl_bal NUMBER(10);
Variable v_updat_dtl_bal NUMBER(10);
Variable v_count_tot_bal NUMBER(10);
Begin
execute load_abc.insert_abc_bal(:v_count_dtl_bal,:v_updat_dtl_bal,:v_count_tot_bal);
End;
exit;
So, when this sql script runs it given me the above error. However, all the result looks good and there's no problem with the data or anything else that might be impacted. I suspect this error stems from the code in the sql script above.
Any idea what am I doing wrong?
Thanks in advance for any inputs.Thanks Frank. I still receive the same error if I follow your example or any of the ones explained above. This is what I am getting and still an error underneath:
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n CHAR) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | REFCURSOR |
BINARY_FLOAT | BINARY_DOUBLE ] ]
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n CHAR) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | REFCURSOR |
BINARY_FLOAT | BINARY_DOUBLE ] ]
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n CHAR) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | REFCURSOR |
BINARY_FLOAT | BINARY_DOUBLE ] ]
SP2-0552: Bind variable "V_COUNT_TOT_BAL" not declared. -
Please help on binding variables
Hello,
I need to bind the variable in following function.
If the single deptno is passed into function, the function will return correct result. However, if the set of deptno is passed, the function cannot give correct result.
Could anyone please help me on it...
Thanks in advance!!!
scott@ORA111>create or replace function my_test (p_deptno_list varchar2)
2 return sys_refcursor
3 as
4 rec sys_refcursor;
5 v_sql varchar2(1000);
6
7 begin
8 v_sql := 'select * from emp where deptno in (:1)';
9 open rec for v_sql using p_deptno_list;
10 return rec;
11
12 end;
13 /
Function created.
Elapsed: 00:00:00.04
scott@ORA111>var my_rec refcursor
scott@ORA111>execute :my_rec := my_test('10');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
scott@ORA111>print :my_rec
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
9999 TEMP SALESMAN 7782 27-APR-09 6000 10
Elapsed: 00:00:00.01
scott@ORA111>var my_rec refcursor
scott@ORA111>execute :my_rec := my_test('10, 20');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.01
scott@ORA111>print :my_rec
ERROR:
ORA-12801: error signaled in parallel query server P000
ORA-01722: invalid number
no rows selected
Elapsed: 00:00:00.01Hello Beijing,
Thank you so much for your help!!!
We need a procedure/function to take the inputs as query conditions, execute the query, store the result in refer cursor and pass it into Client Application.
With the help on this Forum, I have done following test. Only one thing that I have not figure out is the variable-list. As you know the line 15 in my procedure will not give the correct result. However, I am not able to concatenate the bind variable in the string.
Please help, thanks again.
SQL> create or replace function test_function
2 (p_deptno_list varchar2,
3 p_hire_start emp.hiredate%type,
4 p_hire_end emp.hiredate%type,
5 p_job emp.job%type)
6 return sys_refcursor
7 as
8 rec sys_refcursor;
9 v_sql varchar2(1000);
10
11 begin
12 v_sql := 'select * from emp';
13
14 if p_deptno_list is not null then
15 v_sql := v_sql ||' where instr(replace(:1, '' '', ''''), deptno) > 0';
16 else
17 v_sql := v_sql ||' where :1 is null';
18 end if;
19
20 if p_hire_start is not null and p_hire_end is not null then
21 v_sql := v_sql ||' and hiredate between :2 and :3';
22 else
23 v_sql := v_sql ||' and :2 is null and :3 is null';
24 end if;
25
26
27 if p_job is not null then
28 v_sql := v_sql ||' and job = :4';
29 else
30 v_sql := v_sql ||' and :4 is null';
31 end if;
32
33 open rec for v_sql using p_deptno_list, p_hire_start, p_hire_end, p_job;
34 return rec;
35
36 end;
37 /
Function created.
SQL> set lin 200
SQL> var rec refcursor
SQL> execute :rec := test_function(null, null, null, null);
PL/SQL procedure successfully completed.
SQL> print rec
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 3450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 6000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 4300 10
14 rows selected.
SQL> execute :rec := test_function('20, 30', null, null, null);
PL/SQL procedure successfully completed.
SQL> print rec
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
11 rows selected.
SQL> execute :rec := test_function('20', to_date('01-JAN-1981','DD-MON-YYYY'), to_date('31-DEC-1981','DD-MON-YYYY'), null);
PL/SQL procedure successfully completed.
SQL> print rec
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2975 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
SQL> execute :rec := test_function('20', to_date('01-JAN-1981','DD-MON-YYYY'), to_date('31-DEC-1981','DD-MON-YYYY'), 'MANAGER');
PL/SQL procedure successfully completed.
SQL> print rec
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2975 20
SQL> spool off -
Number of Bind variables passed AND Ref Cursor
hi all
well i have an interesting problem
i need to construct a query for a refcursor besade on conditions....
as:
input params: in_lname and in_fname
sql_stmt := 'Select first_name, ' || 'last_name, ' ||
'from table1';
IF in_lname is not null then
-- we are selecting guests by Lastname, Lastname & Firstname,
-- Lastname & Firstname & Zip, or Lastname & Zip
sql_stmt := sql_stmt || 'where gc_last_name = :1 ';
IF in_fname is not null then
sql_stmt := sql_stmt || ' and gc_first_name = :2 ';
END IF;
END IF;
open fm_lom_cv for sql_stmt using in_lname, in_fname;
Now here the in_lname is required BUT in_fname can be NULL
hence we dont excute or create the sql with sql_stmt || ' and gc_first_name = :2 ';
so :2 will not be used
hence it gives error IF in_fname is null ie ORA-01006: bind variable does not exist
Please suggest how to handle it....
thanksmay you have to set a some variable to check
not perfect but somthing like this
declare
ct number:=1;
sql_stmt varchar2(150);
BEGIN
sql_stmt := 'Select first_name, ' || 'last_name, ' ||
'from table1';
IF in_lname is not null then
-- we are selecting guests by Lastname, Lastname & Firstname,
-- Lastname & Firstname & Zip, or Lastname & Zip
sql_stmt := sql_stmt || 'where gc_last_name = :1 ';
ct:=1;
IF in_fname is not null then
sql_stmt := sql_stmt || ' and gc_first_name = :2 ';
ct:=2;
END IF;
END IF;
case when ct=1 then
Open fm_lom_cv for sql_stmt using in_lname;
when ct=2 then
Open fm_lom_cv for sql_stmt using in_lname,in_fname;
end case;
-
HOW to get the bind variables list.
I've the following problem : I've some SQL queries stored in my DB as VARCHAR2 values.
I need to use DBMS_SQL in order to execute them.
In theese SQL statements I have some bind variables like :NUMORD. (ex. SELECT 'X' FROM YYYY WHERE FIELD_1 = :NUMORD).
I don't know "a priori" names and number of such variables.
Is there any way to have a list of such bind variables ?
I found DBMS_DESCRIBE but is seems to act only on stored procedures/functions.
I know I can tray to inspect the code looking for every ':' but a cleaner solution woulf be appreciated.
Tks
TullioI don't know "a priori" names and number of such variables.
Is there any way to have a list of such bind variables ?The names are probably not important, but you can get the count (and other useful information) like this:
SQL> var cur refcursor
SQL> declare
cl clob;
begin
dbms_lob.createtemporary (cl, true);
sys.utl_xml.parsequery (user, 'select e.deptno, :x x from emp e where deptno = :deptno', cl);
open :cur for select cl cl from dual union all
select 'Count binds: ' || xmlquery('count(//BIND_VARIABLE)' passing xmltype(cl) returning content).getclobval() from dual;
dbms_lob.freetemporary (cl);
end;
PL/SQL procedure successfully completed.
SQL> print cur
CL
<QUERY>
<SELECT>
<SELECT_LIST>
<SELECT_LIST_ITEM>
<COLUMN_REF>
<SCHEMA>MICHAEL</SCHEMA>
<TABLE>EMP</TABLE>
<TABLE_ALIAS>E</TABLE_ALIAS>
<COLUMN>DEPTNO</COLUMN>
</COLUMN_REF>
</SELECT_LIST_ITEM>
<SELECT_LIST_ITEM>
<BIND_VARIABLE>1</BIND_VARIABLE>
<COLUMN_ALIAS>X</COLUMN_ALIAS>
</SELECT_LIST_ITEM>
</SELECT_LIST>
</SELECT>
<FROM>
<FROM_ITEM>
<SCHEMA>MICHAEL</SCHEMA>
<TABLE>EMP</TABLE>
<TABLE_ALIAS>E</TABLE_ALIAS>
</FROM_ITEM>
</FROM>
<WHERE>
<EQ>
<COLUMN_REF>
<SCHEMA>MICHAEL</SCHEMA>
<TABLE>EMP</TABLE>
<COLUMN>DEPTNO</COLUMN>
</COLUMN_REF>
<BIND_VARIABLE>2</BIND_VARIABLE>
</EQ>
</WHERE>
</QUERY>
Count binds: 2
2 rows selected. -
HI
Can anyone help with why I am getting BIND variable undeclared please ?
Here is my spec file:
create or replace
package common as
type recordList is RECORD
recList VARCHAR2(100)
type recordList_CV is ref cursor return recordList;
PROCEDURE generateRecord(p_owner IN VARCHAR2, p_tableName IN VARCHAR2, recMainlist IN OUT recordList_CV);
end;
SQLPlus File:
--variable o VARCHAR2(40);
--variable t VARCHAR2(40);
var rc refcusror;
--:o := 'store';
--:t := 'employees';
rem exec common.generateRecord(:o,:t,:rec);
execute common.generateRecord('store','employees',:rc);
print rec;
Output:
SQL> @common_test.sql
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | BLOB | BFILE
REFCURSOR | BINARY_FLOAT | BINARY_DOUBLE ] ]
SP2-0552: Bind variable "RC" not declared.
SP2-0552: Bind variable "REC" not declared.> var rc refcusror;
Check spelling.
Also, REC not declared. -
Report Performance with Bind Variable
Getting some very odd behaviour with a report in APEX v 3.2.1.00.10
I have a complex query that takes 5 seconds to return via TOAD, but takes from 5 to 10 minutes in an APEX report.
I've narrowed it down to one particular bind. If I hard code the date in it returns in 6 seconds, but if I let the date be passed in from a parameter it takes 5+ minutes again.
Relevant part of the query (an inline view) is:
,(select rglr_lect lect
,sum(tpm) mtr_tpm
,sum(enrols) mtr_enrols
from ops_dash_meetings_report
where meet_ev_date between to_date(:P35_END_DATE,'DD/MM/YYYY') - 363 and to_date(:P35_END_DATE,'DD/MM/YYYY')
group by rglr_lect) RPV
I've tried replacing the "to_date(:P35_END_DATE,'DD/MM/YYYY') - 363" with another item which is populated with the date required (and verified by checking session state). If I replace the :P35_END_DATE with an actual date the performance is fine again.
The weird thing is that a trace file shows me exactly the same Explain Plan as the TOAD Explain where it runs in 5 seconds.
Another odd thing is that another page in my application has the same inline view and doesn't hit the performance problem.
The trace file did show some control characters (circumflex M) after each line of this report's query where these weren't anywhere else on the trace queries. I wondered if there was some sort of corruption in the source?
No problems due to pagination as the result set is only 31 records and all being displayed.
Really stumped here. Any advice or pointers would be most welcome.
Jon.Don't worry about the Time column, the cost and cardinality are more important to see whther the CBO is making different decisions for whatever reason.
Remember that the explain plan shows the expected execution plan and a trace shows the actual execution plan. So what you want to do is compare the query with bind variables from an APEX page trace to a trace from TOAD (or sqlplus or whatever). You can do this outside APEX like this...
ALTER SESSION SET EVENTS '10046 trace name context forever, level 1';Enter and run your SQL statement...;
ALTER SESSION SET sql_trace=FALSE;This will create a a trace file in the directory returned by...
SELECT value FROM v$parameter WHERE name = 'user_dump_dest' Which you can use tkprof to format.
I am assuming that your not going over DB links or anything else slightly unusual?
Cheers
Ben -
How to Dene a Data Link Between Queries: Bind Variables
This is an interesting topic and I cannot get it to work using Bind Variables.
I have 2 queries: Q1 and Q2. Q2 needs c_id, account_code and account_type from Q1.
Whe I run the data template below, I get only the data for Q1.
Now people may argue that there is no data in Q2 for the relevant clause. So if I even remove the where clause in Q2 I still get no joy i.e Data appears for Q1 but not for Q2
<dataTemplate name="FLCMR519_DATA_SET" description="Termination Quote Report">
<parameters>
<parameter name="cid" dataType="number" defaultValue="1"/>
<parameter name="p_cln_id" dataType="number" defaultValue="62412"/>
</parameters>
<dataQuery>
<sqlStatement name="Q1">
<![CDATA[SELECT qm.qmd_id,
qm.contract_period,
qm.quo_quo_id||'/'||qm.quote_no||'/'||qm.revision_no reference_no,
qm.contract_distance,
qm.mdl_mdl_id,
q.qpr_qpr_id,
q.quo_id,
q.drv_drv_id,
qm.revision_user username,
pb.first_name||' '||pb.last_name op_name,
pb.telephone_no,
pb.facsimile_no,
pb.email,
q.c_id c_id,
q.account_type account_type,
q.account_code account_code,
m.model_desc,
ph.payment_description payment_head_desc,
cl.fms_fms_id,
cl.start_date,
cl.end_date,
cl.actual_end_date,
cl.con_con_id,
cl.cln_id,
cl.term_qmd_id term_qmd_id,
qm2.contract_period term_period,
qm2.contract_distance term_distance
FROM quotations q,
quotation_models qm,
contract_lines cl,
personnel_base pb,
models m,
model_types mt,
payment_headers ph,
quotation_models qm2
WHERE q.quo_id = qm.quo_quo_id
AND cl.cln_id = :p_cln_id
AND qm.qmd_id = cl.qmd_qmd_id
AND qm2.revision_user = pb.employee_no (+)
AND qm.mdl_mdl_id = m.mdl_id
AND m.mtp_mtp_id = mt.mtp_id
AND qm.payment_id = ph.payment_header_id
AND qm2.qmd_id (+) = cl.term_qmd_id
]]>
</sqlStatement>
<sqlStatement name="Q2">
<![CDATA[SELECT ea.c_id, ea.account_type,ea.account_code,ea.account_name
FROM external_accounts ea
WHERE ea.c_id = :c_id
AND ea.account_type = :account_type
AND ea.account_code = :account_code
]]>
</sqlStatement>
</dataQuery>
</dataTemplate>Defining dataStructure section is mandatory for multiple queries.
-
Can I use bind variable instaed of writing static COLUMN Name
Hi , I am having a table containing id and column names, the data is stored against that id in other tables. Now I wish to update data into another table so that it goes into apppropriate column without using decode function.
I am trying to do this:
EXECUTE IMMEDIATE 'update TEST set :1 = :2
where PROJECT_ID= :3 and UNIQUE_ID= :4' using P_DEST_COLUMN, P_TEXT_VALUE, P_PROJ_ID, P_TASK_UID;
the values P_DEST_COLUMN, P_TEXT_VALUE, P_PROJ_ID, P_TASK_UID are populated using a cursor in PL/SQl
Is this statement valid? If not can you tell me how to do it as I am getting some error I am unable to comprehend.
thanks
RishabhColumn names cannot be substituted at run-time as bind variables. If you need to specify the column name at run-time, you'd need to construct a new string and execute that string dynamically, i.e.
EXECUTE IMMEDIATE 'UPDATE test SET ' || p_dest_column || ' = :1 ' || ...From a data model standpoint, storing column names as data elements in another table is generally a rather poor idea. It's likely to make ad-hoc reporting nearly impossible and to cause a lot more parsing than would otherwise be required.
Justin
Maybe you are looking for
-
PopulateAttributeAsChanged() and primary keys
I have a column on a table wich is populated from several entries into transient attributes eg. Field1 A Field2 B Field3 C postChanges() method in the entity has been overriden to concatenate these values together ie. A/B/C I call populateAttributeAs
-
Horizontal spry menu misalignment
my horizontal submenu items do not align with main menu items in ie7. works fine in ff (duh). i have tried altering the margins--no go. i have tried making left: auto--no go i have tried playing with zindex--no go. i hate ie.
-
Hi All, i have to upload signature in SAP fo purchase order. I try to upload signature in SAP by usnig program RSTXLDMC but it gives me following error. TIFF format error: No baseline TIFF 6.0 file Please help me to find out the solution. Thanks Piy
-
Hi all, I need sample code and processing steps for EDI-IDocs for WMS concept.
-
Hi All, When I run a page in Jdeveloper (even the successfully tested projects), i get the output page as Cannot find the server. In my Embedded OC4j preferences, i have given my local IP as prefered as I am using VPN. Then , the proxy settings as No