1,运行下面语句
select 
case t3.name 
when'varchar' then 'alter table '+T1.NAME +'   ALTER COLUMN '+T2.NAME+' nvarchar('+convert(varchar,t2.length)+')  '
when  'char' then 'alter table '+T1.NAME +'   ALTER COLUMN '+T2.NAME+' nchar('+convert(varchar,t2.length)+')  ' end as 更改语句
from sysobjects t1,syscolumns t2,systypes t3 where t1.id=t2.id and t1.xtype='U' 
and t2.xtype=t3.xtype  
and t3.name in ( 'varchar' ,'char')
2,复制结果,在新的窗口里运行

解决方案 »

  1.   

    /*--将所有的表中,数值类型由char,varchar改为nchar,nvarchar  --*//*--调用示例:
    exec p_set
    --*/
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_set]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_set]
    GO--用下面的存储过程
    create procedure p_set
    as
    declare tb cursor for
    SELECT sql='alter table ['+d.name
    +'] alter column ['+a.name+'] n'
    +d.name,长度+'('+cast(a.length*2 as varchar)+')'
    FROM syscolumns a
    left join systypes b on a.xtype=b.xusertype
    inner join sysobjects d on a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
    where 
    b.name in('char','varhar'
    and 
    not exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
    SELECT name FROM sysindexes WHERE indid in(
    SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
    ))) --主键不能修改
    order by d.name,a.namedeclare @sql varchar(1000)
    open tb
    fetch next from tb into @sql
    while @@fetch_status = 0
    begin
    exec(@sql)
    fetch next from tb into @tbname,@fdname
    end
    close tb
    deallocate tb
    go
      

  2.   

    --上面的有些错,这个才是正确的./*--将所有的表中,数值类型由char,varchar改为nchar,nvarchar  --*//*--调用示例:
    exec p_set
    --*/
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_set]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_set]
    GO--用下面的存储过程
    create procedure p_set
    as
    declare tb cursor for
    SELECT sql='alter table ['+d.name
    +'] alter column ['+a.name+'] n'
    +b.name+'('+cast(a.length*2 as varchar)+')'
    FROM syscolumns a
    left join systypes b on a.xtype=b.xusertype
    inner join sysobjects d on a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
    where 
    b.name in('char','varchar')
    and 
    not exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
    SELECT name FROM sysindexes WHERE indid in(
    SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
    ))) --主键不能修改
    order by d.name,a.namedeclare @sql varchar(1000)
    open tb
    fetch next from tb into @sql
    while @@fetch_status = 0
    begin
    exec(@sql)
    fetch next from tb into @sql
    end
    close tb
    deallocate tb
    go