修改用户定义的数据类型
--sp_rebuildallview 见本Blog中的其它页面
create procedure sp_rechangfieldtype(@typename varchar(50), @newtype varchar(50))
as
begin
declare @typeid int
declare @tablename varchar(50)
declare @column varchar(50)
declare @sqlstr varchar(200)
declare @defaultid int
织梦内容管理系统
select @typeid = xusertype
from systypes
where name = @typename and xusertype > 256
AND (is_member('db_owner') = 1 OR is_member('db_ddladmin') = 1 OR is_member(user_name(uid))=1)
declare mycursor cursor for
select o.name, c.name, c.cdefault
from syscolumns c, systypes t, sysusers u, sysobjects o
where c.xusertype = @typeid
and t.xusertype = @typeid
and o.uid = u.uid
and c.id = o.id
and o.type = 'u' 本文来自织梦
open mycursor
fetch next from mycursor into @tablename, @column, @defaultid
while @@fetch_status = 0
begin
if @defaultid <> 0
begin
set @sqlstr = 'alter table ' @tablename ' drop ' object_name(@defaultid)
exec(@sqlstr)
set @sqlstr = 'alter table ' @tablename ' alter column ' @column ' ' @newtype
exec(@sqlstr)
-- set @sqlstr = 'alter table ' @tablename ' add contraint ' @tablename 'df' @column ' default 0' 织梦内容管理系统
end
else
begin
set @sqlstr = 'alter table ' @tablename ' alter column ' @column ' ' @newtype 本文来自织梦
print @sqlstr
exec(@sqlstr)
end
--if @@error <> 0
-- continue
fetch next from mycursor into @tablename, @column, @defaultid
end
--假如没有约束,则可以直接删除。假如有约束。先处理约束。
织梦内容管理系统
close mycursor
deallocate mycursor 内容来自dedecms
end
GO
create procedure SP_CHANGEFIELD(@OLDTYPENAME VARCHAR(50), @NEWDTYPE VARCHAR(50))
as
begin
exec('sp_addtype U_LOCALTYPE, ''' @newdtype '''')
exec SP_rechangfieldtype @OLDTYPENAME, 'U_LOCALTYPE' copyright dedecms
EXEC sp_rebuildallview
内容来自dedecms
EXEC('sp_droptype ' @OLDTYPENAME)
内容来自dedecms
EXEC('sp_addtype ' @OLDTYPENAME ', ''' @newdtype '''') 织梦内容管理系统
exec SP_rechangfieldtype 'U_LOCALTYPE', @OLDTYPENAME 织梦好,好织梦
EXEC sp_rebuildallview 内容来自dedecms
EXEC sp_droptype 'U_LOCALTYPE'
dedecms.com
end
GO
--以下是示例。将U_HELLO的长度改为 30
SP_ADDTYPE U_HELLO, 'VARCHAR(10)'
dedecms.com
GO
CREATE TABLE TESTTYPE(NAME U_HELLO)
GO
内容来自dedecms
SP_CHANGEFIELD 'U_HELLO', 'VARCHAR(30)'
织梦内容管理系统
文章评论
共有位Admini5网友发表了评论 查看完整内容