Search code examples
vbscriptjscriptsqlcmdwsh

sqlcmd hangs in jscript but not vbscript via WSH


I have two scripts, vbtest.vbs and jstest.js:

vbtest.vbs:

Set sh = CreateObject("WScript.Shell")
WScript.echo(sh.exec("sqlcmd -S my-server\name -U user -P pass -Q ""exit(USE [schemaName];SELECT COUNT(*) as Count FROM [myView] where [myColumn] = 'mySearchString';)""").StdOut.ReadAll)

jstest.js:

var sh = new ActiveXObject("WScript.Shell");
WScript.Echo(sh.exec("sqlcmd -S my-server\name -U user -P pass -Q \"exit(USE [schemaName];SELECT COUNT(*) as Count FROM [myView] where [myColumn] = 'mySearchString';)\"").StdOut.ReadAll());

When I run vbtest, I get the expected output. When I run jstest, the new command window for sqlcmd hangs with nothing in it for a few seconds, then I get a jscript message box with this output:

HResult 0x35, Level 16, State 1
Named Pipes Provider: Could not open a connection to SQL Server [53].

Solution

  • JScript string literals need escaping for \:

    my-server\name
    

    =>

    my-server\\name