Using XML in SQL2008 to deal with parameters of an indeterminate structure

Source: Internet
Author: User

Objective: Unified interface, when the data structure changes, the front-end service interface does not change, by the business specific analytic structure.

Rules: The identified interfaces are presented with a parameter table (MultiRow), and the indeterminate parameters are in XML

?

  1. DECLARE @r TABLE
  2. ???? (
  3. ?????? Hardwareno INT,
  4. ?????? Emvno INT,
  5. ?????? DATA XML
  6. ????)
  7. INSERT Into @r
  8. VALUES (1, 1,
  9. ??????????‘ <d><k>meter</k><v>1</v></d><d><k>run</k><v>100 </v></d><d><k>work</k><v>80</v></d><d><k>dead</ K><v>20</v></d>'),
  10. ???????? (2, 1,
  11. ??????????‘ <d><k>meter</k><v>21</v></d><d><k>run</k><v> 100</v></d><d><k>work</k><v>80</v></d><d><k>dead </k><v>20</v></d>'),
  12. ???????? (3, 1,
  13. ??????????‘ <d><k>meter</k><v>31</v></d><d><k>run</k><v> 100</v></d><d><k>work</k><v>80</v></d><d><k>dead </k><v>20</v></d><d><k>good</k><v>210</v></d> ‘ )
  14. ?
  15. ?
  16. SELECT *
  17. from @r
  18. ?
  19. ?
  20. DECLARE @xml XML
  21. SELECT @xml = Data
  22. from @r
  23. SELECT t.c.value ('k[1]', 'varchar') K,
  24. ???????? T.c.value ('v[1]', 'varchar') v
  25. from @xml. Nodes ('d') T (c)
  26. ?
  27. ?
  28. ?
  29. SELECT Hardwareno,
  30. ???????? T.c.value ('k[1]', 'varchar') K,
  31. ???????? T.c.value ('v[1]', 'varchar') v
  32. from @r
  33. ???????? Cross APPLY data.nodes ('d') as T (c)

?

Output Result:

?

  1. (3 rows affected)
  2. Hardwareno Emvno DATA
  3. 1 1 <d><k>meter</k><v>1</v></d><d><k>run</k><v>100 </v></d><d><k>work</k><v>80</v></d><d><k>dead</ K><v>20</v></d>
  4. 2 1 <d><k>meter</k><v>21</v></d><d><k>run</k><v>100 </v></d><d><k>work</k><v>80</v></d><d><k>dead</ K><v>20</v></d>
  5. 3 1 <d><k>meter</k><v>31</v></d><d><k>run</k><v>100 </v></d><d><k>work</k><v>80</v></d><d><k>dead</ K><v>20</v></d><d><k>good</k><v>210</v></d>
  6. ?
  7. (3 rows affected)
  8. ?
  9. Kv
  10. -------------------- --------------------
  11. Meter 31
  12. Run 100
  13. Work the
  14. Dead 20
  15. Good 210
  16. ?
  17. (5 rows affected)
  18. ?
  19. Hardwareno K V
  20. ----------- -------------------- --------------------
  21. 1 meter 1
  22. 1 run 100
  23. 1 Work 80
  24. 1 dead 20
  25. 2 Meter 21
  26. 2 Run 100
  27. 2 Work 80
  28. 2 dead 20
  29. 3 Meter 31
  30. 3 Run 100
  31. 3 Work 80
  32. 3 dead 20
  33. 3 Good 210
  34. ?
  35. (13 rows affected)

?

?

?

?

?

?

Using XML in SQL2008 to deal with parameters of an indeterminate structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.