Monday, June 6, 2016

ViewDependency in SQL Server

//Dependency in Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SP_ViewDependency]
@TableName varchar(50),
@Database VARCHAR(50)

AS
begin
DECLARE @@Query VARCHAR(max)
SET @@Query =
'SELECT DISTINCT o.name
    FROM '+ @Database +'.dbo.sysobjects o
    INNER JOIN ' + @Database+'.dbo.syscomments c ON c.Id = o.Id
    WHERE category = 0 AND c.text like ''%' + @TableName + '%''
    ORDER BY o.name '
    PRINT @@Query
    EXEC (@@Query)
end

No comments: