我的MS SQL表有以下数据
ID CONTENT FLAG TEXT
----------------------------------
1 content1 T text1
2 content2 F text2
3 content3 T text2
并且我想以下面的格式从这个表中选择值而没有任何根节点。
<ADDRESS ID="1" FLAG="T">
<FORM CONTENT="content1" TEXT="text1"/>
</ADDRESS>
<ADDRESS ID="2" FLAG="F">
<FORM CONTENT="content2" TEXT="text2"/>
</ADDRESS>
<ADDRESS ID="3" FLAG="T">
<FORM CONTENT="content3" TEXT="text3"/>
</ADDRESS>
答案 0 :(得分:0)
这是很久以前的事了,但是 - 如果你仍然对解决方案感兴趣 - 这就是查询:
DECLARE @tbl TABLE(ID INT,CONTENT VARCHAR(100),FLAG VARCHAR(1),TEXT VARCHAR(100));
INSERT INTO @tbl VALUES
(1,'content1','T','text1')
,(2,'content2','F','text2')
,(3,'content3','T','text2');
SELECT tbl.ID AS [@ID]
,tbl.FLAG AS [@FLAG]
,tbl.CONTENT AS [Form/@CONTENT]
,tbl.TEXT AS [Form/@Text]
FROM @tbl AS tbl
FOR XML PATH('ADDRESS') /*,ROOT('Root')*/ --add Root if you need it
结果
<ADDRESS ID="1" FLAG="T">
<Form CONTENT="content1" Text="text1" />
</ADDRESS>
<ADDRESS ID="2" FLAG="F">
<Form CONTENT="content2" Text="text2" />
</ADDRESS>
<ADDRESS ID="3" FLAG="T">
<Form CONTENT="content3" Text="text2" />
</ADDRESS>