zoukankan      html  css  js  c++  java
  • 批量修改字段的类型(SQL Server)varchar到nvarchar

    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   
        
      
    /*--将所有的表中,数值类型由char,varchar改为nchar,nvarchar       
        
      --
    */
       
        
      
    /*--调用示例:   
      exec   p_set   
      --
    */
       
      
    --修改的存储过程   
      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.name   
        
      
    declare   @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
  • 相关阅读:
    LeetCode对撞指针汇总
    167. Two Sum II
    215. Kth Largest Element in an Array
    2018Action Recognition from Skeleton Data via Analogical Generalization over Qualitative Representations
    题解 Educational Codeforces Round 84 (Rated for Div. 2) (CF1327)
    题解 JZPKIL
    题解 八省联考2018 / 九省联考2018
    题解 六省联考2017
    题解 Codeforces Round #621 (Div. 1 + Div. 2) (CF1307)
    题解Codeforces Round #620 (Div. 2)
  • 原文地址:https://www.cnblogs.com/Tonyyang/p/1209683.html
Copyright © 2011-2022 走看看