我的存儲過程的列表,我試圖確定他們應該在執行順序。SQL Server 2005的存儲過程依賴
有沒有一種方法來確定存儲過程是如何互相依賴。我在想我可以使用sysobjects和syscomments。
感謝
我的存儲過程的列表,我試圖確定他們應該在執行順序。SQL Server 2005的存儲過程依賴
有沒有一種方法來確定存儲過程是如何互相依賴。我在想我可以使用sysobjects和syscomments。
感謝
這裏是如何創建一個UDF,將做到這一點的文章。它按照您的建議使用sysobjects和syscomments表。
http://www.eggheadcafe.com/community/aspnet/13/12562/list-sql-stored-procedure.aspx
下面是UDF的SQL:
CREATE FUNCTION dbo.uf_FindProcedureDependencies(@ProcName varchar(256))
RETURNS @blah TABLE (depth tinyint not null,
tree varchar(7700) COLLATE SQL_Latin1_General_CP437_BIN not null,
objectname varchar(256) COLLATE SQL_Latin1_General_CP437_BIN not null,
dependencytype varchar(16) COLLATE SQL_Latin1_General_CP437_BIN not null)
AS
BEGIN
DECLARE @depth tinyint
SELECT @depth = 1
INSERT @blah (depth, tree, objectname, dependencytype)
SELECT DISTINCT @depth, @ProcName + ' -> ' + so2.name, so2.name, 'stored procedure' FROM
sysobjects so1, syscomments sc1, sysobjects so2, syscomments sc2 WHERE
so1.id = sc1.id AND so2.id = sc2.id AND so1.id <> so2.id AND so1.type = 'P' AND so2.type = 'P'
AND so1.name = @ProcName AND REPLACE(sc1.text,so1.name,'') LIKE '%EXEC%' + so2.name + '%'
WHILE (@@ROWCOUNT > 0)
BEGIN
SET @depth = @depth + 1
INSERT @blah (depth, tree, objectname, dependencytype)
SELECT DISTINCT @depth, b.objectname + ' -> ' + so2.name, so2.name, 'stored procedure' FROM
sysobjects so1, syscomments sc1, sysobjects so2, syscomments sc2, @blah b WHERE
so1.id = sc1.id AND so2.id = sc2.id AND so1.id <> so2.id AND so1.type = 'P' AND so2.type = 'P'
AND so1.name = b.objectname AND REPLACE(sc1.text,so1.name,'') LIKE '%EXEC%' + so2.name + '%'
AND b.depth = @depth - 1
END
RETURN
END
一種方法是
EXEC sp_depends 'your_procedure_name'
下面是一些代碼,我在幾個星期前扔起來,構成了對象的排序:施用於數據庫基於其依賴。第一個存儲過程只是刷新元數據,以便確保sql_dependencies準確無誤。
這是沒有完全測試的代碼。另外,如果我再次這樣做,我可能會使用INFORMATION_SCHEMA而不是直接針對系統表。
run_level告訴你何時可以創建每個對象。所以,爲0的run_level所有對象都可以首先在run_level 1運行(順序並不一個run_level內關係),則所有的對象,等
IF (OBJECT_ID('dbo.RefreshSqlModuleForAllObjs') IS NOT NULL)
DROP PROCEDURE dbo.RefreshSqlModuleForAllObjs
GO
CREATE PROCEDURE dbo.RefreshSqlModuleForAllObjs
AS
BEGIN
DECLARE
@object_id INT,
@schema_name SYSNAME,
@object_name SYSNAME,
@full_name NVARCHAR(776)
DECLARE cur_objects CURSOR FOR
SELECT
object_id,
OBJECT_SCHEMA_NAME(object_id),
OBJECT_NAME(object_id)
FROM
sys.objects
WHERE
type IN ('P', 'TR', 'V', 'FN', 'TF', 'IF')
OPEN cur_objects
FETCH NEXT FROM cur_objects INTO @object_id, @schema_name, @object_name
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @full_name = QUOTENAME(@schema_name) + '.' + QUOTENAME(@object_name)
EXEC sp_refreshsqlmodule @full_name
FETCH NEXT FROM cur_objects INTO @object_id, @schema_name, @object_name
END
CLOSE cur_objects
DEALLOCATE cur_objects
END
GO
IF (OBJECT_ID('dbo.GenObjApplyOrder') IS NOT NULL)
DROP PROCEDURE dbo.GenObjApplyOrder
GO
CREATE PROCEDURE dbo.GenObjApplyOrder
AS
BEGIN
DECLARE
@i INT,
@count INT
DECLARE
@run_order TABLE (object_id INT, run_level SMALLINT)
SET @i = 0
SET @count = 1
EXEC dbo.RefreshSqlModuleForAllObjs
WHILE (@count > 0)
BEGIN
INSERT INTO @run_order (object_id, run_level)
SELECT
object_id,
@i
FROM
sys.objects o
WHERE
NOT EXISTS (SELECT * FROM sys.sql_dependencies d WHERE d.object_id = o.object_id AND d.referenced_major_id NOT IN (SELECT object_id FROM @run_order)) AND
NOT EXISTS (SELECT * FROM @run_order ro WHERE ro.object_id = o.object_id) AND
o.type IN ('U', 'P', 'V', 'TR', 'TF', 'IF', 'FN')
SELECT @count = @@ROWCOUNT
SELECT @i = @i + 1
END
INSERT INTO @run_order (object_id, run_level)
SELECT
o.object_id,
-999
FROM
sys.objects o
WHERE
NOT EXISTS (SELECT * FROM @run_order ro WHERE ro.object_id = o.object_id) AND
o.type IN ('U', 'P', 'V', 'TR', 'TF', 'IF', 'FN')
SELECT OBJECT_NAME(object_id), run_level FROM @run_order ORDER BY ABS(run_level)
END
GO