当前位置: 代码迷 >> SQL >> oralce存储过程施用动态sql
  详细解决方案

oralce存储过程施用动态sql

热度:21   发布时间:2016-05-05 13:47:30.0
oralce存储过程使用动态sql

在存储过程中,我想根据条件拼装sql,这个时候select xx into v_xx这样就不行了,返回不了值。要使用

?

execute immediate v_sql into v_access_number;

?

create or replace procedure p_access_user(   v_starttime in varchar2,   v_endtime in varchar2,   v_adjusttype in varchar2,   v_results in varchar2,   v_province in varchar2,   v_access_number out adjust_power_transaction_his.id%type) is   v_char_16  varchar2(20);   v_char_two_16 varchar2(20);   v_like varchar2(20000);   v_sql varchar2(25000):='select count(*)from adjust_power_transaction_his t where 1=1 ';begin   --时间段   if(v_starttime is not null and v_endtime is not null) then     v_sql:=v_sql||' and t.begintime>= to_date('''||v_starttime||''',''yyyy-MM-dd hh24:mi:ss'')'||'and t.begintime<=to_date('''||v_endtime||''',''yyyy-MM-dd hh24:mi:ss'')';               end if;       --校准方式   if(v_adjusttype is not null)then     v_sql:=v_sql||' and t.adjusttype='||v_adjusttype||'';   end if;      --校准处理结果   if(v_results is not null)then     v_sql:=v_sql||' and t.result= '''||v_results||'''';   end if;      --省份   if(v_province is not null)then     select to_char(v_province,'xxx') into v_char_16 from dual;     if length(trim(v_char_16)) = 1         then          v_char_two_16 :='0'||trim(v_char_16);        else          v_char_two_16 := trim(v_char_16);           end if;       v_like :=  upper('01'||v_char_two_16||'%');             v_sql:=v_sql||' and t.cardno like'''|| v_like||'''';   end if;     --接入用户统计   execute immediate v_sql into v_access_number;      --dbms_output.put_line(v_sql); end p_access_user;
?

?

  相关解决方案