从触发器调用的存储过程不 运行
Stored procedure called from trigger does not run
我正在尝试 运行 来自触发器的存储过程,以确保按照订单插入数据库的顺序打印订单。如果我手动 运行 存储过程,它会在不到 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 '+@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
触发器只是
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 程序包正在远程服务器上打开访问数据库并生成表单。如果我只是 运行 存储过程,这个工作会很快,但如果我使用触发器,它永远不会完成。
求助!!!
大卫布朗为我回答了这个问题。
您不能在触发器中执行此操作,因为数据尚未提交。在任何受支持的 SQL 服务器版本中,您需要使用 IDENTITY 键将这些命令写入另一个 table,并让后台任务按顺序读取 table 并执行命令。这是否也适用于 SQL 2000 我不能说 :)
我正在尝试 运行 来自触发器的存储过程,以确保按照订单插入数据库的顺序打印订单。如果我手动 运行 存储过程,它会在不到 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 '+@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
触发器只是
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 程序包正在远程服务器上打开访问数据库并生成表单。如果我只是 运行 存储过程,这个工作会很快,但如果我使用触发器,它永远不会完成。
求助!!!
大卫布朗为我回答了这个问题。
您不能在触发器中执行此操作,因为数据尚未提交。在任何受支持的 SQL 服务器版本中,您需要使用 IDENTITY 键将这些命令写入另一个 table,并让后台任务按顺序读取 table 并执行命令。这是否也适用于 SQL 2000 我不能说 :)