Solved

SQL 2005 - Memory Table Column Names

Posted on 2016-10-13
11
69 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 65

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 65

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
 

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 65

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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 12

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 12

Accepted Solution

by:
Nakul Vachhrajani earned 500 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 48

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now