Solved

Error in Update using CASE

Posted on 2009-05-12
5
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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