select-for-xml

FOR XML SQL Server - Variable Element name in output XML

放肆的年华 提交于 2019-12-13 14:17:12
问题 I'm quite new to FOR XML in SQL Server, I've searched considerable and I can't find an answer to this. Can I have a variable element name using 'for xml' where the element name is not hard-coded and is instead take from a cell in each row? Take the following example... Table ORDERS : ID STATUS TIME AMOUNT ------------------------------------ 1 COMPLETE 02:31 2355 2 ACCEPTED 02:39 6653 3 ACCEPTED 04:21 4102 4 RECEIVED 05:03 4225 FOR XML query: select ID, TIME as STATUS_TIME, AMOUNT as CURRENT