SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Create Temporary Tables Dynamically in cursor loop


Create Temporary Tables Dynamically in cursor loop

Author
Message
elmerbotha
elmerbotha
SSC Journeyman
SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)

Group: General Forum Members
Points: 87 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.
Kent Waldrop
Kent Waldrop
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1530 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.
GSquared
GSquared
SSC Guru
SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)

Group: General Forum Members
Points: 106073 Visits: 9730
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)

Group: General Forum Members
Points: 381706 Visits: 42995
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??? Blink

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
RBarryYoung
RBarryYoung
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61412 Visits: 9519
Ouch! w00t

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)

Group: General Forum Members
Points: 381706 Visits: 42995
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... Wink

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
GSquared
GSquared
SSC Guru
SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)

Group: General Forum Members
Points: 106073 Visits: 9730
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... Wink


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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)SSC Guru (381K reputation)

Group: General Forum Members
Points: 381706 Visits: 42995
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. BigGrin

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
elmerbotha
elmerbotha
SSC Journeyman
SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)

Group: General Forum Members
Points: 87 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
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)

Group: General Forum Members
Points: 73506 Visits: 20322
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
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