2014-12-19 95 views
0

需要觸發器的幫助。
如果我從它運行的where子句中刪除我的內部連接,所以我猜這是問題出在哪裏。如果我使用這個where子句它的作品「WHERE oehdhdr_sql.A4GLIdentity = @ ID」SQL觸發器故障

任何幫助,將不勝感激。下面是代碼...

ALTER trigger [dbo].[bdordermakeonhold] 
on [dbo].[oeordhdr_sql] 
for insert 
as 
declare @edi as char(1) 
declare @id as numeric(9,0) 
declare @ordno as char(8) 
declare @ordtype as char(1) 
declare @cus_no as char(12) 
declare @status as char(1) 
declare @ship_to_addr_3 as char(40) 
set @edi=(select edi_fg from inserted) 
set @id=(select a4glidentity from inserted) 
set @ordno=(select ord_no from inserted) 
set @ordtype=(select ord_type from inserted) 
set @cus_no=(select cus_no from inserted) 
set @status=(select status from inserted) 
set @ship_to_addr_3=(select status from inserted) 
set nocount on 
if @edi is null or @edi='E' 
if not exists (SELECT ID, Cus_No, State, CertificateID, Expiration FROM BDSalesTaxCerts  
WHERE State = 'FL' AND @cus_no = Cus_No) 

begin 
UPDATE oeordhdr_sql 
SET   status = 'C',hold_fg = 'H' 
FROM   inserted AS i INNER JOIN 
     oeordlin_sql AS LN ON LN.ord_no = i.ord_no INNER JOIN 
     ARCUSFIL_SQL as CS ON i.cus_no = CS.cus_no 
WHERE  oeordhdr_sql.A4GLIdentity [email protected] and cs.state not in ('FL','PR',' ','lima') and  
(LN.loc = 'SE') AND (@ship_to_addr_3 LIKE '%FL%') AND @status in ('1') 
if not exists (select ID from BDInvoiceSupport where [email protected] and orig_ord_type 
[email protected]) 
begin 
INSERT INTO [dbo].[BDInvoiceSupport] 
([ID] 
,[LastUpdate] 
,[RecordRevLevel] 
,[Inv_No] 
,[TaxProcessingStatus] 
,[Orig_Ord_no] 
,[orig_ord_type] 
,[InvoiceSent] 
,[OnHoldReason]) 
select NEWID(),GETDATE(),0,inv_no,0,ord_no,ord_type,0,'Not checked yet.' 
from inserted 
end 
end 
+0

我想你需要'UPDATE'和'IF'語句之間的分號。 – Barmar

+0

當您嘗試編譯觸發器時發生錯誤,或者當觸發器執行時發生錯誤。你遇到了什麼錯誤? – Spock

+0

觸發器旨在一次對所有受影響的記錄進行操作。如果修改了多個記錄,則插入的表將包含多條記錄。在這種情況下,變量只能保存一個值,而插入的表有很多值,可能是導致錯誤或失敗的原因。我建議不要將插入表中的值存儲到變量中,而是以面向集合的方式處理。 – Jeremy

回答

0

如果兩行被一次插入,這會給你帶來問題。您應該使用連接將它寫入inserted表,而不是使用變量。