How to drop columns from a table after first 5 columns in MS SQL Server -


how drop column table after first 5 columns in ms sql server. have table default 5 columns there can more column added dynamically. need drop dynamic columns without default columns. in case first 5 columns.

you can use dynamic sql this. need generate alter table... drop column... script each column of table. columns, need query sys.columns.

test data:

create table testtable(     col1    int identity(1,1),     col2    int,     col3    int,     col4    int,     col5    int,     col6    int,     col7    int ) 

dynamic sql

declare @sql varchar(max) = ''  select @sql = @sql + char(10) + 'alter table testtable drop column ' + quotename(name) + ';' sys.columns      object_id = object_id('testtable')     , column_id > 5  print @sql exec(@sql) 

still, you're doing bad practice.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -