Solved

SQL 2005 - Memory Table Column Names

Posted on 2016-10-13
11
87 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

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
 

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 13

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 13

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server 2012 r2 and SQL 2014 6 34
SQL Server Pivot 5 44
Creating Scalar Function 3 19
Sql server query 8 21
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

752 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