我正在尝试使用存储过程创建数据库日志。但问题是它不跟踪多列更新或修改。
以下是我的存储过程
ALTER Procedure [dbo].[Proc_TrackFollowup]
@Followup_ID int,@FollowType nvarchar(50),@ContactPerson nvarchar(50),
@FollowDate datetime,@FollowTime nvarchar(50),@Status nvarchar(50),@Comment nvarchar(MAX)
AS
DECLARE @OldFollowup_ID int
DECLARE @OldFollowType nvarchar(50)
DECLARE @OldContactPerson nvarchar(50)
DECLARE @OldFollowDate datetime
DECLARE @OldFollowTime nvarchar(50)
DECLARE @OldStatus nvarchar(50)
DECLARE @OldComment nvarchar(MAX)
DECLARE @OldCompanyName nvarchar(50)
DECLARE @audit_action varchar(100)
SELECT @OldFollowup_ID=Followup_ID,@OldCompanyName=CompanyName,@OldFollowType=FollowType,@OldContactPerson=ContactPerson,@FollowDate=FollowDate,@OldFollowTime=FollowTime,@OldStatus=Status,@OldComment=Comment
from FollowUpTable where Followup_ID=@Followup_ID
UPDATE FollowUpTable SET FollowType=@FollowType,ContactPerson=@ContactPerson,FollowDate=@FollowDate,FollowTime=@FollowTime,Status=@Status,Comment=@Comment where Followup_ID=@Followup_ID
IF @OldFollowType<>@FollowType
SET @audit_action='Follow Type Changed From ' + Cast(@OldFollowType AS VarChar) + ' to ' + Cast(@FollowType As Varchar)
IF @OldContactPerson<>@ContactPerson
SET @audit_action='Contact Person Changed From ' + Cast(@OldContactPerson AS VarChar) + ' to ' + Cast(@ContactPerson As Varchar)
IF @OldFollowDate<>@FollowDate
SET @audit_action='Follow Date Changed From ' + Cast(@OldFollowDate AS VarChar) + ' to ' + Cast(@FollowDate As Varchar)
IF @OldFollowTime<>@FollowTime
SET @audit_action='Follow Time Changed From ' + Cast(@OldFollowTime AS VarChar) + ' to ' + Cast(@FollowTime As Varchar)
IF @OldStatus<>@Status
SET @audit_action='Status Changed From ' + Cast(@OldStatus AS VarChar) + ' to ' + Cast(@Status As Varchar)
IF @OldComment<>@Comment
SET @audit_action='Comment Changed From ' + Cast(@OldComment AS VarChar) + ' to ' + Cast(@Comment As Varchar)
INSERT INTO LoggerFollowUpTable
(Followup_ID,CompanyName,FollowType,ContactPerson,FollowDate,FollowTime,Status,Comment,Audit_Action,Audit_Timestamp)
VALUES(@Followup_ID,@OldCompanyName,@FollowType,@ContactPerson,@FollowDate,@FollowTime,@Status,@Comment,@Audit_Action,getdate());
我已经在c#表单中给出了更新选项,其中Type是一个下拉列表,我将其传递给@ Type.But每当我从下拉列表中选择另一个项目并将其传递给存储过程时,它都不会跟踪修改。甚至之后在表单中编辑多个字段,我面临同样的问题
请建议更改存储过程
由于
答案 0 :(得分:1)
如果你从一个不同的角度接近它可能会更容易。有一个跟踪更改的表格。
CREATE TABLE [dbo].[LoggerFollowUpTable](
[LogId] [int] IDENTITY(1,1) NOT NULL,
[TableName] [varchar](50) NOT NULL,
[FieldName] [varchar](50) NOT NULL,
[RecordId] [int] NOT NULL,
[OldValue] [varchar](50) NULL,
[NewValue] [varchar](50) NULL,
[DateChanged] [datetime] NOT NULL
) ON [PRIMARY]
GO
然后在你的存储过程中更像(重复每个字段)
IF @OldFollowType<>@FollowType
BEGIN
INSERT INTO LoggerFollowUpTable (TableName, FieldName, RecordId, OldValue, DateChanged), NewValue) VALUES ('FollowUpTable', 'FollowType', @Followup_ID, Cast(@OldFollowType AS VarChar), Cast(@FollowType AS VarChar), GetDate())
END
然后,每个值都会更改一行。
如果你想在每次价值测试之后坚持你所需要的INSERT
......那就像......
IF (@OldFollowDate<>@FollowDate)
BEGIN
SELECT @audit_action='Follow Date Changed From ' + Cast(@OldFollowDate AS VarChar) + ' to ' + Cast(@FollowDate As Varchar)
INSERT INTO LoggerFollowUpTable
(Followup_ID,CompanyName,FollowType,ContactPerson,FollowDate,FollowTime,Status,Comment,Audit_Action,Audit_Timestamp)
VALUES(@Followup_ID,@OldCompanyName,@FollowType,@ContactPerson,@FollowDate,@FollowTime,@Status,@Comment,@Audit_Action,getdate());
END
答案 1 :(得分:0)
您错过了开始和结束关键字,请尝试此操作,右键单击数据库并选择新查询打开新的SQL查询窗口,然后使用代码:
ALTER Procedure Proc_TrackFollowup
@Followup_ID int,
@FollowType nvarchar(50),
@ContactPerson nvarchar(50),
@FollowDate datetime,
@FollowTime nvarchar(50),
@Status nvarchar(50),
@Comment nvarchar(MAX),
@OldFollowup_ID int,
@OldFollowType nvarchar(50),
@OldContactPerson nvarchar(50),
@OldFollowDate datetime,
@OldFollowTime nvarchar(50),
@OldStatus nvarchar(50),
@OldComment nvarchar(MAX),
@OldCompanyName nvarchar(50),
@audit_action varchar(100),
AS
Begin
SELECT @OldFollowup_ID=Followup_ID,@OldCompanyName=CompanyName,@OldFollowType=FollowType,@OldContactPerson=ContactPerson,@FollowDate=FollowDate,@OldFollowTime=FollowTime,@OldStatus=Status,@OldComment=Comment
from FollowUpTable where Followup_ID=@Followup_ID
UPDATE FollowUpTable SET FollowType=@FollowType,ContactPerson=@ContactPerson,FollowDate=@FollowDate,FollowTime=@FollowTime,Status=@Status,Comment=@Comment where Followup_ID=@Followup_ID
IF @OldFollowType<>@FollowType
SET @audit_action='Follow Type Changed From ' + Cast(@OldFollowType AS VarChar) + ' to ' + Cast(@FollowType As Varchar)
IF @OldContactPerson<>@ContactPerson
SET @audit_action='Contact Person Changed From ' + Cast(@OldContactPerson AS VarChar) + ' to ' + Cast(@ContactPerson As Varchar)
IF @OldFollowDate<>@FollowDate
SET @audit_action='Follow Date Changed From ' + Cast(@OldFollowDate AS VarChar) + ' to ' + Cast(@FollowDate As Varchar)
IF @OldFollowTime<>@FollowTime
SET @audit_action='Follow Time Changed From ' + Cast(@OldFollowTime AS VarChar) + ' to ' + Cast(@FollowTime As Varchar)
IF @OldStatus<>@Status
SET @audit_action='Status Changed From ' + Cast(@OldStatus AS VarChar) + ' to ' + Cast(@Status As Varchar)
IF @OldComment<>@Comment
SET @audit_action='Comment Changed From ' + Cast(@OldComment AS VarChar) + ' to ' + Cast(@Comment As Varchar)
INSERT INTO LoggerFollowUpTable
(Followup_ID,CompanyName,FollowType,ContactPerson,FollowDate,FollowTime,Status,Comment,Audit_Action,Audit_Timestamp)
VALUES(@Followup_ID,@OldCompanyName,@FollowType,@ContactPerson,@FollowDate,@FollowTime,@Status,@Comment,@Audit_Action,getdate());
End