Skip to content

Instantly share code, notes, and snippets.

Created September 24, 2014 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/78c9724d6e8cdb3397eb to your computer and use it in GitHub Desktop.
Save anonymous/78c9724d6e8cdb3397eb to your computer and use it in GitHub Desktop.
CREATE TABLE dbo.Final
(
ID int IDENTITY(1,1) NOT NULL
, text1 varchar(50) NOT NULL
, text2 varchar(50) NOT NULL
, text3 varchar(50) NOT NULL
);
CREATE TABLE dbo.Chain
(
ID int NOT NULL
);
-- Works
INSERT INTO
dbo.Final
(
text1
, text2
, text3
)
OUTPUT
INSERTED.*
SELECT
D.t1
, D.t2
, D.t3
FROM
(
VALUES
('A', 'B', 'C')
, ('D', 'B', 'C')
, ('G', 'B', 'C')
, ('J', 'B', 'C')
) D (t1,t2,t3)
-- Comment out the insert portion to generate
-- A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed in a SELECT statement that is not the immediate source of rows for an INSERT statement.
INSERT INTO
dbo.Chain
(
ID
)
SELECT
X.ID
FROM
(
INSERT INTO
dbo.Final
(
text1
, text2
, text3
)
OUTPUT
INSERTED.*
SELECT
D.t1
, D.t2
, D.t3
FROM
(
VALUES
('A', 'B', 'C')
, ('D', 'B', 'C')
, ('G', 'B', 'C')
, ('J', 'B', 'C')
) D (t1,t2,t3)
) x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment