Earlier this week, I posted an interesting interview question How to Build Three Part Name from Object_ID? – Interview Question of the Week #134. In its response, I have received a very interesting reply from Rin Sitah over here.
Rin suggested an alternative method to my earlier suggested method which works just fine and also builds three part name from the object_ID.
Well, honestly, I love this new method much more than my original method, hence decided to write this blog post.
SELECT '['+DB_Name()+'].['+s.name+'].['+o.name+']' FROM sys.objects o INNER JOIN sys.schemas s ON s.schema_id = o.schema_id WHERE object_ID = 1154103152
Well, let me know which method you prefer in either case.
Additionally, I had asked users if they can tell me where exactly they use this method and interesting enough the majority answer I have received that user use this method when they want to do some kind of administration maintenance. Honestly, I still think DBA and developers should write code which is more dependant on a combination of name and schema and not on object_id. It is quite possible if the table is regenerated with different object_id.
Well, that’s it. Let me know if you think of any other idea in the comment section of this blog post.
Reference: Pinal Dave (https://blog.sqlauthority.com)
4 Comments. Leave new
why not directly
SELECT
‘[‘+DB_Name()+’].[‘+schema_name(schema_id)+’].[‘+name+’]’
FROM sys.objects
Why not use SELECT QUOTENAME(DBNAME()) + ‘.’ + QUOTENAME(OBJECT_SCHEMA_NAME(@object_id)) + ‘.’ + QUOTENAME(OBJECT_NAME(@object_id))? Good post Pinal Dave.