Solved

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

Posted on 2016-10-07
3
46 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
ID: 41834039
Actually figured it out... had to get it to drop the brackets in the original temp table  name.
0
 
LVL 28

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
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…

685 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