How do I get the schema name of the objects in the table “sys.all_objects?”
Using the OBJECT_SCHEMA_NAME function returns “sys” when I know the schema is dbo…
select OBJECT_SCHEMA_NAME(o.schema_id),* from sys.all_objects o
I don’t want to use another view but I can join to other tables if necessary.
Are you talking about system objects such as
sp_helptext? You can callsp_helptextand other system objects using multiple formats and these are translated for you (mostly for backward compatibility reasons):But the object can only belong to one schema. In this case it is
syseven if it answers todboas well.If you have user objects that are showing up as
sys, please let us know what they are. If you are just trying to track down user objects, perhaps you named something asdbo.foothat also exists under thesysschema. I might suggestsys.objectsinstead ofsys.all_objectsif you don’t want to return system objects.EDIT:
And as Ray pointed out, you are passing
schema_idto the function. The documentation shows you need to passobject_id, notschema_id. So either do one of the following:Personally, I prefer a join, e.g.:
Why? Because this query can work across databases, whereas several of the metadata functions will return NULL or – even worse – the wrong object when called from a different database.