I wanted to do a cool experiment on SSIS that I haven’t tried before. I am really interested in this whole mapping component on SSRS, and I know that most likely the biggest challenge of companies is how to geocode addresses that are already in their databases. So what I wanted to do was to try and retrieve lat/long information from public geocoding web services within SSIS.
Please note that the exercise below is purely for experimentation. Please read the restrictions of each of the geocoding services, and go with whichever is appropriate for your purpose.
Also this is a simplistic code to show you how to get the latitude/longitude information. On your own versions, test thoroughly and add the appropriate exception handlers.
Ok, let’s start.
Jacob Sebastian is a SQL Server XML Guru!
Check out his collection for XQUery Labs. This is the first 12 of his series, and right now he has 43 and counting:
XQuery Sample Scripts
* XQuery Lab 1 – Transforming rows to columns
* XQuery Lab 2 – An example using OUTER APPLY
* XQuery Lab 3 – Filtering specific nodes
* XQuery Lab 4 – Joining XML Nodes with a Relational Table
* XQuery Lab 5 – Working with Namespaces
* XQuery Lab 6 – Processing Header-Detail information
* XQuery Lab 7 – Extracting a comma separated list of values
* XQuery Lab 8 – How to update the attribute value of an XML variable?
* XQuery Lab 9 – How to delete an attribute from an XML variable?
* XQuery Lab 10 – How to insert an attribute to an XML variable
* XQuery Lab 11 – How to insert an element to an XML variable
* XQuery Lab 12 – Different ways of reading values from an XML variable
Jacob Sebastian has also released a free ebook via RedGate – The Art of XSD – SQL Server XML Schema Collections
Check it out, all 483 pages! 🙂
Assume this is your XML snippet
[sql collapse=”false” firstline=”1″ gutter=”true” smarttabs=”true” tabsize=”4″ toolbar=”true”]
DECLARE @authorsXML XML
SET @authorsXML = ‘
<Street>10932 Bigge Rd.</Street>
Note that the examples below show how you can manipulate XML nodes – but most operations require singleton values. Ie, the changes must affect one and only one node. Thus in most the examples we specify the index of the node we want to target.
[sql collapse=”false” firstline=”1″ gutter=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]
which means we are only targetting the first instance of LastName under the Author node. If you need to do a mass update, you may need to use a cursor.
The following is an example of how to create an XML Schema in SQL Server.
1: -- DROP IF EXISTS
2: IF EXISTS (SELECT 1
3: FROM sys.xml_schema_collections
4: WHERE name='SampleSchema')
5: DROP XML SCHEMA COLLECTION SampleSchema
7: CREATE XML SCHEMA COLLECTION SampleSchema AS
8: '<?xml version="1.0" encoding="utf-8"?>
9: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
10: <xsd:element name="Sample">
12: <xsd:attribute name="SampleID" type="xsd:integer" />
13: <xsd:attribute name="Name" type="xsd:string" />
14: <xsd:attribute name="Description" type="xsd:string" />
To check your XML Schema:
Method 1: Go to your database > Programmability > Types > XML Schema Collections
Method 2: You can use the xml_schema_namespace function to query the schema from within SSMS
Later on I will post additional sample schemas which use different SQL XML data types.