0

I want to find which database is querying from, here is my output:

EXEC sp_example @stat = N'SELECT stat FROM [dbo].[statsUSers] AS [UserStats];

What I want is like this:

EXEC sp_example @stat=N'SELECT stat  FROM [MyOwnDataBase].[statsUSers] AS [UserStats];

I've already tried this tip: SQL Server Profiler - how do I find which database is being connected?

but still it's [dbo] and not showing the name of the database.

Question

How can I access name of database?

I don't want [dbo] changes to something meaningless - I want the actual name of database.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

When creating the trace, you can select Show all columns, which will then display the DatabaseID and DatabaseName columns.

Note that dbo is the schema name, not the database name. There is no option to capture the default schema of the user, this is the one they would refer to if accessing a table like SELECT * FROM table. To capture the default schema you would instead have to capture the username then work out what the user's default schema is.

enter image description here

I would advise you to move away from the essentially deprecated Profiler, to Extended EVents, which provides far more information and puts far less load on the server.

In Extended Events, you can add the database_name also.

enter image description here

Charlieface
  • 52,284
  • 6
  • 19
  • 43