1

Is there a way to generate a xml from OUTPUT clause, something like the following

DECLARE @Test TABLE (Id int, Name varchar(max))

INSERT @Test 
OUTPUT (SELECT INSERTED.* FOR XML RAW('data'), ELEMENTS XSINIL, TYPE)
VALUES (1, 'One'), (2, 'Two'), (3, 'Three')

UPDATE @Test SET Name = Name
OUTPUT (SELECT DELETED.*, INSERTED.* FOR XML RAW('data'), ELEMENTS XSINIL, TYPE)

DELETE FROM @Test
OUTPUT (SELECT DELETED.* FOR XML RAW('data'), ELEMENTS XSINIL, TYPE)

but working as this code fails with "Subqueries are not allowed in the OUTPUT clause."

Thanks, Slava

2 Answers 2

1

From MSDN

The OUTPUT clause is not supported in the following statements:
• DML statements that reference local partitioned views, distributed partitioned views, or remote tables.
• INSERT statements that contain an EXECUTE statement.
• Full-text predicates are not allowed in the OUTPUT clause when the database compatibility level is set to 100.
• The OUTPUT INTO clause cannot be used to insert into a view, or rowset function.
• A user-defined function cannot be created if it contains an OUTPUT INTO clause that has a table as its target.

You can't generate xml using output clause. So another alternative is declare an intermediary table variable to be the target then SELECT from that while insert or update table. something like this -

DECLARE @T TABLE (Id int, Name varchar(max))

INSERT @Test 
OUTPUT INSERTED.*
INTO @T
VALUES (1, 'One'), (2, 'Two'), (3, 'Three')

DELETE FROM @Test
OUTPUT DELETED.*
INTO @T

SELECT *
FROM  @T FOR XML RAW('data'), ELEMENTS XSINIL, TYPE
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

DECLARE @Test TABLE (Id int, Name varchar(max))
DECLARE @Inserted TABLE (Id int, Name varchar(max))

INSERT @Test 
OUTPUT INSERTED.* INTO @Inserted
VALUES (1, 'One'), (2, 'Two'), (3, 'Three')

SELECT * From @Inserted FOR XML RAW('data'), ELEMENTS XSINIL, TYPE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.