Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using if/else in insert and update statements

I have a staging table (in SQL SERVER 2008) with nullable fields.
I want to Insert or Update the records from the staging table to main table.
During this I want a comparison to be done

Update main
set main.field1 = (
if(staging.field1 isnull)
    then ---- 
else if(staging.field2 isnull)
    then ---- 
else
    then
)

How can I embed the above condition in my insert and update statements?

like image 863
Kamil Avatar asked Dec 28 '22 04:12

Kamil


1 Answers

The (sort of) equivalent is to use CASE expressions:

UPDATE main
SET main.field1 =
  CASE
    WHEN staging.field1 IS NULL
      THEN --
    WHEN staging.field2 IS NULL
      THEN --
    ELSE --
  END;
like image 110
Yuck Avatar answered Jan 10 '23 10:01

Yuck