OBJECT_ID (Transact‑SQL)
Function
Returns the database object ID for a schema-scoped object.
Syntax
OBJECT_ID ( 'object_name' [ , 'object_type' ] )
object_name – Is the name of the object. It can be a one-part name or a two-part name (schema.object). The default schema is dbo.
object_type – (Optional) Is a string that specifies the type of object to look up. For many object types see the table below.
Arguments
| Argument | Type | Description |
|---|---|---|
| object_name | nvarchar(776) | Name of the object. Either one- or two-part name. |
| object_type | nvarchar(2) | Object type code (e.g., 'U' for user table, 'P' for stored procedure). |
Return Value
Returns int representing the object ID. Returns NULL if the object does not exist or if the caller does not have permission to view the object.
Object Type Codes
| Code | Object Type |
|---|---|
| 'AF' | Aggregate function (CLR) |
| 'C' | CHECK constraint |
| 'D' | DEFAULT (constraint or stand‑alone) |
| 'F' | FOREIGN KEY constraint |
| 'FN' | SQL scalar function |
| 'FS' | Assembly (CLR) scalar-function |
| 'FT' | Assembly (CLR) table-valued function |
| 'IF' | SQL inline table-valued function |
| 'IT' | Internal table |
| 'P' | SQL stored procedure |
| 'PC' | Assembly (CLR) stored procedure |
| 'PK' | PRIMARY KEY constraint |
| 'S' | System base table |
| 'U' | User table |
| 'V' | View |
| 'X' | Extended stored procedure |
Examples
Example 1 – Get the ID of a table
SELECT OBJECT_ID('dbo.Employee');
Result: 123456789 (example value)
Example 2 – Get the ID of a stored procedure
SELECT OBJECT_ID('dbo.usp_GetSales', 'P');
Result: 987654321
Remarks
- If
object_namecontains a dot, the part before the dot is treated as the schema name. - When
object_typeis omitted, the function searches for any object with that name. - The function returns
NULLwhen the caller lacks permissions on the object.