What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 65345 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > Stored Procedure with XML as input

Stored Procedure with XML as input

1 vote(s)
Rating: 5 out of 5
Article posted by Arunsp16 on 6/6/2011 | Views: 9694 | Category: Sql Server | Level: Beginner | Points: 250 red flag


How to give Xml as input to Stored Procedure.

Introduction


In the following Stored Procedure article, i have written how to use XML as input to SP

Save XML data in a Table:

 

First step:

Create a table
Create table empxml(LastName nvarchar(255), FirstName nvarchar(255), EmployeeID int)
Second step:

create a stored procedure

Create Procedure spEmpXml @exml xml

as

begin

Insert into empxml(LastName,FirstName,EmployeeID)

Select p.value('@LastName','nvarchar(255)')as LastName,

       p.value('@FirstName','nvarchar(255)')as FirstName,

       p.value('@EmployeeID','int')as EmployeeID from @exml.nodes('/employeeData/employee') as abc(p)

end

In the above insert query, the "empxml" table number of columns and  "exml "Xml field should match.
"nodes" represent the xml fields. "p.Value" used to take xml data's.

Third step:

Execute the stored procedure

Exec spEmpXml @exml = '<employeeData>

                  <employee LastName="Smith" FirstName="Randolph" EmployeeID="25000"/>

                  <employee LastName="Jack" FirstName="Wayn" EmployeeID="45000"/>

               </employeeData>'

Result will be:

LastName  FirstName   EmployeeID
Smith         Randolph      25000
Jack           Wayn          45000

Save XML data in temporary Table:

Create a stored procedure

Create Proc teeXml @prdxml xml

as

begin

Select x.value('.','VARCHAR(5)') as id from @prdxml.nodes('/Products/id') as params(x)

end

Execute SP and give XML input

Exec teeXml @prdxml ='<Products><id>3</id><id>6</id><id>15</id></Products>'

Result will be:

ID
3
6
15

Conclusion


I hope this article will help for beginners to understand how to use XML as input.

Thank you and have a great day.

Regards,
ArunKumar SP

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Arunsp16

About Arun Kumar

Experience:3 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, June 06, 2011
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

In this article we will learn Lag function of Sql Server 2012 (Denali).

This is part 14 of the series of article on SSIS. In this article I shall describe the folder structure used in Deploying the SSIS package and the usage of those folders.

Delete Duplicate rows from the table.

This is part 24 of the series of article on SSIS. In this article we are going to learn how to delete Remote File using FTP Task in SSIS Package.

This is part 21 of the series of article on SSIS. In this article we are going to see know about different FTP tasks operations available in SSIS.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 12:41:21 PM