Solved

DML on CTE?

Posted on 2013-10-24
4
423 Views
Last Modified: 2013-11-04
what DMLs can do inside a CTE resultset?
0
Comment
Question by:25112
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:MohitPandit
Comment Utility
Hello,

You can have
SELECT
statement inside of CTE parenthesis but after close CTE parenthesis then you can have
INSERT, DELETE, UPDATE, SELECT

For example,
USE AdventureWorks2012;
GO
WITH Parts(AssemblyID, ComponentID, PerAssemblyQty, EndDate, ComponentLevel) AS
(
    SELECT b.ProductAssemblyID, b.ComponentID, b.PerAssemblyQty,
        b.EndDate, 0 AS ComponentLevel
    FROM Production.BillOfMaterials AS b
    WHERE b.ProductAssemblyID = 800
          AND b.EndDate IS NULL
    UNION ALL
    SELECT bom.ProductAssemblyID, bom.ComponentID, p.PerAssemblyQty,
        bom.EndDate, ComponentLevel + 1
    FROM Production.BillOfMaterials AS bom 
        INNER JOIN Parts AS p
        ON bom.ProductAssemblyID = p.ComponentID
        AND bom.EndDate IS NULL
)
UPDATE Production.BillOfMaterials
SET PerAssemblyQty = c.PerAssemblyQty * 2
FROM Production.BillOfMaterials AS c
JOIN Parts AS d ON c.ProductAssemblyID = d.AssemblyID
WHERE d.ComponentLevel = 0; 

Open in new window


Kindly take a look over it and let me know in case you have any concern.

Best Regards,
Mohit Pandit
0
 
LVL 5

Author Comment

by:25112
Comment Utility
thanks Mohit.. in your example, can you do INSERT, DELETE, UPDATE on Parts CTE?
0
 
LVL 5

Accepted Solution

by:
MohitPandit earned 500 total points
Comment Utility
Hello,

Please find below example with temp table data:

CREATE TABLE #T1 (Pid Int IDENTITY PRIMARY KEY, Name VarChar(50), Address VarChar(50))

INSERT INTO #T1(Name)
VALUES ('Ram'), ('Sham'), ('Sudhir')

-- Just fetch
WITH parts (id, name)
AS
(
	SELECT Pid, Name
	FROM #T1
)
SELECT * FROM parts;

-- Insert using CTE
WITH parts (id, name)
AS
(
	SELECT Pid, Name
	FROM #T1
)
INSERT INTO parts (Name)
VALUES ('Henri');

-- Update using CTE
WITH parts (id, name)
AS
(
	SELECT Pid, Name
	FROM #T1
)
UPDATE parts
SET name = 'Ramlal'
WHERE id = 1;

-- Delete using CTE
WITH parts (id, name)
AS
(
	SELECT Pid, Name
	FROM #T1
)
DELETE parts
WHERE id = 2

SELECT * FROM #T1;

-- Can create different table as well, here temp table
	-- on this #T2 temp table, you can do further calculation if want.
WITH parts (id, name)
AS
(
	SELECT Pid, Name
	FROM #T1
)
SELECT * INTO #T2 FROM parts

SELECT '#T2 Table', * FROM #T2

DROP TABLE #T1
DROP TABLE #T2

Open in new window


Kindly take a look over it and let me know in case you have any concern.

Best Regards
Mohit Pandit
0
 
LVL 5

Author Comment

by:25112
Comment Utility
thank you Mohit
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now