一个存储过程:带参数动态执行sql语句:Execute Immediate

博客围绕统计功能展开,根据客户需求增加统计表,需依据基本表动态执行SQL语句。介绍了在存储过程中用Execute Immediate命令动态生成并执行SQL语句,还给出了更新xt_tj_mx表的存储过程示例,涉及动态游标及不同表的统计逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据客户需要,我们增加了一个统计表,用来汇总统计数据,统计数据的产生,需要根据一个基本表,动态执行sql语句.在存储过程中,动态生成sql语句然后执行,需要用到Execute Immediate命令.我想写一个通用的统计用存储过程,似乎不大好办,if语句的应用在所难免了.呵呵.

特此存档.

create or replace procedure P_INSERT_XT_TJ_MX(sDate in varchar2) is
--author:李春雷
--create time:2005.8.4
--purpose:更新xt_tj_mx表
sXh xt_tj.xh%type; --主表序号
sDwmc xt_tj.dwmc%type;
sDw xt_tj.dw%type;
sDwzd xt_tj.dwzd%type;
sTable xt_tj.hzbmc%type;
sSjzd xt_tj.sjzd%type;
sSqlStr varchar2(300);
iCount int;
cursor curSort is
select xh,dwmc,hzbmc,sjzd,dwzd,dw from xt_tj ;
begin
open curSort;
loop
fetch curSort into sXh,sDwmc,sTable,sSjzd,sDwzd,sDw;
exit when curSort%notfound;
sSqlStr := 'select count(*) from '|| sTable || ' where to_char('||sSjzd||','||'''YYYYMM'')=:sDate and '||sDwzd ||
' in (select dwxh from xt_dw connect by prior dwxh = dwfxh start with dwxh =:sDw)';
Execute Immediate sSqlStr into iCount using sDate,sDw;
delete from xt_tj_mx where fxh=sXh and sjz=sDate;
insert into xt_tj_mx(xh,fxh,hzsm,sjz)values(SEQ_XT_TJ_MX.NEXTVAL,sXh,iCount,sDate);
commit;
end loop;
end P_INSERT_XT_TJ_MX;

今天增加了一个统计项目,用到了动态游标,存档.

create or replace procedure P_INSERT_XT_TJ_MX(sDate in varchar2) is
--author:李春雷
--create time:2005.8.8
--purpose:更新xt_tj_mx表
sXh xt_tj.xh%type; --主表序号
sDwmc xt_tj.dwmc%type;
sDw xt_tj.dw%type;
sDwzd xt_tj.dwzd%type;
sTable xt_tj.hzbmc%type;
sSjzd xt_tj.sjzd%type;
sSqlStr varchar2(300);
iCount int;
iTemp int;
sYear varchar2(4);
sQxflbh qx_qxjl.qxflbh%type;

cursor curSort is
select xh,dwmc,hzbmc,sjzd,dwzd,dw from xt_tj ;

type t_Cursor is REF CURSOR;
qxCursor t_Cursor;

begin
dbms_output.put_line(sDate);
sYear:=substr(sDate,1,4);
open curSort;
loop
fetch curSort into sXh,sDwmc,sTable,sSjzd,sDwzd,sDw;
exit when curSort%notfound;
--统计各公司工作任务单数目:
if sTable = 'rw_gzrwd' then
sSqlStr := 'select count(*) from '|| sTable || ' where to_char('||sSjzd||','||'''YYYYMM'')=:sDate and '||sDwzd ||
' in (select dwxh from xt_dw connect by prior dwxh = dwfxh start with dwxh =:sDw)';
Execute Immediate sSqlStr into iCount using sDate,sDw;
delete from xt_tj_rw_gzrwd where fxh=sXh and sjz=sDate;
insert into xt_tj_rw_gzrwd(xh,fxh,hzsm,sjz)values(SEQ_XT_TJ_MX.NEXTVAL,sXh,iCount,sDate);
end if;
--统计各公司各缺陷数目
if sTable = 'qx_qxjl' then
sSqlStr := 'select count(*),t.qxflbh from ' || sTable ||
' t where qxdjrdw in (select dwxh from xt_dw connect by prior dwxh = dwfxh start with dwxh =:sDw) and to_char(t.fxrq,''YYYY'')=:sYear group by t.qxflbh';
open qxCursor for sSqlStr using sDw,sYear;
loop
fetch qxCursor into iCount,sQxflbh;
exit when qxCursor%notfound;
dbms_output.put_line('dwbh='||sDw||'flbh='||sQxflbh||',iCount='||iCount);
select count(*) into iTemp from xt_tj_qx_qxjl t where t.dwxh=sDw and t.qxflbh=sQxflbh and t.sjz=sYear;
if iTemp=0 then
insert into xt_tj_qx_qxjl(XH,DWXH,QXFLBH,SJZ,HZSM)values(seq_xt_tj_qx_qxjl.nextval,sDw,sQxflbh,sYear,iCount);
else
update xt_tj_qx_qxjl set hzsm=iCount where dwxh=sDw and qxflbh=sQxflbh and sjz=sYear;
end if;
end loop;
close qxCursor;
end if;
end loop;
close curSort;
commit;
end P_INSERT_XT_TJ_MX;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值