Solved

Error in Update using CASE

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Count with a subquery showing details 10 49
SQL Server 2012 r2 - Make Temp Table Query Faster 5 50
sql server insert 12 40
SQL trigger 5 23
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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