Get Fully Qualified Name of Database Object in SQL Server
To find fully qualified name of a database table in SQL Server, developers can use getFullyQualifiedName SQL function to read FQN formed of schema, database and server name together with table name. Although these database object properties can be easily fetched from sys.tables system view columns, I tried to code all in one user-defined SQL function.
Following SQL Select script displays object properties like object_id, object name, schema name, database name where the object is created and the current server name
Additionally by using SQL CONCAT function, SQL programmers can concatenate basic properties of a database table object which forms the fully qualified name for that table.
SQL QUOTENAME function encapsulates the object name and prevents SQL Server syntax error incase the database object includes a space character or a dot (.) in its name.
When I execute above code on my SQL Server sample database "kodyaz.development" I got following output
Of course, Transact-SQL programmers can easily convert this code block into a user-defined SQL function as follows.
And above SQL function can be called within SQL codes as follows