question

ivanzgb avatar image
ivanzgb asked

Elements and attributes when using for xml explicit

Hi, I'm using FOR XML EXPLICIT to generate xml file from sql 2000 database... I need to generate something like this ` 150.45 ` How can I do this if I have a table Product with column Price?
sql-server-2000xml
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image
WilliamD answered
After a little messing around, I got it: DECLARE @TestTable TABLE (ProductId int, Price numeric(10, 2), Currency char(3)) INSERT INTO @TestTable SELECT 1, 150.45, 'EUR' SELECT 1 'TAG', NULL 'PARENT', Currency AS 'PRICE!1!currency', Price AS 'PRICE!1!' FROM @TestTable FOR XML EXPLICIT Result: 150.45 If you were to write the select giving the `Price` node a name (after the last "!") it would supply you with a slightly different XML result:
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
+1 - Don't be sorry! :)
1 Like 1 ·
ivanzgb avatar image ivanzgb commented ·
Thank you very much, that's exactly what I need.
0 Likes 0 ·
Matt Whitfield avatar image
Matt Whitfield answered
In SQL Server 2000 I don't think you can do it. For SQL Server 2005, you could use: SELECT [currency], CAST(CAST([price] AS nvarchar(40)) AS XML) FROM [sys].[objects] [Object] FOR XML AUTO Here is an example for objects and types: SELECT RTRIM([type]) AS ObjectType, CAST([name] AS XML) FROM [sys].[objects] [Object] FOR XML AUTO
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image WilliamD commented ·
Sorry matt, it can be done. I thought not at first, but got it after messing around a bit (see my answer)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.