Reputation: 149
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?
Upvotes: 1
Views: 3561
Reputation: 18837
Try to debug your command with wscript.echo or a MsgBox like this :
Command = "sqlcmd -U sa -P *** -S (local) -Q "& DblQuote("sp_attach_db 'Aztec','D:\Aztecdata\Aztec.mdf','D:\Aztecdata\Aztec_log.ldf'")
wscript.echo Command
'***********************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***********************************************************************
Upvotes: 2
Reputation: 16950
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'
""
"
Upvotes: 0