简体   繁体   中英

How Can I Run A Batch File (.Bat) Using SQL Triggers

I am trying to run a .bat file stored in a D:\\ drive using SQL triggers .
when some operation occurs in a table this trigger need to be fired and Batch file has to be executed. for that i have created a sample trigger.

Create TRIGGER [dbo].[TestBatch] ON [dbo].[RawMaterial] 
FOR UPDATE
AS
DECLARE @PassedVariable VARCHAR(100)
DECLARE @CMDSQL VARCHAR(1000)
begin
--SET @PassedVariable = 'SqlAuthority.com'
SET @CMDSQL = 'D:\AutoServer.bat'-- + @PassedVariable
EXEC master..xp_CMDShell @CMDSQL
end

but i am not getting a proper output. when i run the above script it is giving some error message saying.

"The device is not ready. NULL"

I would request all to give me a solution for my above issue. Thanks in advance.

Not a good idea to run a bat file on a table trigger it can be very problematic depending on what your bat file is doing and how much it has to do. Also how often the table trigger is fired can also exacerbate the problem too. Better to store tables affected trigger records to another seperate table with say a processed flag and run bat file periodically thru a SQL scheduled job that once record is processed through the bat file will flip the processed flag to true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM