超级菜鸟问题
如何将数据库里面的所有表中字段类型vchar转换为nvchar.
表很多,要求用语句来改
------解决方案--------------------提示:数据字典,动态sql
------解决方案--------------------declare @sql nvarchar(4000)
set @sql= ' '
select @sql=@sql+ 'alter table '+a.name+ ' alter column '+b.name + ' nvarchar( '+left(b.length,99)+ ') '+char(13)
from
sysobjects a inner join syscolumns b on a.id=b.id inner join systypes c on b.xusertype=c.xusertype
where a.xtype= 'U ' and c.name= 'varchar ' --and name > = '? '
order by b.name
print @sql
如果没被截断就直接执行,截断就多运行几次
------解决方案--------------------declare @sql varchar(8000)
select @sql = ' '
select @sql = @sql + ' alter table [ ' + object_name(id) + '] alter column [ '+name+ '] nvarchar( '+convert(varchar(4),length)+ ') '+char(13) from syscolumns
where objectproperty(id, 'IsUserTable ')=1 and xusertype=167
print @sql