Suppose you have table with many columns and often you need data from first 15 or 20 columns. In that case you have to specify all the columns in your select statement. This procedure will select top N columns you want. All you have to do is to supply table name and number of columns you wantHere is the procedureCreate procedure TopNcolumns (@tableName varchar(100),@n int) as Declare @s varchar(2000) set @s='' If exists(Select * from information_Schema.tables where table_name=@tablename and table_type='Base Table') Begin If @n>=0 Begin set rowcount @n Select @s=@s+','+ column_name from information_schema.columns where table_name=@tablename order by ordinal_position Set rowcount 0 Set @s=substring(@s,2,len(@s)-1) Exec('Select '+@s+' from '+@tablename) End else Select 'Negative values are not allowed' as Error End else Select 'Table '+@tableName+' does not exist' as Error
MadhivananFailing to plan is Planning to fail