Solved

Error in Update using CASE

Posted on 2009-05-12
5
162 Views
Last Modified: 2012-05-06
I get an error when executing the following code. Can someone tell me where I am going wrong?
UPDATE IC

	SET IC.InvoiceSiteID = (CASE WHEN IC.SiteID IN(SELECT InvoiceToSite FROM QuoteInvoicing 

								WHERE QuoteID = IC.QuoteID)

								THEN InvoiceToSite

							ELSE MIN(InvoiceToSite) FROM QuoteInvoicing 

								WHERE QuoteID = IC.QuoteID

									AND InvoiceToSite <> IC.SiteID 

							END)

FROM #InvoiceConsolidation IC

INNER JOIN QuoteInvoicing IQ ON IC.QuoteID = IQ.QuoteID

Open in new window

0
Comment
Question by:BrookK
5 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 50 total points
ID: 24367545
UPDATE IC
      SET IC.InvoiceSiteID = (CASE WHEN IC.SiteID IN(SELECT InvoiceToSite FROM QuoteInvoicing
                                                WHERE QuoteID = IC.QuoteID)
                                                THEN InvoiceToSite
                                          ELSE (SELECT MIN(InvoiceToSite) FROM QuoteInvoicing
                                                WHERE QuoteID = IC.QuoteID
                                                      AND InvoiceToSite <> IC.SiteID  )
                                          END)
FROM #InvoiceConsolidation IC
INNER JOIN QuoteInvoicing IQ ON IC.QuoteID = IQ.QuoteID
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24367551
this should do:
UPDATE IC
      SET IC.InvoiceSiteID = (CASE WHEN IC.SiteID IN(SELECT InvoiceToSite FROM QuoteInvoicing 
                                                WHERE QuoteID = IC.QuoteID)
                                                THEN InvoiceToSite
                                          ELSE SELECT MIN(InvoiceToSite) FROM QuoteInvoicing 
                                                WHERE QuoteID = IC.QuoteID
                                                  AND InvoiceToSite <> IC.SiteID 
                                          END)
FROM #InvoiceConsolidation IC
INNER JOIN QuoteInvoicing IQ ON IC.QuoteID = IQ.QuoteID

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24367566
or like this

UPDATE IC
      SET IC.InvoiceSiteID = (CASE WHEN EXISTS (SELECT null FROM QuoteInvoicing
                                                WHERE QuoteID = IC.QuoteID AND IC.SiteID = InvoiceToSite)
                                                THEN InvoiceToSite
                                          ELSE (SELECT MIN(InvoiceToSite) FROM QuoteInvoicing
                                                WHERE QuoteID = IC.QuoteID
                                                      AND InvoiceToSite <> IC.SiteID  )
                                          END)
FROM #InvoiceConsolidation IC
INNER JOIN QuoteInvoicing IQ ON IC.QuoteID = IQ.QuoteID
0
 

Author Comment

by:BrookK
ID: 24368817
Thank you All.

There was a syntax error and I have corrected it as per Aneeshattingal's first post but had to use CAST for MIN(InvoiceToSite) to convert to INT.

Thanks all
-B
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24371268
Great!  Please close the question.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now