SQLCMD "-r" option

OK, so I'm playing with SQLCMD and trying to get the "-r" option to work. According to BOL it's supposed to redirect error messages of severity level 17 or higher to the screen (stderr). Presumably this would be useful in situations where you're running a script with the "-o" option, so that output would be sent to a file and error messages would be sent to the screen and not included in the output file. When I run it in the following query:

sqlcmd -S SQL2005 -Q"raiserror('Error in text', 17, 1)" -r 0 -o test.txt

or

sqlcmd -S SQL2005 -Q"raiserror('Error in text', 17, 1)" -r 1 -o test.txt

The error message is sent to the output file (test.txt) and not to the screen. Is there another option that needs to be set, or am I reading the documentation wrong

Thanks



Answer this question

SQLCMD "-r" option

  • ERK

    Yes, I saw that in the documentation but it did not seem to do much. Could not get much out of -r...

    In the end, I used the following options

    sqlcmd -S MyServer -E -V 10 -t 120 -i MyFile.sql

    The most interesting one being -V 10 which made sqlcmd report an error if any error is raised of severity 10 or over.

    Another bizarre thing is that the error messages (raiserror, etc) are still sent through Standard Output rather than Standard Error... but I suppose it's normal.

    Eric


  • Engage

    So the -r ended up not being very useful for you in this case.

    I am not sure if the behavior for raiserror was considered in the context you are applying it. -- As you acknowledge it might be "normal." I'll poke around on this one to see what I comes up.

    Terrence Nevins

    SQL Server Program Manager



  • rogerdubie

    From the SQL BOL.
    -r [ 0 | 1]

    Redirects the error message output to the screen (stderr). If you do not specify a parameter or if you specify 0, only error messages with a severity level of 17 or higher are redirected. If you specify 1, all error message output including PRINT is redirected.

    I might be misunderstanding the question, but the documentation and the behavior implies either/or behavior. With 0 being the default.

    Terrence Nevins

    SQL Server Program Manager



  • Merten1982

    Mike

    I am trying to detect errors when firing sqlcmd from within a C# program.

    So far, I have not been able to collect error messages within StandardError but by using the "-V" option I can get sqlcmd to give me the severity of the last error

    Maybe that helps a bit...

    Can anybody explain the "-r" flag, please

    Cheers

    Eric


  • SQLCMD "-r" option