1 ----------------1、直接输出数据集------------------- 2 CREATE OR REPLACE PROCEDURE "BI_DM"."SP_XINGUANQUERY" ( 3 startdate varchar(20) 4 ,enddate varchar(20) 5 ,querydiagname varchar(64) 6 ) 7 dynamic result sets 1 8 LANGUAGE SQL 9 10 begin 11 --------------定义输出游标,定义必须在变量赋值的上面,否则会报错。max合并前三列,和最后一列------------------------------------- 12 declare clientcur cursor with return 13 for SELECT * 14 FROM ........... 15 ; 26 open clientcur; 27 28 end 29 30 31 ----------------2、先声明游标,在处理的最后,输出数据集------------------- 32 CREATE OR REPLACE PROCEDURE "BI_DM"."SP_SPECIALDRUG_REPORT_0403" ( 33 startdate varchar(20) 34 ,enddate varchar(20) 35 ,exeflag varchar(8) --说明:0 报表执行,返回结果 1定时任务执行,每天早上生成短信信息 2不管月份判断,重新生成数据 36 ) 37 dynamic result sets 1 38 LANGUAGE SQL 39 40 begin 41 42 declare D_StartDate date; 43 DECLARE v_sql VARCHAR(2000); 44 DECLARE v_st STATEMENT; 45 DECLARE v_cursor CURSOR WITH RETURN FOR v_st; 46 47 -----------------中间任意多的sql进行逻辑处理----------- 48 set v_sql='select RESULTXH,PERIODID, XH, ITEMIDAGO, ITEMID, SPEC, DRUGXH, DRUGNAME, SPECAGO, FACTORY, 使用金额, PRICE, 数量 from BI_DM.REPORT_SPECIALDRUG_OUTTABLE order by RESULTXH asc'; 49 50 PREPARE v_st FROM v_sql; 51 52 53 open v_cursor; 54 55 end 56 57 来自 <https://blog.csdn.net/lswnew/article/details/20704133>
58
1 ----------------3、动态sql 最后统一执行。 ------------------- 2 CREATE PROCEDURE "BI_DM"."SP_GCYP_REPORT" ( 3 startdate varchar(20) 4 ) 5 dynamic result sets 1 6 LANGUAGE SQL 7 8 begin 9 10 ---动态sql变量声明 11 12 declare sqldelete varchar(5000); 13 declare sqlinsert varchar(5000); 14 declare sql1 varchar(5000); 15 declare sql2 varchar(5000); 16 declare sql3 varchar(5000); 17 declare sqlinsert2 varchar(5000); 18 19 --循环参数 20 declare num1 varchar(32); 21 declare num2 varchar(32); 22 23 24 -----定义输出游标,定义必须在变量赋值的上面,否则会报错。max合并前三列,和最后一列------------------- 25 declare clientcur cursor with return 26 for 27 select ……… from table a 28 join tab b on a.ID=b.itemid 29 group by id,b.ITEMNAME; 30 31 32 ------------通过set不断处理sql,最后统一执行----------------------------------- 33 set sqlinsert2='………………'; 34 set delete1='………………'; 35 36 37 PREPARE delete1 FROM sqldelete; --清除历史记录 38 EXECUTE delete1; 39 PREPARE insert2 FROM sqlinsert2;--插入院内使用量 40 EXECUTE insert2; 41 42 43 ------------这种方式,也可以在最后输出游标数据集----------------------------------- 44 45 46 open clientcur; 47 48 end
标签:存储,5000,varchar,汇总,游标,sql,20,DB2,declare From: https://www.cnblogs.com/deng779256146/p/17848506.html