Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a SQL table in DB that has a column which has data in XML. I need to parse the XML data column for each Row of the table and do a bulk insert into another table.

I tried using SQL and wrote a cursor which goes through each row of the table; looks for a string pattern in the attribute value of the XML and returns the result set. It does the job for me but it is slow and luckily there are few rows to process so it works but had it been more rows than it could have been a problem. I was wondering if there is any way SSIS can do the same job

Let's say table name is EquipmentChartGroupLayout and the column which has XML data is equipmentchartlayout

declare cur cursor for select equipmentchartlayout.query('(/carChart/item)') FROM VIMSVehicle_Func_Sprint.Admin.EquipmentChartGroupLayout
declare @xmlCar xml

open cur

Fetch NEXT FROM cur INTO @xmlCar
While (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
begin
SELECT DISTINCT
CAST(Attribute.Name.query('local-name(.)') AS VARCHAR(100)) Attribute,
Cast(Attribute.Name.value('.','VARCHAR(100)') AS VARCHAR(100)) Value
FROM @xmlCar.nodes('/item/@*') Attribute(Name)
Where CAST(Attribute.Name.query('local-name(.)') AS VARCHAR(100)) like '%displayName%'
and (
Cast(Attribute.Name.value('.','VARCHAR(100)') AS VARCHAR(100)) like '%Power%'
Or
Cast(Attribute.Name.value('.','VARCHAR(100)') AS VARCHAR(100)) like '%Pwr%'
)
order by 2
end

FETCH NEXT FROM cur INTO @xmlCar

END

CLOSE cur
DEALLOCATE cur
Posted

1 solution

http://msdn.microsoft.com/en-US/library/ms189887(v=sql.90).aspx[^]

if you're storing it as XML, you can search inside it.
 
Share this answer
 
Comments
AbhiTrip 10-Feb-12 12:34pm    
thanks for the reply Christian .. I tried that. Please look into my post i have been using SQL to search inside the XML. Here is the problem. I need to search XML for the attribute values. and this search is not for an exact attribute value; it is a search based on a pattern. and that is the same thing i need to do for each row of the table and that is where i ended up writing a cursor. if you wish i can post a sample XML
Christian Graus 10-Feb-12 12:36pm    
XML itself allows for searching based on a pattern, if SQL Server supports XPath, which it should.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900