Solved

need a good xml format to use as stored proc input

Posted on 2010-08-25
3
405 Views
Last Modified: 2012-05-10
i have a stored proc that takes an xml string that is like <param @operation="ins" @data="test" />

but using this is problematic because i cant validate any params as they are all strings...

using sql parameters would have at least given me strong data types...

im lookign to regain this by using an xml like this and want to know how to extract the xml data for use in my stored procs and to come up with a simple xml layout to make extracting the data from the xml simple in SQL proc.


<param>
    <operation>ins</operation>
    <data>some data here</data>
</param>

Open in new window

0
Comment
Question by:realcoding
3 Comments
 
LVL 10

Expert Comment

by:Jini Jose
Comment Utility
0
 

Author Comment

by:realcoding
Comment Utility
anyone with more insight than a link?
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
Comment Utility
Please see attached code:
CREATE PROCEDURE dbo.my_XML_update

	@XMLData NTEXT,

	@myId SMALLINT

AS

SET NOCOUNT ON



-- Retrieve XML data

DECLARE @hocDoc INT



DECLARE @Comments NVARCHAR(2048)



EXEC SP_XML_PREPAREDOCUMENT @hocDoc OUTPUT, @XMLData 



-- OPEN XML 

-- Param1 is Pointer to DOC

-- Param2 is XML doc hiearchy

-- Param3 is Use Element Structure rather than Attributes





SELECT

	ContactId,

	ContactName

INTO 

	#tmpContact

FROM 

	OPENXML(@hocDoc, '/root/Contact', 2) 

WITH (ContactId  INTEGER, ContactName NVARCHAR(50))



--this is how you populate the contact xml

SELECT Contact.ContactId,

		ISNULL(Contact.ContactName, '') AS ContactName

FROM #tmpContact Contact, Contact C

WHERE	Contact.ContactId = C.Id

ORDER BY	Contact.ContactName

FOR XML AUTO, ELEMENTS



-- Then you can do an INSERT

INSERT INTO Contact (Id, ContactName)

	SELECT ContactId,ContactName 

	FROM #tmpContact 



--or an UPDATE 

UPDATE Contact SET ContactName = t.ContactName 

FROM #tmpContact t

			WHERE	contact.Id = #tmpContact.ContactId 

		

-- Drop the temp Table

DROP TABLE #tmpContact



-- and MUST release XML doc to avoid memeory issues

EXEC SP_XML_REMOVEDOCUMENT @hocDoc

GO

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now