I wonder if something like the following is possible to achieve:
ALTER TABLE dbo.[Foo]
ALTER COLUMN * NVARCHAR(500)
WHERE Columns NVARCHAR(n) < NVARCHAR(500);
you can use information_schema.columns
and build a dynamic sql query to do generate query. Still you need to copy the result and run them manually.
select 'alter table '+TABLE_SCHEMA+'.'+TABLE_NAME+' alter column '+COLUMN_NAME+' NVARCHAR(500)'
from INFORMATION_SCHEMA.columns
where DATA_TYPE = 'nvarchar'
and CHARACTER_MAXIMUM_LENGTH < 500
and table_name = 'foo'
and TABLE_SCHEMA = 'dbo'
edited below
use the below code to include null
/ not null
select 'alter table ['+TABLE_SCHEMA+'].['+TABLE_NAME+'] alter column ['+COLUMN_NAME+'] NVARCHAR(500) '+case when IS_NULLABLE = 'YES' then 'NULL' else 'NOT NULL' end
from INFORMATION_SCHEMA.columns
where DATA_TYPE = 'nvarchar'
and CHARACTER_MAXIMUM_LENGTH < 500
and table_name = 'foo'
and TABLE_SCHEMA = 'dbo'
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.