OBJECT_ID (TSQL Function)

Returns the ID of the specified object.

Syntax

OBJECT_ID ( '<object_qname>' [, 'object_type'] )

<object_qname> ::= [ database_name. [ schema_name ] . | schema_name. ] object_name

Arguments

<object_qname>
is the qualified or unqualified name of the object, of type varchar.
object_type
optional argument is of type varchar. For the moment, only ‘U’ is available, to look for user defined tables. If omitted, ‘U’ is assumed.

Return Types

Returns bigint.

Remarks

The function OBJECT_ID requires a string as argument.

Returns NULL if the object doesn’t exist.

Example

PRINT OBJECT_ID('Sales.dbo.Orders');

The result is:

7000152

Example for checking if an object exists:

IF OBJECT_ID ('Sales.dbo.Orders', 'U') IS NOT NULL
    DROP TABLE Sales.dbo.Orders;