HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How can I find the Database ID of a deleted database?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canthehowdatabasedeletedfind

Problem

Where can I view/find the database ID after a database was deleted?

I work in software/hardware environment and we have locking unit (prototype) synced in our S/W with unique DB ID. In order to reset this locking unut we need to know the DB ID. Unfortunately DB was completely deleted and we have no way to continue our work.

Database was deleted about 6-7 days ago.

Solution

You could inspect the default trace to see the database id for a deleted database, assuming the database was deleted recently enough for the drop to be captured in the trace.

This T-SQL will show you the trace records:

DECLARE @trcfilename VARCHAR(1000);

SELECT @trcfilename = path 
FROM sys.traces 
WHERE is_default = 1;

IF (SELECT COALESCE(OBJECT_ID('tempdb..#trctemp'), 0)) <> 0
BEGIN
    DROP TABLE #trctemp;
END

SELECT *
INTO #trctemp
FROM sys.fn_trace_gettable(@trcfilename, default) tt

SELECT tt.DatabaseID
    , tt.DatabaseName
    , tt.StartTime
    , tt.HostName
    , tt.LoginName
    , tt.ApplicationName
FROM #trctemp tt
WHERE tt.eventclass = 47 
    AND tt.objecttype = 16964
ORDER BY tt.StartTime DESC;

Code Snippets

DECLARE @trcfilename VARCHAR(1000);

SELECT @trcfilename = path 
FROM sys.traces 
WHERE is_default = 1;

IF (SELECT COALESCE(OBJECT_ID('tempdb..#trctemp'), 0)) <> 0
BEGIN
    DROP TABLE #trctemp;
END

SELECT *
INTO #trctemp
FROM sys.fn_trace_gettable(@trcfilename, default) tt

SELECT tt.DatabaseID
    , tt.DatabaseName
    , tt.StartTime
    , tt.HostName
    , tt.LoginName
    , tt.ApplicationName
FROM #trctemp tt
WHERE tt.eventclass = 47 
    AND tt.objecttype = 16964
ORDER BY tt.StartTime DESC;

Context

StackExchange Database Administrators Q#134298, answer score: 6

Revisions (0)

No revisions yet.