使用SQL Server,如何根据带参数的Select语句的值创建CASE语句?
如果表Products中的ID不存在(NULL),我需要插入数据,如果存在,我需要更新数据。
我有以下CASE语句查询,但语法错误:
Select CASE WHEN (select ID from Products where ProdID = @ProdID) = NULL
then
(INSERT INTO Table...)
Else
(Update Table...)
END as Prods
FROM Products
答案 0 :(得分:3)
IF EXISTS( select ID from Products where ProdID = @ProdID )
BEGIN
(Update Table...)
END
ELSE
(INSERT INTO Table...)
对于SQL Server 2008 +版本,请参阅MERGE语句