Sql 复制和触发器
所有SQL Server都是SQL Server 2008 我有一个复制到数据中心的表。我在这个表上写了一个AFTER INSERT&AFTER UPDATE触发器。将数据复制到表中时,订阅会报告错误 提供的值的列名或数目与表不匹配 定义 我可以手动插入记录&触发器工作正常,但当复制尝试插入记录时,我收到了上面的消息。下面是我的触发器 我确信错误来自于触发器,但我不知道为什么 阿希亚 拉瑞尔Sql 复制和触发器,sql,tsql,sql-server-2008,Sql,Tsql,Sql Server 2008,所有SQL Server都是SQL Server 2008 我有一个复制到数据中心的表。我在这个表上写了一个AFTER INSERT&AFTER UPDATE触发器。将数据复制到表中时,订阅会报告错误 提供的值的列名或数目与表不匹配 定义 我可以手动插入记录&触发器工作正常,但当复制尝试插入记录时,我收到了上面的消息。下面是我的触发器 我确信错误来自于触发器,但我不知道为什么 阿希亚 拉瑞尔 -- ============================================= --
-- =============================================
-- Description: Updates tblReceivingHeaderStatus_1
-- =============================================
create TRIGGER [dbo].[UPD_tblReceivingHeaderStatus_1]
ON [dbo].[tblReceivingHeader]
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @SeqNum numeric ,@Location numeric
select @SeqNum = i.SeqNum, @Location = i.Location
from tblReceivingHeaderStatus_1 D
left join inserted i on D.Location = i.Location and D.SeqNum = i.SeqNum
UPDATE tblReceivingHeaderStatus_1
SET AdjTax = inserted.AdjTax,
AdjDeliveryFee = inserted.AdjDeliveryFee,
AdjDiscount = inserted.AdjDiscount,
AdjInvoiceTotal = inserted.AdjInvoiceTotal,
AdjItemCount= inserted.AdjItemCount,
AdjInvoiceInfo = inserted.AdjInvoiceInfo,
InvoiceAdjReason = ISNULL(inserted.InvoiceAdjReason,''),
PaidFlag = inserted.PaidFlag,
StartDate = inserted.StartDate,
CheckComments = inserted.CheckComments,
POeMailSent =
case inserted.CheckComments
when '.' then 'P'
else ''
end,
PONumber = inserted.PONumber,
[Status] = inserted.[Status],
MiscFlag2 = 'T'
FROM
inserted
WHERE
inserted.seqnum = tblReceivingHeaderStatus_1.seqnum AND
inserted.location = tblReceivingHeaderStatus_1.location ;
--this assigns all inventory PO receivers to someone in pricing to approve
update tblReceivingHeaderStatus_1
set NextApprover = 1
from tblReceivingHeaderStatus_1
left join apvendp on vmvend = vendornum
where
recdevice = 'P' and
status = '1' and
NextApprover <> 1 and
vminex = 'I' ;
--update tblReceivingHeader to show the record has been
--updated in tblReceivingHeaderStatus_1
update tblReceivingHeader
set MovedToAs400 = 'Y'
where tblReceivingHeader.SeqNum = @SeqNum
and tblReceivingHeader.Location = @Location ;
END
-- ==========================================================
-- Description: Insert records into tblReceivingHeaderStatus_1
-- ==========================================================
create TRIGGER [dbo].[INS_INTO_tblReceivingHeaderStatus_1]
ON [dbo].[tblReceivingHeader]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @SeqNum numeric ,@Location numeric
--get the seqnum & location from the inserted record
select @SeqNum = i.SeqNum, @Location = i.Location
from tblReceivingHeaderStatus_1 D
left join inserted i on D.Location = i.Location and D.SeqNum = i.SeqNum;
INSERT INTO tblReceivingHeaderStatus_1
select
SeqNum,VendorNum,InvoiceNum,InvoiceTotal,ItemCount,
InvoiceDate,Status,Location,AdjTax,AdjDeliveryFee,AdjDiscount,
AdjInvoiceTotal,AdjItemCount,isnull(AdjInvoiceInfo,''),Tax,
DeliveryFee,Discount,ApprovedTime,ApprovedDate,ApprovedBy,
InvoiceAdjReason,'N', GETDATE(),PaidFlag,StartDate,
case CheckComments
when '.' then 'P'
else ''
end,
' ', 0, PONumber, recDevice, '', '', '', 'T', '', '', '', '', 0, 0, 0, '',
msrepl_tran_version
FROM inserted;
-- update tblReceivingHeader to show the record has been
-- inserted into tblReceivingHeaderStatus_1
update tblReceivingHeader
set MovedToAs400 = 'Y'
where
tblReceivingHeader.SeqNum = @SeqNum
and tblReceivingHeader.Location = @Location;
END
你的问题就在这里
INSERT INTO tblReceivingHeaderStatus_1
select ...
不要那样做。而是指定要插入的列,例如
INSERT INTO tblReceivingHeaderStatus_1
( fieldA,
FieldB
...
)
SELECT ...
你的问题就在这里
INSERT INTO tblReceivingHeaderStatus_1
select ...
不要那样做。而是指定要插入的列,例如
INSERT INTO tblReceivingHeaderStatus_1
( fieldA,
FieldB
...
)
SELECT ...
我打赌您在tblReceivingHeaderStatus_1上有一个标识列,标记为不用于复制…我打赌您在tblReceivingHeaderStatus_1上有一个标识列,标记为不用于复制…+1是,绝对-始终明确定义列,以避免在表突然更改列列表时出现严重错误…Conrad…我曾想过这样做,但当我手动插入数据时,无论是将数据复制到表格网格还是插入语句,它都会起作用…为什么?+1是,绝对-始终明确定义列,以避免在表突然更改列列表时出现严重错误…Conrad…我曾想过这样做,但当我手动插入数据时,无论是将数据复制到表格网格还是插入语句,它都会起作用…为什么???