Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Replacements for cursors


Replacements for cursors

Author
Message
sqldriver
sqldriver
Mr or Mrs. 500
Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)

Group: General Forum Members
Points: 586 Visits: 2492
Hi,

I inherited a few stored procedures that use cursors to build create table statements, reading column information stored in another table:

ID_Project ID_Field Field_Seq Field_Vartype Source_Type Source_Name Field_VarSize
2934 memberID 108 nvarchar 4 memberID 64

I can post them if anyone wants to see them to get a better idea of what's going on, but I'm mostly just looking for pointers to replace cursors for a task like this.

We have a standard layout that pipes information from Excel files into things like first/last name, phone number, etc., and then adds any additional columns to the end.

I'd like to take a crack at re-writing them without involving a cursor.

Thanks
Abu Dina
Abu Dina
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 3323
If that's the only use for the cursors then I wouldn't waste your time rewriting them if they already work. I doubt you'll notice much performance difference with a set-based approach anyway. I mean it's not like your tables have thousands of columns!

---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
sqldriver
sqldriver
Mr or Mrs. 500
Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)

Group: General Forum Members
Points: 586 Visits: 2492
Abu Dina (6/18/2013)
If that's the only use for the cursors then I wouldn't waste your time rewriting them if they already work. I doubt you'll notice much performance difference anyway.


It's not a matter of performance, really. Just trying to learn. If there's a performance gain, cool.
Abu Dina
Abu Dina
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 3323
Fair enough :-)

Can you post one of the sproc or that part which creates the tables using cursors?

---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
sqldriver
sqldriver
Mr or Mrs. 500
Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)

Group: General Forum Members
Points: 586 Visits: 2492
Sure. Here you go:

https://www.dropbox.com/s/rkzv28ifjfgabed/SProcs_Erik.zip

Let me know if you can't get to Dropbox, and I'll just post the code.

Also, I only mentioned the table create cursor as an example. Two of them use cursors to insert data, which is where I thought there might be some room for improvement.

Thanks
Abu Dina
Abu Dina
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 3323
Yea got it thanks. Not sure these sprocs work, I've seen a couple of syntax errors in there. Will have a go but difficult with no sample data.

---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
sqldriver
sqldriver
Mr or Mrs. 500
Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)

Group: General Forum Members
Points: 586 Visits: 2492
They work. They've been in use for years. If there are errors, they're not fatal, and they haven't made too much of a mess of things.

The only data I have is client data, which I can't really post obviously. But, before you get too involved creating any, I'm just looking for some direction as to what would work as a replacement for these cursors, not a total re-write of the code. I totally appreciate any help you have time for, just don't want to be too much of a drag on your day!

Thanks
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2212 Visits: 6490
There's nothing wrong in the CreateProcessTable script, in fact you wouldn't be able to do this in any different way anyhow. Building the column list in the LoadProcessFromTarget script will also constitute a neglible part of the time the entire load takes you, and in fact the LoadTargetFromFile script can't be improved much either, except maybe for the fact that you could TRUNCATE the @TargetTable instead of DELETEing from it (assuming there are no foreign keys referring to it of course).

But yes, you could build the column list in the two latter scripts in a few less milliseconds by not using a cursor, I guess that won't make a difference, unless you have a few million tables to create. It certainly won't make a difference unless the majority of tables to load are empty, otherwise the load of data times will outdwarf the time it takes to build the tables.

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
sqldriver
sqldriver
Mr or Mrs. 500
Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)

Group: General Forum Members
Points: 586 Visits: 2492
Jan Van der Eecken (6/18/2013)
There's nothing wrong in the CreateProcessTable script, in fact you wouldn't be able to do this in any different way anyhow. Building the column list in the LoadProcessFromTarget script will also constitute a neglible part of the time the entire load takes you, and in fact the LoadTargetFromFile script can't be improved much either, except maybe for the fact that you could TRUNCATE the @TargetTable instead of DELETEing from it (assuming there are no foreign keys referring to it of course).

But yes, you could build the column list in the two latter scripts in a few less milliseconds by not using a cursor, I guess that won't make a difference, unless you have a few million tables to create. It certainly won't make a difference unless the majority of tables to load are empty, otherwise the load of data times will outdwarf the time it takes to build the tables.




I think if I asked how to do this with a cursor and a loop, I would have gotten a hundred ways to do it without a cursor and a loop ;-)

I do appreciate the feedback, but my question wasn't performance related, really. I'm curious what people use instead of cursors and loops. The code I posted was an example of where I use them, so determining the appropriate replacement method wouldn't be a total shot in the dark.

Thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search