Are you talking about retrieving this from the transaction logs?
This isn't ideal as you have no guarantee that the relevant rows will still be available in the active log and it is less efficient to query but something like the below would do it (returns USER_NAME()
rather than the likely more useful SUSER_NAME()
though).
Change dbo.X
to your actual table name.
DECLARE @allocation_unit_ids TABLE (
allocation_unit_id BIGINT PRIMARY KEY )
INSERT INTO @allocation_unit_ids
SELECT allocation_unit_id
FROM sys.allocation_units au
JOIN sys.partitions p
ON au.container_id = CASE
WHEN au.type IN ( 1, 3 ) THEN p.hobt_id
WHEN au.type = 2 THEN p.partition_id
END
WHERE p.object_id = OBJECT_ID('dbo.X');
WITH L1
AS (SELECT [Transaction ID],
[Begin Time],
[End Time],
[Transaction SID],
CASE
WHEN Operation = 'LOP_INSERT_ROWS'
AND AllocUnitId IN (SELECT allocation_unit_id
FROM @allocation_unit_ids) THEN 1
END AS I
FROM sys.fn_dblog(NULL, NULL) l),
L2([Transaction ID], TransactionBegin, TransactionEnd, sid)
AS (SELECT [Transaction ID],
MAX([Begin Time]),
MAX([End Time]),
MAX([Transaction SID])
FROM L1
GROUP BY [Transaction ID]
HAVING 1 = MAX(I))
SELECT TransactionBegin,
TransactionEnd,
p.name AS PrincipalName
FROM L2
JOIN sys.database_principals p
ON p.sid = L2.sid