SQL Server lets me drop tables and views that are still referenced by user defined functions without any issues:
CREATE TABLE SomeTable (Value INT NOT NULL)
GO
CREATE FUNCTION DoSomething(@foo INT) RETURNS INT
BEGIN
RETURN (SELECT * FROM SomeTable)
END
GO
DROP TABLE SomeTable
Executing "DoSomething" will always fail now. Is there any way to check all user defined functions on the server if they are still executable?
For stored procedures we use SET SHOWPLAN_ALL ON
in combination with some dynamic SQL. But this does not work for functions unfortunately.
2条答案
按热度按时间bmp9r5qi1#
Perhaps try the following example query specifically for user-defined functions:
vqlkdk9b2#
In SQL Server, you can use a script or query to check if user-defined functions (UDFs) are still executable after dropping tables or views that they depend on. One common method is to use a script that analyzes the dependencies and checks for issues. Here's a step-by-step guide on how to do it:
Use the sys.sql_expression_dependencies system catalog view to identify the dependencies of UDFs. Specifically, you're interested in the referenced_id column, which will contain the ID of the object being referenced.
Query the UDFs and their dependencies to check if any of the referenced objects (like tables or views) have been dropped. You can do this by joining sys.sql_expression_dependencies with sys.objects.
Here's an example query that checks if any UDFs are referencing dropped tables or views:
This query will return a list of UDFs along with the status of their referenced objects. UDFs that have "Dropped" in the Status column are the ones with dropped table or view references.
Remember to run this query in the context of the database where your UDFs are defined.
By regularly running such a query, you can proactively identify UDFs that may have issues due to dropped dependencies and take appropriate action to fix or replace them.