4

我正在尝试在 sql server 中编写一个相当简单的存储过程。如果我的错误的来源很明显,我没有抓住它。SSMS 给我“')' 附近的语法错误。” 任何帮助,将不胜感激。这是程序:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sprocAddSymbols] @symbol NVARCHAR(10)

AS 

BEGIN

DECLARE @symbolCheck NVARCHAR(10)
DECLARE @statusCheck NVARCHAR(10)

SET @symbolCheck = (SELECT Symbol FROM tblSymbolsMain WHERE Symbol = @symbol)
SET @statusCheck = (SELECT SymbolStatus FROM tblSymbolsMain WHERE Symbol = @symbol)

IF (@symbolCheck IS NOT NULL AND @statusCheck = 'Inactive')
BEGIN
    UPDATE tblSymbolsMain
    SET SymbolStatus = 'Active'
    WHERE Symbol = @symbol
END
ELSE
    INSERT INTO tblSymbolsMain (Symbol, DateAdded, Status)
    VALUES (@symbol, GETDATE(), 'Active')
4

1 回答 1

5

你最后错过了一个 END 。总是缩进你的代码。它不仅看起来更好,而且您不会遇到这些情况。此外,即使您在 IF/ELSE 中有单个语句,或者在任何其他不需要 BEING/END 的块中,最好使用 BEGIN 和 END。当以后有人可以添加语句并破坏作为 IF 或 ELSE 块一部分的代码时,它减少了维护的噩梦。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sprocAddSymbols] @symbol NVARCHAR(10)

AS 

BEGIN

    DECLARE @symbolCheck NVARCHAR(10)
    DECLARE @statusCheck NVARCHAR(10)

    SET @symbolCheck = (SELECT Symbol FROM tblSymbolsMain WHERE Symbol = @symbol)
    SET @statusCheck = (SELECT SymbolStatus FROM tblSymbolsMain WHERE Symbol = @symbol)

    IF (@symbolCheck IS NOT NULL AND @statusCheck = 'Inactive')
    BEGIN
        UPDATE tblSymbolsMain
        SET SymbolStatus = 'Active'
        WHERE Symbol = @symbol
    END
    ELSE
    BEGIN
        INSERT INTO tblSymbolsMain (Symbol, DateAdded, Status)
        VALUES (@symbol, GETDATE(), 'Active')
    END

END
于 2012-04-10T22:37:48.733 回答