这是场景 - 我将具体说明。我在Sql上有一个“桥接”数据库[Fulcrum_Xfer]我使用这个桥接,因为名为[Fulcrum UAT]的主Db使用bigint数据类型用于某些字段,从而在我的Access前端显示“ #Deleted data < / em>“在所有领域 - 在当前设计中不能更改此行为(bigint必须保留)所以我在[Fulcrum_Xfer] DB中有确切的表名和字段名 - [Fulcrum_Xfer]中Orders表中的OrderNO字段是int并且没有主键
明天在一些“你让我们失望”的威胁下我需要做的是以下
最初插入或更新数据的表称为Orders,并且在[Fulcrum_Xfer]数据库中,结构如下
OrderNo int Unchecked
OrderDate smalldatetime Unchecked
ApplicationTenantLinkId int Unchecked
OrderStatus int Unchecked
从FulCrum_Xfer中的Orders接收触发数据的表称为Orders,它位于Database Fulcrum UAT中 结构是
OrderNo bigint Unchecked Primarykey
OrderDate smalldatetime Unchecked
ApplicationTenantLinkId Bigint Unchecked
OrderStatus int Unchecked
我需要两个触发器语句,在将它插入[FulCrum_Xfer]中的Orders后,将新记录插入[Fulcrum UAT]中的Orders中
和
当我在[Fulcrum_Xfer]
中对订单进行更改时,我需要一个触发器来更新[Fulcrum UAT]中Orders中的任何字段除了[FulCrum_XFer]中的数据库触发器之外,我不知道触发器的位置,但我被模板语法吓坏了(不认为我需要所有这些)并且我不知道如何为每个触发器编写语法任务
我是一位非常有经验的VB / VBA开发人员,并且使用ADO在SQL上构建和调用存储过程但是从来没有在SQL Server上执行过这种类型的任务 - 请不要把我当成傻瓜 - 但是这个在我的工作中非常重要。
答案 0 :(得分:3)
好吧,我没有办法测试它,但我认为这就是你写INSERT触发器的方式:
USE [Fulcrum_Xfer]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER dbo.trOrders_Insert
ON dbo.Orders
AFTER INSERT AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Just INSERT everything from the [inserted] pseudotable into
--the target table
INSERT INTO [Fulcrum UAT].dbo.Orders
(OrderNo, OrderDate, ApplicationTenantLinkId, OrderStatus)
SELECT OrderNo, OrderDate, ApplicationTenantLinkId, OrderStatus
FROM inserted;
END
GO
将其复制并粘贴到Management Studio的“查询”窗口中并执行它。
这是我如何执行UPDATE触发器的方法。再次,未经测试......
USE [Fulcrum_Xfer]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER dbo.trOrders_Update
ON dbo.Orders
AFTER UPDATE AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Just UPDATE everything matching [inserted] pseudotable
--into the target table
--(NOTE: This assumes that UPDATES will never change the PK/OrderNo)
UPDATE [Fulcrum UAT].dbo.Orders
SET OrderDate = ins.OrderDate,
ApplicationTenantLinkId
= ins.ApplicationTenantLinkId,
OrderStatus = ins.OrderStatus
FROM [Fulcrum UAT].dbo.Orders As tar
JOIN inserted as ins ON tar.OrderNo = ins.OrderNo;
--(also, performance may not be great as the JOIN columns are
-- different datatypes)
END
GO
答案 1 :(得分:0)
试试这个
USE BioStar;// Which data base you want to create trigger
GO
CREATE TRIGGER trgAfterInsertnew ON [dbo].[TB_EVENT_LOG]
FOR INSERT
AS
declare @nDateTime int;
declare @nReaderIdn int;
declare @nEventIdn int;
declare @nUserID int;
declare @nIsLog smallint;
declare @nTNAEvent smallint;
declare @nIsUseTA smallint;
declare @nType smallint;
select @nDateTime=i.nDateTime from inserted i;
select @nDateTime=i.nReaderIdn from inserted i;
select @nEventIdn=i.nEventIdn from inserted i;
select @nUserID=i.nUserID from inserted i;
select @nIsLog=i.nIsLog from inserted i;
select @nTNAEvent=i.nTNAEvent from inserted i;
select @nIsUseTA=i.nIsUseTA from inserted i;
select @nType=i.nType from inserted i;
insert into [HRM].dbo.Device_Data
(nDateTime,nReaderIdn,nEventIdn,nUserID,nIsLog,nTNAEvent,nIsUseTA,nType)
values(@nDateTime,@nDateTime,@nEventIdn,@nUserID,@nIsLog,@nTNAEvent,@nIsUseTA,@nType);
--set @audit_action='Inserted Record -- After Insert Trigger.';
PRINT 'AFTER DELETE TRIGGER fired.'
GO