SQL Server Performance

Using multiple lines of query in sqlcmd using -q switch

Discussion in 'SQL Server 2008 General DBA Questions' started by satya.sqldba, Dec 29, 2010.

  1. satya.sqldba New Member

    Hello
    I am trying to use sqlcmd to run a set of sql statements. If I use -i switch and input a file with sql statements, it is working fine, but I am trying to use the sql statements using -q to avoid the creation of the input file. But I am not having luck with -q, can someone let me know if putting multiple lines of code is possible in -q switch like below?
    A simple restore command like below. If I use the whole restore command in single line it works fine like below:
    sqlcmd -E -S servername -d master -Q "restore database bestst_test from disk='E:Backup esteststestst_20101222.bak' with move 'BESMgmt415_data' to 'E:program FilesMicrosoft SQL ServerMSSQL.1MSSQLDATAestst_test.mdf',move 'BESMgmt415_log' to 'E:program FilesMicrosoft SQL ServerMSSQL.1MSSQLDATAestst_test_log.ldf'"
    but if I split the restore command into 3 lines like below, it fails, can someone let me know how to use the multiple line feature in sqlcmd -q switch?
    sqlcmd -E -S servername -d master -Q "restore database bestst_test from disk='E:Backup esteststestst_20101222.bak'
    with move 'BESMgmt415_data' to 'E:program FilesMicrosoft SQL ServerMSSQL.1MSSQLDATAestst_test.mdf',
    move 'BESMgmt415_log' to 'E:program FilesMicrosoft SQL ServerMSSQL.1MSSQLDATAestst_test_log.ldf'"
    Thanks
    Satya
  2. satya.sqldba New Member

    I figured it out myself with the help of a friend.
    Spanning the same command across multiple lines is not possible, my main intention in asking this question was to see if 2 statements can be executed using just one -q switch like 'restore db1,restore db2' in one -q swith, turns out we can do it by separating them with semi-colon, but everything has got to be in one line like below:
    sqlcmd -E -S servername -d master -Q " restore db1;restoredb2;"
    Satya
  3. satya Moderator

Share This Page