How to test a value and use nested CASE statements

I have a CTE and an update based on it.

I am testing the value of V.CreditLimit to determine if it is 1 or <>1 and need to SET the value of T.InvoicePreferences in the SET statement. I also require to test T.InvoiceEmailAddress for a NULL or empty value no matter what value V.CreditLimit returns.

The logic is if CreditLimit = 1 then InvoicePreferences are to be 'P!FE'.
If CreditLimit <> 1 AND there is an EmailAddress then InvoicePreferences to be 'EFP' ELSE if EmailAddress is NULL or empty then InvoicePreferences are to be 'P!FE'.
The value of InvoicePreferences will then be used to update the table in the SET statement.

I'm guessing the testing is done in the SELECT with the outcome passed to the SET. Just can't seem to get the syntax right.

WITH UpDateCTE AS (SELECT        T.CustomerAccountName AS T01, V.CustomerAccountName AS V01, T.AccountIsOnHold AS T02, V.AccountIsOnHold AS V02, 
								T.CreditLimit AS T03, V.CreditLimit AS V03, T.InvoiceEmailAddress AS T04, T.InvoicePreferences AS T05 
                   FROM            SpindleCustomerPreferences AS T INNER JOIN
                                            floyd_GetSageCustomersVw AS V ON V.SLCustomerAccountID = T.SLCustomerAccountID)
      UPDATE    UpDateCTE
       SET              T01 = V01, T02 = V02, T03 = V03;

Open in new window

TeDeSmAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
WITH UpDateCTE AS (...)
      UPDATE    UpDateCTE
       SET              T01 = V01, T02 = V02, T03 = V03,
                          T05 = case when V03 != 1 and T04 is not null then 'EFP' else 'P!FE' end

Open in new window

But why do you use a CTE at all? The same could easily be performed directly in the UPDATE, without obfuscating the column names and such.
0
 
lluddenCommented:
Make a common field in the cte with the table you are updating and join the two tables together on that key, and do the update on the base table.

WITH UpDateCTE AS (SELECT        T.CustomerAccountName AS T01, V.CustomerAccountName AS V01, T.AccountIsOnHold AS T02, V.AccountIsOnHold AS V02, 
								T.CreditLimit AS T03, V.CreditLimit AS V03, T.InvoiceEmailAddress AS T04, T.InvoicePreferences AS T05, T.SLCustomerAccountID AS T06 
                   FROM            SpindleCustomerPreferences AS T INNER JOIN
                                            floyd_GetSageCustomersVw AS V ON V.SLCustomerAccountID = T.SLCustomerAccountID)
      UPDATE    SpindleCustomerPreferences
       SET              T01 = V01, T02 = V02, T03 = V03
FROM SpindleCustomerPreferences
INNER JOIN UpDateCTE ON SpindleCustomerPreferences.SLCustomerAccountID = UpDateCTE.T06

Open in new window

0
 
TeDeSmAuthor Commented:
Both good solutions:
Qlemo for the CASE statement and lludden for the revised Update syntax (just needed to change the T0x's to the actual field names in the table)

UPDATE    SpindleCustomerPreferences
       SET              CustomerAccountName = V01, AccountIsOnHold = V02, CreditLimit = V03, InvoicePreferences = CASE WHEN V03 != 1 AND InvoiceEmailAddress IS Not NULL THEN 'EFP' ELSE 'P!FE' END
FROM SpindleCustomerPreferences
INNER JOIN UpDateCTE ON SpindleCustomerPreferences.SLCustomerAccountID = UpDateCTE.T06;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.