code_junkie

In SQL Server can I insert multiple nodes into XML from a table?

Nov 11th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. SET @MyXml.modify('
  2. insert
  3. <myNode>
  4. {sql:variable("@MyVariable")}
  5. </myNode>
  6. into (/root[1]) ')
  7.  
  8. DECLARE @NestedInput_SpecificDogNameId int
  9. SET @NestedInput_SpecificDogNameId = 99
  10. SELECT [dbo].[udfGetLowestLevelNestedNode_SpecificDogName]
  11. (@NestedInput_SpecificDogNameId)
  12.  
  13. CREATE FUNCTION dbo.udfGetLowestLevelNestedNode_SpecificDogName
  14. (
  15. @NestedInput_SpecificDogNameId
  16. )
  17. RETURNS XML
  18. AS
  19. BEGIN
  20.  
  21. -- Declare the return variable here
  22. DECLARE @ResultVar XML
  23.  
  24. -- Add the T-SQL statements to compute the return value here
  25. SET @ResultVar =
  26. (
  27. SELECT
  28. @SpecificDogNameId as "@SpecificDogNameId",
  29. t.DogName
  30. FROM tblDogs t
  31. FOR XML PATH('Dog')
  32. )
  33.  
  34. -- Return the result of the function
  35. RETURN @ResultVar
  36.  
  37. END
  38.  
  39. CREATE FUNCTION dbo.udfGetLowestLevelNestedNode_SpecificDogName
  40. (
  41. @NestedInput_SpecificDogNameId
  42. )
  43. RETURNS XML
  44. AS
  45. BEGIN
  46.  
  47. -- Declare the return variable here
  48. DECLARE @ResultVar XML
  49.  
  50. -- Add the T-SQL statements to compute the return value here
  51. SET @ResultVar =
  52. (
  53. SELECT
  54. @SpecificDogNameId as "@SpecificDogNameId",
  55. t.DogName
  56. FROM tblDogs t
  57. FOR XML PATH('Dog')
  58. )
  59.  
  60. -- Return the result of the function
  61. RETURN @ResultVar
  62.  
  63. END
  64.  
  65. <Dog SpecificDogNameId=99>Astro</Dog>
  66.  
  67. CREATE FUNCTION [dbo].[udfGetDogCollectionNode]()
  68. RETURNS XML
  69. AS
  70. BEGIN
  71.  
  72. -- Declare the return variable here
  73. DECLARE @ResultVar XML
  74.  
  75. -- Add the T-SQL statements to compute the return value here
  76. SET @ResultVar =
  77. (
  78. SELECT
  79. [dbo].[udfGetLowestLevelNestedNode_SpecificDogName]
  80. (t.SpecificDogNameId)
  81. FROM tblDogs t
  82.  
  83. FOR XML PATH('DogCollection') ELEMENTS
  84. )
  85. -- Return the result of the function
  86. RETURN @ResultVar
  87.  
  88. END
  89.  
  90. SELECT [dbo].[udfGetDogCollectionNode]()
  91.  
  92. <DogCollection>
  93. <Dog SpecificDogNameId="88">Dino</Dog>
  94. <Dog SpecificDogNameId="99">Astro</Dog>
  95. </DogCollection>
  96.  
  97. CREATE FUNCTION [dbo].[udfGetAnimalCollectionNode]()
  98. RETURNS XML
  99. AS
  100. BEGIN
  101.  
  102. DECLARE @ResultVar XML
  103.  
  104. SET @ResultVar =
  105. (
  106. SELECT
  107. dbo.udfGetDogCollectionNode(),
  108. dbo.udfGetCatCollectionNode()
  109. FOR XML PATH('AnimalCollection'), ELEMENTS XSINIL
  110. )
  111.  
  112. RETURN @ResultVar
  113.  
  114. END
  115.  
  116. SELECT [dbo].[udfGetAnimalCollectionNode]()
  117.  
  118. <AnimalCollection>
  119. <DogCollection>
  120. <Dog SpecificDogNameId="88">Dino</Dog>
  121. <Dog SpecificDogNameId="99">Astro</Dog>
  122. </DogCollection>
  123. <CatCollection>
  124. <Cat SpecificCatNameId="11">Sylvester</Cat>
  125. <Cat SpecificCatNameId="22">Tom</Cat>
  126. <Cat SpecificCatNameId="33">Felix</Cat>
  127. </CatCollection>
  128. </AnimalCollection>
Add Comment
Please, Sign In to add comment