How to remove Scripting variables not defined in sqlcmd RRS feed

  • Question

  • I've been executing the SQL script in sqlcmd and it has more arguments.

    Is there any possibility to declare some of the arguments as optional, If user fails to enter value for any of the arguments then it displays the following message.


    'inputvar1' scripting variable not defined
    'inputvar2' scripting variable not defined
    'inputvar3' scripting variable not defined


    And, the following statement get printed along with above message

    Changed database context to 'testdb'

    I need to remove these messages while getting printed in output file.

    Please help me on t

    Friday, December 5, 2014 2:48 AM

All replies

  • Can anyone help me on this?
    Friday, December 5, 2014 5:03 PM
  • Hello Orion

    Unfortunately it is not possible to mark variables as optional. Is there a reason you cannot set the variables to an empty value? 



    Sunday, December 7, 2014 12:32 AM
  • I was setting up an empty value for the scripting variable which is not passed in SQLCMD, even then scripting variable not defined message displaying on top of the output file.

    DECLARE @whereClause as VARCHAR(max)= N'$(where)':ON ERROR IGNORE IF @whereClause = N'$' + N'(where)'     SET @whereClause = N'';ELSE        SET @whereClause = ' AND ' + N'$(where)';

    From above script, I was assigning the scripting variable ($where) to DECLARE @whereClause as VARCHAR(max) and check whether it has an value. if the value is not passed to ($where), it displaying 'where' scripting variable not defined."

    how do I need to stop displaying this message when user has not passing value?

    Wednesday, December 10, 2014 6:08 AM
  • Coming along late on this, you can use the following:

    • SQLCMD -S YourServer -E -d YourDatabase -i YourScript 2> nul

    That will send the StdErrorOut to the bit bucket.
    • Edited by TulsaDavid Thursday, June 11, 2015 9:12 PM
    Thursday, June 11, 2015 9:11 PM