Solved

SQL Server dynamically return specific columns from a table

Posted on 2010-09-13
6
384 Views
Last Modified: 2012-05-10
Hello.  I've got a table [##UDF_HCLOT_prep] that contains up to 40 columns.  I'm trying to return only those columns that don't begin with 'Lot-UDF%'.  

I have the following code as shown below, but when I execute it all I get back is "Invalid object name ##LotUDFs".

Any help is appreciated.
declare @sql varchar(1000)

SELECT @sql=@sql + Column_name + ','

from INFORMATION_SCHEMA.Columns

where table_name = '##UDF_HCLOT_prep' and Column_name not like '[Lot-UDF]%'



select @sql = left(@sql, len(@sql)-1)

select @sql = 'select ' + @sql + ' Into ##LotUDFs From ##UDF_HCLOT_prep'

exec (@sql)



Select * From ##LotUDFs

Open in new window

0
Comment
Question by:jobprojn
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 33666709
That is because when you execute SQL using exec(@SQL), the temp table goes out of scope once the execution is complete.  Try this...


declare @sql nvarchar(max)
set @SQL = ''
SELECT @sql=@sql + '[' + Column_name + '],'
from INFORMATION_SCHEMA.Columns
where table_name = '##UDF_HCLOT_prep' and Column_name not like '[Lot-UDF]%'

select @sql = left(@sql, len(@sql)-1)
select @sql = 'select ' + @sql + ' From ##UDF_HCLOT_prep'
exec sp_executesql @SQL


0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33666715
declare @sql varchar(1000)
SELECT @sql=@sql + c.name + ','
from tempdb.sys.objects o
inner join tempdb.sys.columns c on c.object_id=o.object_id
where o.name = '##UDF_HCLOT_prep' and c.name not like '[Lot-UDF]%'

select @sql = left(@sql, len(@sql)-1)
select @sql = 'select ' + @sql + ' Into ##LotUDFs From ##UDF_HCLOT_prep'
exec (@sql)

Select * From ##LotUDFs
0
 

Author Comment

by:jobprojn
ID: 33666806
Brandon, thanks for quick reply.  Because I'm using a global temp table "##" the table should remain in scope even after execution.  Nonetheless, I tried your code and get:

Msg 536, Level 16, State 5, Line 7
Invalid length parameter passed to the SUBSTRING function.
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'From'.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:jobprojn
ID: 33666814
Cyberwiki, thanks for quick reply.  Using your code I get the following:

Msg 208, Level 16, State 1, Line 11
Invalid object name '##LotUDFs'.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 33666860
Global temp tables will remain in scope until the last process (SPID) accessing it exits.  Since ##LotUDFs is only accessed by the exec(), it goes out of scope immediately upon completion.

Mine failed because I wasn't accessing tempdb.

Try:

declare @sql nvarchar(max)

SELECT @sql=isnull(@sql + ',' ,'') + '[' + Column_name + ']'
from tempdb.INFORMATION_SCHEMA.Columns
where table_name = '##UDF_HCLOT_prep' and Column_name not like '[Lot-UDF]%'


select @sql = 'select ' + @sql + ' From med##UDF_HCLOT_prepium'
--exec sp_executesql @SQL
PRINT @SQL


0
 

Author Closing Comment

by:jobprojn
ID: 33666920
Worked, thanks much.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
the whoisactive update 12 39
SQL profiler equivalent in MS-Access 3 42
Sql Query Datatype 2 18
ORA-00923: FROM keyword not found where expected 3 24
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

23 Experts available now in Live!

Get 1:1 Help Now