Windows Task Scheduler: OSQL command does not return error when fails?

  • Hi everyone,

    I need some help with this.

    I've scheduled a task on Windows Task Scheduler, that calls the command OSQL using the parameter: -i "ScriptPath"
    That is OSQL command executes the script stored in the path.

    My problem is this:
    - My script starts doing a USE [DatabaseX]
    - DatabseX does not exist on the server, so the script fails.
    - But the Scheduled Task ends with a Successful result 0x0, even if the script fails.

    If the script fails, I need to see that the last run failed in the scheduled task.
    Is this possible? Could somebody help me with this?

    EDIT: Changed OSQL to SQLCMD, but the problem is the same. The Scheduled Task result is OK even if the script fails.

    Thanks & Regards,

  • pepes135 - Friday, January 26, 2018 8:48 AM

    Hi everyone,

    I need some help with this.

    I've scheduled a task on Windows Task Scheduler, that calls the command OSQL using the parameter: -i "ScriptPath"
    That is OSQL command executes the script stored in the path.

    My problem is this:
    - My script starts doing a USE [DatabaseX]
    - DatabseX does not exist on the server, so the script fails.
    - But the Scheduled Task ends with a Successful result, even if the script fails.

    If the script fails, I need to see that the last run failed in the scheduled task.
    Is this possible? Could somebody help me with this?

    EDIT: Changed OSQL to SQLCMD, but the problem is the same. The Scheduled Task result is OK even if the script fails.

    Thanks & Regards,

    Changing to use SQLCMD is a good thing since OSQL is deprecated and SQLCMD is the replacement.

    Task scheduler just runs the actions setup for a task and if the task itself runs, its a success. It doesn't know anything about the script or it's results.
    With that said, I don't know of anyway to make the schedule task to appear as a failure. I think there are other options though. One I can think of is to use an output file with sqlcmd. The results of the script would be logged. You can email that file or use Powershell to look for messages. 

    Sue

  • Thanks for the good idea.
    BTW i found that you can use the SQLCMD parameter -b (should do something like send the exceptions to the above layer)
    I've tried it and the Scheduled task ends with 0x1 result using the parameter.
    It seems to be what i was looking for.
    Thanks!

Viewing 3 posts - 1 through 2 (of 2 total)

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