SQL Server How to find non-existing tables in user defined functions?

jdzmm42g  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(128)

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.

bmp9r5qi

bmp9r5qi1#

Perhaps try the following example query specifically for user-defined functions:

select o.[name]
from sys.objects o
where exists (
  select * from sys.sql_expression_dependencies d 
  where d.referencing_id = o.object_id
    and o.type = 'FN' and o.is_ms_shipped = 0 
    and d.referenced_id is null and d.referenced_entity_name is not null
);
vqlkdk9b

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:

SELECT
OBJECT_NAME(udf.object_id) AS UDF_Name,
CASE WHEN t.name IS NOT NULL THEN 'Table ' + t.name
     WHEN v.name IS NOT NULL THEN 'View ' + v.name
     ELSE 'Other Object' END AS Referenced_Object,
CASE WHEN t.name IS NOT NULL THEN 'Dropped'
     WHEN v.name IS NOT NULL THEN 'Dropped'
     ELSE 'OK' END AS Status
FROM sys.objects AS udf
LEFT JOIN sys.sql_expression_dependencies AS dep
     ON udf.object_id = dep.referencing_id
LEFT JOIN sys.tables AS t
     ON dep.referenced_id = t.object_id
LEFT JOIN sys.views AS v
     ON dep.referenced_id = v.object_id
WHERE udf.type = 'FN' -- UDFs
     AND (t.name IS NULL OR t.type = 'U') -- Filter out non-table references
     AND (v.name IS NULL OR v.type = 'V') -- Filter out non-view references
     AND (t.name IS NULL OR v.name IS NULL); -- Filter out objects that still exist

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.

相关问题