Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Dynamically create temp tables with unique names

Author  Topic 

rlull
Starting Member

39 Posts

Posted - 2011-04-18 : 18:31:15
I am working with an existing stored procedure. It currently returns 2 result sets. I now need to extend it and return up to 5 result sets. The number of result sets is determined by some logic within the proc and will vary each time it's called. The current logic uses temp tables to return the data. Is there a way to dynamically create up to 3 more temp tables with names that are created on the fly? Perhaps there is a better solution to return a variable number of result sets?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-18 : 18:34:06
If it's only up to 5 temp tables, then just hardcode the names in the sproc. They are unique amongst other sessions, so there is no reason to add extra logic to make it dynamic.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rlull
Starting Member

39 Posts

Posted - 2011-04-18 : 23:06:11
Thanks, Tara. That makes sense but what if I do go beyond 5 and need to make it dynamic? Is there a better way to structure the logic in the proc to use something other than temp tables?
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-04-18 : 23:14:33
You can have many more temp tables, just give them unique names within the proc.



CODO ERGO SUM
Go to Top of Page

rlull
Starting Member

39 Posts

Posted - 2011-04-18 : 23:29:32
Thanks, Michael - but how do I create them dynamically with unique names? For example, one time the proc is called, I may need 5 tables. Another time I may need only 3. Next time, maybe 7. I don't want to hard code all the names ahead of time. I only want to create as many as I need each time the proc is called.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-18 : 23:40:00
It sounds like you have a design issue. Could you show us some variations?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rlull
Starting Member

39 Posts

Posted - 2011-04-19 : 11:29:21
It's definitely a design issue. Here's the scenario: The proc is called and TempTable1 is created from a subset of a view. TempTable2 is created from a subset of TempTable1. Based on the number of rows (usually 3 but theoretically could be up to 10) in yet another table we'll call OutsideTable, I need to create the corresponding number of temp tables. For example, if 3 rows are returned, I need to create an additional 3 temp tables that are a subset of TempTable1. If 5 rows are returned, I need to create an additional 5 temp tables. So in the end, the proc will return a result set containing the results from TempTable1, TempTable2 and additional temp tables for however many rows are returned from OutsideTable. Does that help?
Go to Top of Page

rlull
Starting Member

39 Posts

Posted - 2011-04-19 : 11:46:30
Nevermind, I figured it out. I was able to redesign the proc in such a way as to avoid additional temp tables. Thanks for your time.
Go to Top of Page
   

- Advertisement -