0

我是一名初学者程序员,我正在尝试学习 SQL,但在这里遇到了一个问题,我得到的问题是:

编写一个UpdateSuggestedPrice接受 ISBN 编号和新建议价格的程序。如果 ISBN 不存在,则引发错误消息。如果没有错误,将建议价格更新为新价格。

Create PROCEDURE UpdateSuggestedPrice (@ISBN char(10), @SuggestedPrice smallmoney)
AS
BEGIN
   if @ISBN is null
   Begin
     Raiserror ('Provide a valid ISBN',16,1)
   End
   else
   Begin    
     if (select COUNT(*) from Title where SuggestedPrice = @SuggestedPrice) = 0
     begin
        Select 'Description sucessfully added'

        insert into Title (SuggestedPrice)
        values (@SuggestedPrice)
     End
     Else
        Raiserror ('Description already exists',16,1)
     End
   End

   Return

-- Here I'm trying to execute the procedure, search for ISBN and 
-- then update the suggested price, can someone please tell me 
-- what I'm doing wrong.
execute UpdateSuggestedPrice @ISBN= '1021031040', @SuggestedPrice = '40'
4

1 回答 1

1

您的代码有几个问题。首先,您在数据中搜索记录,SuggestedPrice = @SuggestedPrice这实际上应该是在寻找 ISBN,如ISBN = @ISBN. 您还缺少END存储过程末尾的 an 。

我建议重新阅读这个问题。

CREATE PROCEDURE UpdateSuggestedPrice (@ISBN char(10), @SuggestedPrice smallmoney)
AS
BEGIN
   IF @ISBN is null
   BEGIN
     RAISERROR ('Provide a valid ISBN',16,1)
   END
   ELSE
   BEGIN    
     IF (SELECT COUNT(*) FROM Title WHERE ISBN = @ISBN) = 0
     BEGIN
        RAISERROR ('ISBN does not exist.',16,1)
     END
     ELSE
     BEGIN
        SELECT 'Price sucessfully updated.';
        UPDATE Title /* Title is the table to be updated */
        SET SuggestedPrice = @SuggestedPrice /* this is the field to update */
        WHERE ISBN = @ISBN; /* this selects which record to update */
     END
   END
END 
GO
EXECUTE UpdateSuggestedPrice @ISBN= '1021031040', @SuggestedPrice = '40'
于 2013-04-02T17:28:24.570 回答