Skip to main content
1 of 3
Marcello Miorelli
  • 16.6k
  • 53
  • 170
  • 311

how to find the database context of the last sql statement?

I would like to find out from the code below, what was the name of the database where the last command was run.

In this case I am looking for my_other_database.

his there any way of finding this? Is there any DMV that I should be looking at for this info?

Also, what if the update was wrapped in a dynamic sql, would it be possible to track it then?

use my_database
go

begin tran t1

UPDATE my_other_database.REF.applicationReference
SET uploadPaperReferences = 0
WHERE REFERENCEID IN (
69361,
69690,
69354,
69358,
69362,
69732,
69863,
70187
)

commit tran t1
Marcello Miorelli
  • 16.6k
  • 53
  • 170
  • 311