有一個非常好的系統存儲過程會幫助你分析數據庫的依賴關系,它就是:sp_depends。這個過程會指出哪些數據庫對象依賴于對應的數據庫對象,和哪些數據庫對象為對應的數據庫對象引用了。
如果所有的對象按依賴順序創建的,那么這個系統存儲過程會更魯棒。那些依賴于其他對象的對象總是在它們引用的對象之后創建的。
使用這個過程的一個原因是為了確定一個過程或者表的變化的影響。如果你有一個對象,它引用了30個對象,那么很有可能在編碼期間,你為了改變這30個對象而須做更多的工作。
下面的腳本展示一個存儲過程和一些對象,這個存儲過程引用了一些對象,其他對象又引用了這個存儲過程。
if exists(select name
from sysobjects
where name = n'test_table'
and type = 'u')
drop table test_table
go
create table test_table (
c1 varchar(255) null)
go
if exists (select name
from sysobjects
where name = n'test_proc1'
and type = 'p')
drop procedure test_proc1
go
create procedure test_proc1 @name sysname = null
as
if @name is not null
begin
insert test_table values (@name)
end
else
begin
return
end
go
if exists (select name
from sysobjects
where name = n'test_proc2'
and type = 'p')
drop procedure test_proc2
go
create procedure test_proc2
as
declare @myvar sysname
select @myvar = name from sysobjects where id = 1
exec test_proc1 @myvar
go
exec sp_depends test_proc1
go
drop procedure test_proc2, test_proc1
go
drop table test_table
go
新聞熱點
疑難解答