Question : Export Data to XML File from SQL Database

We are running this query against our SQL (2008) database:

select p_brand, count(*) from Products
group by p_brand

where it will find several thousand unique brand names and display their count next to them.  We would like to sort this list by 0-9 and A-Z and export the data to an XML file with the p_brand value and the count.

So there would be 26 files, one for each letter, A, B, C and so on until Z, and 1 more XML file for all values beginning with 0-9.  These xml files should be generated and updated every X hours (i.e. maybe once daily) and saved to a location in the public wwwroot folder so that we can import them into third party scripts and style them through XSLT files.

Please explain in layman terms how we can accomplish this so that the xml files are generated based on the A-Z and updated automatically once every 24 hours.  Thanks.

Answer : Export Data to XML File from SQL Database

1) Change your variable

DECLARE @dataToWrite VARCHAR(MAX)

2)  Add an IF condition

  IF LEN(@brand) < 38 BEGIN
    SET @dataToWrite = @dataToWrite + @xmlData
    SET @dataToWrite  = REPLACE(REPLACE(@dataToWrite, '@item1@', @brand), '@item2@', @total)
    SET @dataToWrite  = REPLACE(@dataToWrite, '@item3@', REPLACE( @brand, ' ', '+'))
  END

I attached the modified script.

Random Solutions  
 
programming4us programming4us