Search code examples
sqlsql-serverdatabasehostname

How to Know who creates (host name) a database in sql server?


I want to know who created a database in sql server. Not the owner user, but the HOST_NAME or the machine name of the user who creates the database. Someone created a database on the server, but we do not know who it was. The only data we have is the user owner of the database, but that user is used by any developer. But what I want is to be able to identify the name of the machine from where the database is created. For example the host name of creation.

I tried with

select * from sys.databases

but it doesn't I need.

Then I tried with

Select
database_name,server_Name,Machine_Name,b.[name]as Backup_Name,Backup_Start_Date,Backup_Finish_Date
,Physical_Device_Name
from  master.sys.databases a
Inner Join msdb..backupset
b             on
a.Name = b.Database_Name
Inner Join msdb..backupmediafamily
c     on    c.media_set_id
= b.media_set_id
WHERE database_name = 'database'

But I also do not get the data I need


Solution

  • I'd use extended events for this:

    CREATE EVENT SESSION [Create Database] ON SERVER 
    ADD EVENT sqlserver.database_created(
        ACTION(sqlserver.client_hostname)) 
    ADD TARGET package0.ring_buffer;
    GO
    ALTER EVENT SESSION [Create Database] ON SERVER STATE = START;
    

    I leave the consumption of the target data as an exercise for the reader.