存储过程与游标

图片描述

存储过程与游标_第1张图片

记录开发实例(外包供应商排名报表数据-存储过程)
-- 外包供应商排名报表数据
ALTER PROCEDURE p_getOutsourceSupplierRankingData

AS

BEGIN

-- 每天更新数据,开启游标
DECLARE @years VARCHAR(8)
DECLARE @supplierCode VARCHAR(64)
DECLARE @supplierName VARCHAR (256)
DECLARE @outsourcePersonCode VARCHAR(64)
DECLARE @outsourcePersonName VARCHAR (128)
DECLARE @totalPrice DECIMAL ( 32, 2 )
DECLARE @outsourceItemQuantity VARCHAR(64) 
DECLARE @m1 DECIMAL(32,2)
DECLARE @m2 DECIMAL(32,2)
DECLARE @m3 DECIMAL(32,2)
DECLARE @m4 DECIMAL(32,2)
DECLARE @m5 DECIMAL(32,2)
DECLARE @m6 DECIMAL(32,2)
DECLARE @m7 DECIMAL(32,2)
DECLARE @m8 DECIMAL(32,2)
DECLARE @m9 DECIMAL(32,2)
DECLARE @m10 DECIMAL(32,2)
DECLARE @m11 DECIMAL(32,2)
DECLARE @m12 DECIMAL(32,2)
DECLARE @counts INT = 0 
DECLARE supplier_ranking_cursor CURSOR

	FOR (
		SELECT
			agent_supplier_code,
			agent_supplier_name,
			agent_user_code,
			agent_user_name,
			DATEPART( yy, agent_open_case_time ) AS years,
			COUNT(*) AS quantity
		FROM
			ms_testitem_agent_main 
		WHERE
			valid_ind = '1' 
			AND agent_supplier_code IS NOT NULL 
			AND agent_open_case_time IS NOT NULL 
		GROUP BY
			agent_supplier_code,
			agent_supplier_name,
			agent_user_code,
			agent_user_name,
			DATEPART( yy, agent_open_case_time )
		)
	
-- 打开游标
	OPEN supplier_ranking_cursor;
	
-- 循环游标
	FETCH NEXT 
	FROM
		supplier_ranking_cursor INTO @supplierCode,@supplierName,@outsourcePersonCode,@outsourcePersonName,@years,@outsourceItemQuantity;

	WHILE @@FETCH_STATUS = 0 
	
	BEGIN
					
	SET @counts = (
		SELECT COUNT
			( 1 ) 
		FROM
			ms_outsource_supplier_ranking 
		WHERE
			supplier_code = @supplierCode 
			AND outsource_person_code = @outsourcePersonCode 
			AND years = @years
		)
				
	IF @counts > 0 -- 更新数据
			   
		BEGIN
	
				SET		@m1 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 1 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m2 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 2 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m3 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 3 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m4 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 4 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m5 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 5 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m6 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 6 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m7 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 7 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m8 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 8 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m9 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 9 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m10 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 10 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m11 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 11 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m12 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 12 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years )

		    SET   @totalPrice = @m1+@m2+@m3+@m4+@m5+@m6+@m7+@m8+@m9+@m10+@m11+@m12
							
				UPDATE ms_outsource_supplier_ranking SET m1 = @m1 , m2 = @m2 , m3 = @m3 , m4 = @m4 , m5 = @m5 , m6 = @m6 ,m7 = @m7 , m8 = @m8 , m9 = @m9 , m10 = @m10 , m11 = @m11 ,m12 = @m12 , total_price = @totalPrice , outsource_item_quantity = @outsourceItemQuantity	WHERE  years = @years AND  supplier_code = @supplierCode AND outsource_person_code = @outsourcePersonCode
					
		END ;
		
	ELSE -- 插入数据
	
		BEGIN
					
				SET		@m1 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 1 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m2 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 2 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m3 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 3 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m4 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 4 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m5 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 5 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m6 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 6 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m7 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 7 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m8 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 8 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m9 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 9 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m10 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 10 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m11 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 11 AND agent_supplier_code = @supplierCode AND  agent_user_code = @outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years)
				
				SET 	@m12 = (SELECT ISNULL(SUM(rmb_actual_agent_price),0) FROM ms_testitem_agent_main  WHERE  DATEPART(mm, agent_open_case_time)   = 12 AND agent_supplier_code = @supplierCode AND  agent_user_code =	@outsourcePersonCode AND DATEPART(yy, agent_open_case_time) = @years )
					
		    SET  @totalPrice = @m1+@m2+@m3+@m4+@m5+@m6+@m7+@m8+@m9+@m10+@m11+@m12
					
				INSERT INTO ms_outsource_supplier_ranking(years,supplier_code,supplier_name,outsource_person_code,outsource_person_name,outsource_item_quantity,total_price,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12) VALUES(@years,@supplierCode,@supplierName,@outsourcePersonCode,@outsourcePersonName,@outsourceItemQuantity,@totalPrice,@m1,@m2,@m3,@m4,@m5,@m6,@m7,@m8,@m9,@m10,@m11,@m12)
					
		END;
					
	FETCH NEXT 
	FROM
		supplier_ranking_cursor INTO @supplierCode,@supplierName,@outsourcePersonCode,@outsourcePersonName,@years,@outsourceItemQuantity;
					
	END ;
	
--关闭游标
 close supplier_ranking_cursor; 
 
 deallocate supplier_ranking_cursor;
 
 -- 外包人员部门代码、部门名称
UPDATE  mosr
SET mosr.person_department_code = soe.fd_parentid
FROM ms_outsource_supplier_ranking mosr 
JOIN sys_org_element soe ON mosr.outsource_person_code  = soe.fd_id 

UPDATE  mosr
SET mosr.person_department_name = soe.fd_name
FROM ms_outsource_supplier_ranking mosr 
JOIN sys_org_element soe ON mosr.person_department_code  = soe.fd_id 
		
END;
连接更新
UPDATE mtior 
SET mtior.testitem_id = mltm.testitem_id 
FROM
	ms_lab_testitem_main mltm
	JOIN ms_testItem_outlay_record mtior ON mltm.detail_id  = mtior.detail_id 
WHERE
	CHARINDEX( '.', mltm.lab_testitem_serialno ) = 0

你可能感兴趣的:(数据库,存储过程,游标)