Search code examples
vbscriptsqlcmd

SQLCMD within a VBS


I am trying to write a section into a script which attaches a DB via SQLCMD.

If I run this from a CMD line/batch file it attaches the DB successfully:

sqlcmd -U sa -P RubbishPassword -S (local) -Q "sp_attach_db 'MyDBInstance','D:\AFolder\MyDB.mdf','D:\MyDBInstance\MyDB_log.ldf'"

However if I run it within a VBS by doing the below it doesn't error but doesn't attach the DB.

    Set objShell = CreateObject("WScript.Shell")
objShell.Run "sqlcmd -U sa -P RubbishPassword -S (local) -Q ""sp_attach_db 'MyDBInstance','D:\AFolder\MyDB.mdf','D:\MyDBInstance\MyDB_log.ldf'" 

Any ideas on why and/or a better way of passing a SQLCMD via VBS?


Solution

  • Trailing double quotes for -Q option is missing in your command.

    objShell.Run "sqlcmd -U sa -P RubbishPassword -S (local) -Q ""sp_attach_db 'MyDBInstance','D:\AFolder\MyDB.mdf','D:\MyDBInstance\MyDB_log.ldf'"""