How to find database ID while database is in single user mode in sql server

ajay gupta 1 Reputation point
2023-05-26T14:39:46.99+00:00

How to find database ID while database is in single user mode in sql server?

in SP_helpdb it will not come then where to search.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,149 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 113.6K Reputation points MVP
    2023-05-26T21:32:41.99+00:00
    SELECT database_id FROM sys.databases WHERE name = 'YourDB'
    
    0 comments No comments

  2. PercyTang-MSFT 12,501 Reputation points Microsoft Vendor
    2023-05-29T01:39:57.4433333+00:00

    Hi @ajay gupta

    You can try using this function.

    select DB_ID() as database_ID
    

    For more information, please refer to the official documentation.

    https://learn.microsoft.com/en-us/sql/t-sql/functions/db-id-transact-sql?view=sql-server-ver16

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.