Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Replacements for cursors Expand / Collapse
Author
Message
Posted Tuesday, June 18, 2013 8:45 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 4:02 PM
Points: 492, Visits: 2,040
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
Post #1464701
Posted Tuesday, June 18, 2013 9:36 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, October 31, 2014 10:27 AM
Points: 708, Visits: 3,293
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
Post #1464736
Posted Tuesday, June 18, 2013 9:41 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 4:02 PM
Points: 492, Visits: 2,040
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.

Post #1464740
Posted Tuesday, June 18, 2013 9:43 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, October 31, 2014 10:27 AM
Points: 708, Visits: 3,293
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
Post #1464743
Posted Tuesday, June 18, 2013 9:51 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 4:02 PM
Points: 492, Visits: 2,040
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

Post #1464751
Posted Tuesday, June 18, 2013 10:08 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, October 31, 2014 10:27 AM
Points: 708, Visits: 3,293
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
Post #1464766
Posted Tuesday, June 18, 2013 10:25 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 4:02 PM
Points: 492, Visits: 2,040
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



Post #1464776
Posted Tuesday, June 18, 2013 4:50 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 6, 2014 5:01 AM
Points: 2,116, Visits: 6,441
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)
Post #1464909
Posted Tuesday, June 18, 2013 8:28 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 4:02 PM
Points: 492, Visits: 2,040
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
Post #1464934
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse