mantawolf

SQL XML Output

Mar 26th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.79 KB | None | 0 0
  1. -- create some users
  2. CREATE TABLE ##users (Id INT, firstName VARCHAR(50), lastName VARCHAR(50), residentState VARCHAR(2), userName VARCHAR(25));
  3. INSERT INTO ##users (Id, firstName, lastName, residentState, userName)
  4. VALUES
  5.     (1, 'Jack', 'Smith', 'TX', 'jjSmith')
  6.     , (2, 'Elvis', 'Presley', 'LA', 'the_king')
  7.     , (3, 'Jack', 'Johnson', 'FL', 'some_dude')
  8.     , (4, 'Bruce', 'Willis', 'NC', 'actionMan')
  9.     , (5, 'Arnold', 'Kinston', 'ND', 'whos_this')
  10.  
  11. -- create some phone numbers for the users
  12. CREATE TABLE ##phone (phoneId INT, userId INT, phoneType VARCHAR(10), phoneNumber VARCHAR(15));
  13. INSERT INTO ##phone (phoneId, userId, phoneType, phoneNumber)
  14. VALUES
  15.     (1, 1, 'home', '111-222-3333')
  16.     , (2, 1, 'cell', '222-333-4444')
  17.     , (3, 1, 'work', '333-444-5555')
  18.     , (4, 2, 'home', '444-555-6666')
  19.     , (5, 2, 'work', '555-666-7777')
  20.     , (6, 4, 'home', '666-777-8888')
  21.     , (7, 5, 'home', '777-888-9999')
  22.  
  23. -- create some email addresses for the users
  24. CREATE TABLE ##email (emailId INT, userId INT, emailType VARCHAR(10), emailAddress VARCHAR(100));
  25. INSERT INTO ##email (emailId, userId, emailType, emailAddress)
  26. VALUES
  27.     (1, 2, 'home', '[email protected]')
  28.     , (2, 2, 'work', '[email protected]')
  29.     , (3, 3, 'home', '[email protected]')
  30.     , (4, 4, 'home', '[email protected]')
  31.     , (5, 5, 'home', '[email protected]')
  32.     , (6, 5, 'work', '[email protected]')
  33.     , (7, 5, 'disposable', '[email protected]')
  34.  
  35. -- raw example where everything in the row goes in as attributes with a default name of ROW, you can specify the name though
  36. SELECT
  37.     u.Id
  38.     , u.firstName
  39.     , u.lastName
  40.     , u.residentState
  41.     , u.userName
  42. FROM ##users u
  43. FOR XML RAW('user')
  44.  
  45. -- you can also give it a root element
  46. SELECT
  47.     u.Id
  48.     , u.firstName
  49.     , u.lastName
  50.     , u.residentState
  51.     , u.userName
  52. FROM ##users u
  53. FOR XML RAW, ROOT('users')
  54.  
  55. -- basic example with attributes, '@varchar' alias declaration with @ makes the column an attribute, all attributes declarations must come before data elements
  56. SELECT
  57.     u.Id AS '@userId'
  58.     , u.firstName AS '@firstName'
  59.     , u.lastName  AS '@lastName'
  60.     , u.residentState
  61.     , u.userName
  62. FROM ##users u
  63. FOR XML PATH('User'), TYPE, ROOT('Users')
  64.  
  65. -- slightly complex nested XML data
  66. SELECT
  67.     u.Id AS '@userId'
  68.     , u.firstName AS '@firstName'
  69.     , u.lastName  AS '@lastName'
  70.     , u.residentState
  71.     , u.userName
  72.     , (
  73.         SELECT p.phoneType AS 'PhoneNumber/@Type', p.phoneId AS 'PhoneNumber/@PhoneId', p.phoneNumber AS PhoneNumber
  74.         FROM ##phone p
  75.         WHERE p.userId = u.Id
  76.         FOR XML PATH(''), TYPE, ROOT('PhoneNumbers')
  77.     )
  78.     , (
  79.         SELECT e.emailId AS 'Email/@EmailId', e.emailAddress AS 'Email/@EmailAddress'
  80.         FROM ##email e
  81.         WHERE e.userId = u.Id
  82.         FOR XML PATH(''), TYPE
  83.     )
  84. FROM ##users u
  85. FOR XML PATH('User'), TYPE, ROOT('Users')
  86.  
  87. -- clean up
  88. DROP TABLE ##users
  89. DROP TABLE ##email
  90. DROP TABLE ##phone
Advertisement
Add Comment
Please, Sign In to add comment