How to Get an OBJECT_NAME() from a Different Database in SQL Server

If you ever need to use the OBJECT_NAME() function to get an object’s name from a different database in SQL Server, you might run into issues if you don’t know how it works.

You probably know that OBJECT_NAME() accepts an object_id argument that tells SQL Server which object to get the name from.

What you may or may not know, is that this function also accepts an optional database_id argument that tells SQL Server which database the object_id belongs to.

By default, SQL Server assumes that object_id is in the context of the current database. In this case, a query that references an object_id in another database will return NULL or (even worse) incorrect results.

Continue reading

Use OBJECT_NAME() to Get an Object’s Name from its object_id in SQL Server

When using SQL Server, if you ever find yourself about to do a join against the sys.objects system catalog view in order to get the name of an object, maybe stop and read this first.

Transact-SQL has a built-in function called OBJECT_NAME() that returns the name of an object, based on its ID.

In other words, if you have the object’s ID (for example, the object_id column), you can simply pass that ID to the OBJECT_NAME() function, and it will return the object’s name for you – no join required!

Continue reading

4 Ways to Get a Stored Procedure’s Definition using Transact-SQL

This article presents 4 ways of using T-SQL to get the definition of a stored procedure in SQL Server.

The definition is the actual T-SQL statement used to create the stored procedure.

Three of the methods here are exactly the same as the ones used for returning the definition of a view (except here, they’re being used on stored procedures instead of views).

Continue reading

Get View Information with the VIEWS Information Schema View in SQL Server

In SQL Server, you can use the Transact-SQL VIEWS system information schema view to return information about one or more views in the current database. It returns one row for views that can be accessed by the current user in the current database.

To use this view, specify the fully qualified name of INFORMATION_SCHEMA.VIEWS.

Continue reading

Return the Parameters of a Stored Procedure or User-Defined Function in SQL Server (T-SQL Examples)

In SQL Server, you can use the Transact-SQL PARAMETERS system information schema view to find the parameters used in a stored procedure or user-defined function.

More specifically, it returns one row for each parameter of a user-defined function or stored procedure that can be accessed by the current user in the current database.

To use this view, specify the fully qualified name of INFORMATION_SCHEMA.PARAMETERS.

Continue reading

Find the Columns Returned by a Table-Valued Function (T-SQL Examples)

In SQL Server, you can use the Transact-SQL ROUTINE_COLUMNS system information schema view to find the columns returned by a table-valued function.

More specifically, it returns one row for each column returned by the table-valued functions that can be accessed by the current user in the current database.

To use this view, specify the fully qualified name of INFORMATION_SCHEMA.ROUTINE_COLUMNS.

Continue reading

Return Stored Procedures & Functions in a SQL Server Database: ROUTINES (T-SQL Examples)

In SQL Server, you can use the Transact-SQL ROUTINES system information schema view to return a list of stored procedures and functions in the current database.

More specifically, it returns a list of all stored procedures and functions that can be accessed by the current user in the current database.

You can also use ROUTINES simply to return information about a specific procedure or function if required.

To use this view, use the fully qualified name of INFORMATION_SCHEMA.ROUTINES.

Continue reading

Don’t Use sp_depends in SQL Server (it’s Deprecated)

SQL Server has a system stored procedure called sp_depends that returns information about dependencies between objects within the current database.

Microsoft has marked this stored procedure as deprecated, which means that it’s in maintenance mode and may be removed in a future version of SQL Server. You should avoid using sp_depends in new development work, and you should modify applications that currently use it to use either sys.dm_sql_referencing_entities() or sys.dm_sql_referenced_entities() instead (depending on whether you need referencing entities, or referenced entities to be returned.

Continue reading