1

我的 MS SQL 表有以下数据

ID    CONTENT     UDH         TO       FROM
-------------------------------------------
1     HELLO       1          9812      22
2     HELLO       1          9813      22
3     HELLO       1          9814      22

我想在没有任何根节点的情况下以下面的格式从此表中选择值。

<SMS CONTENT="HELLO" UDH="1">
<ADDRESS TO="9812" FROM="22"/>
</SMS>

<SMS CONTENT="HELLO" UDH="1">
<ADDRESS TO="9813" FROM="22"/>
</SMS>

<SMS CONTENT="HELLO" UDH="1">
<ADDRESS TO="9814" FROM="22"/>
</SMS>

这是我之前 在这里发布的内容的清晰版本

4

1 回答 1

2

像这样尝试(根据您的评论):

    DECLARE @tbl TABLE(ID INT,CONTENT VARCHAR(100),UDH INT,[TO] INT,[FROM] INT);
INSERT INTO @tbl VALUES
 (1,'HELLO',1,9812,22)
,(2,'HELLO',1,9813,22)
,(3,'HELLO',1,9814,22);

SELECT tbl.CONTENT AS [@CONTENT]
      ,tbl.UDH AS [@UDH]
      ,tbl.[TO] AS [ADDRESS/@TO]
      ,tbl.[FROM] AS [ADDRESS/@FROM]
FROM @tbl AS tbl
FOR XML PATH('SMS')

/* Result
<SMS CONTENT="HELLO" UDH="1">
  <ADDRESS TO="9812" FROM="22" />
</SMS>
<SMS CONTENT="HELLO" UDH="1">
  <ADDRESS TO="9813" FROM="22" />
</SMS>
<SMS CONTENT="HELLO" UDH="1">
  <ADDRESS TO="9814" FROM="22" />
</SMS>
*/
于 2015-10-19T09:37:48.830 回答