Solved

Error in Update using CASE

Posted on 2009-05-12
5
166 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

9 Experts available now in Live!

Get 1:1 Help Now