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
Solved

SQL 2005 - Memory Table Column Names

Posted on 2016-10-13
11
75 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

840 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