如:在SQL SERVER 执行 drop table 时,可能有外键引用阻止。可以建立如下存储过程:
alterproc DropTable (@tabnvarchar(250) ) asbegin
declare@strnvarchar(max) ; set@str= N'' ;
select@str=@str+ N'ALTER TABLE ['+object_name( parent_object_id) +'] DROP CONSTRAINT ['+ name +'];' from sys.foreign_keys where referenced_object_id =object_id( @tab)
exec sp_executesql @str ;
ifexists (select1 from sysobjects where id =object_id('ResKey') and type ='U') begin set@str= N'Drop table '+@tab ; exec sp_executesql @str ; end