實(shí)現(xiàn)一個(gè)AuditLog的功能,是B/S結(jié)構(gòu)專(zhuān)案。
每個(gè)用戶(hù)可以登錄系統(tǒng),在程序中操作數(shù)據(jù)(添加,更新和刪除)需要實(shí)現(xiàn)記錄操作跟蹤。是誰(shuí)添加,更新和刪除的,這些信息將會(huì)插入至AuditLog表中。
一般情況之下,在SQL的觸發(fā)器中,只能取到(SQL驗(yàn)證sa;Windows驗(yàn)證Domain\xxx)。這些用戶(hù)名,達(dá)不到效果,不能真正反映到是誰(shuí)操作的。
下面是讓你清楚,怎樣實(shí)現(xiàn)在SQL觸發(fā)器或存儲(chǔ)過(guò)程中獲取在程序登錄的用戶(hù),是在插入,更新或刪除的存儲(chǔ)過(guò)程,把登錄程序當(dāng)前用戶(hù)傳入進(jìn)去。在存儲(chǔ)過(guò)程中,再把相關(guān)信息存入局部(#)臨時(shí)表中,這樣子,在觸發(fā)器即可獲取了。
下面代碼示例,以一個(gè)[Member]表作例,可以參詳:
復(fù)制代碼 代碼如下:
Member
CREATE TABLE Member
(
Member_nbr INT IDENTITY(1,1) PRIMARY EKY NOT NULL,
[Name] NVARCHAR(30),
Birthday DATETIME,
Email NVARCHAR(100),
[Address] NVARCHAR(100)
)
GO
插入存儲(chǔ)過(guò)程:
復(fù)制代碼 代碼如下:
MemberSp_Insert
CREATE PROCEDURE MemberSp_Insert
(
--Other parameter
@Operater NVARCHAR(50) --帶到此參數(shù),可從程序的用戶(hù)傳至數(shù)據(jù)庫(kù)
)
AS
BEGIN
--處理插入事務(wù)
---INSERT INTO [dbo].[Member] (xxx) VALUES(xxx)
--把相關(guān)信息存入臨時(shí)表,方便在觸發(fā)器時(shí)取到。
IF OBJECT_ID('#AuditWho') IS NOT NULL
DROP TABLE [#AuditWho]
CREATE TABLE [#AuditWho] (PrimaryKey INT,Operater NVARCHAR(50))
INSERT INTO [#AuditWho] VALUES(SCOPE_IDENTITY(),@Operater)
END
GO
更新存儲(chǔ)過(guò)程:
復(fù)制代碼 代碼如下:
MemberSp_Update
CREATE PROCEDURE MemberSp_Update
(
--Other parameter
@Member_nbr INT,
@Operater NVARCHAR(50) --帶到此參數(shù),可從程序的用戶(hù)傳至數(shù)據(jù)庫(kù)
)
AS
BEGIN
--處理更新事務(wù)
---UPDATE [dbo].[Member] SET [xxx] = xxx, ... WHERE [Member_nbr] = @Member_nbr
--把相關(guān)信息存入臨時(shí)表,方便在觸發(fā)器時(shí)取到。
IF OBJECT_ID('#AuditWho') IS NOT NULL
DROP TABLE [#AuditWho]
CREATE TABLE [#AuditWho] (PrimaryKey INT,Operater NVARCHAR(50))
INSERT INTO [#AuditWho] VALUES(@Member_nbr,@Operater)
END
GO
刪除存儲(chǔ)過(guò)程:
復(fù)制代碼 代碼如下:
MemberSp_Delete
CREATE PROCEDURE MemberSp_Delete
(
@Member_nbr INT,
@Operater NVARCHAR(50) --帶到此參數(shù),可從程序的用戶(hù)傳至數(shù)據(jù)庫(kù)
)
AS
BEGIN
--處理刪除事務(wù)
---DELETE FROM [dbo].[Member] WHERE [Member_nbr] = @Member_nbr
--把相關(guān)信息存入臨時(shí)表,方便在觸發(fā)器時(shí)取到。
IF OBJECT_ID('#AuditWho') IS NOT NULL
DROP TABLE [#AuditWho]
CREATE TABLE [#AuditWho] (PrimaryKey INT,Operater NVARCHAR(50))
INSERT INTO [#AuditWho] VALUES(@Member_nbr,@Operater)
END
GO
從上面的存儲(chǔ)過(guò)程,用戶(hù)相關(guān)的信息(應(yīng)用程序的用戶(hù)信息)已經(jīng)在存儲(chǔ)過(guò)程中存入臨時(shí)表中,接下來(lái),在觸發(fā)器,怎樣獲取呢??梢詤⒖枷旅娴挠|發(fā)器代碼:
插入觸發(fā)器:
復(fù)制代碼 代碼如下:
MemberTr_Insert
CREATE TRIGGER [dbo].[MemberTr_Insert]
ON [dbo].[Member]
FOR INSERT
AS
BEGIN
IF @@ROWCOUNT = 0 RETURN
SET NOCOUNT ON
--事務(wù)處理
DECLARE @Operater NVARCHAR(50),@Member_nbr INT
SELECT @Member_nbr = [Member_nbr] FROM inserted
SELECT @Operater = [Operater] FROM [#AuditWho] WHERE [PrimaryKey] = @Member_nbr
--插入Audit 表中
--INSERT INTO ....
END
GO
更新觸發(fā)器:
復(fù)制代碼 代碼如下:
MemberTr_Update
CREATE TRIGGER [dbo].[MemberTr_Update]
ON [dbo].[Member]
FOR UPDATE
AS
BEGIN
IF @@ROWCOUNT = 0 RETURN
SET NOCOUNT ON
--事務(wù)處理
DECLARE @Operater NVARCHAR(50),@Member_nbr INT
SELECT @Member_nbr = [Member_nbr] FROM deleted
SELECT @Operater = [Operater] FROM [#AuditWho] WHERE [PrimaryKey] = @Member_nbr
--插入Audit 表中
--INSERT INTO ....
END
GO
刪除觸發(fā)器:
復(fù)制代碼 代碼如下:
MemberTr_Delete
CREATE TRIGGER [dbo].[MemberTr_Delete]
ON [dbo].[Member]
FOR DELETE
AS
BEGIN
IF @@ROWCOUNT = 0 RETURN
SET NOCOUNT ON
--事務(wù)處理
DECLARE @Operater NVARCHAR(50),@Member_nbr INT
SELECT @Member_nbr = [Member_nbr] FROM deleted
SELECT @Operater = [Operater] FROM [#AuditWho] WHERE [PrimaryKey] = @Member_nbr
--插入Audit 表中
--INSERT INTO ....
END
GO
每段代碼,有注釋。
此問(wèn)題有在某論壇發(fā)表讓網(wǎng)友討論過(guò),但是效果不佳。如果你有另外見(jiàn)解,可以在討論。謝謝。