UPDATE from CTE

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • 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

  • 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.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • 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

  • Philip Horan (1/6/2009)


    Thanks guys and yes I am an old git 🙂

    Old Dogs, new tricks....possible?

    Thanks,

    Phil.

    'Course! I'm an old git and I learn something new here every day - see above, for instance!

    Now, whether or not them new tricks are remembered for more than...what were we talking about?

    “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

  • Ha! Ha!

    Keep up the good work guys. The day I start answering posts is the day I know I have learnt those new tricks.

    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

  • You start learning newer tricks when you start answering questions.

  • Joe Celko (1/7/2009)


    Why did you use the UPDATE.. FROM.. syntax ? Do you know that it is both proprietary and unpredictable?

    That's interesting Joe, I never heard the "unpredictable" part before. Plus I was not aware that the standard allowed UPDATE to be used on virtual tables?

    If so, then how would you recommend that the following UPDATE statement be written?:

    UPDATE c

    SET [count] = 0

    FROM (Select id, , alpha, beta, gamma, [type], [count]

    , ROW_NUMBER() OVER(Partition By [count],

    Order By [Type], [Alpha], [Beta], [Gamma], [id])

    AS KeyRowCount

    From ccount) c

    WHERE KeyRowCount > 1

    AND [count] <> 0

    Don't bother critiquing the column names, I know they're awful, but they're not mine (except "KeyRowCount").

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Mr. Celko, Why do you have to be so acerbic? Not a good way to make friends and influence people. Also, your notion of truly portable code? Not anytime soon I'm afraid. Every vendor of every "Standard Language" (COBOL, C, C++, SQL) is going to add extensions to differentiate their product from that of their competitor. Even ADA had extenstions to it outside the statndard. If they don't, why compete? There would be only on RDBM System, one OS, one type of processor, etc.

    If using product specific extensions improves the performance of a system, then use it. Yes, it may lock you into a specific vendor, but that happens. Even then, vendors change and systems have to be modified to work with new hardware or OS or RDBM, it is a fact of life.

Viewing 15 posts - 1 through 15 (of 22 total)

You must be logged in to reply to this topic. Login to reply