我有一个不会触发的SQL触发器,因为表中的记录是通过BULK INSERT插入的。我无权访问插入记录的代码,因此我需要修改此触发器来处理BULK INSERT。这是触发器:
USE [testdata]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Trigger_test] ON [dbo].[test]
AFTER INSERT , UPDATE
AS
BEGIN
DECLARE @BatchId int, @Ethanol decimal(18,6), @Glucose decimal(18,6), @SampleAge varchar(50);
SELECT @BatchId = CONVERT(int,bd.[BatchId]),
@Ethanol = CONVERT(decimal(18,2),[Ethanol]),
@Glucose= CONVERT(decimal(18,2),[Glucose]),
@SampleAge = bd.SampleCode
from INSERTED bd
update [dbo].[DeSchedule]
SET
[Ethanol] = @Ethanol,
[Glucose] = @Glucose,
[SampleCompleted] = 1
WHERE [BatchID] = @BatchId AND [SampleAge] = @SampleAge
END
任何人都可以帮我修改此触发器以处理BULK INSERT。
答案 0 :(得分:2)
除非您可以修改BULK INSERT语句,否则您将被卡住。默认情况下,触发器不会在批量插入期间运行。您必须使用FIRE_TRIGGER选项在命令中明确打开它们。
答案 1 :(得分:1)
仅需编辑下面的BULK INSERT文件:
批量插入AdventureWorks2012.Sales.SalesOrderDetail 从'f:\ orders \ lineitem.tbl' 与 ( FIELDTERMINATOR ='|' ,ROWTERMINATOR =':\ n' ,FIRE_TRIGGERS ); );
答案 2 :(得分:0)
问题是它只选择了插入表的最后一行,我想如果你改变这样的查询就行了
update [dbo].[DeSchedule]
SET
[Ethanol] =(select CONVERT(int,bd.[Ethanol]) from inserted bd),
[Glucose] = (select CONVERT(decimal(18,2),[Glucose]) from inserted bd),
[SampleCompleted] = 1
WHERE [BatchID] = (select CONVERT(int,bd.[BatchId]) from inserted bd) AND [SampleAge] = (select bd.SampleCode from inserted bd)