Search code examples
sql-serverstored-procedurestriggerssql-server-2000

Stored procedure called from trigger does not run


I am trying to run a stored procedure from a trigger to ensure orders are printed in the order they were inserted into the database. If I run the stored procedure manually it completes in less than 20 seconds. If I run it from the trigger it never completes.

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 '+@order
-- 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

The trigger is simply

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'

The stored procedure that appears to be bogging things down is

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

The DTS package is opening an access database on the remote server and generating a form. This works lightening quick if I just run the stored procedure but if I use the trigger it never completes.

HELP!!!


Solution

  • David Brown answered this for me.

    You can't do this in a trigger because the data isn't committed yet. In any supported version of SQL Server you would need to write those commands into another table with an IDENTITY key, and have a background task read that table in order and execute the commands. Whether that also works in SQL 2000 I can't say :)