根据空查询的结果将列更新为BIT值

时间:2012-01-05 12:10:14

标签: sql-server tsql

我正在尝试根据另一列是否为空来编写一个将设置位值的查询。

例如:

UPDATE @tmpRank
SET IsXYSale = NOT dbo.Sales_XForY.ID IS NULL
FROM @tmpRank
LEFT JOIN dbo.Sales_XForY ON SaleId = @tmpRank.SaleId

我在NOT关键字上遇到语法错误,但我看不到如何更正查询。

3 个答案:

答案 0 :(得分:9)

SET IsXYSale = CASE WHEN dbo.Sales_XForY.ID IS NULL THEN 0 ELSE 1 END

答案 1 :(得分:1)

UPDATE @tmpRank
SET IsXYSale = CASE WHEN dbo.Sales_XForY.ID IS NULL THEN 0 ELSE 1 END
FROM @tmpRank
LEFT JOIN dbo.Sales_XForY ON SaleId = @tmpRank.SaleId

答案 2 :(得分:1)

SQL Server没有隐含的布尔值

UPDATE @tmpRank
SET IsXYSale = CASE WHEN dbo.Sales_XForY.ID IS NULL THEN 0 ELSE 1 END
FROM @tmpRank
LEFT JOIN dbo.Sales_XForY ON SaleId = @tmpRank.SaleId

在这种情况下,您可以利用位的工作方式:任何非零,非空值都会变为1.它假定您当然不能将0作为ID值...

SET IsXYSale = ISNULL(dbo.Sales_XForY.ID, 0)