我创建了一个批处理脚本,该脚本将表上的信息打印到标签中并将其保存到 pdf 中。目前,我给脚本一个数字,即ItemCode
,它会打印出表格中的其余信息。
现在,我要走的更远,我的目标是每次修改表,添加新行或修改单个字段时都运行脚本。发生这种情况时,它将检查已修改的行,并使用已修改的ItemCode运行脚本。
一直在寻找与此类似的东西,但找不到足够精确的东西,因此任何帮助都将是很好的!
答案 0 :(得分:0)
以下代码是我正在使用的触发器的一部分。它将旧值和新值写入特殊表以跟踪所有重要更改。通过SQL命令发送updated_idx,以告诉用户正在执行此操作。
USE [Demo] -- database
GO
/****** Object: Trigger [dbo].[update_Address] Script Date: 26.07.2018 11:16:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[update_Address] ON [dbo].[Address] for UPDATE AS
DECLARE @fieldname varchar(128) = '- empty -'
DECLARE @newValue varchar(2048) = '- empty -'
DECLARE @oldValue varchar(2048)= '- empty -'
DECLARE @Updated int = datediff(s,'01/01/1970',SYSUTCDATETIME())
DECLARE @Updated_IDX int = -1
DECLARE @ID int = -1
DECLARE db_cursor CURSOR FOR SELECT Address_id, Updated_IDX FROM inserted
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @ID, @Updated_IDX -- this takes the current id
WHILE @@FETCH_STATUS = 0
BEGIN
SET NOCOUNT ON
If UPDATE([ZipCode])
BEGIN
SELECT @OldValue=b.ZipCode, @NewValue=a.ZipCode FROM inserted a, deleted b
IF @NewValue <> @OldValue
BEGIN
INSERT INTO TransactionLog ([ID],[TableName],[Type],[FieldName],[newValue],[oldValue],[Updated],[Updated_IDX]) values (@ID,'Address','U','ZipCode',@newValue,@oldValue,@Updated,@Updated_IDX);
END
END
If UPDATE([City])
BEGIN
SELECT @OldValue=b.City, @NewValue=a.City FROM inserted a, deleted b
IF @NewValue <> @OldValue
BEGIN
INSERT INTO TransactionLog ([ID],[TableName],[Type],[FieldName],[newValue],[oldValue],[Updated],[Updated_IDX]) values (@ID,'Address','U','City',@newValue,@oldValue,@Updated,@Updated_IDX);
END
END
FETCH NEXT FROM db_cursor INTO @ID, @Updated_IDX
End
CLOSE db_cursor
DEALLOCATE db_cursor