ebook img

XML and JSON Recipes for SQL Server PDF

298 Pages·2018·9.655 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview XML and JSON Recipes for SQL Server

XML and JSON Recipes for SQL Server A Problem-Solution Approach Alex Grinberg XML and JSON Recipes for SQL Server Alex Grinberg Richboro, Pennsylvania, USA ISBN-13 (pbk): 978-1-4842-3116-6 ISBN-13 (electronic): 978-1-4842-3117-3 https://doi.org/10.1007/978-1-4842-3117-3 Library of Congress Control Number: 2017962636 Copyright © 2018 by Alex Grinberg Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/ 9781484231166. For more detailed information, please visit http://www.apress.com/ source-code. Contents ■ Part I: XML in SQL Server ����������������������������������������������� 1 ■ Chapter 1: Introducing XML ������������������������������������������������������������3 Stepping into XML �����������������������������������������������������������������������������������3 Sample Database������������������������������������������������������������������������������������������������������4 Understanding XML ��������������������������������������������������������������������������������������������������4 Entitizing XML Characters ����������������������������������������������������������������������������������������6 Exploring the XML Data Type ������������������������������������������������������������������������������������7 1-1� Creating an Untyped XML Column ���������������������������������������������������8 Problem ��������������������������������������������������������������������������������������������������������������������8 Solution���������������������������������������������������������������������������������������������������������������������8 How It Works �����������������������������������������������������������������������������������������������������������10 1-2� Creating an XML Schema in Visual Studio ������������������������������������11 Problem ������������������������������������������������������������������������������������������������������������������11 Solution�������������������������������������������������������������������������������������������������������������������11 How It Works �����������������������������������������������������������������������������������������������������������13 1-3� Creating an XML Schema from SSMS �������������������������������������������14 Problem ������������������������������������������������������������������������������������������������������������������14 Solution�������������������������������������������������������������������������������������������������������������������15 How It Works �����������������������������������������������������������������������������������������������������������16 1-4� Binding XML to a Schema Collection ���������������������������������������������18 Problem ������������������������������������������������������������������������������������������������������������������18 Solution�������������������������������������������������������������������������������������������������������������������18 How It Works �����������������������������������������������������������������������������������������������������������19 1-5� Creating a Typed XML Column �������������������������������������������������������20 Problem ������������������������������������������������������������������������������������������������������������������20 Solution�������������������������������������������������������������������������������������������������������������������21 How It Works �����������������������������������������������������������������������������������������������������������21 Summary �����������������������������������������������������������������������������������������������22 ■ Chapter 2: Building XML ���������������������������������������������������������������23 Fixing the “Unable to show XML” Error �������������������������������������������������24 2-1� Converting Relational Data to a Simple XML Format ���������������������26 Problem ������������������������������������������������������������������������������������������������������������������26 Solution�������������������������������������������������������������������������������������������������������������������26 How It Works �����������������������������������������������������������������������������������������������������������26 2-2� Generating XML Data with Table Names as Element Names ��������28 Problem ������������������������������������������������������������������������������������������������������������������28 Solution�������������������������������������������������������������������������������������������������������������������28 How It Works �����������������������������������������������������������������������������������������������������������30 2-3� Generating Element-Centric XML ���������������������������������������������������30 Problem ������������������������������������������������������������������������������������������������������������������30 Solution�������������������������������������������������������������������������������������������������������������������31 How It Works �����������������������������������������������������������������������������������������������������������31 2-4� Adding a Root Element �������������������������������������������������������������������32 Problem ������������������������������������������������������������������������������������������������������������������32 Solution�������������������������������������������������������������������������������������������������������������������32 How It Works �����������������������������������������������������������������������������������������������������������33 2-5� Including Elements with NULL Values in Your XML Data ����������������33 Problem ������������������������������������������������������������������������������������������������������������������33 Solution�������������������������������������������������������������������������������������������������������������������33 How It Works �����������������������������������������������������������������������������������������������������������34 2-6� Including Binary Data in Your XML �������������������������������������������������34 Problem ������������������������������������������������������������������������������������������������������������������34 Solution�������������������������������������������������������������������������������������������������������������������34 How It Works �����������������������������������������������������������������������������������������������������������35 2-7� Generating Nested Hierarchical XML Data �������������������������������������36 Problem ������������������������������������������������������������������������������������������������������������������36 Solution�������������������������������������������������������������������������������������������������������������������36 How It Works �����������������������������������������������������������������������������������������������������������38 2-8� Building Custom XML ���������������������������������������������������������������������38 Problem ������������������������������������������������������������������������������������������������������������������38 Solution�������������������������������������������������������������������������������������������������������������������38 How It Works �����������������������������������������������������������������������������������������������������������40 2-9� Simplifying Custom XML Generation ���������������������������������������������45 Problem ������������������������������������������������������������������������������������������������������������������45 Solution�������������������������������������������������������������������������������������������������������������������45 How It Works �����������������������������������������������������������������������������������������������������������46 2-10� Adding Special Nodes to Your XML ����������������������������������������������48 Problem ������������������������������������������������������������������������������������������������������������������48 Solution�������������������������������������������������������������������������������������������������������������������48 How It Works �����������������������������������������������������������������������������������������������������������49 Summary �����������������������������������������������������������������������������������������������51 ■ Chapter 3: Manipulating XML Files �����������������������������������������������53 3-1� Storing XML Result in a File from SQL �������������������������������������������53 Problem ������������������������������������������������������������������������������������������������������������������53 Solution�������������������������������������������������������������������������������������������������������������������53 How It Works �����������������������������������������������������������������������������������������������������������55 3-2� Creating XML from an SSIS Package ���������������������������������������������59 Problem ������������������������������������������������������������������������������������������������������������������59 Solution�������������������������������������������������������������������������������������������������������������������59 How It Works �����������������������������������������������������������������������������������������������������������72 3-3� Loading XML from a Stored Procedure ������������������������������������������72 Problem ������������������������������������������������������������������������������������������������������������������72 Solution�������������������������������������������������������������������������������������������������������������������72 How It Works �����������������������������������������������������������������������������������������������������������75 3-4� Loading XML from SSIS Package ���������������������������������������������������78 Problem ������������������������������������������������������������������������������������������������������������������78 Solution�������������������������������������������������������������������������������������������������������������������78 How It Works �����������������������������������������������������������������������������������������������������������90 3-5� Implementing a CLR Solution ���������������������������������������������������������92 Problem ������������������������������������������������������������������������������������������������������������������92 Solution�������������������������������������������������������������������������������������������������������������������92 How It Works �����������������������������������������������������������������������������������������������������������96 Summary �����������������������������������������������������������������������������������������������99 ■ Chapter 4: Shredding XML ����������������������������������������������������������101 4-1� Shredding XML with Internal ENTITY Declarations ���������������������101 Problem ����������������������������������������������������������������������������������������������������������������101 Solution�����������������������������������������������������������������������������������������������������������������101 How It Works ���������������������������������������������������������������������������������������������������������102 4-2� Migrating OPENXML into XQuery �������������������������������������������������108 Problem ����������������������������������������������������������������������������������������������������������������108 Solution�����������������������������������������������������������������������������������������������������������������108 How It Works ���������������������������������������������������������������������������������������������������������109 4-3� Shredding XML from a Column ����������������������������������������������������113 Problem ����������������������������������������������������������������������������������������������������������������113 Solution�����������������������������������������������������������������������������������������������������������������113 How It Works ���������������������������������������������������������������������������������������������������������114 4-4� Dealing with Legacy XML Storage �����������������������������������������������116 Problem ����������������������������������������������������������������������������������������������������������������116 Solution�����������������������������������������������������������������������������������������������������������������117 How It Works ���������������������������������������������������������������������������������������������������������118 4-5� Navigating Typed XML Columns ���������������������������������������������������120 Problem ����������������������������������������������������������������������������������������������������������������120 Solution�����������������������������������������������������������������������������������������������������������������120 How It Works ���������������������������������������������������������������������������������������������������������122 4-6� Retrieving a Subset of Your XML Data �����������������������������������������123 Problem ����������������������������������������������������������������������������������������������������������������123 Solution�����������������������������������������������������������������������������������������������������������������123 How It Works ���������������������������������������������������������������������������������������������������������124 4-7� Finding All XML Columns in a Table ���������������������������������������������127 Problem ����������������������������������������������������������������������������������������������������������������127 Solution�����������������������������������������������������������������������������������������������������������������127 How It Works ���������������������������������������������������������������������������������������������������������129 4-8� Using Multiple CROSS APPLY Operators ���������������������������������������132 Problem ����������������������������������������������������������������������������������������������������������������132 Solution�����������������������������������������������������������������������������������������������������������������132 How It Works ���������������������������������������������������������������������������������������������������������133 Summary ���������������������������������������������������������������������������������������������134 ■ Chapter 5: Modifying XML ����������������������������������������������������������135 5-1� Inserting a Child Element into Your XML ��������������������������������������135 Problem ����������������������������������������������������������������������������������������������������������������135 Solution�����������������������������������������������������������������������������������������������������������������135 How It Works ���������������������������������������������������������������������������������������������������������136 5-2� Inserting a Child Element into an Existing XML Instance with Namespace ���������������������������������������������������������������������������������137 Problem ����������������������������������������������������������������������������������������������������������������137 Solution�����������������������������������������������������������������������������������������������������������������137 How It Works ���������������������������������������������������������������������������������������������������������138 5-3� Inserting XML Attributes ��������������������������������������������������������������140 Problem ����������������������������������������������������������������������������������������������������������������140 Solution�����������������������������������������������������������������������������������������������������������������140 How It Works ���������������������������������������������������������������������������������������������������������141 5-4� Inserting XML Attribute Conditionally� ������������������������������������������143 Problem ����������������������������������������������������������������������������������������������������������������143 Solution�����������������������������������������������������������������������������������������������������������������143 How It Works ���������������������������������������������������������������������������������������������������������144 5-5� Inserting a Child Element with Position Specification �����������������144 Problem ����������������������������������������������������������������������������������������������������������������144 Solution�����������������������������������������������������������������������������������������������������������������144 How It Works ���������������������������������������������������������������������������������������������������������146 5-6� Inserting Multiple Elements ���������������������������������������������������������146 Problem ����������������������������������������������������������������������������������������������������������������146 Solution�����������������������������������������������������������������������������������������������������������������146 How It Works ���������������������������������������������������������������������������������������������������������147 5-7� Updating an XML Element Value ��������������������������������������������������148 Problem ����������������������������������������������������������������������������������������������������������������148 Solution�����������������������������������������������������������������������������������������������������������������148 How It Works ���������������������������������������������������������������������������������������������������������149 5-8� Updating XML Attribute Value ������������������������������������������������������150 Problem ����������������������������������������������������������������������������������������������������������������150 Solution�����������������������������������������������������������������������������������������������������������������150 How It Works ���������������������������������������������������������������������������������������������������������151 5-9� Deleting an XML Attribute ������������������������������������������������������������151 Problem ����������������������������������������������������������������������������������������������������������������151 Solution�����������������������������������������������������������������������������������������������������������������151 How It Works ���������������������������������������������������������������������������������������������������������152 5-10� Deleting an XML Element �����������������������������������������������������������153 Problem ����������������������������������������������������������������������������������������������������������������153 Solution�����������������������������������������������������������������������������������������������������������������153 How It Works ���������������������������������������������������������������������������������������������������������154 Summary ���������������������������������������������������������������������������������������������156 ■ Chapter 6: Filtering XML �������������������������������������������������������������157 6-1� Implementing the exist( ) Method ������������������������������������������������157 Problem ����������������������������������������������������������������������������������������������������������������157 Solution�����������������������������������������������������������������������������������������������������������������157 How It Works ���������������������������������������������������������������������������������������������������������158 6-2� Filtering an XML Value with the exist( ) Method ���������������������������160 Problem ����������������������������������������������������������������������������������������������������������������160 Solution�����������������������������������������������������������������������������������������������������������������160 How It Works ���������������������������������������������������������������������������������������������������������161 6-3� Finding All Occurrences of an XML Element Anywhere Within an XML Instance �����������������������������������������������������������������������164 Problem ����������������������������������������������������������������������������������������������������������������164 Solution�����������������������������������������������������������������������������������������������������������������164 How It Works ���������������������������������������������������������������������������������������������������������166 6-4� Filtering by Single Value ��������������������������������������������������������������167 Problem ����������������������������������������������������������������������������������������������������������������167 Solution�����������������������������������������������������������������������������������������������������������������167 How It Works ���������������������������������������������������������������������������������������������������������168 6-5� Filtering XML by T-SQL Variable ���������������������������������������������������168 Problem ����������������������������������������������������������������������������������������������������������������168 Solution�����������������������������������������������������������������������������������������������������������������168 How It Works ���������������������������������������������������������������������������������������������������������169 6-6� Comparing to a Sequence of Values ��������������������������������������������170 Problem ����������������������������������������������������������������������������������������������������������������170 Solution�����������������������������������������������������������������������������������������������������������������170 How It Works ���������������������������������������������������������������������������������������������������������171 6-7� Matching a Specified String Pattern ��������������������������������������������171 Problem ����������������������������������������������������������������������������������������������������������������171 Solution�����������������������������������������������������������������������������������������������������������������172 How It Works ���������������������������������������������������������������������������������������������������������172 6-8� Filtering a Range of Values ����������������������������������������������������������174 Problem ����������������������������������������������������������������������������������������������������������������174 Solution�����������������������������������������������������������������������������������������������������������������174 How It Works ���������������������������������������������������������������������������������������������������������175 6-9� Filtering by Multiple Conditions ���������������������������������������������������175 Problem ����������������������������������������������������������������������������������������������������������������175 Solution�����������������������������������������������������������������������������������������������������������������175 How It Works ���������������������������������������������������������������������������������������������������������176

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.