求:修改整个MSSQL数据库字段类型语句。
现有一个MSSQL数据库,下面有几百个表,现要统一该库所有表的所有字段类型为nvarchar(max),请问该语句如何写?谢谢! 数据库 mssql 修改字段类型
[解决办法]
select 'alter table '+ a.name+ ' alter column ['+b.name+'] nvarchar(max)'
from sysobjects a
left join syscolumns b on a.id=b.id
left join systypes c on b.xtype=c.xtype
where a.xtype='U' and c.name='nvarchar'
SELECT 'alter table '+OBJECT_NAME(OBJECT_ID)+' alter column '+a.name+' '+b.name+'('+CASE WHEN a.max_length=-1 THEN 'max' ELSE CONVERT(NVARCHAR(10),a.max_length) END+')'+' Collate SQL_Latin1_General_CP1_CI_AS'
FROM sys.columns a INNER JOIN sys.types b ON a.system_type_id=b.system_type_id
WHERE OBJECT_NAME(OBJECT_ID) NOT LIKE 'sys%' AND a.collation_name IS NOT NULL AND a.collation_name <>'SQL_Latin1_General_CP1_CI_AS' AND a.system_type_id IN (167,231,239)