[英]How to resize all NVARCHAR(n) columns at once?
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.您可以使用
information_schema.columns
并构建动态 sql 查询来生成查询。 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
使用以下代码包括
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.