views:

2148

answers:

2

Here's a minimal batch file, demo.bat, to illustrate my problem:

@ECHO off

set /p foo=Enter foo:
echo.
echo you typed "%foo%"

sqlcmd -?

set /p bar=Enter bar:
echo.
echo you typed "%bar%"

I have an input file foo.txt that looks like so:

foo_value
bar_value

I run my batch file as demo.bat < foo.txt. The output is:

Enter foo:
you typed "foo_value"
Microsoft (R) SQL Server Command Line Tool
Version 9.00.3042.00 NT INTEL X86
Copyright (c) Microsoft Corporation.  All rights reserved.

usage: Sqlcmd            [-U login id]          [-P password]
  (...etc...)

Enter bar:
you typed "foo_value"

If I remove the sqlcmd -?, then bar is "typed" as bar_value, which is what I originally expected.

So, it looks to me like sqlcmd is not playing nice somehow with the standard input that wasn't meant for it. Anyone have any bright ideas on how I can work around it? In a perfect world, the solution would not involve changing the original batch file, or involve installing third-party packages to drive the interaction (e.g. Expect).

+2  A: 

I don't know if it helps, but you can try to pipe something else to sqlcmd, for example:

echo. | sqlcmd -?
Wimmel
This will work, but you'd have to change the batch file.
Patrick Cuff
+1  A: 

You can also redirect NUL to sqlcmd:

sqlcmd -? < NUL

but this would require changing the batch script as well.

Patrick Cuff
I don't expect there are any ways to do this without changing the original batch file, so we have a winner. (I did try to create an `sqlcmd.bat` that runs sqlcmd.exe to try to trick it... but then I would need to add `CALL` to the original. Oh well.)
oeuftete