Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,How to write a query in SQL that can retreive the tables that has been used by a stored procedure,by searching a stored procedure?.Thank You
Posted

 
Share this answer
 
The following code will help you search a text that is part of table/view, index, Key or Stored Proc, functions etc... you can give partial names for search too, it works


SQL
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[SearchSqlObjectsForText]
(
  @SearchText	NVARCHAR(100)
)
AS
--
-- Query to find text in stored procedures, and views
--
SELECT DISTINCT
  CASE xtype 
      WHEN 'U' THEN 'User Table'
      WHEN 'P' THEN 'Stored Procedure'
      WHEN 'V' THEN 'View'
      ELSE xtype
    END AS ObjectType
  , o.name
--  , o.*
from 
  sysobjects o
left outer join
  syscomments c
on c.id = o.id
where c.text LIKE '%' + @SearchText + '%'
OR o.name LIKE '%' + @SearchText + '%'

GO
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900