Solved

Select column_name from INFORMATION.SCHEMA.COLUMNS not working with TEMP tables

Posted on 2016-10-07
3
55 Views
Last Modified: 2016-10-10
I'm creating a global temp table using a random id and then trying to get column names from that table using INFORMATION_SCHEMA. Below is an example of what I'm doing --

SELECT 'JOHN' AS FIRST_NAME,'DAVIS' AS LAST_NAME      ,'CHICAGO' AS CITY INTO  [##E0404FF442A04AC7BCCEC3379FBC63]

select column_name from TEMPDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='[##E0404FF442A04AC7BCCEC3379FBC63]'
This does not work, it returns nothing under column_name.  I can take that same table and copy it into another temp table, and it works.        

SELECT * INTO ##OUT_LABEL FROM [##E0404FF442A04AC7BCCEC3379FBC63]

select column_name from TEMPDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='##OUT_LABEL'

column_name
FIRST_NAME
LAST_NAME
CITY

What am I missing here on why this is not working???
0
Comment
Question by:OzarkSunshine
[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
3 Comments
 

Author Comment

by:OzarkSunshine
ID: 41834039
Actually figured it out... had to get it to drop the brackets in the original temp table  name.
0
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 total points
ID: 41834040
Try this ...

Brackets issue

select column_name from TEMPDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='##E0404FF442A04AC7BCCEC3379FBC63'


select column_name from TEMPDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME LIKE '%##E0404FF442A04AC7BCCEC3379FBC63%'
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 41834076
You should stop using INFORMATION_SCHEMA views.  They have high overhead, including causing blocking.  [Yes, I know that when you look at their view definition they look like they shouldn't have any extra overhead, but in actual practice they do.]

SELECT c.name AS column_name
FROM tempdb.sys.columns c
WHERE c.object_id = OBJECT_ID('tempdb.dbo.##E0404FF442A04AC7BCCEC3379FBC63')
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
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 extract information from SQL Server on Database, Connection and Server properties

707 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