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 123»»»

Create Temporary Tables Dynamically in cursor loop Expand / Collapse
Author
Message
Posted Thursday, June 19, 2008 3:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 24, 2008 5:02 AM
Points: 5, Visits: 11
I have a cursor and would like to create a temporary table for each record in that cursor. The name of each temporary table must be obtained partly from a value I get within the cursor data.

Something like
Open cursor;
Fetch Next from cursor into @Variable_Temp_Table_Name;
While @@Fetch_Status = 0
Begin
Create table # (pk int)
Fetch Next from cursor into @Variable_Temp_Table_Name;
End

Newbie says thanks.

Post #519692
Posted Thursday, June 19, 2008 5:30 AM
SSC-Addicted

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

Group: General Forum Members
Last Login: Monday, November 4, 2013 4:22 PM
Points: 480, Visits: 467
Good question! I do not know how beause of scoping problems. If you execute:

exec ('create table #what(id int)')

select * from #what

/* -------- Sample Output: --------
Msg 208, Level 16, State 0, Line 3
Invalid object name '#what'.
*/

The "#what" table never gets created within the same scope as the SELECT statement. You can change "#what" to "##what" but temp tables that start with ## are global temp tables rather than local temp tables. Use of global temp tables can have dangerous side affects.
Post #519737
Posted Thursday, June 19, 2008 12:01 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
I don't think you can do what you're trying to do, and, even if you can, you probably shouldn't.

A cursor that creates an arbitrary number of temp tables with 1 row each? (If I'm understanding you correctly.) That sounds like the kind of code to use if you want to be rear-ended by a glacier.

If you really, really must do so, what you might want to do is create one temp table, insert XML data into it for each "sub table", and then use that. Of course, that's asking for an opportunity to be rear-ended by continental drift, but it would do something that might fit what you're asking for.

Alternately, post something about the end result you're trying to achieve, the code you have for it so far, and we might be able to help you arrive at a better solution.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #520095
Posted Thursday, June 19, 2008 12:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:42 AM
Points: 36,955, Visits: 31,463
elmerbotha (6/19/2008)
I have a cursor and would like to create a temporary table for each record in that cursor. The name of each temporary table must be obtained partly from a value I get within the cursor data.


WHY???


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #520130
Posted Thursday, June 19, 2008 12:47 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
Ouch!

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #520132
Posted Thursday, June 19, 2008 1:03 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:42 AM
Points: 36,955, Visits: 31,463
Honest to goodness... folks keep posting what they think is a solution to a problem instead of posting the actual problem. This particular one is RBAR on sterioids and is akin to flushing yourself down the toilet to try to get to the chow hall without going outside... ;)

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #520146
Posted Thursday, June 19, 2008 1:45 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
Jeff Moden (6/19/2008)
Honest to goodness... folks keep posting what they think is a solution to a problem instead of posting the actual problem. This particular one is RBAR on sterioids and is akin to flushing yourself down the toilet to try to get to the chow hall without going outside... ;)


I tried to word my reply a bit more diplomatically, but it said essentially the same thing.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #520198
Posted Thursday, June 19, 2008 2:25 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:42 AM
Points: 36,955, Visits: 31,463
Heh... I'm just not good at diplomacy when I see people trying to commit "Death by SQL". It's just absolutely impossible to pick up a turd by the clean end. :D

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #520244
Posted Friday, June 20, 2008 1:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 24, 2008 5:02 AM
Points: 5, Visits: 11
I truly feel honored to be graced by your absolutely meaningful and relevant answers. Truly, I am astounded by your infinite wisdom. Without risking being ridiculed once again by a bunch of super famous forum participants, I am going to approach this differently this time. Hopefully someone would be willing to stoop down to my level and give me some insight into how I could possibly approach this problem. Again, I am not proficient in SQL, so please bear with me. If I am at the wrong place, just say so. There are other forums out there.

I have a resultset with a bunch of lab results for certain precious metals which looks something like this:
Metal Result Unit of Measurement
Pt 1.26754 %
Pt 1 ppm
Au 4 ppm
Rd 8 %
etc, etc

What I have to do with the above is to per metal:
- Find the min value
- Find the mx value
-
Find the standard deviation
Post #520391
Posted Friday, June 20, 2008 4:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:46 AM
Points: 7,187, Visits: 13,641
elmerbotha, can you please provide a larger sample size, say 5 or 10 rows for two metals? Presumably in your actual data you will have several rows like this:

Metal  Result         Unit of Measurement
Pt 1.26754 %
Pt 1.26324 %
Pt 1.26132 %
Pt 1.26971 %
Pt 1 ppm
Pt 2 ppm
Pt 1 ppm
Pt 1 ppm

and you want statistics by metal by UOM?

If you're unsure, have a quick read of the link at the bottom of Jeff Moden's post, it will help you to provide the information we need to solve your problem.

Cheers

ChrisM


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #520524
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse