0

How to know that how many database tables, views or any other object attached to a view in sql server 2005 database. I tried with Sp_Depends, but amazingly it is not showing the SP name in which it is getting used.

When I run the statement Sp_depends vw_MyViewName.

I am only getting the name of the tables and columns which I have used inside the vw_MyViewName. I need to know other objects related to this view.

Chris
  • 2,293
  • 11
  • 48
  • 86

4 Answers4

1

In SQL Server 2005 this happens if you create the objects in the wrong order.

You will get a warning message that the dependency information could not be added for a missing object but the object will still be created

You can run sp_refreshsqlmodule on all objects in your database to recreate such missing dependency information (an example script to do that is here How do I find all stored procedures that insert, update, or delete records?)

Community
  • 1
  • 1
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
0

The only completely reliable way I know of to determine object dependencies in a SQL Server database is to load your schema into Visual Studio Database Edition (DBPro) and examine the dependencies there. I have found this to be foolproof, unlike the way SQL Server tracks dependencies.

I wouldn't necessarily fault SQL Server for this. I don't think it ever made the claim that it was able to track dependencies with 100% accuracy, mostly because of the way it binds objects.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • Not sure what version you are working with. In 2008 the dependencies views are much more reliable than 2005. The only dependencies not tracked correctly are dynamic SQL AFAIK. – Martin Smith Feb 11 '11 at 12:51
  • @Martin - Much more reliable or perfectly reliable? In DBPro, they are perfectly reliable. – Randy Minder Feb 11 '11 at 13:11
  • As far as I know `sys.sql_expression_dependencies` is perfectly reliable. In 2008 When the parser encounters an object name in a `CREATE` or `ALTER` it records it even if the referenced object is missing. There's a couple of columns `is_caller_dependent, is_ambiguous` that can be looked at with regard to the object binding point. – Martin Smith Feb 11 '11 at 13:22
0

In SQL Server Management Studio, in Object Explorer panel, right click on the object you want to inspect (stored procedure, table, view, ...) and click on "Show Dependencies".

The window that appear, will show you both dependant and "depended" objects, simply by switching between two radio button ;)

If you want to do it by hand, you need to build a query over the sys.sql_dependencies system view. Here a link to the description with some examples

Beware that stored procedures that depends on nonexistants tables, view, and other objects, will be created, but not only they obviously doesn't work, but dependency informations will not be added, until all "depended" objects are created, AND the SP is REcreated!

BertuPG
  • 653
  • 4
  • 6
0

Until sys.sql_expression_dependencies was introduced (SQL Server 2008), you need query sys.sql_modules

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

Personally, I'd use WITH SCHEMABINDING to ensure dependencies must exist

Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676