Jul 20, 2011

How can select columns from table

Its very intersting question if we have 30 columns in sql table.And want write a sql statement to access only 29 columns from that table.But we don’t like write all column names.


DECLARE @ColumnList Varchar(1000), @SQLStatment VARCHAR(4000)

SET @ColumnList = ''

select @ColumnList = @ColumnList + Name + ' , ' from syscolumns where id =

object_id(‘your_table_name’) AND Name != 'your_column_nmae'

SELECT @SQLStatment = 'SELECT ' + Substring(@ColumnList,1,len(@ColumnList)-1)

+ ' From ‘your_table_name’

EXEC(@SQLStatment)

No comments:

Post a Comment