消息102,级别15,状态1,行1'<附近的语法不正确“

时间:2016-03-31 19:54:47

标签: sql-server-2008 tsql

我正在尝试为MSSQL 2008 R2数据库编写存储过程。 存储过程应该比较一些值并返回一个位/ bool值。 根据参数(@PlatformCode),该过程应选择正确的检查。

但是我得到一个错误,在我尝试将结果存储在输出var。

CREATE PROCEDURE CheckVersion 
    @PlatformCode nvarchar(4),
    @ClientVersion int,
    @ConfigId int = 1, 
    @Response bit OUTPUT 

AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @version int
    IF @PlatformCode = 'a' 
    BEGIN
        SET @version = (SELECT aVersion FROM AppConfigTable WHERE ConfigID = @ConfigId)
        SET @Response = (@version <= @ClientVersion)
    END 
    IF @PlatformCode = 'i'
    BEGIN
        SET @version = (SELECT iVersion FROM AppConfigTable WHERE ConfigID = @ConfigId)
        SET @Response = (@version <= @ClientVersion)
    END
END
GO

是否有更好的方法可以返回结果?

1 个答案:

答案 0 :(得分:2)

您缺少的是CASE声明

CREATE PROCEDURE CheckVersion 
    @PlatformCode nvarchar(4),
    @ClientVersion int,
    @ConfigId int = 1, 
    @Response bit OUTPUT 

AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @version int
    IF @PlatformCode = 'a' 
    BEGIN
        SET @version = (SELECT aVersion FROM AppConfigTable WHERE ConfigID = @ConfigId)
        SET @Response = CASE WHEN (@version <= @ClientVersion) THEN 1 ELSE 0 END
    END 
    IF @PlatformCode = 'i'
    BEGIN
        SET @version = (SELECT iVersion FROM AppConfigTable WHERE ConfigID = @ConfigId)
        SET @Response = CASE WHEN (@version <= @ClientVersion) THEN 1 ELSE 0 END
    END
END
GO

更好的是你可以这样做:

CREATE PROCEDURE CheckVersion 
    @PlatformCode nvarchar(4),
    @ClientVersion int,
    @ConfigId int = 1, 
    @Response bit OUTPUT 

AS
BEGIN
    SET NOCOUNT ON; 

    IF @PlatformCode = 'a' 
    BEGIN
       SELECT @Response = CASE WHEN aVersion <= @ClientVersion  THEN 1 ELSE 0 END FROM AppConfigTable WHERE ConfigID = @ConfigId         
    END 
    IF @PlatformCode = 'i'
    BEGIN
       SELECT @Response = CASE WHEN iVersion <= @ClientVersion  THEN 1 ELSE 0 END FROM AppConfigTable WHERE ConfigID = @ConfigId         
    END
END
GO