0

此查询工作正常

SELECT 1 AS TAG, NULL AS parent, 1 [dog!1]
UNION (SELECT 1,NULL, 3) 
FOR XML EXPLICIT, ROOT('dogs')

我想将它分配给一个变量。没有工会,这可以正常工作:

DECLARE @x xml
SET @x = ((
  (SELECT 1 AS TAG, NULL AS parent, 1 [dog!1]
   FOR XML EXPLICIT, ROOT('dogs'))
  )) 

一旦我介绍了工会,它就不起作用了:

DECLARE @x xml
SET @x = ((
  (SELECT 1 AS TAG, NULL AS parent, 1 [dog!1]
   UNION (SELECT 1,NULL, 3) 
   FOR XML EXPLICIT, ROOT('dogs'))
   )) 

尝试了各种各样的事情。我错过了什么

4

1 回答 1

3

您只需要再添加一个子查询级别,如下所示:

DECLARE @X XML = 
(
  SELECT * FROM
  (
    SELECT 1 AS TAG, NULL AS parent, 1 [dog!1]
    UNION 
    SELECT 1,NULL, 3
  )X
  FOR XML EXPLICIT, ROOT('dogs')
);

SELECT @X;
于 2013-02-13T17:08:03.107 回答