zoukankan      html  css  js  c++  java
  • Oracle中动态SQL拼接

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

    2. 利用chr(39)转义单引号
    v_sql := ' insert into BJTONGRENTANGTEMPTB select distinct h.sellerid,h.sellercode,h.sellername,h.prodcode,h.prodname from historyofsales_day h '
    ||' where h.sellerid in (select distinct ovalorgid from bjtongrentangpc ) '
    ||' and h.prodcode in (select prodcode from buproduct where bucode= '||chr(39)||v_bucode||chr(39)||')'
    ||' and to_char(h.salesdate,''yyyyMM'') =' ||chr(39)|| v_year||v_month||chr(39);
    if v_productcode is not null then
    v_sql := v_sql || ' and h.prodcode = '||chr(39)||v_productcode||chr(39);
    end if;
    if v_seller is not null then
    v_sql := v_sql || ' and h.sellername like '||chr(39)||'%'||v_seller||'%'||chr(39);
    end if;
    if p_provincename is not null then
    v_sql := v_sql || ' and h.buyerprovincename = '||chr(39)||p_provincename||chr(39);
    end if;
    if v_productspec is not null then
    v_sql := v_sql || ' and h.prodspec like '||chr(39)||'%'||v_productspec||'%'||chr(39);
    end if;

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


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

  • 相关阅读:
    window.location.href问题,点击,跳转到首页
    JS indexOf() lastIndexOf()与substring()截取字符串的区别
    原来的方法增加参数时,必须为新增的参数加个默认值
    Chrome不支持本地Ajax请求解决?
    Smarty中{literal}的使用详解
    windows不能在本地计算机启动apache
    Apache虚拟主机配置
    wamp环境网站根目录更改
    Java 读书笔记 (十三) for each 循环
    Java 读书笔记 (十二) Java Character 类
  • 原文地址:https://www.cnblogs.com/mxh168/p/13965876.html
Copyright © 2011-2022 走看看