重置存储过程变量

时间:2012-11-12 13:35:55

标签: sql sql-server

嗯,显然这不起作用。我得到“错误的语法'=''错误。 是否可以在存储过程中多次更改变量的值?

与此示例类似:

    DECLARE @columnCounter INT
    SET @columnCounter = 0

    DECLARE @columnName VARCHAR(255)
    SET @columnName = 'A'

    WHILE (@columnCounter < 4)
    BEGIN
        IF (@columnCounter == 1)
        BEGIN
            SET @columnName = 'B'
        END
        IF (@columnCounter == 2)
        BEGIN
            SET @columnName = 'C'
        END
        IF (@columnCounter == 3)
        BEGIN
            SET @columnName = 'D'
        END
    END

谢谢。

2 个答案:

答案 0 :(得分:2)

==。 Transact-SQL使用简单的=进行相等性比较,如下所示:

DECLARE @columnCounter INT;
SET @columnCounter = 0;

DECLARE @columnName VARCHAR(255);
SET @columnName = 'Set_To_Value_ID';

WHILE (@columnCounter < 4)
BEGIN
    IF (@columnCounter = 1)
    BEGIN
        SET @columnName = 'Set_To_Variable_ID';
    END
    IF (@columnCounter = 2)
    BEGIN
        SET @columnName = 'Changed_In_SP';
    END
    IF (@columnCounter = 3)
    BEGIN
        SET @columnName = 'Set_To_Comment_Input';
    END
    set @columnCounter += 1;
END

答案 1 :(得分:1)

IF (@columnCounter == 1)

比较应该是===是许多语言中的常用语法,但不是t-sql。

http://msdn.microsoft.com/en-us/library/ms175118(v=sql.100).aspx