改正用户定义的数据范例[MSSQL防范]
本文“改正用户定义的数据范例[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
?--SQLSERVER的自定义范例对比好用吧,但是,一旦引用该数据范例后,想改正数据范例,就是一大头疼的事了,本存储历程就是专门对付它的.
--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
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'
? EXEC sp_rebuildallview
? EXEC('sp_droptype ' + @OLDTYPENAME)
? EXEC('sp_addtype ' + @OLDTYPENAME + ', ''' + @newdtype + '''')
? exec SP_rechangfieldtype 'U_LOCALTYPE', @OLDTYPENAME
? EXEC sp_rebuildallview
? EXEC sp_droptype 'U_LOCALTYPE'
end
GO
--以下是示例.将U_HELLO的长度改成 30
SP_ADDTYPE U_HELLO, 'VARCHAR(10)'
GO
CREATE TABLE TESTTYPE(NAME U_HELLO)
GO
SP_CHANGEFIELD 'U_HELLO', 'VARCHAR(30)'
以上是“改正用户定义的数据范例[MSSQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |