Solved

SQL 2005 - Memory Table Column Names

Posted on 2016-10-13
11
64 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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 11

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 11

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 3

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

706 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

15 Experts available now in Live!

Get 1:1 Help Now