2017-05-16 92 views
1

我試圖從觸發器運行存儲過程,以確保訂單按照它們插入數據庫的順序打印。如果我手動運行存儲過程,它會在20秒內完成。如果我從觸發器運行它,它永遠不會完成。從觸發器調用的存儲過程不運行

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

SET ARITHABORT OFF 
GO 
CREATE PROCEDURE [dbo].[usr_despatch_m2] @order VARCHAR(10) 
AS 

SET XACT_ABORT ON 
SET NOCOUNT ON 
--DROP TABLE #bundle (bundle int) 

DECLARE @sql VARCHAR(1000), @result INT, @text varchar(1000) 
SET @sql = 'D:\csserver\bin\runvtorddesp_m.bat '[email protected] 
-- set @sql = 'command.com /c sdespatch '+ @order 
EXEC @result = master..xp_cmdshell @sql, no_output 

IF @result in (0,1010) 
BEGIN 

EXEC manufg.dbo.op_populate_shipping_data_dev @order 

     IF @order LIKE '[T,Y,R,S]%' 
     BEGIN 
     EXEC [SERVER2].sp_manufg.[dbo].[pod_print_can_inv] @order 
     END 


SET @sql = 'command.com /c PRINT /D:\\SERVER1\MEM-SHIPPING-INVOICE \\SERVER1\e$\PODPDF\'+rtrim(@order)+'.PDF' 
EXEC @result = master..xp_cmdshell @sql, NO_OUTPUT 

END 

BEGIN 
SET @text = (SELECT 
CASE @result 
WHEN 0 THEN 'Order despatched successfully' 
WHEN 1010 THEN 'Order despatched successfully' 
WHEN 1 THEN 'Order error not found in order header' 
WHEN 2 THEN 'Order at incorrect status' 
WHEN 3 THEN 'Order despatch held' 
WHEN 4 THEN 'Order is not web order' 
WHEN 5 THEN 'Order not fully processed by DataLinx' 
WHEN 6 THEN 'Order is in error' 
WHEN 7 THEN 'Order is in error' 
WHEN 8 THEN 'Order header is locked' 
WHEN 9 THEN 'Stock item is locked' 
WHEN 13 THEN 'Order number not found' 
WHEN 33 THEN 'Unable to lock stock allocations (stallocm)' 
WHEN 34 THEN 'Unable to lock stock batch file (stquem)' 
WHEN 40 THEN 'Unable to update EIPOLASTDT system key' 
WHEN 50 THEN 'Unable to unlock EDI order' 
ELSE   'Order in error' 
END) 

SET @text = rtrim(@order) +' ' + @text 
SET @sql = 'echo ' + @text + ' > E:\DespatchErrors\Manufg\'+rtrim(@order)+'.txt' 
EXEC @result = master..xp_cmdshell @sql, no_output 

SET @sql = 'command.com /c PRINT /D:\\SERVER1\MEM-SHIPPING-INVOICE \\SERVER1\e$\DespatchErrors\MANUFG\'+rtrim(@order)+'.txt' 
EXEC @result = master..xp_cmdshell @sql, NO_OUTPUT 


END 

觸發器僅僅是

ALTER TRIGGER [dbo].[usr_order_despatch_m_c] on [dbo].[pod_mfg_inv] 

instead of insert 
as 
SET XACT_ABORT ON 
SET NOCOUNT ON 
DECLARE @order varchar(10) 
SET @order = (select order_no from inserted) 

EXEC usr_despatch_m2 'T568138' 

,似乎是受困下來是

GO 
SET QUOTED_IDENTIFIER ON 
SET XACT_ABORT ON 
SET ARITHABORT ON 
GO 
ALTER PROCEDURE [dbo].[pod_print_can_inv] 
    @myOrder CHAR(10) 
AS 

DECLARE 
    @mySql VARCHAR(1000), 
    @result INT 

BEGIN 
    --Select the right report since Trade invoices are different to Retail dispatch notes 
    IF @myOrder LIKE 'R%' 

     BEGIN 
      SET @mySql = 'dtsrun /S SERVER2 /E /N PODCanRetailInvPDF /A "myFilter":"8"="(lfdinv = ""' + RTRIM(@myOrder) + '"")"' 
     END 
    -- Canadian Battle For Vedros Invoices 
    IF @myOrder LIKE 'Y%' 
     BEGIN 
      SET @mySql = 'dtsrun /S SERVER2 /E /N PODVedrosInvoiceWithTerms /A "myFilter":"8"="(lfdinv = ""' + RTRIM(@myOrder) + '"")"' 
     END 
    -- Canadian Trade Invoices 
    ELSE 
     BEGIN 
      SET @mySql = 'dtsrun /S SERVER2 /E /N PODCanTradeInvWithTerms /A "myFilter":"8"="(lfdinv = ""' + RTRIM(@myOrder) + '"")"' 
     END 

    EXEC @result = master..xp_cmdshell @mySql, NO_OUTPUT 


    IF @result = 0 BEGIN 


     WAITFOR DELAY '00:00:01' 

     SET @mySql = 'DEL \\SERVER1\E$\PODPDF\TST\' + RTRIM(@myOrder) + '.pdf' 
     EXEC @result = master..xp_cmdshell @mySql, NO_OUTPUT 

     WAITFOR DELAY '00:00:01' 

     SET @mySql = 'REN \\SERVER1\E$\PODPDF\TST\PDFCreatorDocument.pdf ' + RTRIM(@myOrder) + '.pdf' 
     EXEC @result = master..xp_cmdshell @mySql, NO_OUTPUT 
     WAITFOR DELAY '00:00:01' 
    -- Stop MSAccess on the server 
     SET @mySql = 'PSKILL msaccess.exe' 
     EXEC master..xp_cmdshell @mySql, NO_OUTPUT 


     WAITFOR DELAY '00:00:10' 



     /* 
     IF @myOrder LIKE 'Y%' 
     BEGIN 
     SET @mySql = 'COMMAND AcroRd32.exe /t "\\SERVER1\E$\PODPDF\TST\' + RTRIM(@myOrder) + '.pdf" "\\SERVER1\BACKOFFICE" "Canon iR-ADV C5235/5240 PCL5c" "IP_172.16.235.100"' 
     EXEC master..xp_cmdshell @mySql, NO_OUTPUT 
     END*/ 

     SET @mySQL = 'MOVE \\SERVER1\E$\PODPDF\TST\'+RTRIM(@myOrder)+'.pdf \\SERVER1\E$\PODPDF' 
     EXEC @result = master..xp_cmdshell @mySQL, NO_OUTPUT 

     WAITFOR DELAY '00:00:05' 

    END 

END 

DTS包在遠程服務器和產生上打開一個訪問數據庫中的存儲過程表單。如果我只是運行存儲過程,這可以快速工作,但如果使用觸發器,它永遠不會完成。

幫助!!!

+2

你真的還在使用SQL 2000和DTS嗎? –

+0

我希望我能說不,但公司目前無法更新。 –

+0

您不能在觸發器中執行此操作,因爲數據尚未提交。在任何支持的SQL Server版本中,您都需要使用IDENTITY鍵將這些命令寫入另一個表中,並讓後臺任務按順序讀取該表並執行命令。是否也適用於SQL 2000我不能說:) –

回答

0

大衛布朗爲我解答了這個問題。

您不能在觸發器中執行此操作,因爲數據尚未提交。在任何支持的SQL Server版本中,您都需要使用IDENTITY鍵將這些命令寫入另一個表中,並讓後臺任務按順序讀取該表並執行命令。是否也適用於SQL 2000我不能說:)

相關問題