Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to test a value and use nested CASE statements

Posted on 2012-09-19
3
Medium Priority
?
398 Views
Last Modified: 2012-09-19
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

0
Comment
Question by:TeDeSm
3 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 total points
ID: 38413755
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
 
LVL 18

Assisted Solution

by:lludden
lludden earned 1000 total points
ID: 38413764
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
 

Author Closing Comment

by:TeDeSm
ID: 38414115
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question