oracle存储过程动态声明变量赋值,Oracle PL/SQL存储过程动态赋值有关问题

该博客讨论了一个Oracle PL/SQL存储过程中动态SQL的构建问题,涉及对条件变量的赋值和使用。作者在处理多个条件时遇到了困难,尝试通过字符串拼接构建WHERE子句,但未成功。解决方案在于正确地将动态WHERE子句插入到SQL查询中进行执行。
摘要由CSDN通过智能技术生成

Oracle PL/SQL存储过程动态赋值问题

create or replace procedure record_inp_all

(

areacode varchar2,

beginTime varchar2,

endTime varchar2

)

as

v_areacode varchar2(20);

v_stat varchar2(20);

v_areacode_count number;

v_table type_split;

v_beginTime date;

v_endTime date;

v_sqlWhere varchar2(100) default '';

v_sql varchar2(1000);

begin

if nvl(beginTime,'') is not null then

v_beginTime := to_date(beginTime,'yyyy-mm-dd hh24:mi:ss');

else

v_beginTime := to_date('2014-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss');

end if;

if nvl(endTime,'') is not null then

v_endTime := to_date(endTime,'yyyy-mm-dd hh24:mi:ss');

else

v_endTime := sysdate;

end if;

select strsplit(areacode,',') into v_table from dual;

dbms_output.put_line(v_table.count);

if v_table.count = 1  then

if v_table(1) is null then

v_sqlWhere := v_sqlWhere||'';

else

v_sqlWhere := v_sqlWhere||' area_code_fund ='||v_table(1);

end if;

end if;

if v_table.count > 1 and v_table.count <9 then

--v_sqlWhere :=

for i in 1.. v_table.count loop

v_sqlWhere := v_sqlWhere||chr(39)||v_table(i)||chr(39)||',';

end loop;

v_sqlWhere := '('||substr(v_sqlWhere,1,length(v_sqlWhere)-1)||')';

dbms_output.put_line('v_sqlWhere:'||v_sqlWhere);

end if;

if v_table.count = 9 then

v_sqlWhere := v_sqlWhere||'';

dbms_output.put_line('v_sqlWhere:'||v_sqlWhere);

end if;

v_sqlWhere:='(''111'',''222'')';

dbms_output.put_line('v_sqlWhere:'||v_sqlWhere);

for c_cur in

(select t.area_code_fund,to_char(t.registe_date,'yyyy-mm') c_regtime,count(*) c_count from nc_inp_register t where

t.registe_date>=v_beginTime and t.registe_date<=v_endTime and t.data_source<>'2'

and t.area_code_fund inv_sqlWhere

group by  t.area_code_fund,to_char(t.registe_date,'yyyy-mm')

order by t.area_code_fund,to_char(t.registe_date,'yyyy-mm'))

loop

dbms_output.put_line(c_cur.area_code_fund||','||c_cur.c_regtime||','||c_cur.c_count);

end loop;

end;

v_sqlWhere为什么不能在正确赋值?而别的参数反而可以的

------解决方案--------------------

open cursor for 'select t.area_code_fund,

to_char(t.registe_date, ''yyyy-mm'') c_regtime,

count(*) c_count

from nc_inp_register t

where t.registe_date >= v_beginTime

and t.registe_date <= v_endTime

and t.data_source <> '2'

and t.area_code_fund in '

------解决方案--------------------

v_sqlWhere

------解决方案--------------------

'

group by t.area_code_fund, to_char(t.registe_date, ''yyyy-mm'')

order by t.area_code_fund, to_char(t.registe_date, ''yyyy-mm'')'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值