Marc Chemali
Marc Chemali

Reputation: 109

How to create a CASE statement depending on the result of a Select Statement with parameter?

Using SQL Server, How can I create a CASE statement depending on the value of a Select Statement with a parameter ?

I need to insert data if the ID doesn't exist (NULL) in table Products and if it exists I need to update the data.

I have the below CASE statement query but the syntax is wrong:

Select CASE WHEN (select ID from Products where ProdID = @ProdID) = NULL
then 
(INSERT INTO Table...)
Else
(Update Table...)
END as Prods
FROM Products

Upvotes: 0

Views: 110

Answers (1)

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

IF EXISTS( select ID from Products where ProdID = @ProdID )
BEGIN
   (Update Table...)
END

ELSE
 (INSERT INTO Table...)

For SQL Server 2008 + versions see MERGE statement

Upvotes: 3

Related Questions