我正在处理一些练习题,我遇到了这个问题,我在尝试执行说明
的过程时遇到错误第15行,第1行,第1行,第1行 关键字“过程”附近的语法不正确。“
有人可以帮忙吗?
编写一个接受所有UpdateTitle
表列的过程Title
,并使用这些值更新标题。提出以下错误消息:ISBN不存在类别和/或发布者代码无效。
Create PROCEDURE UpdateTitle (@ISBN char(10), @SuggestedPrice smallmoney,@NumberInStock smallint,@PublisherCode int,@CategoryCode int)
AS
BEGIN
IF @ISBN is null or @CategoryCode is null or @PublisherCode is null
BEGIN
RAISERROR ('ISBN,CategoryCode, or PublisherCode is not valid please enter valid data',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 'Table Sucessfully Updated.';
UPDATE Title
SET SuggestedPrice = @SuggestedPrice
WHERE ISBN = @ISBN;
BEGIN
IF (SELECT COUNT(*) FROM Title WHERE ISBN = @ISBN) = 0
BEGIN
RAISERROR ('ISBN does not exist.',16,1)
END
ELSE
BEGIN
SELECT 'Table Sucessfully Updated.';
UPDATE Title
SET NumberInStock = @NumberInStock
WHERE ISBN = @ISBN;
END
BEGIN
IF (SELECT COUNT(*) FROM Title WHERE ISBN = @ISBN) = 0
BEGIN
RAISERROR ('ISBN does not exist.',16,1)
END
ELSE
BEGIN
SELECT 'Table Sucessfully Updated.';
UPDATE Title
SET PublisherCode = @PublisherCode
WHERE ISBN = @ISBN;
END
BEGIN
IF (SELECT COUNT(*) FROM Title WHERE ISBN = @ISBN) = 0
BEGIN
RAISERROR ('ISBN does not exist.',16,1)
END
ELSE
BEGIN
SELECT 'Table Sucessfully Updated.';
UPDATE Title
SET CategoryCode = @CategoryCode
WHERE ISBN = @ISBN;
END
END
END
END
END
END
END
GO
然后
Execute Procedure UpdateTitle @ISBN ='1021031040', @suggestedproce ='40' , @NumberInStock ='10', @PublisherCode = '200', @CategoryCode = '1'
答案 0 :(得分:1)
Execute Procedure UpdateTitle ...
应该是:
EXEC dbo.UpdateTitle ...
其他一些评论:
RETURN;
作为退出机制,这样您就不必多次嵌套IF
和ELSE
。SET NOCOUNT ON;
。这是一个更简洁的版本,可以满足您的所有要求:
CREATE PROCEDURE dbo.UpdateTitle
@ISBN CHAR(10),
@SuggestedPrice SMALLMONEY,
@NumberInStock SMALLINT,
@PublisherCode INT,
@CategoryCode INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @msg VARCHAR(255);
IF @ISBN IS NULL OR @CategoryCode IS NULL OR @PublisherCode IS NULL
BEGIN
SELECT @msg = 'The following parameter(s) were invalid:'
+ CASE WHEN @ISBN IS NULL THEN ' @ISBN' ELSE '' END
+ CASE WHEN @CategoryCode IS NULL THEN ' @CategoryCode' ELSE '' END
+ CASE WHEN @PublisherCode IS NULL THEN ' @PublisherCode' ELSE '' END;
RAISERROR (@msg, 11, 1);
RETURN;
END
IF NOT EXISTS (SELECT 1 FROM dbo.Title WHERE ISBN = @ISBN)
BEGIN
SET @msg = 'ISBN %s does not exist.';
RAISERROR(@msg, 11, 1, @ISBN);
RETURN;
END
BEGIN TRY
UPDATE dbo.Title
SET SuggestedPrice = @SuggestedPrice,
NumberInStock = @NumberInStock,
PublisherCode = @PublisherCode,
CategoryCode = @CategoryCode
WHERE ISBN = @ISBN;
SELECT 'Update successful.';
END TRY
BEGIN CATCH
SET @msg = ERROR_MESSAGE();
RAISERROR(@msg, 11, 1);
END CATCH
END
GO