Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL 2005 - Memory Table Column Names

Posted on 2016-10-13
11
Medium Priority
?
148 Views
Last Modified: 2016-10-14
Hello Experts!

So I have a table setup like this:

DECLARE @temp TABLE(....)

How can I get the column names from this table?
SELECT ColumnName
FROM @temp

Thanks,
Mark
0
Comment
Question by:smithmrk
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41842548
<Potentially stupid answer>  
If the table was created with a CREATE TABLE statement that you can see, wouldn't you already know?
Perhaps some more details on what you're working on would help..
0
 

Author Comment

by:smithmrk
ID: 41842556
If I do Create Table doesn't that create an actual table in the database?
I don't want to have to do a delete table when I'm done.

For example right now I'm doing
DECLARE @temp TABLE(ID, Name)

SELECT ID,
              Name
FROM @temp

Done...once the stored procedure runs the @temp is gone because it was in memory...if I do CREATE TABLE doesn't that table stay in the database?

Mark
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41842579
Nope.  Temporary tables that are prefixed with @ and # exist in the tempdb database and not the database that your stored procedure runs on.  I don't believe that changes based on Declare @Table TABLE... vs. CREATE TABLE @table.  

Since you're asking it might not be a bad idea to revisit the needs for this table and determine if storing it as a physical table in your database is a better option.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:smithmrk
ID: 41842588
OK, but I found this on Google...to get the column names from Temp Tables, but none of these appear to work for me, was wondering why?

stackoverflow.com/questions/756080/how-to-retrieve-field-names-from-temporary-table-sql-server-2008

Thanks,
Mark
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41842597
Might be a SQL 2005 (your box) vs. SQL 2008 (the SO question) issue.  

Since I don't have a 2005 box in front of me and can't test, I'll back out of this question to encourage another expert to respond.
0
 

Author Comment

by:smithmrk
ID: 41842603
OK, well thanks for your help to this point...and it may not be possible.

Thanks,
Mark
0
 
LVL 14

Expert Comment

by:Nakul Vachhrajani
ID: 41842615
Is your temporary table in scope when you run the solution provided in the Stackoverflow link?

You can only get information/meta-data about a temporary table as long as it actually exists. Once out of scope, temporary objects are discarded by SQL Server and hence you will no longer get the column or any other meta-data information.

The other thing I am looking for is - why do you want the meta-data for these temporary tables. If your stored procedure creates them, you already have this in your DDL statements. The other way of putting the question is - what is the scenario under which you create a temporary table, but are unaware of it's data definition?
0
 
LVL 14

Accepted Solution

by:
Nakul Vachhrajani earned 2000 total points
ID: 41842636
Oh, and just an FYI - if you use "@", it's a table variable not a temporary table. Because it is not an object, it doesn't get an object_id and hence you will not get any meta-data information about it.
1
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41842646
There IS a difference between  # (an object) and @ (a variable) .
You won't find entries in tempdb.sys.columns for that variable.

If you changed to using a temp table (#) instead, then you would.

no points please
0
 
LVL 4

Expert Comment

by:Daniel Jones
ID: 41843083
To get column names of temp table

SELECT *
FROM   tempdb.sys.columns
WHERE  object_id = Object_id('tempdb..#sometemptable'); 

Open in new window

0
 

Author Closing Comment

by:smithmrk
ID: 41843618
That appears to be my problem, I'm using the @ and therefore I don't get the table information.  I will try using the # or just creating a table then dropping it when I'm done.

Thanks,
Mark
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question