run a program as a result of a trigger?

  • Dear all

    I have the following issue:

    I have a sql 2000 database. What i have to do is update another database when a table in my source database is updated. I cannot directly talk to the destination database but i have to go through an api.

    So what i did i created a small program that expects parrameters that i would need to pass through the API to the destination database.

    Is there any way to create a trigger for my source database that when triggered will pass the parameters that are being updated in the source database to the program which will talk through the api to the destination database?

    Would there be any examples of something like this done before?

    Thank you all so much for your help.

    Sincerely

     

  • I am not 100 % sure what your requirements are but I will suggest you try this:

    1. Create a trigger in a table as below:
    2. CREATE TRIGGER TriggerTest ON [dbo].[TestTable]

      FOR INSERT

      AS

      Exec Master..xp_cmdshell 'C:\aaa.bat abcd,efgh'

    3. Create a batch file aaa.bat in the C:\ in your SQL server
    4. The batch file contains one line of command: Echo %1 %2 > C:\aaa.txt
    5. Insert a row into [dbo].[TestTable]
    6. See if the file aaa.txt created in C:\ in your SQL server.

    What I am showing you is how to use Master..xp_cmdshell command to execute external DOS command or EXE or Batch file to pass parameters which I believe what you are looking for.

  • Dear Terry

    That was exactly what i was interested in. I will try that approach now.

    Only one addition to the post:

    Can the trigger receive a return value from the batch file or the exe program. Like for example if the exe program succesfully completes the task and inserts the stuff in the destination then it will all be good. But if the programm for some reason fails to do so then i would like to use the trigger in my source database to ROLLBACK the UPDATE or INSERTION and the user would have to try and do the task again.

     

    Regards

     

  • You may try something like this:

    CREATE TRIGGER TriggerTest ON [dbo].[TransactionLineItems]

    FOR INSERT

    AS

    Exec Master..xp_cmdshell 'C:\aaa.bat abcd, efgh'

    -- Wait for 3 seconds for the bach file execution

    Waitfor Delay '000:00:03'

    -- Check if any failure reported in the output file

    -- Assuming the output file is aaa.txt

    IF Exists(Select Name From tempdb..Sysobjects Where Name Like '%TempResult%')

    Drop Table #TempResult

    Create Table #TempResult (txtResult varchar(200))

    Insert Into #TempResult

    Exec Master..xp_cmdshell 'Type C:\aaa.txt'

    If exists (Select txtResult From #TempResult Where txtResult like '%Fail%')

    Begin

     Print 'Failed'

     --Rollback Tran

    End

    Else

     Print 'Success'

    Actually I never tried this before. So please leave some log if this works.

  • I have been trying to get the trigger to execute my external program  but it does not seem to work...I will paste my whole script here.....I cant figure out why it would not run it:

    CREATE TRIGGER MyTrigger ON [dbo].[TheTable]

    FOR INSERT

    AS

    BEGIN

     DECLARE @var sysname,@cmd sysname,@Client sysname,@Job sysname,@Type sysname

     SET @Client = 'Client'

     SET @Job = 'Job1'

     SET @Type = 'test'

     SET @cmd = 'C:\programDir\SubdirExe\program.exe ' + @Client + ' ' + @Job + ' ' + @Type

     SET @var = 'echo ' + @cmd + ' output.txt'

     Print ('AFTER Trigger []  – Trigger executed !!')

     EXEC Master..xp_cmdshell @cmd

     EXEC Master..xp_cmdshell @var

    END

     

    I cant see why this would not run. The echo command runs without no problems but the running of the program runs. I have tried running the program with the same parameters from the command line and it runs with no problems.But from inside the trigger doesnt want to run..

     

    Any help would be appreciated.

    Sincerely

     

  • Do some check in your trigger as follows:

    1. Create  an ErrorCapture table outside of the trigger:

      Create Table [dbo].[ErrorCapture]

      ( [ErrDesc] Varchar(2000) Null)

    2. Use the table inside the trigger as follows:

      CREATE TRIGGER MyTrigger ON [dbo].[TheTable]

      FOR INSERT

      AS

      BEGIN

      set nocount on

       DECLARE @var sysname,@cmd sysname,@Client sysname,@Job sysname,@Type sysname

       SET @Client = 'Client'

       SET @Job = 'Job1'

       SET @Type = 'test'

       SET @cmd = 'C:\programDir\SubdirExe\program.exe ' + @Client + ' ' + @Job + ' ' + @Type

       SET @var = 'echo ' + @cmd + '> output.txt'

       Print ('AFTER Trigger []  – Trigger executed !!')

       -- clear the ErrorCapture buffer

       Truncate Table [dbo].[ErrorCapture]

       Insert Into [dbo].[ErrorCapture]

       EXEC Master..xp_cmdshell @cmd

       -- display error

       Select * From [dbo].[ErrorCapture]

       EXEC Master..xp_cmdshell @var

      END

    One other trivial check is, is the 'C:\programDir\SubdirExe\program.exe' on the SQL server or your PC? This exe should be on your SQL server. Once you capture the error message, then you are on better position to debug.

  • yup i have the application that i would like to run on the same machine as the sql server.

    just something really dodgy that is happeneing now is:

    i modified the trigger and then clicked apply so i can save the changes but that process appear to take too long and my sql enterprise manager does not respond. Could i have stuffed up the database?

     

    I would probably have to restard the SQL server later on.....I still have few users using the other databases so i cant perform the process now. My test database is on the same server as the production one.

    Cheers

     

  • The error that i am getting is:

    C:\WINDOWS\system32>application.exe Client Job1 Type1

    'application.exe' is not recognized as an internal or external command,

    operable program or batch file.

  • now i just ran it again and now error was produced but no result aswell

    just to do a test i tried to do the operation from the comand line and it ran without any problems

    that seemd interesting and unpredictable

  • Hi, I am not a trained SQL person, but would the fact that you are using variables, the values of which might lose the quote (') characters, be causing your problems?

    Try hardcoding the values on your commandline (without using variables) to test.

    I am interested to see if this works, as I would like to send emails from the external program.

    Regards

    Michael

     

  • The path to the application is RELATIVE to the SQL Server machine.

     


    N 56°04'39.16"
    E 12°55'05.25"

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply