tags:

views:

625

answers:

2

Creating XML out of data in Database by calling proc with bcp as

SET @SQL= 'bcp "exec dbo.proc" queryout '+ @FileName +' -w -r -t  -Sdd\SQL2005 -T '

(proc produced below) Everything is fine => creates XML as desired. Now task is to Add Declaration to this XML (<?xml version="1.0" ?>)

How can this be achieved either in below proc or concating XML with other file (containing the declaration)

SELECT  ( SELECT TOP 1
                    ShiftDate AS "ShiftDate",
                    Shift AS "Shift"
          FROM      [TableName]
        FOR
          XML PATH(''),
              TYPE
        ),
        ( SELECT    EquipmentId AS "WasheryProductionDetails/EquipmentCode",
                    'n/a' AS "WasheryProductionDetails/ActivityCode",
                    'n/a' AS "WasheryProductionDetails/ReasonCode",
                    Parentmaterial AS "WasheryProductionDetails/WasheryFeed/MaterialCode",
                    ParentStockpile AS "WasheryProductionDetails/WasheryFeed/ROMStockpileCode",
                    CAST(ParentTonnes AS DECIMAL(18, 4)) AS "WasheryProductionDetails/WasheryFeed/FeedTonnes",
                    ChildMaterial AS "WasheryProductionDetails/WasheryOutput/MaterialCode",
                    ChildStockpile AS "WasheryProductionDetails/WasheryOutput/ProductStockpileCode",
                    CAST(ChildTonnes AS DECIMAL(18, 4)) AS "WasheryProductionDetails/WasheryOutput/ProductTonnes"
          FROM      [TableName]
        FOR
          XML PATH(''),
              TYPE
        )
FOR     XML PATH(''),
            ROOT('WasheryProduction')

Thanks

+1  A: 

This page suggests that you need to hard-code the declaration:

SELECT

'<?xml version="1.0" ?>'

+
SELECT  ( SELECT TOP 1
... rest of your code goes here...

EDIT: Changed "UNION ALL" (which was pretty clearly wrong) to "+" (both taken from the linked page.)

Dan Breslau
Sorry, I have tried before and the error was All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.Thanks
rmdussa
You're right about UNION. But the page that I linked to also suggests using +, which is the string concatenation operator in SQL Server:SELECT'<?xml version="1.0" ?>'+SELECT ( SELECT TOP 1...
Dan Breslau
Great! Its craeted the xml file but it through an error while openingThe XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. --------------------------------------------------------------------------------End of file reached in invalid state for current encoding. Error processing resource 'file:///C:/20090428NS.xml'. Line 1, ...
rmdussa
Found lot of empty spaces in the end of file,do not know how to get rid automatically while creating file. once I removed manually ,its opend fine.Thanks
rmdussa
It is possible to Create XML File with concatination But it's not opening the XML file,because lot of empty spaces in XML file.So it's not working.I have decided to create XML without declaration
rmdussa
A: 

Declare @SQL varchar(8000)
Declare @xml xml
Declare @strXML as varchar(max)

set @xml = (select * from for xml path(''))
set @strXML='< ? xml version = "1.0" ? >' + convert(varchar(max), @xml)

select @strXML

set @SQL ='bcp "select '''+@strXML+'''" QueryOut "C:\xmlFile.xml" -r -w -t -T -S '

Exec master..xp_cmdshell @SQL

Secret Mong
This solved problem from rmdussa, where the free space did not created any more...
Secret Mong