为变量赋值的Select语句不能与数据检索相结合(不涉及数据检索)

时间:2018-03-14 06:38:40

标签: sql-server-2008-r2

DECLARE @a bit;
DECLARE @b bit;
DECLARE @c bit;

SELECT 
    @a = flagA, @b = flagB, 
    CASE WHEN flagC is null then @c = 0 ELSE @c = 1 END
FROM Table

上面的查询会抛出标题中所述的异常。如果我错了,请纠正我,但在我看来,上述陈述中没有涉及数据检索?

1 个答案:

答案 0 :(得分:1)

您必须将值分配给@c,如下所示:

SELECT
    @a = flagA,
    @b = flagB,
    @c = (CASE WHEN flagC IS NULL THEN 0 ELSE 1 END)
FROM table