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;
**出自
動態SQL拼接
多選刪除,修改筆記的型別,會出現效能差,用乙個sql最好。mybatis提供的動態sql拼接功能,可以優化資料層操作,減少冗餘sql的產生,進而提供資料訪問效能。動態sql經常與陣列,list,map 引數配合使用。動態updateupdate cn note setcn note type id ...
oracle儲存過程動態sql單引號拼接和變數拼接
在oracle中單引號主要有兩個作用 一是字串都用單引號引用,例如 abc 二是轉義符,轉義符在使用時一般會出現多個連在一起的單引號,相對難理解一些。下面舉例說明。例項1 select from dual 例項1結果 例項2 select oracle from dual 例項2結果 例項1解析 第...
拼接SQL語句 Oracle
因為專案需要,有一段select語句中的列,想實現可配置,因此就需要用for迴圈。但嘗試之後發現select語句中是不允許放for迴圈的。需求 select column1,column2,column3,column41,column42,column43,column44.from table1...