存储过程错误 SQL Server 2014

Stored Procedure error SQL Server 2014

我在 运行SQL 中的存储过程中遇到了一些问题。这是我正在学习的 class 并且存储过程来自教科书,但是教科书在他们的 SQL 代码中有很多错误所以如果是这样我不会感到惊讶这里也。我问了教授,他似乎也搞不清楚。

这是我正在使用的存储过程,由 SQL Server Management Studio 的 "Create to" 函数生成:

USE [VGA]
GO

/****** Object:  StoredProcedure [dbo].[InsertCustomerWithTransaction]    Script Date: 4/8/2015 7:55:00 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[InsertCustomerWithTransaction]
    @NewCustomerLastName    CHAR(25),
    @NewCustomerFirstName   CHAR(25),
    @NewCustomerAreaCode    CHAR(3),
    @NewCustomerPhoneNumber CHAR(8),
    @NewCustomerEmail       VARCHAR(100),
    @ArtistLastName         CHAR(25),
    @WorkTitle              CHAR(35),
    @WorkCopy               CHAR(12),
    @TransSalesPrice        NUMERIC(8,2)

AS
    DECLARE @RowCount       AS Int,
            @ArtistID       AS Int,
            @CustomerID     AS Int,
            @WorkID         AS Int,
            @TransactionID  AS Int

    SELECT  @RowCount = COUNT(*)
    FROM    dbo.CUSTOMER
    WHERE   LastName = @NewCustomerLastName
        AND FirstName = @NewCustomerFirstName
        AND AreaCode = @NewCustomerAreaCode
        AND PhoneNumber = @NewCustomerPhoneNumber
        AND Email = @NewCustomerEmail

    IF (@RowCount > 0)
        BEGIN
            PRINT '****************************************************'
            PRINT ''
            PRINT '     The Customer is already in the database.    '
            PRINT ''
            PRINT '     CustomerLastName = '+@NewCustomerLastName
            PRINT '     CustomerFirstName = '+@NewCustomerFirstName
            PRINT ''
            PRINT '****************************************************'
        END

        ELSE
            BEGIN TRANSACTION
                INSERT INTO dbo.CUSTOMER
                    (LastName, FirstName, AreaCode, PhoneNumber, Email)
                    VALUES
                    (@NewCustomerLastName, @NewCustomerFirstName, @NewCustomerAreaCode, @NewCustomerPhoneNumber, @NewCustomerEmail)
                SELECT @CustomerID = CustomerID
                FROM dbo.CUSTOMER
                WHERE   LastName = @NewCustomerLastName
                    AND FirstName = @NewCustomerFirstName
                    AND AreaCode = @NewCustomerAreaCode
                    AND PhoneNumber = @NewCustomerPhoneNumber
                    AND Email = @NewCustomerEmail
        SELECT  @ArtistID = ArtistID
        FROM    dbo.ARTIST
        WHERE   LastName = @ArtistLastName

        IF @ArtistID IS NULL
            BEGIN
                PRINT '****************************************************'
                PRINT ''
                PRINT '     Invalid ArtistID    '
                PRINT ''
                PRINT '****************************************************'
                ROLLBACK TRANSACTION
                RETURN
            END

        SELECT  @WorkID = WorkID
        FROM    dbo.WORK
        WHERE   ArtistID = @ArtistID
            AND Title = @WorkTitle
            AND Copy = @WorkCopy

        IF  @WorkID IS NULL
            BEGIN
                PRINT '****************************************************'
                PRINT ''
                PRINT '     Invalid WorkID  '
                PRINT ''
                PRINT '****************************************************'
                ROLLBACK TRANSACTION
                RETURN
            END
        ELSE
            BEGIN
                PRINT '****************************************************'
                PRINT ''
                PRINT '     WorkID = '+CONVERT(CHAR(6), @WorkID)
                PRINT ''
                PRINT '****************************************************'
            END

        SELECT  @TransactionID = TransactionID
        FROM    dbo.TRANS
        WHERE   WorkID = @WorkID
            AND SalesPrice = NULL

        IF  @TransactionID IS NULL
            BEGIN
                PRINT '****************************************************'
                PRINT ''
                PRINT '     Invalid TransactionID   '
                PRINT ''
                PRINT '****************************************************'
                ROLLBACK TRANSACTION
                RETURN
            END

        BEGIN
            UPDATE  dbo.TRANS
            SET     DateSold = GETDATE(),
                    SalesPrice = @TransSalesPrice,
                    CustomerID = @CustomerID
            WHERE   TransactionID = @TransactionID
            INSERT INTO dbo.CUSTOMER_ARTIST_INT (CustomerID, ArtistID)
                VALUES (@CustomerID, @ArtistID)
        END

    COMMIT TRANSACTION
        BEGIN
            PRINT '****************************************************'
            PRINT ''
            PRINT '     The new Customer is now in the database.    '
            PRINT ''
            PRINT '     Customer Last Name  = '+@NewCustomerLastName
            PRINT '     Customer First Name = '+@NewCustomerFirstName
            PRINT ''
            PRINT '****************************************************'

            PRINT '****************************************************'
            PRINT ''
            PRINT '     Transaction complete.   '
            PRINT ''
            PRINT '     TransactionID   = '+CONVERT(CHAR(6), @TransactionID)
            PRINT '     ArtistID        = '+CONVERT(CHAR(6), @ArtistID)
            PRINT '     WorkID          = '+CONVERT(CHAR(6), @WorkID)
            PRINT '     Sales Price     = '+CONVERT(CHAR(12), @TransSalesPrice)
            PRINT ''
            PRINT '****************************************************'

            PRINT '****************************************************'
            PRINT ''
            PRINT '     New CUSTOMER_ARTIST_INT row added.  '
            PRINT ''
            PRINT '     ArtistID    = '+CONVERT(CHAR(6), @ArtistID)
            PRINT '     CustomerID  = '+CONVERT(CHAR(6), @CustomerID)
            PRINT ''
            PRINT '****************************************************'
        END
GO

这是我运行宁的代码:

EXEC    InsertCustomerWithTransaction
        @NewCustomerLastName = 'Gliddens',
        @NewCustomerFirstName = 'Melinda',
        @NewCustomerAreaCode = '360',
        @NewCustomerPhoneNumber = '765-8877',
        @NewCustomerEmail = 'Melinda.Gliddens@somewhere.com',
        @ArtistLastName = 'Sargent',
        @WorkTitle = 'Spanish Dancer',
        @WorkCopy = '588/750',
        @TransSalesPrice = 350.00;

当我 运行 代码时,它可以正常工作,直到以

开头的部分
SELECT  @TransactionID = TransactionID

此时它给我错误"Invalid TransactionID"。由于输入需要 WorkID 并且 SalesPrice 列为 NULL,因此我在此时添加了一些代码来提供 WorkID,由此输出的是正确的 WorkID。 screenshot I went into the database and double-checked that the SalesPrice column in that row is NULL, and it is. screenshot 但由于某种原因存储过程无法找到正确的 TransactionID,即使我可以在 table 中手动找到它。有人可以帮我弄清楚我在这里做错了什么吗?谢谢!

IS NULL不是=NULL

在这个位

SELECT  @TransactionID = TransactionID
        FROM    dbo.TRANS
        WHERE   WorkID = @WorkID
            AND SalesPrice = NULL

在SQL SERVER中,您应该使用"IS NULL"而不是“=NULL”来检查空值。

SELECT  @TransactionID = TransactionID
FROM    dbo.TRANS
WHERE   WorkID = @WorkID
    --AND SalesPrice = NULL
      AND SalesPrice is NULL

再试一次。 希望对你有帮助。

CRLast86....只是想指出,= Null 和 Is Null 都是有效的。 IS NULL 是 ANSI 标准,但 MS-SQL 允许 = Null,除非您更改了 ANSI_NULLS 模式,就像您所做的那样,在顶部附近使用 SET ANSI_NULLS ON 语句。最佳实践说,使用 SET ANSI_NULLS ON 这样它就不会让你使用 = Null。所以,将来当你看到一个 = Null 并且它正在工作时,你就会知道有条件地,两者都是有效的。在 SET ANSI_NULLS ON 上进行搜索,您将了解整个故事。