Get db_id, db_name, object_id,object_name in SQL Server

Check db_name from db_id , object_name from object_id in SQL Server

Check db_name and db_id in SQL Server

Syntax:
DB_ID('database name')
db_name(dbid)

Example:
select DB_ID('AdventureWorks2022')
select DB_NAME(7)

Check the object_id in SQL Server

Syntax: OBJECT_ID ( '[ database_name . [ schema_name ] . | schema_name . ]   
  object_name' [ ,'object_type' ])

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
PG = Plan guide
PK = PRIMARY KEY constraint
R = Rule (old-style, stand-alone)
RF = Replication-filter-procedure
S = System base table
SN = Synonym
SO = Sequence object
U = Table (user-defined)
V = View

Get the object_id for the table:

select OBJECT_ID(N'[HumanResources].[Department]')

Get the object_id for View:

select OBJECT_ID(N'[Purchasing].[vVendorWithContacts]','V')

Note: Index doesn’t work with OBJECT_ID function (use select * from sys. indexes)

Check the object name from the id:

Syntax: OBJECT_NAME ( object_id [, database_id ] ) 

Example:
select object_name(object_id);

Leave a Reply