2011-07-26 34 views
1

所有SQL Server是SQL Server 2008中....複製和觸發器

我有被複制到數據中心的表。我在這張表上寫了一個AFTER INSERT & AFTER UPDATE觸發器。當數據被複制到表,訂閱報告錯誤

列名或提供值的數目不匹配表 定義

我可以手動插入記錄&觸發工作正常,雖然在複製試圖插入記錄,我得到上面的消息。下面是我的觸發器..

我確信該錯誤觸發發出,但我很茫然,爲什麼...

AHIA,

LarryR ...

-- ============================================= 
-- 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 

回答

4

你的問題是在這裏

INSERT INTO tblReceivingHeaderStatus_1 

select ... 

不要那樣做。請指定要插入的列,例如

INSERT INTO tblReceivingHeaderStatus_1 
( fieldA, 
    FieldB 
    ... 
) 
SELECT ... 
+0

+1是的,絕對是 - ** **總是明確地定義你的列保存自己的嚴重錯誤(當表突然改變他們的列的列表...) –

+0

康拉德...我想這樣做,但它工作時手動插入數據,要麼複製和過去的表格或插入語句...爲什麼??? – larryr

0

我敢打賭,你有一個被標記爲不復制上tblReceivingHeaderStatus_1標識列...