How to get the database which the query is executed in a batch - Microsoft Community


hi ,

i have question how database query executed in batch. please see below scripts.

-- test query
use anotherdb
go
select * table
go

--the query database test query executed.

select

db_name(cast(dbid as int)) databasename,

user_id as schemaid

from

(

    select top 2 epa.attribute, epa.value  

      from sys.dm_exec_query_stats

           outer apply sys.dm_exec_plan_attributes(plan_handle) as epa

     where q.query_hash = 0xe39428067c9a330a and query_plan_hash = 0x6571771b329719e6

       and epa.attribute in ('dbid', 'user_id')

     order by q.last_execution_time desc

) as ecpa 

pivot (max(ecpa.value) for ecpa.attribute in ("dbid", "user_id")) as pvt;

if current database "master", can correct database "anotherdb" after execute second query. however, if remove "go" statement between "use anotherdb", executed database "master".  is bug or expected behavior?

-- test query
use anotherdb
select * table
go

--the query database test query executed.

select

db_name(cast(dbid as int)) databasename,

user_id as schemaid

from

( 

    select top 2 epa.attribute, epa.value   

      from sys.dm_exec_query_stats q  

           outer apply sys.dm_exec_plan_attributes(plan_handle) as epa 

     where q.query_hash = 0xe39428067c9a330a and query_plan_hash = 0x6571771b329719e6

       and epa.attribute in ('dbid', 'user_id')

     order by q.last_execution_time desc

) as ecpa  

pivot (max(ecpa.value) for ecpa.attribute in ("dbid", "user_id")) as pvt;

i have question how database query executed in batch. please see below scripts.

-- test query
use anotherdb
go
select * table
go

if sql server question, should asked @ technet.

click [view all] on left side of page select appropriate forum.

https://social.technet.microsoft.com/forums/en-us/home

don



Community Participation Center / Discuss the Microsoft Community site / Other feedback



Comments

Popular posts from this blog

Windows 10 does not have any of my previous Windows 7 email address. - Microsoft Community