mercredi 16 décembre 2015

SQL Server Multiple Assignments within IF or CASE WHEN block

I was wondering if it's possible to assign values to multiple fields within a block of an IF or CASE WHEN statement in SQL Server 2008? In a project I am working on, when a condition is met, I want to assign specific values to 4 different fields, if not met I want to assign other values to the sames fields. See examples below. Thanks in advance for your help. Any tip will be welcome.

SELECT With CASE WHEN

SELECT with IF

SELECT  Field1
        ,Field2
        ,Field3
        CASE 
        WHEN Condtion1_Met THEN
           ,Field4 = Value4A
           ,Field5 = Value5A
           ,Field6 = Value6A
           ,Field7 = Value7A
       ELSE
           ,Field4 = Value4B
           ,Field5 = Value5B
           ,Field6 = Value6B
           ,Field7 = Value7B
       END
FROM Table1
______________________________________________

SELECT  Field1
       ,Field2
       ,Field3
       IF Condtion1_Met
          BEGIN 
             ,Field4 = Value4A
             ,Field5 = Value5A
             ,Field6 = Value6A
             ,Field7 = Value7A
          END
       ELSE
          BEGIN
             ,Field4 = Value4B
             ,Field5 = Value5B
             ,Field6 = Value6B
             ,Field7 = Value7B
          END

FROM Table1

Aucun commentaire:

Enregistrer un commentaire