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 erorr : Must declare the table variable "@test".