有没有办法从OUTPUT子句生成xml,如下所示
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)
但是因为此代码失败,“OUTPUT子句中不允许使用子查询。”
谢谢, 斯拉瓦
答案 0 :(得分:1)
来自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.
您无法使用output子句生成xml。所以另一种方法是在插入或更新表时将中间表变量声明为目标,然后从中声明SELECT。像这样的东西 -
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
答案 1 :(得分:0)
试试这个:
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