通过sql server的default trace和tempdb中的sys.objects视图,你能够获得创建临时表的session id,下面是相应的sql语句:
DECLARE @FileName VARCHAR(MAX)
SELECT @FileName = SUBSTRING(path, 0, LEN(path)-CHARINDEX(‘\‘,
REVERSE(path))+1) + ‘\Log.trc‘
FROM sys.traces
WHERE is_default = 1;
SELECT
o.name,
o.OBJECT_ID,
o.create_date,
gt.NTUserName,
gt.HostName,
gt.SPID,
gt.DatabaseName,
gt.TEXTData
FROM sys.fn_trace_gettable(
@FileName, DEFAULT ) AS gt
JOIN tempdb.sys.objects AS o
ON
gt.ObjectID = o.OBJECT_ID
WHERE gt.DatabaseID = 2
AND gt.EventClass =
46 -- (Object:Created Event from sys.trace_events)
AND o.create_date
>= DATEADD(ms, -100, gt.StartTime)
AND o.create_date <=
DATEADD(ms, 100, gt.StartTime)