Chapter 13. Code Is Data, Too!
This chapter is interesting for those who may not have any use for examples about customer data. It just might expand your concept of “data” and the kinds of fuzzy things we can do with it in SQL! It includes a second case study based on work I have done repeatedly for employers and clients over the years. We’re going to approach it stepwise, all aimed at looking at code as data: data that can be read and written!
Working with XML Data
Often you get your import data as a CSV, tab-separated values (TSV), or Excel file. Sometimes there are other more esoteric delimiting schemes. Perhaps the most esoteric textual data format ever devised was XML. I won’t go into the depths of how to deal with it with namespaces, entity encodings, all that. Once you understand the basics like XPath, the SQL documentation is easy enough to read. I just want to point out that if you get some XML data, via a file transfer, web service response payload, or whatever, the first thing you can do is import it into a SQL table and then ETL out of that. Let’s check it out:
CREATETABLEstaging.XMLDemo(ImportLineXMLNULL);
INSERTINTOstaging.XMLDemoVALUES('<Customer><LastName>Snedley</LastName><FirstName>Mortimer</FirstName></Customer>');
SELECT*FROMstaging.XMLDemo;
| ImportLine |
|---|
| <customer><lastname>Snedley</lastname><firstname>Mortimer</firstname></customer> |
Note that in tools like SQL Server Management Studio (SSMS), the preceding result would ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access