February 12, 2010 by Christoff Truter SQL
The following post is a bit of a follow-up to the post I did last month
about TVP's in SQL 2008, what
we're going to do is send a set of data to an xml parameter (introduced in SQL 2005)
and insert it into a table. (Previously we sent a set of data using TVP's in 2008)
Like last month, we create a table, called friends:
CREATE TABLE [dbo].[friends]( [friendID] [int] IDENTITY(1,1) NOT NULL, [firstname] [varchar](50) NOT NULL, [lastname] [varchar](50) NOT NULL, CONSTRAINT [PK_friends] PRIMARY KEY CLUSTERED ( [friendID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
CREATE PROCEDURE [dbo].[addFriends]
@friends XML
AS
BEGIN
INSERT INTO friends(firstname, lastname)
SELECT item.value('@firstname', 'varchar(50)') firstname,
item.value('@lastname', 'varchar(50)') lastname
FROM @friends.nodes('//friend') t (item)
END
<?xml version="1.0" encoding="utf-8" ?> <friends> <friend firstname="w" lastname="x" /> <friend firstname="y" lastname="z" /> </friends>
using (SqlConnection connection = new SqlConnection(@"some connectionstring"))
{
using (SqlCommand command = new SqlCommand("addFriends", connection) { CommandType = CommandType.StoredProcedure })
{
friends f = new friends();
f.Items = new friendsFriend[]
{
new friendsFriend
{
firstname = "Jurgens",
lastname = "Truter"
},
new friendsFriend
{
firstname = "Roland",
lastname = "Cooper"
}
};
StringWriter sw = new StringWriter();
XmlSerializer xml = new XmlSerializer(typeof(friends));
xml.Serialize(sw, f);
connection.Open();
command.Parameters.Add("@friends", SqlDbType.Xml).Value = sw.ToString();
command.ExecuteNonQuery();
}
}
CREATE XML SCHEMA COLLECTION friendCollection AS
'<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="friends" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="friends" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="friend">
<xs:complexType>
<xs:attribute name="firstname" type="xs:string" />
<xs:attribute name="lastname" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>'
ALTER PROCEDURE [dbo].[addFriends]
@friends XML(friendCollection)
AS
BEGIN
INSERT INTO friends(firstname, lastname)
SELECT item.value('@firstname', 'varchar(50)') firstname,
item.value('@lastname', 'varchar(50)') lastname
FROM @friends.nodes('//friend') t (item)
END