/*--修改自定义数据类型精度的示例 假设要修改的自定义变量名为aa--*/--1.修改自定义变量类型的名称
exec sp_rename 'aa','aa_bak','USERDATATYPE'
go--2.新增自定义变量(按新的精度)
EXEC sp_addtype N'aa', N'numeric(20,2)', N'not null'
go--3.修改表,使用新增的自定义变量declare @s varchar(8000)
declare tb cursor local 
for select 'alter table ['+object_name(a.id)+'] alter column ['
+a.name+'] aa'
from syscolumns a join systypes b on a.xusertype=b.xusertype
where b.name='aa_bak'
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb

解决方案 »

  1.   

    楼主只需要将上述处理中的: aa 修改为你的自定义类型名EXEC sp_addtype N'aa', N'numeric(20,2)', N'not null'
    改为: 
    EXEC sp_addtype N'aa', N'char(16)', N'not null'
    然后执行就可以了.
      

  2.   

    谢谢,已经备份了,但执行出错:
    服务器: 消息 4909,级别 16,状态 1,行 1
    无法更改 'vwRepair',因为它不是表。
    服务器: 消息 4909,级别 16,状态 1,行 1
    无法更改 'vwRepair',因为它不是表。
    服务器: 消息 515,级别 16,状态 2,行 1
    无法将 NULL 值插入列 'reserve_imei',表 'asc.dbo.tbRepair';该列不允许空值。UPDATE 失败。
    语句已终止。
      

  3.   

    1.看来你在存储过程/自定义函数/触发器中也用了自定义函数,这些需要手工修改.2.既然你的自定义类型允许NULL值,那么在添加自定义类型时,指定:NULL即上面的代码改为:--1.修改自定义变量类型的名称
    exec sp_rename 'aa','aa_bak','USERDATATYPE'
    go--2.新增自定义变量(按新的精度)
    EXEC sp_addtype N'aa', N'char(16)', N'null'
    go--3.修改表,使用新增的自定义变量declare @s varchar(8000)
    declare tb cursor local 
    for select 'alter table ['+object_name(a.id)+'] alter column ['
    +a.name+'] aa'
    from syscolumns a join systypes b on a.xusertype=b.xusertype
    where b.name='aa_bak' and objectproperty(id,'IsUserTable')=1
    open tb
    fetch next from tb into @s
    while @@fetch_status=0
    begin
    exec(@s)
    fetch next from tb into @s
    end
    close tb
    deallocate tb
      

  4.   

    --试试这个if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_changeusertype]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_changeusertype]
    GO/*--修改自定义数据类型的长度及精度

    修改当前库中定义的自定义数据类型的长度及精度
    并自动修改所有的表/视图/存储过程/触发器/自定义函数中的对应定义
    由于数据库的复杂性,建议修改前先备份--邹建 2004.05--*//*--调用示例

    exec p_changeusertype 'test','nvarchar(20)'
    --*/
    create proc p_changeusertype
    @typename sysname, --要修改的自定义数据类型名
    @newdef sysname, --新的定义
    @allownull bit=1, --是否允许NULL,为1表示允许,为0表示不允许
    @deloldtype bit=1 --是否在处理完成后删除备份的类型名,默认为删除
    as
    declare @bktypename nvarchar(36)if not exists(select 1 from systypes where name=@typename)
    begin
    print '------------------------------------------------'
    print ' 要修改的自定义类型不存在'
    print '------------------------------------------------'
    return
    endset nocount on
    set @bktypename=cast(newid() as varchar(36))
    print '------------------------------------------------'
    print ' 原来的自定义数据类型将被改名为: '+@bktypename
    print '------------------------------------------------'set xact_abort on
    begin tran
    --1.修改自定义变量类型的名称
    exec sp_rename @typename,@bktypename,'USERDATATYPE' --2.新增自定义变量(按新的精度)
    if @allownull=1
    exec sp_addtype @typename,@newdef,N'null'
    else
    exec sp_addtype @typename,@newdef,N'not null' --3.修改表,使用新增的自定义变量
    declare @s varchar(8000)
    declare tb cursor local 
    for select 'alter table ['+object_name(a.id)+'] alter column ['
    +a.name+'] '+@typename
    from syscolumns a join systypes b on a.xusertype=b.xusertype
    where b.name=@bktypename and objectproperty(a.id,N'IsUserTable')=1
    open tb
    fetch next from tb into @s
    while @@fetch_status=0
    begin
    exec(@s)
    fetch next from tb into @s
    end
    close tb
    deallocate tb

    --刷新视图
    declare hCForEach cursor global for 
    select '[' + REPLACE(user_name(uid), N']', N']]')+'].['
    + REPLACE(object_name(id), N']', N']]')+ ']' 
    from dbo.sysobjects
    where xtype='V' and status>=0
    exec sp_MSforeach_worker 'sp_refreshview ''?'''

    --刷新存储过程,自定义函数,触发器
    declare hCForEach cursor global for 
    select '[' + REPLACE(user_name(uid), N']', N']]')+'].['
    + REPLACE(object_name(id), N']', N']]')+ ']' 
    from dbo.sysobjects
    where xtype in('TR','FN','IF','TF','P') and status>=0
    exec sp_MSforeach_worker 'sp_recompile ''?''' if @deloldtype=1
    exec sp_droptype @bktypename
    commit tran
    set nocount off
    go