SQL Server has a lot of useful functionality built-in when it comes to XML. Today I want to show you an easy way to parse XML data in to a usable format inside your query.
For this example, I will be parsing XML data into a temp table, then inserting that data in to a table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
DECLARE@XMLToParseXML
-- Load the XML data in to a variable to work with.
-- This would typically be passed as a parameter to a stored proc
SET@XMLToParse='<Animals>
<LandAnimals>
<Animal>Baboon</Animal>
<Animal>Yak</Animal>
<Animal>Zebra</Animal>
</LandAnimals>
</Animals>'
-- Declare temp table to parse data into
DECLARE@ParsingTableTABLE
(AnimalVARCHAR(100))
-- Parse the XML in to the temp table declared above
This part says to parse the @XMLToParse variable (previously filled with the XML file) using the method built in to SQL Server called .nodes. It specifies the repeating node to be Animals/LandAnimals/Animal. It assigns an aliases this XML parsed records as a table named xmlData. The (A) is the column name of the rowset. This will be referenced in the select part of the statement.
1
SELECTxmlData.A.value('.','VARCHAR(100)')ASAnimal
The select part of this statement references xmlData (which is the table aliases) and A (which is the column named for that table). It calls the .value function to return the value from the table/column. For the .value function, you pass in 2 elements.
The first element is the field. In this case we are passing in just a period. We do this because there is no node below the Animal node. If there was, we would need to specify it here..
The second parameter is the datatype that you would like the value to be casted as. Then I always aliases the field to something relevant.
Although parsing XML can be a little confusing in SQL Server, it is very powerful. This is a great way to pass bulk data to a stored procedure from any type of client application.
Although some of these keyboard shortcuts may seem a little odd at first… once you start using them I think that you’ll love them and use them all the time.
Ctrl + R – Show/Hide the results pane
Ctrl + N – New query window
Ctrl + Shift + U – Switch current selection to uppercase
Ctrl + Shift + L – Switch current selection to lowercase
F5 – Execute the currently selected query (or entire query if nothing is selected)
Ctrl + Tab – Switch to the next open tab
Ctrl + Shift + Tab – Switch to the previous open tab
Ctrl + Space – Pops up IntelliSense complete if it is not already open
Ctrl + Shift + R – Refresh local IntelliSense cache
The UNION operation and the UNION ALL operation perform almost the same operation. They are both used to combine two result sets in to one result set. The main difference between the two operations is that the UNION operation will return the unique records in the final result set. The UNION ALL operation will return any duplicates in the final result set.
In the examples below, we will assume we have two tables with the following data. Notice that the values Cat and Dog exist in both tables.
Table1
Table2
Alligator
Cat
Beaver
Dog
Cat
Eagle
Dog
Frog
The UNION Operation
1
2
3
4
5
6
7
SELECT*
FROMTable1
UNION
SELECT*
FROMTable2
Result
Alligator
Beaver
Cat
Dog
Eagle
Frog
Notice that the duplicate Cat and Dog values were removed from the result.
The UNION ALL Operation
1
2
3
4
5
6
7
SELECT*
FROMTable1
UNIONALL
SELECT*
FROMTable2
Result
Alligator
Beaver
Cat
Dog
Cat
Dog
Eagle
Frog
Notice that the duplicate Cat and Dog values are left in the result.
Speed Considerations
One final difference between UNION and UNION ALL is the speed difference. Because the UNION operation needs to return a distinct set of values, this will take extra processing time. A general rule is that if you know that the combined data is going to be unique already… just use the plain UNION ALL operation. Only use the UNION operation if there are duplicates that you would like to have remove.
NOTE: The UNION and UNION ALL operations are actually one operation. The ALL keyword is just an attribute. Because they are so different, I find it is easier to think of them both as separate operations.
One question that we get a lot is how to get the quarter from a date. SQL Server has a very easy way to get the quarter from a date (since SQL 2005) using the DATEPART command.