如何一次调整所有 NVARCHAR(n) 列的大小?
How to resize all NVARCHAR(n) columns at once?
我想知道是否可以实现类似下面的东西:
ALTER TABLE dbo.[Foo]
ALTER COLUMN * NVARCHAR(500)
WHERE Columns NVARCHAR(n) < NVARCHAR(500);
您可以使用 information_schema.columns
并构建动态 sql 查询来生成查询。您仍然需要复制结果并 运行 手动复制它们。
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
使用下面的代码来包含 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'
我想知道是否可以实现类似下面的东西:
ALTER TABLE dbo.[Foo]
ALTER COLUMN * NVARCHAR(500)
WHERE Columns NVARCHAR(n) < NVARCHAR(500);
您可以使用 information_schema.columns
并构建动态 sql 查询来生成查询。您仍然需要复制结果并 运行 手动复制它们。
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
使用下面的代码来包含 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'