|
Hello, I currently have the following block of SQL 2005 code. What I need to do is import the XML as a bulk operation, however for each record I might need to do some additional processing (maybe insert into a separate table). At the moment I can only query the first item, how can I query the complete data in a cursor style where I look around each DTO?
DECLARE @userid nvarchar(255)
SELECT @userid =
tab.col.value('UserId[1]','VARCHAR(20)')
FROM @open_xml.nodes('//DataDTO') tab(col)
select @userid
-- Do some stuff
-- Get next UserID
-- Do some stuff
Any help on this would be great! Thanks Ben
(comments are locked)
|
|
I think dvroman's answer is great (and I've already voted for it), but here's something else that might help you. You can create a table variable to store all of the user IDs and then do whatever you want (cursor, joins, etc.) with that simple table.
(comments are locked)
|
|
Since you need to do "stuff" in your insert, it appears that you either want to do a cursor or a loop. The easiest to do is a cursor:
This cursor is the fastest one.
Personally, in this case, I would write a DTS/SSIS package or preferably a .NET app to handle it. That way you can get the best of both worlds. Excelent looping and data filtration without using too much of the SQL memory and cycles to handle the data. It takes the work away from the central server and puts it on a workstation.
(comments are locked)
|
|
Adding on to Tom Staab's answer (which I voted for), instead of trying to loop through the data, why not take the table that you can create and run set based operations against it. Join or insert or what have you based on the criteria you need to check, as a set instead of row-by-agonizing-row. Exactly. And if you're not sure how to convert your idea or code from cursor-based to set-based, just post another question. :) I'm a SQL geek who loves figuring out those sorts of problems, and I know I'm not the only one here who feels that way.
Oct 20 '09 at 03:23 PM
Tom Staab
(comments are locked)
|

