June 18, 2013 at 8:45 am
Hi,
I inherited a few stored procedures that use cursors to build create table statements, reading column information stored in another table:
ID_ProjectID_FieldField_SeqField_VartypeSource_TypeSource_NameField_VarSize
2934 memberID108 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
June 18, 2013 at 9:36 am
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[/url]
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
June 18, 2013 at 9:41 am
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.
June 18, 2013 at 9:43 am
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[/url]
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
June 18, 2013 at 9:51 am
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
June 18, 2013 at 10:08 am
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[/url]
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
June 18, 2013 at 10:25 am
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
June 18, 2013 at 4:50 pm
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.
June 18, 2013 at 8:29 pm
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
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply