I have one table table_name
with 50 decimal(18, 0) columns and I want to change it's datatype to decimal(18, 6)
I am trying
ALTER TABLE [table_name] ALTER COLUMN [column_name] [data_type]
But for this, I have to queries 50 times.
How I do it in a single Query
You can do it easily by executing a dynamic sql query.
Query
declare @sql as varchar(max);
select @sql = stuff((
select 'alter table [your_table_name]
alter column [' + [column_name] + '] decimal(18, 6);'
from information_schema.columns
where [table_name] = 'your_table_name'
and [data_type] = 'decimal'
for xml path('')
)
, 1, 0, ''
);
exec(@sql);
You have to add all colunm seprated by comma ie
ALTER TABLE [table_name] ALTER COLUMN [column_name1] [data_type],[column_name2] [data_type],so on;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.