Labels

Wednesday, May 4, 2011

How to Read first 5 columns data from table?

--select * from sys.sysobjects where name = 'Employee'
declare @tablename varchar(30)
declare @temp varchar(1000)
declare @sql varchar(5000)
select @tablename = 'Employee'
select @temp=(
select  column_name+','
from information_schema.columns WHERE table_name = @tablename and
ordinal_position < 6 for xml path(''))
select @temp=SUBSTRING(@temp,0,len(@temp))
select @temp
select @sql='select '+@temp+' from HumanResources.Employee'
exec (@sql)

No comments:

Post a Comment