Solved

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

Posted on 2016-10-07
3
28 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
3 Comments
 

Author Comment

by:OzarkSunshine
Comment Utility
Actually figured it out... had to get it to drop the brackets in the original temp table  name.
0
 
LVL 16

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 250 total points
Comment Utility
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:
ScottPletcher earned 250 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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

7 Experts available now in Live!

Get 1:1 Help Now