Best way to generate XML using TSQL - sql-server

The code below is wrong on so many levels I can't list them all.
I imagine anyone trying to answer this would only make use of the test data table. Ha!
I've cobbled together several posts, bouncing between EXPLICIT, RAW and PATH and it's getting a little much.
I think someone who uses SQL to generate XML frequently would know this off the top of their head.
I don't have a preference for Ex, Raw or Path - I just need the best tool for the job. At the end of the day there will be 65 columns making up various levels of the final document.
I need code to query that table and generate this:
<batchContactList>
<contact contactID="123" action="AddOrModify">
<contactField name="FirstName">Johnny</contactField>
<contactField name="LastName">Testguy</contactField>
<contactPointList>
<contactPoint type="Email">
<contactPointField name="Label">Email</contactPointField>
<contactPointField name="Address">example#sendwordnow.com</contactPointField>
</contactPoint>
</contactPointList>
</contact>
</batchContactList>
Test Code
--Test Data
DECLARE #tvTest TABLE (contactID INT, FirstName VARCHAR(25), LastName VARCHAR(25), [type] VARCHAR(25), [address] VARCHAR(25))
INSERT INTO #tvTest (contactID, FirstName, LastName, [type], [address])
SELECT 123, 'Johnny', 'Testguy', 'email', 'example#sendwordnow.com'
UNION
SELECT 321, 'Sally', 'Testgirl', 'email', '1example#sendwordnow.com';
--Outer
SELECT
A.contactID AS "#contactID"
, 'AddOrModify' AS "#action"
FROM
#tvTest A
FOR XML PATH('contact'), ROOT('batchContactList')
--Level 1
DECLARE #xmldata XML;
SELECT #xmldata = (SELECT contactID, FirstName, LastName FROM #tvTest FOR XML PATH (''));
SELECT
ColumnName AS "#name"
, ColumnValue AS "text()"
FROM
(SELECT
i.value('local-name(.)','varchar(100)') ColumnName
, i.value('.','varchar(100)') ColumnValue
FROM
#xmldata.nodes('//*[text()]') x(i)) tmp
FOR XML PATH ('contactField'), ROOT('contact')
SELECT #xmldata = (SELECT contactID, [type], [address] FROM #tvTest FOR XML PATH (''));
--Level 2, not complete
SELECT
ColumnName AS "#name"
, ColumnValue AS "text()"
FROM
(SELECT
i.value('local-name(.)','varchar(100)') ColumnName
, i.value('.','varchar(100)') ColumnValue
FROM
#xmldata.nodes('//*[text()]') x(i)) tmp
FOR XML PATH ('contactPoint'), ROOT('contactPointList')

I use PATH, always.
Something like this should give you what you want.
select T.contactID as [#contactID],
'AddOrModify' as [#Action],
'FirstName' as [contactField/#name],
T.FirstName as [contactField],
null,
'LastName' as [contactField/#name],
T.LastName as [contactField],
(
select 'Email' as [contactPoint/#type],
'Label' as [contactPointField/#name],
T.type as [contactPointField],
null,
'Address' as [contactPointField/#name],
T.address as [contactPointField]
for xml path('contactPointList'), type
)
from #tvTest as T
for xml path('contact'), root('catchContactList')
Result:
<catchContactList>
<contact contactID="123" Action="AddOrModify">
<contactField name="FirstName">Johnny</contactField>
<contactField name="LastName">Testguy</contactField>
<contactPointList>
<contactPoint type="Email" />
<contactPointField name="Label">email</contactPointField>
<contactPointField name="Address">example#sendwordnow.com</contactPointField>
</contactPointList>
</contact>
<contact contactID="321" Action="AddOrModify">
<contactField name="FirstName">Sally</contactField>
<contactField name="LastName">Testgirl</contactField>
<contactPointList>
<contactPoint type="Email" />
<contactPointField name="Label">email</contactPointField>
<contactPointField name="Address">1example#sendwordnow.com</contactPointField>
</contactPointList>
</contact>
</catchContactList>

Related

All steps required to produce a XML file from a SQL Server table

I need to produce output in specified XML format, thought it would be easy in 2021, tried first to convert it into XSD using online services and use it with my SQL (using CREATE XML SCHEMA COLLECTION Schema1 AS #Schema1);but this didn't work, I suspect that there is no easy way to generate XML using XSD, still need to work with your SELECT, please correct me if I'm wrong. My example below is not final. I'm using SQL Server 2017 and have only SSMS as my tool. (no .NET available).
picxxx
Here is my sample XML.
ryba
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User Active="0" gender="Male" DOB="1965-02-12" mi="X" lName="John" fName="Dorx">
<CInfo ResArea="Montigo"/>
<Demographic race="Asian" HE="No"/>
<RA>
<Memb StartDD="2004-06-11" eStatus="Active" StatusAsOf="2004-05-12" UserID="XHD15"/>
</RA>
</User>
<User Active="0" gender="Male" DOB="1977-04-14" mi="X" lName="Mario" fName="Ma">
<CInfo ResArea="Blanco"/>
<Demographic race="White" HE="Yes"/>
<RA>
<Memb StartDD="2004-02-22" eStatus="Active" StatusAsOf="2004-03-26" UserID="MMX12"/>
<Memb StartDD="2004-12-22" eStatus="Active" StatusAsOf="2004-05-26" UserID="MMX12"/>
</RA>
</User>
</Users>
And here is my test code and SQL (which is not final yet, I still struggling with formatting all tags, and I probably need to do subquery and grouping to list 2 Memb under same User).
/*
SELECT * INTO #t FROM (
SELECT 'XHD15' UserID, '1965-02-12' DOB, 'John' lName, 'Dorx' fName, 'x' mi, 'Montigo' ResArea, '2004-06-11' StartDD, 'Active' eStatus, '2004-05-12' StatusAsOf, 'Asian' race, 'No' HE union
SELECT 'MMX12' UserID, '1977-04-14' DOB, 'Mario' lName, 'Ma' fName, 'x' mi, 'Blanco' ResArea, '2004-02-22' StartDD, 'Active' eStatus, '2004-03-26' StatusAsOf, 'White' race, 'No' HE union
SELECT 'MMX12' UserID, '1977-04-14' DOB, 'Mario' lName, 'Ma' fName, 'x' mi, 'Blanco' ResArea, '2004-12-22' StartDD, 'Active' eStatus, '2004-12-26' StatusAsOf, 'White' race, 'No' HE )x
*/
SELECT Lname AS [#Lname], Fname [#Fname], mi [#mi], DOB [#DOB],
MAX(ResArea) AS [CInfo/#ResArea],
MAX(race) AS [Demographic/#race], MAX(HE) AS [Demographic/#HE]
, MAX(StartDD) AS [RA/Memb/#StartDD], MAX(eStatus) AS [RA/Memb/#eStatus], MAX(StatusAsOf) AS [RA/Memb/#StatusAsOf]
-- Need subquery for multi StartDD
FROM #t AS [User]
GROUP BY Lname, Fname , mi , DOB
FOR XML PATH ('User'), ROOT ('Users')-- , ELEMENTS
Please try the following solution.
The SQL below is using two aliases: p(arent) and c(hild).
When generating nested XML, parent and child data sets are joined via WHERE clause.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (
ID INT IDENTITY PRIMARY KEY,
UserID CHAR(5), DOB DATE,
lName VARCHAR(20),
fName VARCHAR(20),
mi VARCHAR(20),
ResArea VARCHAR(30),
StartDD DATE,
eStatus VARCHAR(10),
StatusAsOf DATE,
race VARCHAR(20),
HE VARCHAR(5)
);
INSERT INTO #tbl VALUES
('XHD15', '1965-02-12', 'John' , 'Dorx', 'x', 'Montigo', '2004-06-11', 'Active', '2004-05-12', 'Asian', 'No'),
('MMX12', '1977-04-14', 'Mario', 'Ma' , 'x', 'Blanco' , '2004-02-22', 'Active', '2004-03-26', 'White', 'No'),
('MMX12', '1977-04-14', 'Mario', 'Ma' , 'x', 'Blanco' , '2004-12-22', 'Active', '2004-12-26', 'White', 'No');
-- DDL and sample data population, end
SELECT Lname AS [#Lname], Fname [#Fname], mi [#mi], DOB [#DOB]
, ResArea AS [CInfo/#ResArea]
, race AS [Demographic/#race], HE AS [Demographic/#HE]
, (SELECT StartDD AS [#StartDD]
, eStatus AS [#eStatus]
, StatusAsOf AS [#StatusAsOf]
, UserID AS [#UserID]
FROM #tbl AS c
WHERE p.UserID = c.UserID
FOR XML PATH('Memb'), TYPE, ROOT('RA')
)
FROM #tbl AS p
GROUP BY p.UserID, p.Lname, p.Fname, p.mi, p.DOB, p.ResArea, p.race, p.HE
FOR XML PATH ('User'), TYPE, ROOT ('Users');
Output XML
<Users>
<User Lname="Mario" Fname="Ma" mi="x" DOB="1977-04-14">
<CInfo ResArea="Blanco" />
<Demographic race="White" HE="No" />
<RA>
<Memb StartDD="2004-02-22" eStatus="Active" StatusAsOf="2004-03-26" UserID="MMX12" />
<Memb StartDD="2004-12-22" eStatus="Active" StatusAsOf="2004-12-26" UserID="MMX12" />
</RA>
</User>
<User Lname="John" Fname="Dorx" mi="x" DOB="1965-02-12">
<CInfo ResArea="Montigo" />
<Demographic race="Asian" HE="No" />
<RA>
<Memb StartDD="2004-06-11" eStatus="Active" StatusAsOf="2004-05-12" UserID="XHD15" />
</RA>
</User>
</Users>

How to join ids from an xml stored in a column with ids from a column in another table

I try to join a list of ids from a xml stored in a column with ids from a column in another table. How can I extract them to get the matching rows.
I cant change the table. The schema can be reduced as:
CREATE TABLE abc([Data] [ntext] NOT NULL, [Group] varchar(3));
INSERT INTO abc
([Data], [Group])
VALUES
('<div>
<test name="Test1" enabled="True"/>
<ul>
<i><a id="t.foo"/></i>
<i><a id="t.bar"/></i>
</ul>
</div>', 'ADG')
;
GO
CREATE TABLE ForgeRock
([productName] varchar(13), [description] varchar(57), [Group] varchar(3))
INSERT INTO ForgeRock ([productName], [description], [Group])
VALUES
('foo', 'Platform for building enterprise provisioning solutions', 'ADG'),
('bar', 'Full-featured access management', 'ADG'),
('taz', 'Robust LDAP server for Java', 'ADG')
;
And here is the query I wrote:
SELECT
[productName]
, [description]
FROM ForgeRock
INNER JOIN abc on ForgeRock.[Group] = abc.[Group]
WHERE cast(abc.[Data] as xml).exist('//div/test[#name="Test1" and #enabled="True"]') = 1
AND ('t.' + productName) LIKE ('%' + cast(abc.[Data] as xml).value('(//ul/i/a/#id)[1]', 'varchar(50)') + '%')
I know that the problem come from the last line, but I am not sure how to tackle the problem.
The actual output is:
productName description
foo Platform for building enterprise provisioning solutions
But the expected output should be:
productName description
foo Platform for building enterprise provisioning solutions
bar Full-featured access management
I tried two solutions from SO (no they are not duplicated as far as I tried):
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
Extracting Attributes from XML Fields in SQL Server 2008 Table
Try it Online
I would personally use STUFF to remove the 2 leading characters, and then compare, rather than a LIKE. I also, for my tests, have changed the data type of the column Data to xml as it is storing XML data, and ntext has been deprecated for 15~ years:
SELECT *
FROM dbo.ForgeRock FR
WHERE EXISTS (SELECT 1
FROM dbo.abc a
CROSS APPLY a.[Data].nodes('/div/ul/i/a') i(a)
WHERE STUFF(i.a.value('#id','varchar(15)'),1,2,'') = FR.productName);
db<>fiddle
Please try the following solution.
I used NVARCHAR(MAX) data type instead of NTEXT. You may need to adjust the finalSELECT clause columns to accommodate your needs. I picked all of them just to show the all column values.
SQL
DECLARE #abc TABLE (xmldata NVARCHAR(MAX) NOT NULL, [Group] CHAR(3));
INSERT INTO #abc (xmldata, [Group]) VALUES
(N'<div>
<test name="Test1" enabled="True"/>
<ul>
<i>
<a id="t.foo"/>
</i>
<i>
<a id="t.bar"/>
</i>
</ul>
</div>', 'ADG');
DECLARE #ForgeRock TABLE ([productName] VARCHAR(13), [description] VARCHAR(57), [Group] char(3));
INSERT INTO #ForgeRock ([productName], [description], [Group]) VALUES
('foo', 'Platform for building enterprise provisioning solutions', 'ADG'),
('bar', 'Full-featured access management', 'ADG'),
('taz', 'Robust LDAP server for Java', 'ADG');
;WITH rs AS
(
SELECT [Group], TRY_CAST(xmldata AS XML) AS xmldata
FROM #abc
)
SELECT rs.[Group], c.value('.', 'VARCHAR(30)') AS id
, f.*
FROM rs
CROSS APPLY xmldata.nodes('div[test[#name="Test1" and #enabled="True"]]/ul/i/a/#id') AS t(c)
INNER JOIN #ForgeRock AS f ON rs.[Group] = f.[Group]
AND SUBSTRING(c.value('.', 'VARCHAR(30)'), 3, 100) = f.productName;
Here is a pretty straightforward approach:
DECLARE #abc table ( [Data] [ntext] NOT NULL, [Group] varchar(3) );
INSERT INTO #abc ( [Data], [Group] ) VALUES
( '<div><test name="Test1" enabled="True"/><ul><i><a id="t.foo"/></i><i><a id="t.bar"/></i></ul></div>', 'ADG' );
DECLARE #ForgeRock table ( [productName] varchar(13), [description] varchar(57), [Group] varchar(3) );
INSERT INTO #ForgeRock ( [productName], [description], [Group] ) VALUES
('foo', 'Platform for building enterprise provisioning solutions', 'ADG'),
('bar', 'Full-featured access management', 'ADG'),
('taz', 'Robust LDAP server for Java', 'ADG');
SELECT
[productName], [description], x.f.value( '#id', 'VARCHAR(13)' ) AS id
FROM #ForgeRock ForgeRock
INNER JOIN #abc abc
ON ForgeRock.[Group] = abc.[Group]
CROSS APPLY ( SELECT CAST( abc.[data] AS xml ) AS x ) cx
CROSS APPLY cx.x.nodes( 'div[test[#name="Test1" and #enabled="True"]]/ul/i/a' ) x(f)
WHERE
x.f.value( '#id', 'VARCHAR(13)' ) LIKE '%' + productName + '%';
Returns
+-------------+---------------------------------------------------------+-------+
| productName | description | id |
+-------------+---------------------------------------------------------+-------+
| foo | Platform for building enterprise provisioning solutions | t.foo |
| bar | Full-featured access management | t.bar |
+-------------+---------------------------------------------------------+-------+
Optionally, you can pass your #name value in as a SQL variable:
DECLARE #name varchar(50) = 'Test1';
SELECT
[productName], [description], x.f.value( '#id', 'VARCHAR(13)' ) AS id
FROM #ForgeRock ForgeRock
INNER JOIN #abc abc
ON ForgeRock.[Group] = abc.[Group]
CROSS APPLY ( SELECT CAST( abc.[data] AS xml ) AS x ) cx
CROSS APPLY cx.x.nodes( 'div[test[#name=sql:variable("#name") and #enabled="True"]]/ul/i/a' ) x(f)
WHERE
x.f.value( '#id', 'VARCHAR(13)' ) LIKE '%' + productName + '%';

Reading non latin character from XML variable and inserting into table

I am trying to insert some information in Arabic from a XML variable into a SQL Server table.
I did something like the below. However it does not work. Could you please tell what is missing?
Create Table EmployeeTbl
(
firstname nvarchar(100),
familyName nvarchar(100)
)
Insert into EmployeeTbl (firstName, FamilyName)
select
t.x.value('(./firstname)[1]', 'nvarchar(100)') as firstname,
t.x.value('(./familyname)[1]', 'nvarchar(100)') as familyname
from
#XMLVariable.nodes('//employeexml) t(x)
Yes, You missed a single-quotes(') at last line:
Create Table EmployeeTbl
(
firstname nvarchar(100),
familyName nvarchar(100)
)
Insert into EmployeeTbl (firstName, FamilyName)
select
t.x.value('(./firstname)[1]', 'nvarchar(100)') as firstname,
t.x.value('(./familyname)[1]', 'nvarchar(100)') as familyname
from
#XMLVariable.nodes('//employeexml') t(x)
Your XML file should be like that :
<employeexml Id="1">
<firstname>بشیر</firstname>
<familyname>المنادی</familyname>
</employeexml>
<employeexml Id="2">
<firstname>یعغوب</firstname>
<familyname>سمیر</familyname>
</employeexml>

Unable to pass Table Column name in xml path in SQL Server

SELECT
CASE WHEN D.DocumentCode='SA' THEN D.Name END AS 'IDProofofsigningauthority',
CASE WHEN D.DocumentCode='GSTIN' THEN D.Name END AS 'GSTINRegistrationCopy',
CASE WHEN D.DocumentCode='SA_T' OR D.DocumentCode='SA_E' THEN D.Name END AS 'IDProofofsigningauthority',
CASE WHEN D.DocumentCode='PAN_T' THEN D.Name END AS 'PANCard',
CONCAT ('https://abc/xyz/',"Filename") AS KYCDocumentUrl,
CASE WHEN D.DocumentCode='GSTIN' THEN BPD.DocumentNumber END AS 'GSTINRegistrationCopyDocumentNumber',
CASE WHEN D.DocumentCode='PAN_T' THEN BPD.DocumentNumber END AS 'PANCardDocumentNumber'
FROM
[dbo].[Documents] D
INNER JOIN
[dbo].[BusinessPartyDcoument] BPD WITH (NOLOCK) ON D.Id = BPD.DocumentId
FOR XML PATH('Document')
This is my current output:
<Document>
<GSTINRegistrationCopy>GSTIN Number</GSTINRegistrationCopy>
<KYCDocumentUrl>https://abc/xyz/R1NUSU5fMTEy.pdf</KYCDocumentUrl>
<GSTINRegistrationCopyNumber>1111</GSTINRegistrationCopyNumber>
</Document>
<Document>
<PANCard>PAN Card</PANCard>
<KYCDocumentUrl>https://abc/xyz/UEFOX1RfNjFfOC8yLzIwMTkgN.pdf</KYCDocumentUrl>
<PANCardDocumentNumber>BBBBB1111V</PANCardDocumentNumber>
</Document>
<Document>
<IDProofauthority>ID Proof of signing authority</IDProofauthority>
<KYCDocumentUrl>https://abc/xyz/U0FfNjFfOC8yLzIwMTkgNjo1.pdf</KYCDocumentUrl>
</Document>
This is my desired output:
<GSTINRegistrationCopy>
<DocumentName>GSTIN Number</DocumentName>
<KYCDocumentUrl>https://abc/xyz/R1NUSU5fMTEy.pdf</KYCDocumentUrl>
<DocumentNumber>1111</DocumentNumber>
</GSTINRegistrationCopy>
<PANCard>
<DocumentName>PAN Card</DocumentName>
<KYCDocumentUrl>https://abc/xyz/UEFOX1RfNjFfOC8yLzIwMTkgN.pdf</KYCDocumentUrl>
<DocumentNumber>BBBBB1111V</DocumentNumber>
</PANCard>
<IDProofauthority>
<DocumentName>ID Proof of signing authority</DocumentName>
<KYCDocumentUrl>https://abc/xyz/U0FfNjFfOC8yLzIwMTkgNjo1.pdf</KYCDocumentUrl>
</IDProofauthority>
I need to get document name instead of <Document> tag as shown in the output using SQL Server query. I need to get expected XML output using my query.
Please suggest how to get this done.
Actually, you want nested xml for each "case" in you "case when".
You can use "for xml path" and "for xml path, type" in nested queries.
But this solution has obvious problems with performance, you can't use it for big amounts of data
declare #Document table
(
DocumentCode varchar(10),
Name varchar(100),
filename varchar(100),
DocumentNumber varchar(20)
)
insert into #document values
('PAN_T','PAN Card','UEFOX1RfNjFfOC8yLzIwMTkgN.pdf','BBBBB1111V'),
('GSTIN','GSTIN Number','R1NUSU5fMTEy.pdf','1111'),
('SA','D Proof of signing authority','U0FfNjFfOC8yLzIwMTkgNjo1.pdf',NULL);
SELECT
CASE WHEN D.DocumentCode='GSTIN' THEN
(
select D.Name 'DocumentName',
'https://abc/xyz/'+d.Filename 'KYCDocumentUrl',
D.DocumentNumber 'DocumentNumber'
for xml path('') ,type
) END GSTINRegistrationCopy
,CASE WHEN D.DocumentCode='PAN_T' THEN
(
select D.Name 'PANCard',
'https://abc/xyz/'+d.Filename 'KYCDocumentUrl',
D.DocumentNumber 'DocumentNumber'
for xml path('') ,type
) END PANCard
,CASE WHEN D.DocumentCode='SA' THEN
(
select D.Name 'DocumentName',
'https://abc/xyz/'+d.Filename 'KYCDocumentUrl'
for xml path('') ,type
) END IDProofauthority
FROM
#Document D
FOR XML PATH('')
(Similar to vitalygolub's answer...)
Given source data structured like the OPs:
if object_id('[dbo].[Documents]') is not null drop table [dbo].[Documents];
if object_id('[dbo].[BusinessPartyDcoument]') is not null drop table [dbo].[BusinessPartyDcoument];
select * into [dbo].[Documents]
from (values
(1, 'GSTIN', 'GSTIN Number'),
(2, 'PAN_T', 'PAN Card'),
(3, 'SA', 'ID Proof of signing authority')
) Src ([Id], [DocumentCode], [Name]);
select * into [dbo].[BusinessPartyDcoument]
from (values
(1, 'R1NUSU5fMTEy.pdf', '1111'),
(2, 'UEFOX1RfNjFfOC8yLzIwMTkgN.pdf', 'BBBBB1111V'),
(3, 'U0FfNjFfOC8yLzIwMTkgNjo1.pdf', null)
) Src ([DocumentId], [Filename], [DocumentNumber]);
The following SQL:
SELECT
(
SELECT
D.Name AS 'DocumentName',
DocumentUrl AS 'KYCDocumentUrl',
BPD.DocumentNumber AS 'DocumentNumber'
WHERE D.DocumentCode='GSTIN'
FOR XML PATH('GSTINRegistrationCopy'), TYPE
),
(
SELECT
D.Name AS 'DocumentName',
DocumentUrl AS 'KYCDocumentUrl',
BPD.DocumentNumber AS 'DocumentNumber'
WHERE D.DocumentCode='PAN_T'
FOR XML PATH('PANCard'), TYPE
),
(
SELECT
D.Name AS 'DocumentName',
DocumentUrl AS 'KYCDocumentUrl'
WHERE D.DocumentCode='SA'
FOR XML PATH('IDProofofsigningauthority'), TYPE
)
FROM
[dbo].[Documents] D
INNER JOIN
[dbo].[BusinessPartyDcoument] BPD WITH (NOLOCK) ON D.Id = BPD.DocumentId
OUTER APPLY (
SELECT DocumentUrl = CONCAT('https://abc/xyz/', "Filename")
) DU
FOR XML PATH('');
Outputs the following XML:
<GSTINRegistrationCopy>
<DocumentName>GSTIN Number</DocumentName>
<KYCDocumentUrl>https://abc/xyz/R1NUSU5fMTEy.pdf</KYCDocumentUrl>
<DocumentNumber>1111</DocumentNumber>
</GSTINRegistrationCopy>
<PANCard>
<DocumentName>PAN Card</DocumentName>
<KYCDocumentUrl>https://abc/xyz/UEFOX1RfNjFfOC8yLzIwMTkgN.pdf</KYCDocumentUrl>
<DocumentNumber>BBBBB1111V</DocumentNumber>
</PANCard>
<IDProofofsigningauthority>
<DocumentName>ID Proof of signing authority</DocumentName>
<KYCDocumentUrl>https://abc/xyz/U0FfNjFfOC8yLzIwMTkgNjo1.pdf</KYCDocumentUrl>
</IDProofofsigningauthority>

Puzzling xml select syntax in Sql Server

In the below sql code, what does T(C) mean? What is T and what is C?
declare #employeeData xml --this would be your XML input parameter
set #employeeData = '<employeeData>
<employee LastName="Smith" FirstName="Randolph" EmployeeID="1234567"/>
</employeeData>'
declare #xmlTable table (LastName nvarchar(255), FirstName nvarchar(255), EmployeeID int)
insert into #xmlTable (LastName, FirstName, EmployeeID)
select
C.value('#LastName','nvarchar(255)') as LastName,
C.value('#FirstName','nvarchar(255)') as FirstName,
C.value('#EmployeeID','int') as EmployeeID
from
#employeeData.nodes('/employeeData/employee') T(C)
select * from #xmlTable
Check MSDN: http://msdn.microsoft.com/en-us/library/ms188282.aspx
T - Table
C - Column

Resources