February 7, 2020 at 9:48 pm
Here I have a common table expression followed by INSERT INTO. After that, I want to SELECT the results so I can see the newly inserted data, however I get an error. I presume this is because after the first occurrence of SELECT, INSERT INTO, UPDATE or DELETE the CTE can no longer be referenced.
With ProductName(ProductID, Description)
AS
(
SELECT ProductID, ProductName
FROM dbo.Products
)
INSERT INTO ProductName -- You can use INSERT or INSERT INTO. Either way you get the same result
VALUES
(70, 'Neck and Back Massager'),
(71, 'Goggles'),
(72, 'Back scratcher'),
(73, 'Xylitol Mouthwash')
SELECT *
FROM ProductName; --ProductName not recognized since INSERT INTO was already used
How can I see the newly inserted data that was inserted into the CTE? I know I can create multiple CTEs with one WITH statement, but I want to reference the same CTE that INSERT INTO is referencing. Is this possible?
February 7, 2020 at 10:35 pm
A CTE has an execution scope of a single (the important word) statement that follows the declaration of the CTE. So just for the select, insert, update, delete, etc that follows the CTE. When you have another statement after your insert, the CTE is no longer available. It's just a temporary result set. What your trying to do may be better with a table variable.
Sue
February 7, 2020 at 10:50 pm
you need to use the output clause
With ProductName(ProductID, Description)
AS
(
SELECT ProductID, ProductName
FROM dbo.Products
)
INSERT INTO ProductName -- You can use INSERT or INSERT INTO. Either way you get the same result
output inserted.*
VALUES
(70, 'Neck and Back Massager'),
(71, 'Goggles'),
(72, 'Back scratcher'),
(73, 'Xylitol Mouthwash')
February 8, 2020 at 9:22 pm
Or, put the CTE with the select that is calling it:
With ProductName(ProductID, Description)
AS
(
SELECT ProductID, ProductName
FROM dbo.Products
)INSERT INTO ProductName -- You can use INSERT or INSERT INTO. Either way you get the same result
VALUES
(70, 'Neck and Back Massager'),
(71, 'Goggles'),
(72, 'Back scratcher'),
(73, 'Xylitol Mouthwash'); -- << Be sure to terminate the statement prior to the CTE
With ProductName(ProductID, Description)
AS
(
SELECT ProductID, ProductName
FROM dbo.Products
)
SELECT *
FROM ProductName;
February 11, 2020 at 2:17 am
you need to use the output clause
With ProductName(ProductID, Description)
AS
(
SELECT ProductID, ProductName
FROM dbo.Products
)
INSERT INTO ProductName -- You can use INSERT or INSERT INTO. Either way you get the same result
output inserted.*
VALUES
(70, 'Neck and Back Massager'),
(71, 'Goggles'),
(72, 'Back scratcher'),
(73, 'Xylitol Mouthwash')
Thank you. I have never used the OUTPUT clause before. I will look into that.
February 11, 2020 at 2:17 am
Or, put the CTE with the select that is calling it:
With ProductName(ProductID, Description)
AS
(
SELECT ProductID, ProductName
FROM dbo.Products
)INSERT INTO ProductName -- You can use INSERT or INSERT INTO. Either way you get the same result
VALUES
(70, 'Neck and Back Massager'),
(71, 'Goggles'),
(72, 'Back scratcher'),
(73, 'Xylitol Mouthwash'); -- << Be sure to terminate the statement prior to the CTE
With ProductName(ProductID, Description)
AS
(
SELECT ProductID, ProductName
FROM dbo.Products
)
SELECT *
FROM ProductName;
That's neat. I didn't think you could reuse a CTE name again. I'll try it.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply