Dynamic SQL in REF Cursor [message #328381] |
Fri, 20 June 2008 00:30 |
himang
Messages: 282 Registered: March 2005 Location: Bangalore
|
Senior Member |
|
|
Want to know some expert advice on dynamic SQL. Below I have 2 ways of building REF Cursor. There are parameters in the SELECT and I am using Dynamic SQL for reporting purpose and this report will hardly run once in a day, so there is no point in using BIND variables and I don’t see any performance impact as well?
DECLARE
TYPE t_line_cursor IS REF CURSOR;
l_cursor t_line_cursor;
sql_stmt VARCHAR2(1000);
BEGIN
sql_stmt := 'Select 'X' from DUAL';
OPEN l_cursor for sql_stmt;
END;
/
DECLARE
TYPE t_line_cursor IS REF CURSOR;
l_cursor t_line_cursor;
BEGIN
OPEN l_cursor for Select 'X' from DUAL;
END;
/
My reviewer commented to avoid Dynamic SQL in the query but for me both the way it’s same. Is there any performance or any good practice issue using dynamic SQL?
|
|
|
Re: Dynamic SQL in REF Cursor [message #328384 is a reply to message #328381] |
Fri, 20 June 2008 00:40 |
|
Michel Cadot
Messages: 68718 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Apart from the fact that the first one does not compile and if it did it'd require a little bit more parsing than the second one, not really.
Only valid for this case.
Don't use dynamic SQL when you can use static one.
Use dynamic SQL only when you can't use static one.
Simple rule.
Regards
Michel
[Updated on: Fri, 20 June 2008 00:45] Report message to a moderator
|
|
|
|
|
|
|
Re: Dynamic SQL in REF Cursor [message #328430 is a reply to message #328417] |
Fri, 20 June 2008 03:08 |
|
Michel Cadot
Messages: 68718 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
What is the difference between Oracle gives a built-in type and you create a type?
Here another way but less efficient:
SQL> var maliste varchar2(100)
SQL> exec :maliste := '5,11,13,22,23,31,44,45'
PL/SQL procedure successfully completed.
SQL> select id, valeur
2 from t
3 where id IN (
4 select substr(:maliste,
5 instr(','||:maliste||',', ',', 1, rn),
6 instr(','||:maliste||',', ',', 1, rn+1)
7 - instr(','||:maliste||',', ',', 1, rn) - 1) value
8 from (select rownum rn from dual
9 connect by level
10 <= length(:maliste)-length(replace(:maliste,',',''))+1)
11 )
12 order by id
13 /
ID VALEUR
---------- ------------------------------
5 SYSTEM
11 OUTLN
22 MICHEL
23 OPS$MCADOT101205
31 SCOTT
5 rows selected.
Regards
Michel
|
|
|
Re: Dynamic SQL in REF Cursor [message #328446 is a reply to message #328430] |
Fri, 20 June 2008 03:56 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
If you put a leading and trailing comma on the list in pi_val, you can do it pretty easily. You just need to wrap the COL1 value in commas as well, and use INSTR: 1 DECLARE
2 pi_val VARCHAR2(100) := ',1,2,3,5,';
3 TYPE t_line_cursor IS REF CURSOR;
4 l_cursor t_line_cursor;
5 BEGIN
6 for rec in (SELECT * from P1 WHERE instr(pi_val, ','||COL1||',')>0) loop
7 dbms_output.put_line(rec.col1);
8 end loop;
9* END;
SQL> /
1
2
3
5
PL/SQL procedure successfully completed.
|
|
|
Re: Dynamic SQL in REF Cursor [message #328470 is a reply to message #328446] |
Fri, 20 June 2008 05:10 |
himang
Messages: 282 Registered: March 2005 Location: Bangalore
|
Senior Member |
|
|
Nice and simple idea... As the database is not too heavy and so performance is not a big issue... (as I suppose this would ignore the indexes on the column)...
Thanks
|
|
|