Retrieving the database id

It can sometimes be useful to know the id of a given database, e.g. for filtering events when profiling. Here are a few ways to do that:

-- Current database
select db_id()

-- Named database
select db_id('Northwind')

-- All databases
use master
select name, dbid from sysdatabases

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.