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

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

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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