سؤال

I am trying to build a dependency tree for a database by tracing object relationships through sys.sql_expression_dependencies (or I might use sys.dm_sql_referenced_entities instead if it's the only way to accomplish this).

I notice that for some referenced objects, I get the referenced_entity_name but not the referenced_schema_name or (crucially) the referenced_id. Looking through Microsoft's page on sys.sql_expression_dependencies I see that these columns can be NULL when "The schema of the referenced entity depends on the schema of the caller and is resolved at run time." Looking into the referencing stored procedure, I see that it refers to the referenced object as simply MyObject, not dbo.MyObject. (Virtually all of this database is in the dbo schema, so some people - myself included - have sometimes lazily neglected to include explicit schema references). I tried changing the procedure definition to explicitly reference dbo.MyObject, and then looked again in sys.sql_expression_dependencies, and I do indeed see the referenced_schema_name and the referenced_id that were previously missing.

I can't, at the moment, fix all objects in the database so that they make all references explicitly by schema. So, my question is: where the Microsoft page says "The schema of the referenced entity depends on the schema of the caller and is resolved at run time" does this simply mean that the SQL engine will always infer that the schema of the referenced object is the same as the schema of the referencing object? If so, I can then use MyReferencingObjectSchema + MyReferencedObjectSchema to look up the object_id for the referenced object and continue building my tree. Or is it more complicated than that?

هل كانت مفيدة؟

المحلول

It's more complicated than that, I'm afraid. It will look for an object with the right name in the connection's default schema as well. This is why you usually don't have to put the schema name in for dbo - that's the usual default schema.

What order it looks for things I don't know - default or current first - but that should be easily testable.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top