SQL SERVER 存储过程示例
USE TEST_DEV; SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: -- Create date: -- Description:存储过程示例 -- ============================================= CREATE PROCEDURE PROC_DEMON @IN_PLANT VARCHAR(20), ------- 输入参数 -------- @IN_ASSEMBLY VARCHAR(20), @IN_SUPPLIER VARCHAR(20), @IN_DOCK VARCHAR(20), @IN_PARTS VARCHAR(20), @IN_PART_NUM INT, @OUTPUT INT OUTPUT ------- 输出参数 -------- AS BEGIN ------- 创建变量 -------- DECLARE @PLANT VARCHAR(20); DECLARE @ASSEMBLY_LINE VARCHAR(20); DECLARE @SUPPLIER_NUM VARCHAR(20); DECLARE @DOCK VARCHAR(20); DECLARE @BOX_PARTS VARCHAR(20); DECLARE @PART_NUM INT; -------- 创建临时表 -------- CREATE TABLE #TEMP([PLANT] VARCHAR(20),[ASSEMBLY_LINE] VARCHAR(20),[SUPPLIER_NUM] VARCHAR(20),[DOCK] VARCHAR(20),[BOX_PARTS] VARCHAR(20),[PART_NUM] INT) -------- 插入数据 -------- INSERT #TEMP(PLANT,ASSEMBLY_LINE,SUPPLIER_NUM,DOCK,BOX_PARTS,PART_NUM) VALUES (@IN_PLANT,@IN_ASSEMBLY,@IN_SUPPLIER,@DOCK,@IN_PARTS,@IN_PART_NUM); -------- 创建游标 -------- DECLARE cur1 CURSOR FOR SELECT [PLANT],[ASSEMBLY_LINE],[SUPPLIER_NUM],[DOCK],[BOX_PARTS],[PART_NUM] FROM #TEMP WHERE PLANT IS NOT NULL GROUP BY [PLANT],[ASSEMBLY_LINE],[SUPPLIER_NUM],[DOCK],[BOX_PARTS],[PART_NUM]; -------- 打开游标并填充数据 -------- OPEN cur1 FETCH NEXT FROM cur1 INTO @PLANT,@ASSEMBLY_LINE,@SUPPLIER_NUM,@DOCK,@BOX_PARTS,@PART_NUM PRINT @@FETCH_STATUS; WHILE (@@FETCH_STATUS = 0) BEGIN DECLARE @PARTNUM INT; SELECT @PARTNUM = ISNULL(PART_NUM,0) FROM #TEMP WHERE BOX_PARTS IS NOT NULL; IF @PARTNUM > 0 BEGIN SET @OUTPUT = 1; END ELSE BEGIN SET @OUTPUT = 0; END -------- 填充下一条数据 -------- FETCH NEXT FROM cur1 INTO @PLANT,@ASSEMBLY_LINE,@SUPPLIER_NUM,@DOCK,@BOX_PARTS,@PART_NUM -------- 查看游标状态 -------- PRINT @@FETCH_STATUS; END CLOSE cur1 SELECT * FROM #TEMP; -------- 删除临时表 -------- --DROP TABLE #TEMP; -------- 返回值 -------- RETURN 1; END GO SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO --DROP PROC dbo.PROC_DEMON -------- 带输入输出参数 --------- --DECLARE @RESULT AS INT; --EXEC dbo.PROCDEMON -- @IN_PLANT = 'I033', -- @IN_ASSEMBLY = 'AF01', -- @IN_SUPPLIER = 'LOC', -- @IN_DOCK = 'LOC05', -- @IN_PARTS = 'ZONE1', -- @IN_PART_NUM = 0, -- @OUTPUT = @RESULT OUTPUT --SELECT @RESULT AS RESULT; -------- 带输入输出带返回值 --------- DECLARE @RESULT AS INT, @STATUS AS INT = 0; EXEC @STATUS = dbo.PROC_DEMON @IN_PLANT = 'I033', @IN_ASSEMBLY = 'AF01', @IN_SUPPLIER = 'LOC', @IN_DOCK = 'LOC05', @IN_PARTS = 'ZONE1', @IN_PART_NUM = 11, @OUTPUT = @RESULT OUTPUT SELECT @RESULT AS RESULT,@STATUS AS STATUS;