我有两个关于动态SQL的问题;请解释原因,谢谢

问题描述 投票:0回答:0
This is first code,no error
L_SQL := 'SELECT COUNT(1)
          FROM TABLE A
          WHERE COL1 = ''' || V_1 || '''     -- where col1 = '2021YEAR_REPORT'
          AND COL2 LIKE ''%' || V_2 || '''   -- and col2 like '%202302'
          AND COL3 = ''' || 
          V_4 || '''';

This is second code,error PLS-00103
create or replace noneditionable procedure p1 is
  v1    varchar2(10);
  v_new number(10);
  sql_1 varchar2(200);
begin
  for i in 1..10 loop
    begin
    v1 := to_char( i);
    end;
  end loop;
  sql_1 := 'select count(1) from table_1 a where a.col1 ='|| ''v1''; -- error
  EXECUTE IMMEDIATE sql_1
          into v_new;
end; 

都是动态SQL的问题,第一个代码是正确的,但是我很迷惑其中'的使用,为什么会有那么多''',以后遇到这种情况应该怎么写代码,或者读别人的代码?为了研究如何使用',我写下第二段代码,他报错,这让我更加困惑,请帮我整理一下这个知识点,我会很感激

sql oracle plsql dynamic-sql
© www.soinside.com 2019 - 2024. All rights reserved.