Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need your input

Author  Topic 

roizanladiero
Starting Member

5 Posts

Posted - 2011-09-09 : 23:29:25
declare @temp varchar(1000)

select @temp=(case when @temp is null then '' else @temp + ',' end)+''''+column_name+''''
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = '##BCP_Results'
--PRINT @temp

INSERT INTO ##BCP_Results
exec ('select' + @temp)
select * from ##BCP_Results



--The result is Server: Msg 170, Level 15, State 1, Line 1
--Line 1: Incorrect syntax near 'select'.


Please help!
Thank you
Roizan Ladiero

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-10 : 00:35:17
are you trying to insert column names or are you trying to insert data from columns?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

roizanladiero
Starting Member

5 Posts

Posted - 2011-09-11 : 21:42:41
@visakh16: I tried to insert column names.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-12 : 01:01:12
i'm assuming you want a single row in ##BCP_Results with entire column list coming as comma separated values. in that case what you want is below


declare @temp varchar(1000)


select @temp=case when @temp is null then '' else @temp + ',' end +column_name
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = '##BCP_Results'

PRINT @temp

INSERT INTO #BCP_Results
exec ('select' + ''''+ @temp +'''' )


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -