0

I have a stored procedure where i am passing a simple XML:

'<Products><ProductId>1</ProductId><ProductId>2</ProductId></Products>'

I have a @temp table in SQL which has a ProductId Column:

DECLARE @Temp TABLE (
ProductId NVARCHAR(10)
)     

I need to write an insert statement which will loop through the ProductId's in the XML (which can be infinite) and keep on inserting (in the @temp table) until the XML has no more ProductId nodes left.

A solution involving cursors is not feasible!

Following is the code I am trying to execute:

Declare @test XML
SET @test = '<Products><ProductId>1</ProductId><ProductId>2</ProductId></Products>'
DECLARE @Temp TABLE(        
    ProductId NVARCHAR(10)
   )  
INSERT INTO @Temp(ProductId)
SELECT tab.col.value('./ProductId[1]','NVARCHAR(10)') AS 'ProductId'
FROM @test
CROSS APPLY
xml_data.nodes('//Products') AS tab(col)

I keep getting the error:

Must declare the table variable "@test".
3
  • Try running just the SELECT portion of the INSERT, swapping out the @test with the xml string. Make sure that works first Commented Aug 27, 2009 at 19:07
  • Won't allow me to do a FROM on a string. Commented Aug 27, 2009 at 19:10
  • Yeah, need to use OPENXML - see my updated answer. Ref: OPENXML: msdn.microsoft.com/en-us/library/aa276847%28SQL.80%29.aspx Commented Aug 27, 2009 at 19:15

3 Answers 3

2
DECLARE @test XML
    SET @test = '<Products><ProductId>1</ProductId><ProductId>2</ProductId></Products>'

DECLARE @Temp TABLE(ProductId NVARCHAR(10))  

DECLARE @docHandle int 
EXEC sp_xml_preparedocument @docHandle OUTPUT, @doc 

INSERT INTO @Temp(ProductId)
     SELECT t.value('./ProductId[1]','NVARCHAR(10)') AS 'ProductId'
       FROM OPENXML(@docHandle, '//Products', 1) t

EXEC sp_xml_removedocument @docHandle 
Sign up to request clarification or add additional context in comments.

1 Comment

Just added a standalone code to explain my example still throws an error !
2
DECLARE @idoc int

DECLARE @doc varchar(1000)

SET @doc ='
<OutLookContact>
<Contact FirstName="Asif" LastName="Ghafoor" EmailAddress1="[email protected]" />
<Contact FirstName="Rameez" LastName="Ali" EmailAddress1="[email protected]" />
<Contact FirstName="Aneel" LastName="Maqsood" EmailAddress1="[email protected]" />
</OutLookContact>'

--Create an internal representation of the XML document.

EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

-- Execute a SELECT statement that uses the OPENXML rowset provider.

DECLARE @Temp TABLE(FirstName VARCHAR(250),LastName VARCHAR(250),Email1 VARCHAR(250))  

INSERT INTO @Temp(FirstName,LastName,Email1)



SELECT *

FROM OPENXML (@idoc, '/OutLookContact/Contact',1)

WITH (FirstName varchar(50),LastName varchar(50),EmailAddress1 varchar(50))


select FirstName,LastName,Email1 from @Temp

Comments

0

You could put the xml into another temp table, and then do an insert statement from there:

http://www.lockergnome.com/sqlsquirrel/2008/05/23/how-to-turn-imported-xml-into-a-relational-format-in-sql-server-2005/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.