In a SQL Server database I need to find all stored procedures and views containing references to other databases. (I'm required to replace them with synonyms.)
I have many stored procedures and I do not want to search them manually.
Any ideas how to find them easily?
Depending on the version of SQL Server, you might also find value in this query:
SELECT OBJECT_SCHEMA_NAME(referencing_id), OBJECT_NAME(referencing_id)
FROM sys.sql_expression_dependencies
WHERE referenced_database_name = 'YourDatabaseName';
In reality you'll need a combination of searching the views and procedures (as @M_M pointed out), this, and you'll potentially need to weed some out. For example sys.sql_modules will show false positives if your database name is a string that could appear naturally in your modules for other reasons. But this answer won't find the database name when it's embedded in dynamic SQL, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With