Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

UPDATE from CTE Expand / Collapse
Author
Message
Posted Monday, January 5, 2009 6:39 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:01 AM
Points: 625, Visits: 1,084
Hi. I have a CTE that returns 2 cost values.

WITH UpdateAverageLabourCost (ProductId,OH,Labour) AS

(
SELECT
AC.ProductId,
AC.OH,
AC.AVGDirectLabour + MAN + SUB - OH AS Labour

FROM dbo.AVGCost AC
INNER JOIN dbo.Products p ON AC.ProductId = p.CrossReference
WHERE Type <> 'P' AND AVGDirectLabour >0
)

UPDATE dbo.Products
SET Products.SLC = Labour
SET Products.SOC = OH
FROM
UpdateAverageLabourCost
WHERE Products.CrossReference = UpdateAverageLabourCost.ProductId

Is it possible to UPDATE multiple fields in one hit?
Currently if I run as is I receive error 'incorrect syntax near the keyword 'SET' (I tried adding a , at the end of the first SET)

I am able to run individually OK by commenting out one of the SETs after the UPDATE.

Can anyone help me out with the correct syntax if this is possible?

Regards,
Phil.


-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper
Post #629743
Posted Monday, January 5, 2009 6:43 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, May 10, 2012 9:49 AM
Points: 800, Visits: 1,759
You write SET only once in update clause:
WITH UpdateAverageLabourCost (ProductId,OH,Labour) AS
(
SELECT
AC.ProductId,
AC.OH,
AC.AVGDirectLabour + MAN + SUB - OH AS Labour

FROM dbo.AVGCost AC
INNER JOIN dbo.Products p ON AC.ProductId = p.CrossReference
WHERE Type <> 'P' AND AVGDirectLabour >0
)

UPDATE dbo.Products
SET Products.SLC = Labour,
Products.SOC = OH
FROM
UpdateAverageLabourCost
WHERE Products.CrossReference = UpdateAverageLabourCost.ProductId


Regards

Piotr


...and your only reply is slàinte mhath
Post #629752
Posted Monday, January 5, 2009 6:46 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:01 AM
Points: 625, Visits: 1,084
Thanks, not a million miles away from getting one right! :)
Phil.


-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper
Post #629755
Posted Monday, January 5, 2009 7:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
Philip Horan (1/5/2009)
Thanks, not a million miles away from getting one right! :)
Phil.


Rewritten without the CTE it's much easier on the eye...
UPDATE p SET 
SLC = (AC.AVGDirectLabour + AC.MAN + AC.SUB - AC.OH),
SOC = AC.OH
FROM dbo.Products p
INNER JOIN dbo.AVGCost AC ON AC.ProductId = p.CrossReference
WHERE Type <> 'P' AND AVGDirectLabour >0

Note the use of "UPDATE p", and also that the table which is the UPDATE target is referenced as the FROM table.There are many possible ways to lay out this type of update, and some of them are known to be performance killers. The best known is where the UPDATE target is not the FROM table but is referenced as an explicit JOIN.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #629780
Posted Monday, January 5, 2009 2:56 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:01 AM
Points: 625, Visits: 1,084
Chris I was just playing with the CTE concept as I am quite new to TSQL.
Moving forward part of the learning process is to know when to employ the correct method as a solution for the problem.
Many thanks for your input.
Phil


-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper
Post #630161
Posted Monday, January 5, 2009 3:08 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
Philip Horan (1/5/2009)
Chris I was just playing with the CTE concept as I am quite new to TSQL.
Moving forward part of the learning process is to know when to employ the correct method as a solution for the problem.
Many thanks for your input.
Phil


Phil,
Best way to learn is to play. I'd never considered using a CTE in an UPDATE before, but right there's a new challenge - to find a scenario where a CTE is the right way to go. I reckon someone will post one within a day or so.
"part of the learning process is to know when to employ the correct method as a solution for the problem" - so you're an old git too, right? :)


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #630168
Posted Monday, January 5, 2009 4:47 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
Chris Morris (1/5/2009)
Philip Horan (1/5/2009)
Chris I was just playing with the CTE concept as I am quite new to TSQL.
Moving forward part of the learning process is to know when to employ the correct method as a solution for the problem.
Many thanks for your input.
Phil


Phil,
Best way to learn is to play. I'd never considered using a CTE in an UPDATE before, but right there's a new challenge - to find a scenario where a CTE is the right way to go. I reckon someone will post one within a day or so.

See that Update solution that I just posted in the "Cursors Be Gone" thread. It's got a derived table, that arguably could/should be a CTE: http://www.sqlservercentral.com/Forums/FindPost630181.aspx


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #630222
Posted Tuesday, January 6, 2009 12:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
RBarryYoung (1/5/2009)
See that Update solution that I just posted in the "Cursors Be Gone" thread. It's got a derived table, that arguably could/should be a CTE: http://www.sqlservercentral.com/Forums/FindPost630181.aspx

Thanks Barry, I missed that thread and it's got tons of useful info.
Your update applies the UPDATE directly to the derived table! I would have expected a join to be necessary between the base table and the derived table with the UPDATE applied to the base table. How interesting!


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #630344
Posted Tuesday, January 6, 2009 6:33 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
Yeah, AFAIK, Derived Tables and CTEs work just like Views in that respect: as long as they follow the rules for writable Views, you can write through them.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #630505
Posted Tuesday, January 6, 2009 9:46 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:01 AM
Points: 625, Visits: 1,084
Thanks guys and yes I am an old git :)

Old Dogs, new tricks....possible?
Thanks,
Phil.


-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper
Post #630727
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse