• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 100
  • Last Modified:

Help with a Sql search.

I need to search all of the stored procedures in my database that contain a table named "Customers". I have this query which partially works:

SELECT OBJECT_NAME(object_id), OBJECT_DEFINITION(object_id) FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) like '%Customers%'. This works because I am using the wildcards % padding the string. However if I do a straight search for the table name with no wild cards, it does not work:
So how can I search precisely for the specific table name without using wild cards?

SELECT OBJECT_NAME(object_id), OBJECT_DEFINITION(object_id) FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) like 'Customers'.
0
brgdotnet
Asked:
brgdotnet
2 Solutions
 
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
The object definition would contain the actual DDL (e.g. In case of a stored procedure, it would be CREATE PROCEDURE). When wild cards are not used, the database engine does a perfect match which would fail because no object would have a definition that only consists of the table name.
0
 
brgdotnetcontractorAuthor Commented:
No, I think there is some confusion here. I want to find all stored procedures in a database which have a particular string in them. Like for example a table name. I don't care what kind of DML is done on the table name I am searching for. I don't even care if the table name is commented out in the stored procedure. I just want to find all stored procedures which contain a table name I am searching for. For example, suppose I have this stored procedure  named UniverseParser with some the following Sql below. If I search the database for all stored procedures that have the table name "MarsTable", then this stored procedure UniverseParser would come up.

Begin
Update MarsTable
SELECT PlanetName From Planets
INSERT INTO UNIVERSE Values ('Moon')
0
 
Deepak ChauhanSQL Server DBACommented:
I guess you want to search based on the exact table name. You can use this script.

select p.name as SPNAME, s.referenced_entity_name as TableName from sys.procedures p
join sys.sql_expression_dependencies s on p.object_id=s.referencing_id
where s.referenced_entity_name = <Your TableName>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now