使用输出参数

时间:2015-09-08 18:23:15

标签: sql-server stored-procedures output ssms-2012

背景

我有一系列存储过程,可以将一条新记录插入到5个表中(都具有一对一的关系)。每个存储过程都为相关表创建唯一ID。最后一个存储过程将所有这些唯一ID添加到最后一个表中(以允许INNER JOIN检索各个表中的所有信息)。

我创建了一个额外的存储过程(让我们称之为spWrapper),它调用4个单独的存储过程(如上所述)。我想使用spWrapper来插入和更新记录。当前(插入)spWrapper只插入一条记录; 5个唯一ID声明为OUTPUT个参数。我的T-SQL知识仍然是基本的,我不确定OUTPUT参数如何影响存储过程。

当前的spWrapper代码(缩短示例):

ALTER PROCEDURE [dbo].[spWrapper] 

-- Return values
@idAddress                                      INT = NULL OUTPUT,
@idDetermination                                INT = NULL OUTPUT,
@idLegalDescription                             INT = NULL OUTPUT,
@idAddresses_LegalDescriptions_Determinations   INT = NULL OUTPUT,
@idLOMC                                         INT = NULL OUTPUT,

-- [Table#1] parameters
@Street     VARCHAR(50) = NULL,
@City       VARCHAR(50) = NULL,
@State      VARCHAR(2)  = NULL,
@ZipCode5   VARCHAR(5)  = NULL,
@ZipCode4   VARCHAR(4)  = NULL,
@GISCode    VARCHAR(15) = NULL

AS
SET NOCOUNT OFF
SET ROWCOUNT 0

-- =================================
-- Declare and initialize variables
-- =================================

DECLARE @Error      INT,
    @RC         INT,
    @Trancount  INT,
    @Message    VARCHAR(255)

SELECT  @Error      = 0,
    @RC         = 0,
    @Trancount  = @@TRANCOUNT,
    @Message    = NULL

-- ==========================================
-- Insert record into [Table#1]
-- ==========================================

IF @idAddress IS NULL
    BEGIN

    IF @Trancount = 0 BEGIN TRANSACTION

        EXEC    @RC = [spTable#1]
                @idLogin        = @idLogin,
                @idAddress      = @idAddress,
                @Street         = @Street,  
                @State          = @State,
                @City           = @City,
                @ZipCode5       = @ZipCode5,
                @ZipCode4       = @ZipCode4,
                @GISCode        = @GISCode

        SELECT  @Error = @@ERROR

            IF @RC <> 0 OR @Error <> 0
                BEGIN
                    IF @Trancount = 0 ROLLBACK TRANSACTION
                    SELECT @Message = 'dbo.spWrapper: Error inserting record into [Table#1]'
                GOTO lbl_abort
                END
    END

IF @Trancount = 0 COMMIT TRANSACTION

当前的spTable#1代码

ALTER PROCEDURE [spTable#1]
@idLogin    INT,
@idAddress  INT = NULL OUTPUT,
@Street     VARCHAR(50),
@State      CHAR(2),
@City       VARCHAR(50),
@ZipCode5   CHAR(5),
@ZipCode4   CHAR(4),
@GisCode    VARCHAR(15)

IF @TranCount = 0 BEGIN TRANSACTION

IF @idAddress IS NULL
BEGIN 
    INSERT [dbo].[Table#1]
        (Street,
        [State],
        City,
        ZipCode5,
        ZipCode4,
        GisCode,
        InsertedidLogin,
        InsertedDate)
    VALUES
        (@Street,
        @State,
        @City,
        @ZipCode5,
        @ZipCode4,
        @GisCode,
        @idLogin,
        GETDATE())

SELECT  @Error  = @@ERROR,
        @RC     = @@ROWCOUNT,
        @idAddress = SCOPE_IDENTITY()

IF @Error <> 0 OR @RC <> 1
    BEGIN
        IF @TranCount = 0 ROLLBACK TRANSACTION
        SELECT @Message = 'spTable#1: Error inserting record into [Table#1]'
    GOTO lbl_abort
    END
END

我的主要问题

如果将唯一ID设置为OUTPUT参数,是否可以插入现有ID来调用spTable#1的UPDATE部分(以及随后的spWrapper)? OR 我可以声明这些唯一ID(即:不是OUTPUT参数)吗?

建议的更改

CREATE PROCEDURE [dbo].[spWrapper]
@idLOMC     INT = NULL

DECLARE @idAddress          INT,
    @idLegalDescription INT,
    @idDetermination    INT,
    @idCommunity        INT,
    @idFirm             INT,
    @idCounty           INT,
    @idFloodZone        INT,
    @Error              INT,
    @RC                 INT,
    @Trancount          INT,
    @Message            VARCHAR(255)

SELECT  @idAddress          = NULL,
    @idLegalDescription = NULL,
    @idDetermination    = NULL,
    @Error              = 0,
    @RC                 = 0,
    @Trancount          = @@TRANCOUNT,
    @Message            = NULL

/* Lookup idAddress, idLegalDescription, idDetermination */

IF @idLOMC IS NOT NULL
BEGIN
    SELECT @idAddress = A.idAddress, @idLegalDescription = LD.idLegalDescription, @idDetermination = D.idDetermination
    FROM [Table#2] L
    INNER JOIN [Table#5] ALD
    ON L.idAddresses_LegalDescriptions_Determinations = ALD.idAddresses_LegalDescriptions_Determinations
    INNER JOIN [Table#1] A
    ON ALD.idAddress = A.idAddress
    INNER JOIN [Table#3] LD
    ON ALD.idLegalDescription = LD.idLegalDescription
    INNER JOIN [Table#4] D
    ON ALD.idDetermination = D.idDetermination
    WHERE L.idLOMC = @idLOMC

    IF @@ROWCOUNT = 0
        BEGIN
            SELECT @Message = 'dbo.spWrapper: Invalid idLOMC'
            GOTO lbl_abort
        END
END

/* Insert record into [Table#1] */

IF @idAddress IS NULL
BEGIN
        IF @Trancount = 0 BEGIN TRANSACTION

        EXEC    @RC = [spTable#1]
                @idLogin    = @idLogin,
                @idAddress  = @idAddress OUTPUT,
                @Street     = @Street,
                @State      = @State,
                @City       = @City,
                @ZipCode5   = @ZipCode5,
                @ZipCode4   = @ZipCode4,
                @GisCode    = @GisCode

                SELECT @Error = @@ERROR

                    IF @RC <> 0 OR @Error <> 0
                        BEGIN
                            IF @Trancount = 0 ROLLBACK TRANSACTION
                                SELECT @Message = 'spWrapper: Error inserting record into [Table#1]'
                                GOTO lbl_abort
                        END
END

/* Update record into [Table#1] */

ELSE
BEGIN
        IF @Trancount = 0 BEGIN TRANSACTION

        EXEC    @RC = [spTable#1]
                @idLogin    = @idLogin,
                @idAddress  = @idAddress,
                @Street     = @Street,
                @State      = @State,
                @City       = @City,
                @ZipCode5   = @ZipCode5,
                @ZipCode4   = @ZipCode4,
                @GisCode    = @GisCode

                SELECT @Error = @@ERROR

                    IF @RC <> 0 OR @Error <> 0
                        BEGIN
                            IF @Trancount = 0 ROLLBACK TRANSACTION
                                SELECT @Message = 'spWrapper: Error updating record in [TspTable#1]'
                                GOTO lbl_abort
                        END
END

0 个答案:

没有答案