SQL Server??????????????DML??????????
????????????? ???????[ 2017/3/23 17:07:38 ] ????????????? SQL Server
??????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Щ??????????????????????????в??????????????????????Щ??????????Щ??ó?????????????INSERT??UPDATE???????????????????????????????????
???????????
?????????????汾?????棬??????????????????????????DML????????????????????磬Host_Name??Program_Name?? ?????????????????????????????????????TEST????????????????????Щ??÷????????????Щ??ó??????TEST????INSERT??UPDATE??????
USE [AdventureWorks2014]
GO
IF NOT EXISTS (SELECT 1 FROM sys.sysobjects WHERE id=object_id(N'[dbo].[TEST]') AND OBJECTPROPERTY(id?? N'IsTable')=1 )
BEGIN
CREATE TABLE [dbo].[TEST](
[OBJECT_ID] [INT] NOT NULL??
[NAME] [VARCHAR](8) NULL??
CONSTRAINT PK_TEST PRIMARY KEY (OBJECT_ID)
)
END
GO
INSERT INTO dbo.TEST
SELECT 1?? 'kerry' UNION ALL
SELECT 2?? 'jimmy'
?????????????????????????????????? [HOST_NAME]??[PROGRAM_NAME]??LOGIN_NAME???????????????ü??????????????????????TRG_TEST?????????????
ALTER TABLE TEST ADD [HOST_NAME] NVARCHAR(256)
ALTER TABLE TEST ADD [PROGRAM_NAME] NVARCHAR(256);
ALTER TABLE TEST ADD LOGIN_NAME NVARCHAR(256);
CREATE TRIGGER TRG_TEST ON dbo.TEST AFTER
INSERT??UPDATE
AS
IF (EXISTS(SELECT 1 FROM INSERTED))
BEGIN
UPDATE dbo.TEST
SET dbo.TEST.[HOST_NAME] = ( SELECT host_name
FROM sys.dm_exec_sessions
WHERE session_id = @@SPID
) ??
dbo.TEST.PROGRAM_NAME = ( SELECT program_name
FROM sys.dm_exec_sessions
WHERE session_id = @@SPID
) ??
dbo.TEST.LOGIN_NAME = ( SELECT login_name
FROM sys.dm_exec_sessions
WHERE session_id = @@SPID
)
FROM dbo.TEST t
INNER JOIN INSERTED i ON t.OBJECT_ID = i.OBJECT_ID
END
GO
??????????????????????????£???????????????????????????
????INSERT INTO dbo.TEST(OBJECT_ID??NAME)
????SELECT 3??'ken'
????UPDATE dbo.TEST SET NAME='Richard' WHERE OBJECT_ID=2;
???????????????????????SSMS???£????????????????????Microsoft SQL Server Management Studio - Query??
????????????????????????????????ó????SQL??д?ò????????????????????λ??????????ó?????????????磬??ó???????????????SQL????????κ????????
????INSERT INTO dbo.TEST
????SELECT 3??'ken'
?????????????????????У?????????ó??????Bug??????????????????????????????????TEST??INSERT??UPDATE?????????????????????????????????????????????????????????????Σ?????????TEST??????????OBJECT_ID????????????????OBJECT_ID??
USE [AdventureWorks2014]
GO
DROP TABLE dbo.[TRG_TEST_SESSION_INFO];
GO
IF NOT EXISTS (SELECT 1 FROM sys.sysobjects WHERE id=object_id(N'[dbo].[TRG_TEST_SESSION_INFO]') AND OBJECTPROPERTY(id?? N'IsTable')=1 )
BEGIN
CREATE TABLE [TRG_TEST_SESSION_INFO](
[ID] INT NOT NULL IDENTITY(1??1)??
[OBJECT_ID] INT??
[HOST_NAME] NVARCHAR(256)??
[PROGRAM_NAME] NVARCHAR(256)??
[LOGIN_NAME] NVARCHAR(256)??
CONSTRAINT PK_TRG_TEST_SESSION_INFO PRIMARY KEY (ID)
)
END
GO
CREATE TRIGGER TRG_TEST_SESSION ON dbo.TEST
AFTER INSERT ??UPDATE
AS
IF (EXISTS(SELECT 1 FROM INSERTED))
BEGIN
/*
INSERT INTO dbo.[TRG_TEST_SESSION_INFO]
SELECT (SELECT I.OBJECT_ID FROM INSERTED I)?? HOST_NAME??program_name??login_name
FROM sys.dm_exec_sessions
WHERE session_id = @@SPID*/
INSERT INTO dbo.[TRG_TEST_SESSION_INFO]
SELECT I.OBJECT_ID?? S.HOST_NAME??S.PROGRAM_NAME??S.LOGIN_NAME
FROM sys.dm_exec_sessions s??
Inserted i
WHERE session_id = @@SPID
END
GO
???????????С???????????????????Щ??????????Щ??ó???????Щ???????????????????????Щ????????????????????У?????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11