SqlShare.com Logo
 
Skip Navigation Links
Home
Channels
Classes
About Us
Login / Register
Subscribe to the All channels feed  Goto the SqlShare.com blog
Image of Plamen Ratchev
Author:

Author Bio:
Plamen Ratchev has over two decades experience in the software architecture and development field. He has enjoyed building solutions for UNIX, DOS and Windows platforms, with main focus on SQL Server since version 4.21. Plamen is founder of Tangra, specializing in relational database applications analysis, implementation, and tuning. His particular interest is in design patterns, performance a...
Performing Updates with Common Table Expressions (CTE)

Performing Updates with Common Table Expressions (CTE)

Common table expressions are a more readable version of a derived table. This videos shows you how to utilize a common table expression to update data.

Video Information
Publish Date:
November 06, 2008
Length:
2:49
Skill Level:
100
Rating:
3.98 out of 5

After watching you should be able to answer these questions:
  1. A common table expression can be best thought of as?

  2. Common table expressions were first available in which version of SQL Server?
You must be logged in to view this video.    

Bookmark and Share

  Rate This Video:  
 
1=Poor, 3=Good, 5=Excellent
Comments:
Johnny on 11/11/2008
It looks good, but I think the UPDATE .. FROM .. WHERE is simpler and more logical than CTE.

Plamen Ratchev on 11/12/2008
In my opinion the use of the CTE makes the code more readable and more convenient to troubleshoot and maintain. It is very easy to test the CTE with a SELECT statement to check what values will get updated. Plus you get the same efficient plan as you would get with the UPDATE with JOIN syntax. Also, there are some cases where incorrectly written UPDATE with FROM clause that does not reference the target table for the update may produce very unpleasant results (see more details in the following article by Jeff Moden: http://www.sqlservercentral.com/articles/Performance+Tuning/62278/).

Blake on 1/7/2009
talks too fast and does't explain what he is doing

Bill Nicolich on 1/7/2009
Good example.

Adam Gojdas on 1/7/2009
I would have to slightly disagree with your statement: "created once then used multiple times in a query or script". I would only say that it can be used multiple times in a query. It would not qualify for multiple times in a script because it would need to be redefined within the execution scope of each SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement that utilizes it. You cannot just define it once in a script and use it throughout multiple/separate queries within the script. I do find it an interesting alternative to the "UPDATE .. FROM .. WHERE" syntax though.

Plamen Ratchev on 1/7/2009
Adam, you are correct. I am not sure why that description was added to the video, I do not discuss the nature of CTEs in the video at all. The CTE is valid only in the scope of the consuming statements as you pointed out (including the MERGE statement in SQL Server 2008).

Harvey Schwartz on 1/7/2009
Is it possible to get a version of the t-sql that I can cut and paste and then execute? It would be useful for this and most other demos.

544525734F on 1/7/2009
Very good. Simple and Precise.

Andy Warren on 1/8/2009
Plamen, Adam, I updated the description to simplify!

Chris Kasten on 1/8/2009
make it twice as long with another example. I *think* I get it... but would love a bit more reinforcement

Mike Mastro on 1/8/2009
Very Good. Simple & solid example.

Jon on 1/10/2009
Good job, but I had a little trouble with accent. Lack of exposure on my part, I suppose.

Plamen Ratchev on 1/14/2009
Harvey, the sample scripts used in the video are attached now under References.

Narasmha Rao on 1/28/2009
good

Chris Helm on 2/13/2009
another example may have helped but I think I get the idea.

Tom Grumbling on 2/28/2009
It would have been helpful to have an example of what the update statement would be if you hadn't used the CTE

Plamen Ratchev on 3/1/2009
Tom, using the T-SQL specific UPDATE with JOIN (without CTE), the query will look like this: UPDATE Products SET product_desc = U.product_desc, price = U.price FROM products AS P JOIN ProductUpdates AS U ON P.sku = U.sku WHERE U.effective_date < CURRENT_TIMESTAMP;

suresh.kala on 12/1/2009
its good

Feodor Georgiev on 12/2/2009
Great video!

seth buxton on 2/8/2010
UPDATE a SET some_value = 200 --select * FROM TableA a JOIN TableB b on b.some_value = a.some_value WHERE a.some_other_value = 400 Highlight from SELECT down to view the affected rows, highlight the entire UPDATE statement to affect the UPDATE. You don't have to use a CTE to preview the affected rows.

seth buxton on 2/8/2010
The formatting got mangled.



Must Be Logged In

References
Example Script for Performing Updates with Common Table Expressions

Sponsored Ads
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions