Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help with XML input parameter for stored procedure

Author  Topic 

rogdawg
Starting Member

23 Posts

Posted - 2008-03-09 : 16:30:34
I am trying to send XML as an input parameter for a stored procedure. I have seen many articles that do a good job of describing different variations but all the examples show the stored procedure only pulling one value (field) per record from the XML input. I need to pull 3 fields for each record.

Here is an example of the XML being passed:
<object>
<property @propID="14" @propType="4" @propValue="Blah blah text" />
<property @propID="217" @propType="2" @propValue="Some other text" />
</object>

I have a table like this in my database:
CREATE TABLE SCENE_PROPERTY_LINK (ID INT, OBJ_ID INT, PROPERTY_ID INT, PROPERTY_VALUE NTEXT)
and I want a stored procedure that will accept XML and update this table.
Here is what I am trying:
CREATE PROCEDURE sp_UpdateObject
@inValues XML
AS
BEGIN
--create a temporary table
DECLARE @props TABLE(PROPID INT, PROPTYPE INT, PROPVALUE NTEXT)

--And then insert the values from the @inValues XML argument into the temporary table
--I am sure the SELECT statement is VERY wrong

INSERT INTO @props(PROPID, PROPTYPE, PROPVALUE)
SELECT @inValues('@propID', INT), @inValues('@propType', INT), @inValues('@propValue', NTEXT)
FROM @inValues.nodes('/object/property')

--...and then I will use the temp table to update the DB table (SCENE_PROPERTY_LINK) for each record where SCENE_PROPERTY_LINK.PROPERTY_ID = @props.PROPID
AND @props.PROPTYPE != 6


END

I am sure it would be more efficient to update the DB table directly from the XML argument, without using the temporary table. But, I will settle for this solution using the temp table. I have done some work creating XML output from several stored procedures but, this is the first time I have been faced with consuming XML input in SQL.

I apologize for the long post.
Thanks in advance for any help you can provide.


karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2008-03-10 : 07:09:46
check this out.,

http://www.eggheadcafe.com/articles/xmlserializer_bulkload.asp
Go to Top of Page

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2008-03-10 : 07:24:18
I think this should do what you want. You should be able to update this easily enough if you want to stick with the intermediate temp table approach:

-- Declare and populate XML variable
DECLARE @inValues XML
SET @inValues =
'<object>
<property propID="14" propType="4" propValue="Blah blah text" />
<property propID="217" propType="2" propValue="Some other text" />
<property propID="111" propType="6" propValue="Some other text" />
</object>'

-- Create temp table (drop first if exists)
IF OBJECT_ID('tempdb..#SCENE_PROPERTY_LINK') IS NOT NULL
DROP TABLE #SCENE_PROPERTY_LINK
CREATE TABLE #SCENE_PROPERTY_LINK (ID INT, OBJ_ID INT, PROPERTY_ID INT, PROPERTY_VALUE NVARCHAR(MAX))

-- Populate temp table
INSERT INTO #SCENE_PROPERTY_LINK
SELECT 1, 1, 14, 'Some old text'
INSERT INTO #SCENE_PROPERTY_LINK
SELECT 2, 2, 217, 'Some other old text'
INSERT INTO #SCENE_PROPERTY_LINK
SELECT 3, 3, 111, 'Yet more old text'

-- Return current values
SELECT * FROM #SCENE_PROPERTY_LINK

-- Perform update based on incoming XML
---- Note that ID 111 is not updated due to propType = 6
UPDATE SPL
SET SPL.PROPERTY_VALUE = I.ID.value('@propValue', 'VARCHAR(MAX)')
FROM @inValues.nodes('/object/property') AS I(ID)
JOIN #SCENE_PROPERTY_LINK AS SPL
ON SPL.PROPERTY_ID = I.ID.value('@propID', 'INT')
WHERE I.ID.value('@propType', 'INT') <> 6

-- Return new values
SELECT * FROM #SCENE_PROPERTY_LINK


Mark
Go to Top of Page

rogdawg
Starting Member

23 Posts

Posted - 2008-03-10 : 08:43:17
Mark,

That appears to be it, exactly.

I still have a lot to learn about the XML syntax in SQL.

Thanks very much for your help.
Go to Top of Page

rogdawg
Starting Member

23 Posts

Posted - 2008-03-10 : 08:46:46
quote:
Originally posted by karthik_padbanaban

check this out.,

http://www.eggheadcafe.com/articles/xmlserializer_bulkload.asp



Karthik,

Thanks for your response. I am studying the linked article you provided, as it may be the best way to go for my application in the long term.

Thanks again.
Go to Top of Page
   

- Advertisement -