欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

记录SQLSERVER的CURSOR的使用过程

程序员文章站 2022-03-10 15:47:52
...
ALTER PROCEDURE [dbo].[generateServDatas]
AS
BEGIN	
    declare  @sp_id varchar(50), --客户id
             @sold_to_code varchar(50), --客户编码
             @catg_id varchar(50), --客户分类id
             @catg_name varchar(100),--客户分类名称
             @serv_type varchar(50), --正常服务,特殊服务
             @department_code varchar(50), --部门编码
						 @bus_catg_code varchar(50), --业务大类编码
             @s_bus_catg_code varchar(50), --业务小类编码
             @recordCount int, --客户别记录数
             @currYear varchar(10), --当前年份
						 @currMoth varchar(10); --当前月份
    BEGIN
			select @currYear=convert(varchar(10),DATENAME(yy,getdate()));
      select @currMoth=convert(varchar(10),DATENAME(mm,getdate()));
		end 

		DECLARE rs CURSOR LOCAL SCROLL FOR
    select sp.id as sp_id,sp.sold_to_code,cg.id as catg_id ,cg.cust_catg_name from  t_base_cust_sp  sp , t_cust_catg  cg where sp.catg_id=cg.id;
		OPEN rs
		FETCH NEXT FROM  rs INTO @sp_id,@sold_to_code,@catg_id,@catg_name
		WHILE @@FETCH_STATUS = 0
		BEGIN
		print(@sp_id+''[email protected]_to_code+''[email protected]_id+''[email protected]_name);
			select @recordCount=count(t.id) from t_data_cust_serv_relation_sp t where t.type='客户别' and [email protected]_id
			if (@recordCount=0)
				begin  
						DECLARE catg_serv_cursor CURSOR LOCAL SCROLL FOR 
							select t.department_code,t.business_category_code,t.sbusiness_category_code from t_data_extra_service t,t_data_cust_serv_relation_sp tr where t.id=tr.serv_id and tr.type='分类别' and [email protected]_id group by  t.department_code,t.business_category_code,t.sbusiness_category_code;
						open catg_serv_cursor
							FETCH NEXT FROM  catg_serv_cursor INTO @department_code,@bus_catg_code,@s_bus_catg_code
						WHILE @@FETCH_STATUS = 0
							BEGIN
							print(@department_code+''[email protected]_catg_code+''[email protected]_bus_catg_code);
								BEGIN
								 insert into t_serv_month_report(sp_id,sold_to_code,catg_id,catg_name,serv_type,department_code,bus_catg_code,s_bus_catg_code,create_year,create_month) 
																								values (@sp_id,@sold_to_code,@catg_id,@catg_name,'正常服务区分',@department_code,@bus_catg_code,@s_bus_catg_code,@currYear,@currMoth);
								END
							FETCH NEXT FROM catg_serv_cursor INTO @department_code,@bus_catg_code,@s_bus_catg_code
							END
						CLOSE catg_serv_cursor
            DEALLOCATE catg_serv_cursor
				end
      ELSE
				BEGIN
						DECLARE cust_serv_cursor CURSOR LOCAL SCROLL FOR 
							select t.department_code,t.business_category_code,t.sbusiness_category_code from t_data_extra_service t,t_data_cust_serv_relation_sp tr where t.id=tr.serv_id and tr.type='客户别' and [email protected]_id group by  t.department_code,t.business_category_code,t.sbusiness_category_code;
						open cust_serv_cursor
						FETCH NEXT FROM  cust_serv_cursor INTO @department_code,@bus_catg_code,@s_bus_catg_code
						WHILE @@FETCH_STATUS = 0
							BEGIN
							print(@department_code+''[email protected]_catg_code+''[email protected]_bus_catg_code);
								BEGIN
								 insert into t_serv_month_report(sp_id,sold_to_code,catg_id,catg_name,serv_type,department_code,bus_catg_code,s_bus_catg_code,create_year,create_month) 
																								values (@sp_id,@sold_to_code,@catg_id,@catg_name,'特殊服务区分',@department_code,@bus_catg_code,@s_bus_catg_code,@currYear,@currMoth);
								END
							FETCH NEXT FROM cust_serv_cursor INTO @department_code,@bus_catg_code,@s_bus_catg_code
							END
					CLOSE cust_serv_cursor
          DEALLOCATE cust_serv_cursor

					
				END
				
	  FETCH NEXT FROM rs INTO @sp_id,@sold_to_code,@catg_id,@catg_name
		END
		CLOSE rs
    DEALLOCATE rs

 

相关标签: SQLSERVER CURSOR