Need to create step in SQL job which check if file exist because if file exists it needed to be stop but if file not exist to continue with next step. xp_fileexist not allowed in sql query.

  • if you use SQLServer Agent, can't you make an Operating System or Powershell command ?
    – irimias
    Nov 30, 2016 at 10:30
  • yes, but on one way or another SQL agent will finish with success, I want to stop if file exists. Nov 30, 2016 at 10:37

1 Answer 1


You can use a Powershell command on your job and raise an error if your file is not found so that your job will stop. Something like:

if (-not (Test-Path $file)) 
    throw [System.IO.FileNotFoundException] "$file not found."
  • different logic. If file exists then stop Nov 30, 2016 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.