Sql – Having trouble piping between cygwin and sqlcmd

cygwinsql

This simple command fails in bash:

echo "print 'tsql'" | sqlcmd -E

it works correctly in a regular old command prompt.

It used to work with previous versions of cygwin. I've also tried inserting various flavors of unix2dos in there, and I've looked at the output with "od -c". So I'm quite sure it's not just a LF vs. CR/LF problem.

Looking at this thread: http://cygwin.com/ml/cygwin/2010-01/msg00205.html

leads me to believe something got broken in cygwin, but I've been unable to chase down any more leads.

Does anyone have any clues, other than generating a temp file, and then using

sqlcmd -E -i tempfile

which does work, but it's ugly.

Best Answer

I am using MSYS2 (x86-64, installed with the 20150916 and updated using pacman to the latest default packages as of two days ago) but I ended up encountering the same error (which makes sense as MSYS2 is a cygwin derivative). I never had this with MSYS, but since MSYS is kind of dead and I wanted more modern tools I am switching to MSYS2 and porting my scripts. I found a workaround that is working for me at the moment:

$ echo hi | cmd //c "cat | sqlcmd"

I assume it works because we’re asking cmd to set up the pipe between the MSYS2 process (cat) and the Windows process rather than letting MSYS2/bash do it. Though I also suspect that SQLCMD is doing some evil nasty trickery from Windows programming in the ’90s which it shouldn’t be doing in the first place making it a special case which perhaps deserves a workaround be developed in cygwin/MSYS2.

Note that with this method passing arguments to sqlcmd becomes really difficult, especially if you want to pass a literal ".

Full output from me demonstrating the error, its solution, and showing where my sqlcmd is coming from:

ohnob@WIN-F5A6PNAUAJ2  ~
$ echo hi |sqlcmd -S localhost\\SQLEXPRESS12
Sqlcmd: Error: Internal error at ReadTextLine (Reason: Unspecified error).

ohnob@WIN-F5A6PNAUAJ2  ~
$ echo hi | cmd //c "cat | sqlcmd -S localhost\\SQLEXPRESS12"
Msg 2812, Level 16, State 62, Server WIN-F5A6PNAUAJ2\SQLEXPRESS12, Line 1
Could not find stored procedure 'hi'.

ohnob@WIN-F5A6PNAUAJ2  ~
$ which sqlcmd
/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/110/Tools/Binn/sqlcmd

ohnob@WIN-F5A6PNAUAJ2  ~
$ WHERE cat | head -n 1
C:\msys64\usr\bin\cat.exe