Important alert: (current site time 7/15/2013 7:58:44 PM EDT)
 

article

Use XML as a stored procedure parameter in SQL 2000!

Email
Submitted on: 5/25/2001 7:13:03 PM
By: Brandon McPherson 
Level: Intermediate
User Rating: By 7 Users
Compatibility: Other
Views: 29390
author picture
(About the author)
 
     In SQL 2000, most of the XML documentation involves using IIS to pass the data in, or Visual Basic. However, you can also simply pass the XML code itself as an ntext parameter, and just load it in as though it were a recordset. See the below example on the Northwind database.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				create procedure dbo.up_Insert_Company(@xmldata ntext)

as
begin
declare @FileHandle int -- Filehandle to the xml stuff
/* convert the xml data into a recordset */
exec dbo.sp_xml_preparedocument @FileHandle output, @xmldata
insert into dbo.categories
select * from openxml(@FileHandle, '/record')
with dbo.categories
end


/*
And here's how you execute the SQL...
*/

dbo.up_Insert_Company '<record CategoryName=''Sample Insert'' Description=''Testing'' />'


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/30/2001 12:28:54 PMPaul

Where can I find dbo.sp_xml_preparedocument ?
(If this comment was disrespectful, please report it.)

 
5/30/2001 12:30:35 PMPaul Sturgill

Nevermind... :)
(If this comment was disrespectful, please report it.)

 
6/15/2001 5:02:53 PMJulius

I was getting a null value error. Adding CategoryID to CategoryName and Description did the trick...
(If this comment was disrespectful, please report it.)

 
8/26/2002 6:41:10 AMPranab

In this case we are passing the entire XML string as a parameter to the stored procedure .
But can we in any way pass the reference to the XML file (say its path )?
(If this comment was disrespectful, please report it.)

 
2/4/2005 8:41:59 PM

This is great! XML as a parameter to a stored procedure! What does it cost in cpu usage?
(If this comment was disrespectful, please report it.)

 
2/22/2006 4:37:20 AMAdi

Great Idea, would aded:
exec sp_xml_removedocument @FileHandle --to the end of the procedure to release the dom
(If this comment was disrespectful, please report it.)

 
5/28/2007 1:36:24 PMcarlos third

hi Mr. Brandon, Thxs for sharing this to us.
Right now, I wanted to make a simple apps, that extract Data from a SQL Server, then export that Data as XML, then load that data in another machine (PC - still using a SQL Server).
Can you help me with this? by making a sample application and submit it here in PSC?
Thanks you so much
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.