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;
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值