Sql-Server

MS SQL Express - 查找執行查詢的時間

  • March 8, 2021

我在 .NET 中有一個電子商務網站,並使用 MS SQL Server Express,版本資訊:

Microsoft SQL Server 2014 - 12.0.4100.1 (X64) Apr 20 2015 17:29:27

版權所有 (c) Microsoft Corporation Express Edition (64-bit) o​​n Windows NT 6.3 (Build 9600:) (Hypervisor)

從我的程式碼中執行了一個 DELETE 查詢,我無法弄清楚是什麼觸發了它。(我的整個系統顯示一個地方有刪除程式碼,但這不是問題。)

我需要在刪除執行時從數據庫中找出,以便我可以嘗試使用該數據來幫助我解決問題。

即使我找不到倒退,我也想安裝/配置一些東西來幫助我將來找到它,因為這是一個反復出現的問題

下面是一個創建delete觸發器來擷取結果的範例delete

--demo setup
DROP TABLE IF EXISTS TestDelete;
DROP TABLE IF EXISTS TestDeleteSave;

--create a TestDelete table and a TestDeleteSave
--the TestDeleteSave table will hold the contents of deleted rows
--from TestDelete when the trigger is fired

CREATE TABLE TestDelete (Id int, Description varchar(50));
CREATE TABLE TestDeleteSave (Id int, Description varchar(50));
go

--insert sample row into TestDelete
INSERT INTO TestDelete (
   Id
   ,Description
   )
VALUES (
   1
   ,'Test Description'
   )
go

--create a delete trigger on the TestDelete table.
--the 'deleted' virtual table will contain the rows that were deleted
--during a transaction
CREATE TRIGGER [dbo].[TestDelete_Delete] ON [dbo].[TestDelete]
AFTER DELETE
AS
BEGIN
   SET NOCOUNT ON;

INSERT INTO TestDeleteSave
SELECT *
FROM deleted

END;
GO

ALTER TABLE [dbo].[TestDelete] ENABLE TRIGGER [TestDelete_Delete]
GO

--now, delete a row from the TestDelete table which will fire the delete trigger
delete from TestDelete where id = 1

--now, select affected rows from the TestDeleteSave table
select * from TestDeleteSave

除了 Scott 的絕妙想法之外,您還可以通過執行Profiler或記錄TraceExtended Events Session來擷取DELETE查詢的詳細資訊、執行時間以及執行者的詳細資訊。希望在這些選項和 Scott 的想法之間,您能夠解決您的問題。

引用自:https://dba.stackexchange.com/questions/286650