Guest User

Untitled

a guest
Dec 11th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. /*
  2. To export the data inb Xml format:
  3. 1) Store the Xml into a Xml object
  4. 2) Create a global temp table and populate it with the Xml
  5. 3) Bcp the data out from the global temp table
  6. 4) Drop the global temp table
  7. */
  8.  
  9. DECLARE @xmlData XML
  10.  
  11. SELECT @xmlData =
  12. (SELECT
  13. '@Id' = CustomerId,
  14. '@CompanyName' = CompanyName,
  15. 'Orders' =
  16. (SELECT
  17. '@Orderid' = o.OrderId,
  18. '@OrderDate' = o.OrderDate,
  19. '@OrderLineCount' = COUNT(od.OrderId)
  20. FROM
  21. dbo.Orders o
  22. JOIN
  23. dbo.[Order Details] od on o.OrderId = od.OrderId
  24. WHERE
  25. c.CustomerId = o.CustomerId
  26. GROUP BY
  27. o.OrderId,
  28. o.OrderDate
  29. FOR
  30. XML PATH('Order'),
  31. TYPE
  32. )
  33. FROM
  34. dbo.Customers c
  35. ORDER BY
  36. c.CustomerId
  37. FOR
  38. XML PATH('Customer'),
  39. ROOT('doc')
  40. )
  41.  
  42. create table ##tempXmlData (XmlData Xml)
  43.  
  44. insert into ##tempXmlData (XmlData)
  45. select @xmlData
  46.  
  47. declare @cmd nvarchar(4000)
  48. select @cmd = 'bcp "##tempXmlData" out c:\data.xml –w -t "|" -S' + @@servername + ' -T'
  49. exec master..xp_cmdshell @cmd
  50.  
  51. drop table ##tempXmlData
  52.  
  53. go
Add Comment
Please, Sign In to add comment