在返回结果集的存储过程中设置参数

时间:2017-05-10 16:31:41

标签: sql sql-server vb.net vba

我试图整合一些代码,但在我打开这个特殊的蠕虫之前,我想从你们那里找到答案。如果我有几个存储过程...

sproc1 - " master proc"设置@test

sproc2 - 如果@test存在则执行proc并返回结果集和(如果可能)重置@serial

sproc3 - 如果@test不存在则执行proc并返回结果集和(如果可能)重置@serial

sproc1

    @leftStack INT,
    @leftTray INT,
    @midStack INT,
    @midTray INT,
    @rightStack INT,
    @rightTray INT

    AS


    DECLARE @soLineNumber varchar(50)
    DECLARE @serial VARCHAR(50)
    DECLARE @rack INT
    DECLARE @tray INT
    DECLARE @position INT

    SELECT @test = oL.[SERIAL_NUMBER]
        FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_ORDER_OPTICS] AS oL
            WHERE NOT EXISTS 
            ( 
            SELECT [SERIAL_NUMBER] 
                FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_OPTIC_RESULTS] AS rL 
                    WHERE oL.[SERIAL_NUMBER] = rL.[SERIAL_NUMBER] 
            ) 
            AND NOT EXISTS 
                ( 
                SELECT [SERIAL_NUMBER] 
                    FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_OPTIC_INSERTED] AS oI 
                        WHERE oL.[SERIAL_NUMBER] = oI.[SERIAL_NUMBER] 
                ) 
    --      AND oL.[SO_LINE_NUMBER] = @soLineNumber --pick regardless of SO line number, to reduce gaps between lines
            AND ((oL.[RACK] = @leftStack AND oL.[TRAY] = @leftTray) 
            OR (oL.[RACK] = @midStack AND oL.[TRAY] = @midTray) 
            OR (oL.[RACK] = @rightStack AND oL.[TRAY] = @rightTray))
         ORDER BY [SO_LINE_NUMBER] ASC
IF NULLIF(@test, '') IS NOT NULL
    BEGIN
        EXEC sproc2
    END
IF NULLIF(@test, '') IS NULL
    BEGIN
        EXEC sproc3
    END

UPDATE [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_ORDER_OPTICS] SET [PICKED] = 'True' WHERE [SERIAL_NUMBER] = @serial
END

我的问题: 1)如何从sproc2和sproc3重置@serial?

2)在ADO记录集查询中,将执行存储过程的结果拉入,如果是,如何?

1 个答案:

答案 0 :(得分:1)

为了实现这一目标,sproc2sproc3应该像这样定义:

CREATE PROC sproc2 @test VARCHAR(50), @serial VARCHAR(50) OUTPUT

这样做,它将@test的值作为值参数发送。 OUTPUT上的@serial关键字可让您跟踪@serial上所做的任何更改。