9

This is the case that in the DB I'm checking, there is an archive table which keeps the user history, and there is a trigger or store procedure that after some time delete rows from this table, in order to avoid the oversize of the same, I didn't design the DB, I'm just taking the maintenance of an application that use this DB, so I don't know the name of these stored procedures or triggers, what I want to do is locate this stored procedure or trigger, check the code and modify it to leave this "user history" longer on the table.

Someone told me to check the "sysobjects" table, where I can actually see something with the same name of the table, but this is the only information I have been able to retrieve, any advise?

Thank you.

4 Answers 4

6

Search all code using sys.sql_modules

SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules sm
WHERE definition LIKE '%Mytable%'

Or use Red Gate SQL Search which is completely free

Do not use syscomments or INFORMATION_SCHEMA.RUBBISH

7
  • Can't the fellow also right-click on the target table and hit "Show Dependencies" (or some text to that effect)? Commented Nov 16, 2011 at 14:27
  • @Nick Chammas: nope, this is well known as being unreliable
    – gbn
    Commented Nov 16, 2011 at 14:29
  • @gbn, I understand that syscomments is for backwards compatability and may be removed from a future release, but can you elaborate on why not to use INFORMATION_SCHEMA? thx
    – datagod
    Commented Nov 16, 2011 at 14:32
  • @datagod: same why syscomments as always a poor choice: the definition column is nvarchar(4000). This means you may miss a table reference if in string position >4000 or spans the boundary between nvarchar(4000) rows (depends how you handle it, could concat I suppose if you really wanted...
    – gbn
    Commented Nov 16, 2011 at 18:51
  • @gbn Actually syscomments has multiple rows for an object that goes over the 4000 character limit, making queries using it just as valid as sys.sql_modules. I still recommend sys.sql_modules however since syscomments is depreciated. Commented Oct 18, 2013 at 21:26
4

Try out ApexSQL Search

ApexSQL Search is a FREE SQL Server Management Studio and Visual Studio add-in that, among other features, has the View Dependencies feature. The View Dependencies feature has the ability to visualize all SQL database objects’ relationships, including those between encrypted and system objects, SQL server 2012 specific objects, and objects stored in databases encrypted with Transparent Data Encryption (TDE)

The View Dependencies feature also allows you to set up and customize the visual dependencies diagram layout, including the relationships that will be presented, the layout and size of the generated diagram, and the drill-down depth of the dependencies

Disclaimer: I work for ApexSQL as a Support Engineer

0
2

For future reference as of 2008 there is also a DMV that can be used. sys.dm_sql_referencing_entities. I generally prefer it to using SQL_Modules as it avoids false positives among other things. I discussed it here but basically if you have a piece of code like this:

SELECT OBJECT_SCHEMA_NAME(object_id), OBJECT_NAME(object_id) 
FROM sys.sql_modules WHERE [definition] LIKE '%ABC%'

You will end up with results for the table ABC, the table ABCLog the view vw_ABC, the stored procedure sp_Update_ABC etc. Also to the best of my knowledge the DMV will handle encrypted SPs as well as unencrypted while the sql_modules method only works with unencrypted SPs.

The DMV version of the same query is this:

SELECT * FROM sys.dm_sql_referencing_entities('dbo.ABC', 'OBJECT')
1

Also you can use the sys.sql_expression_dependencies catalog view. Use this query:

SELECT 
referencing_object_name = obj.name, 
referencing_object_type_desc = obj.type_desc, 
referenced_object_name = referenced_entity_name
FROM sys.sql_expression_dependencies sd 
INNER JOIN sys.objects obj 
ON sd.referencing_id = obj.[object_id] 
WHERE referenced_entity_name = 'MyTable'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.