Solved

need a good xml format to use as stored proc input

Posted on 2010-08-25
3
420 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
ID: 33523602
0
 

Author Comment

by:realcoding
ID: 33523719
anyone with more insight than a link?
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 33863350
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

861 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

26 Experts available now in Live!

Get 1:1 Help Now