存储过程无法正常工作

时间:2014-11-18 16:34:35

标签: sql-server stored-procedures

我正在进行一项任务,我必须在Microsoft SQL Server中编写一个存储过程。确切的代码在书中给出,我已经多次检查以确保我没有错过任何东西,但是当我查询它时,我得不到正确的结果。

以下是该程序的代码:

CREATE PROCEDURE InsertCustomerWithTransaction
      @NewCustomerLastName            char(25),
      @NewCustomerFirstName           char(25),
      @NewCustomerAreaCode            char(3),
      @NewCustomerPhoneNumber         char(8),
      @NewCustomerEmail               varchar(100),
      @ArtistLastName                 char(25),
      @WorkTitle                      char(25),
      @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 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 '  Customer Last Name  = ' + @NewCustomerLastName
            PRINT '  Customer First Name  = ' + @NewCustomerFirstName
            PRINT ''
            PRINT '************************************************'
        RETURN
      END   

      ELSE
         BEGIN TRANSACTION
                INSERT INTO Customer
        (LastName, FirstName, AreaCode, PhoneNumber, Email)
        VALUES( @NewCustomerLastName, @NewCustomerFirstName, @NewCustomerAreaCode, @NewCustomerPhoneNumber, @NewCustomerEmail)

        SELECT @CustomerID = CustomerID
        FROM Customer
        WHERE LastName = @NewCustomerLastName
         AND FirstName = @NewCustomerFirstName
         AND AreaCode = @NewCustomerAreaCode
         AND PhoneNumber = @NewCustomerPhoneNumber
         AND Email = @NewCustomerEmail

         SELECT @ArtistID = ArtistID
         FROM Artist
         WHERE LastName = @ArtistLastName

IF @ArtistID is null
   Begin
     PRINT '************************************************'
     PRINT ''
     PRINT '    Invalid ArtistID'
     PRINT ''
     PRINT '************************************************'
     ROLLBACK TRANSACTION
     RETURN
END

SELECT @WorkID = WorkID
FROM 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

   SELECT @TransactionID = TransactionID
   FROM 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 Trans
       SET   DateSold = getDate(),
             SalesPrice = @TransSalesPrice,
             CustomerID = @CustomerID
       WHERE TransactionID = @TransactionID

       INSERT INTO CUSTOMER_ARTIST_INT(CustomerID, ArtistID)
            VALUES (@CustomerID, @ArtistID)
      END

      COMMIT TRANSACTION

        BEGIN


           PRINT '************************************************'
           PRINT ''
           PRINT '  The 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 

当我尝试使用下面的代码进行测试时,我在本书中也会得到“无效的TransactionID”消息。根据该书,客户应该成功添加。

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;

我错过了哪些想法?我可以使用任何解决方法吗?

2 个答案:

答案 0 :(得分:2)

您应该使用SalesPrice IS NULL,而不是SalesPrice = NULL

SalesPrice = NULL不会抛出错误,但它也不会像你想象的那样匹配。

答案 1 :(得分:0)

您可以使用SalesPrice = NULL但是您需要设置ANSI_NULL OFF默认为ON

SET ANSI_NULLS OFF

SalesPrice = NULL

SET ANSI_NULLS ON

何时设置OFF

表示正在遵循ISO标准

=<>应该用于空比较。