如何为 XML 编写包含联合逻辑的 SQL Select?
How to write an SQL Select for XML including Union logic?
我有以下语句将两个日期都输入@xmlData
declare @xmlData OUTOUT
SET @xmlData = (SELECT @FileDate AS [FileDate] UNION SELECT @satDate AS [FileDate] FOR XML RAW, ELEMENTS)
然后我会把它插入table:
DECLARE @ListOfDates TABLE (FileDate varchar(50))
INSERT @ListOfDates (FileDate)
SELECT Tbl.Col.value('FileDate[1]', 'varchar(50)')
FROM @xmlData.nodes('//row') Tbl(Col)
在执行我的 select
逻辑时,我收到一条错误消息:
The FOR XML and FOR JSON clauses are invalid in views, inline
functions, derived tables, and subqueries when they contain a set
operator. To work around, wrap the SELECT containing a set operator
using derived table or common table expression or view and apply FOR
XML or FOR JSON on top of it.
如何解决?
我完全不明白你为什么要在这里使用 XML。但错误是告诉您将该查询下推到派生的 table(子查询)或 CTE,如下所示:
declare @xmlData xml
declare @filedate date = getdate()
declare @satdate date = '20140101'
SET @xmlData = (
select * from
( SELECT @FileDate AS [FileDate] UNION ALL SELECT @satDate AS [FileDate] ) d
FOR XML RAW, ELEMENTS, type
)
select @xmlData
我有以下语句将两个日期都输入@xmlData
declare @xmlData OUTOUT
SET @xmlData = (SELECT @FileDate AS [FileDate] UNION SELECT @satDate AS [FileDate] FOR XML RAW, ELEMENTS)
然后我会把它插入table:
DECLARE @ListOfDates TABLE (FileDate varchar(50))
INSERT @ListOfDates (FileDate)
SELECT Tbl.Col.value('FileDate[1]', 'varchar(50)')
FROM @xmlData.nodes('//row') Tbl(Col)
在执行我的 select
逻辑时,我收到一条错误消息:
The FOR XML and FOR JSON clauses are invalid in views, inline functions, derived tables, and subqueries when they contain a set operator. To work around, wrap the SELECT containing a set operator using derived table or common table expression or view and apply FOR XML or FOR JSON on top of it.
如何解决?
我完全不明白你为什么要在这里使用 XML。但错误是告诉您将该查询下推到派生的 table(子查询)或 CTE,如下所示:
declare @xmlData xml
declare @filedate date = getdate()
declare @satdate date = '20140101'
SET @xmlData = (
select * from
( SELECT @FileDate AS [FileDate] UNION ALL SELECT @satDate AS [FileDate] ) d
FOR XML RAW, ELEMENTS, type
)
select @xmlData