Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DML on CTE?

Posted on 2013-10-24
4
Medium Priority
?
524 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
ID: 39596811
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
ID: 39596944
thanks Mohit.. in your example, can you do INSERT, DELETE, UPDATE on Parts CTE?
0
 
LVL 5

Accepted Solution

by:
MohitPandit earned 2000 total points
ID: 39597000
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
ID: 39622418
thank you Mohit
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

886 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