Home » SQL & PL/SQL » SQL & PL/SQL » Problem in Handling value with single quote in Dynamic SQL (11g Enterprise Edition Release 11.1.0.7.0/Linux)
Problem in Handling value with single quote in Dynamic SQL [message #614415] Wed, 21 May 2014 09:53 Go to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Hi All,

Please let me know how to handle data having single quote as in below anonymous block refcursor is not returning data.

CREATE TABLE tmp_mstr(NAME VARCHAR2(100),VAL VARCHAR2(100))
/
INSERT INTO tmp_mstr values('KEY1',q'[B2.LKP_VAL||'-'||B4.LKP_VAL]')
/
INSERT INTO tmp_mstr values('KEY1',q'[B1.LKP_VAL||'-'||B3.LKP_VAL]')
/
CREATE TABLE TMP_DYNASQL(ID NUMBER,NAME VARCHAR2(100))
/
INSERT INTO TMP_DYNASQL values(2,q'[B2.LKP_VAL||'-'||B4.LKP_VAL]')
/
INSERT INTO TMP_DYNASQL values(2,q'[B1.LKP_VAL||'-'||B3.LKP_VAL]')
/
COMMIT
/
--In SQLPLUS define
SQL>var vref refcursor
--Run Below code in SQLPLUS
DECLARE
v_sel_str VARCHAR2(500);
p_select_str VARCHAR2(500);
v_strt_str VARCHAR2(500);
v_len NUMBER;
v_sql VARCHAR2(5000);
--vref SYS_REFCURSOR;
BEGIN
--select q'[B2.LKP_VAL||'-'||B4.LKP_VAL,B1.LKP_VAL||'-'||B3.LKP_VAL]' into v_sel_str from dual;
SELECT NAME into v_sel_str FROM TMP_DYNASQL where rownum=1;

      dbms_output.put_line(v_sel_str);
      v_len := LENGTH(v_sel_str) - LENGTH(REPLACE(v_sel_str, ',', ''));
      dbms_output.put_line('delimited string is ::'||substr(v_sel_str,1,instr(v_sel_str,',')-1));
      FOR i in 1 .. v_len + 1 LOOP
        
        v_strt_str := SUBSTR(v_sel_str,
                             1,
                             CASE
                               WHEN INSTR(v_sel_str, ',') = 0 THEN
                                LENGTH(v_sel_str)
                               ELSE
                                INSTR(v_sel_str, ',') - 1
                             END);
     --dbms_output.put_line(v_strt_str);           
     --v_sel_str := substr(v_sel_str, instr(v_sel_str, ',') + 1);
     v_sql     := v_sql ||
                     'SELECT name, val
                          FROM tmp_mstr
                      WHERE name = :v_strt_str';
        v_sel_str := substr(v_sel_str, instr(v_sel_str, ',') + 1);
        --v_sql := SUBSTR(v_sql, 1, INSTR(v_sql, 'U', -1) - 1);
        DBMS_OUTPUT.PUT_LINE('v_sql '||v_sql);
        DBMS_OUTPUT.PUT_LINE('v_sql '||v_strt_str);
    END LOOP;
    open :vref for v_sql using v_strt_str;
end;
SQL> print :vref;

no rows selected


Thanks,
Anil MK
Re: Problem in Handling value with single quote in Dynamic SQL [message #614422 is a reply to message #614415] Wed, 21 May 2014 10:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
below anonymous block refcursor is not returning data.


What does this block intend to do? What should be the result? Use dbms_output to see if the generated is correct and if not fix the code accordingly.

Re: Problem in Handling value with single quote in Dynamic SQL [message #614424 is a reply to message #614422] Wed, 21 May 2014 10:22 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The problem has nothing to do with quotes and everything to do with the fact that you're checking the wrong column in your where clause.
Previous Topic: Retain first and last record according to id.
Next Topic: Increment sequence to max value of column
Goto Forum:
  


Current Time: Fri Apr 19 16:13:49 CDT 2024