Solved

Error in Update using CASE

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 2 51
removing unwanted rows from an sql server ranked table 13 35
how to make geography query faster?  SQL 7 45
SQL query 7 14
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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