Monday, January 25, 2010

Find Content into Database


SET NOCOUNT ON

DECLARE @NoOfCount INT
DECLARE @CurrentId INT
DECLARE @ProecdueName VARCHAR(100)
DECLARE @FindContent VARCHAR(100)
DECLARE @XType VARCHAR(10)

SET @CurrentId = 1
SET @ProecdueName = ''
SET @FindContent = 'procAlertsUpdateAlert' -- Type Content for find

DECLARE @Table TABLE
(
ID INT IDENTITY(1,1)
, ProecdueName VARCHAR(100)
, XType VARCHAR(10)
)

DECLARE @TableProcedureContent TABLE
(
Content VARCHAR(MAX)
)

INSERT INTO @Table
SELECT Name, XType
FROM SysObjects
WHERE XType IN ( 'FN', 'IF', 'TF', 'P', 'TR', 'V')
AND Category = 0
ORDER BY XType, Name

SELECT @NoOfCount = COUNT(ID) FROM @Table

WHILE ( @CurrentId <= @NoOfCount )
BEGIN
DELETE FROM @TableProcedureContent

SELECT @ProecdueName = ProecdueName
, @XType = XType
FROM @Table
WHERE Id = @CurrentId

INSERT INTO @TableProcedureContent
EXECUTE sp_helptext @ProecdueName

IF ( EXISTS(
SELECT *
FROM @TableProcedureContent
WHERE Content Like '%' + @FindContent + '%'
) )
BEGIN
PRINT ( CASE @XType
WHEN 'P' THEN 'Procedure'
WHEN 'FN' THEN 'Function'
WHEN 'TR' THEN 'Trigger'
ELSE ''
END ) + ' ' + @ProecdueName
END

SET @CurrentId = @CurrentId + 1
END

No comments: