Oracle中动态SQL拼接



    
    
  1. 1. 直接用单引号,单引号的使用是就近配对,即就近原则。从第二个单引号开始被视为转义符
  2. v_sql := ' insert into BJTONGRENTANGTEMPTB select distinct h.sellerid,h.sellercode,h.sellername,h.prodcode,h.prodname from historyofsales_day h '
  3. ||' where h.sellerid in ( select distinct ovalorgid from bjtongrentangpc ) '
  4. ||' and h.prodcode in ( select prodcode from buproduct where bucode= '''||v_bucode||''') '
  5. ||' and to_char(h.salesdate, ''yyyyMM '') = ''' || v_year||v_month||'''';
  6. if v_productcode is not null then
  7. v_sql := v_sql || ' and h.prodcode = '''||v_productcode||'''';
  8. end if;
  9. if v_seller is not null then
  10. v_sql := v_sql || ' and h.sellername like ''% '||v_seller||'% ''';
  11. end if;
  12. if v_provincecode is not null then
  13. v_sql := v_sql || ' and h.buyerprovincecode = '''||v_provincecode||'''';
  14. end if;
  15. if v_productspec is not null then
  16. v_sql := v_sql || ' and h.prodspec like ''% '||v_productspec||'% ''';
  17. end if;
  18. execute immediate v_sql;
  19. commit;

    
    
  1. 2. 利用chr(39)转义单引号
  2. v_sql := ' insert into BJTONGRENTANGTEMPTB select distinct h.sellerid,h.sellercode,h.sellername,h.prodcode,h.prodname from historyofsales_day h '
  3. ||' where h.sellerid in ( select distinct ovalorgid from bjtongrentangpc ) '
  4. ||' and h.prodcode in ( select prodcode from buproduct where bucode= '||chr(39)||v_bucode||chr(39)||') '
  5. ||' and to_char(h.salesdate, ''yyyyMM '') = ' ||chr(39)|| v_year||v_month||chr(39);
  6. if v_productcode is not null then
  7. v_sql := v_sql || ' and h.prodcode = '||chr(39)||v_productcode||chr(39);
  8. end if;
  9. if v_seller is not null then
  10. v_sql := v_sql || ' and h.sellername like '||chr(39)||'% '||v_seller||'% '||chr(39);
  11. end if;
  12. if p_provincename is not null then
  13. v_sql := v_sql || ' and h.buyerprovincename = '||chr(39)||p_provincename||chr(39);
  14. end if;
  15. if v_productspec is not null then
  16. v_sql := v_sql || ' and h.prodspec like '||chr(39)||'% '||v_productspec||'% '||chr(39);
  17. end if;

    
    
  1. 3. 利用 execute immediate using占位符语法处理
  2. v_sql := ' insert into BJTONGRENTANGTEMPTB select distinct h.sellerid,h.sellercode,h.sellername,h.prodcode,h.prodname from historyofsales_day h '
  3. || ' where h.sellerid in (select distinct ovalorgid from bjtongrentangpc ) '
  4. || ' and h.prodcode in (select prodcode from buproduct where bucode= :1)'
  5. --||' and to_char(h.salesdate,''yyyyMM'') =:v2:v3';
  6. || ' and to_char(h.salesdate,''yyyy'') =:v2';
  7. --execute immediate v_sql using v_bucode,v_year,v_month; --error ORA-01006:绑定变量不存在
  8. execute immediate v_sql using v_bucode,v_year;
  9. commit;

    
    
  1. 4. 其他的
  2. select q '[it's a cat] ' from dual;






  
  
  1. 1. 直接用单引号,单引号的使用是就近配对,即就近原则。从第二个单引号开始被视为转义符
  2. v_sql := ' insert into BJTONGRENTANGTEMPTB select distinct h.sellerid,h.sellercode,h.sellername,h.prodcode,h.prodname from historyofsales_day h '
  3. ||' where h.sellerid in ( select distinct ovalorgid from bjtongrentangpc ) '
  4. ||' and h.prodcode in ( select prodcode from buproduct where bucode= '''||v_bucode||''') '
  5. ||' and to_char(h.salesdate, ''yyyyMM '') = ''' || v_year||v_month||'''';
  6. if v_productcode is not null then
  7. v_sql := v_sql || ' and h.prodcode = '''||v_productcode||'''';
  8. end if;
  9. if v_seller is not null then
  10. v_sql := v_sql || ' and h.sellername like ''% '||v_seller||'% ''';
  11. end if;
  12. if v_provincecode is not null then
  13. v_sql := v_sql || ' and h.buyerprovincecode = '''||v_provincecode||'''';
  14. end if;
  15. if v_productspec is not null then
  16. v_sql := v_sql || ' and h.prodspec like ''% '||v_productspec||'% ''';
  17. end if;
  18. execute immediate v_sql;
  19. commit;

  
  
  1. 2. 利用chr(39)转义单引号
  2. v_sql := ' insert into BJTONGRENTANGTEMPTB select distinct h.sellerid,h.sellercode,h.sellername,h.prodcode,h.prodname from historyofsales_day h '
  3. ||' where h.sellerid in ( select distinct ovalorgid from bjtongrentangpc ) '
  4. ||' and h.prodcode in ( select prodcode from buproduct where bucode= '||chr(39)||v_bucode||chr(39)||') '
  5. ||' and to_char(h.salesdate, ''yyyyMM '') = ' ||chr(39)|| v_year||v_month||chr(39);
  6. if v_productcode is not null then
  7. v_sql := v_sql || ' and h.prodcode = '||chr(39)||v_productcode||chr(39);
  8. end if;
  9. if v_seller is not null then
  10. v_sql := v_sql || ' and h.sellername like '||chr(39)||'% '||v_seller||'% '||chr(39);
  11. end if;
  12. if p_provincename is not null then
  13. v_sql := v_sql || ' and h.buyerprovincename = '||chr(39)||p_provincename||chr(39);
  14. end if;
  15. if v_productspec is not null then
  16. v_sql := v_sql || ' and h.prodspec like '||chr(39)||'% '||v_productspec||'% '||chr(39);
  17. end if;

  
  
  1. 3. 利用 execute immediate using占位符语法处理
  2. v_sql := ' insert into BJTONGRENTANGTEMPTB select distinct h.sellerid,h.sellercode,h.sellername,h.prodcode,h.prodname from historyofsales_day h '
  3. || ' where h.sellerid in (select distinct ovalorgid from bjtongrentangpc ) '
  4. || ' and h.prodcode in (select prodcode from buproduct where bucode= :1)'
  5. --||' and to_char(h.salesdate,''yyyyMM'') =:v2:v3';
  6. || ' and to_char(h.salesdate,''yyyy'') =:v2';
  7. --execute immediate v_sql using v_bucode,v_year,v_month; --error ORA-01006:绑定变量不存在
  8. execute immediate v_sql using v_bucode,v_year;
  9. commit;

  
  
  1. 4. 其他的
  2. select q '[it's a cat] ' from dual;




猜你喜欢

转载自blog.csdn.net/huiyeyeyey/article/details/82787545