3

I do some command line batch (.bat) with sqlcmd as this way:

sqlcmd -i Scripts\STEP01.sql -o PROCESS.log -S MYSERVER -E -d MYDATABASE

and i need an output file (it's works currently) and also the output trought the screen to do something like:

@echo off
echo The result of the query was:
    sqlcmd -i Scripts\STEP01.sql -o PROCESS.log -S MYSERVER -E -d MYDATABASE
pause
CHOICE /C:YN /M "Is the result accord?"
IF ERRORLEVEL 2 GOTO ENDWITHERROR
IF ERRORLEVEL 1 GOTO STEP2

Note:

  • Yes, the script works suscefull, it's not an issue question.
  • And yes, I've a "print "something" on my sql. The log file gets the output.

Thanks a lot!

6
  • I don't think there's a way to do this through regular sqlcmd - you may need to use something like a powershell script. Is powershell an option for you?
    – JNK
    Commented Feb 17, 2012 at 13:38
  • I don't think i can use it in staging and production servers :( Commented Feb 17, 2012 at 13:48
  • But you can use command prompt to execute scripts?
    – JNK
    Commented Feb 17, 2012 at 13:53
  • I think I can use scripts, but I dont think third-party implementer approves the method, but i can try Commented Feb 17, 2012 at 13:55
  • 1
    Powershell is a free product provided by microsoft and is explicitly designed to interact with sql server. SQL Server 2008 SSMS has powershell integration build right into management studio. It's a pretty powerful tool that you get for FREE, so you have a case to lobby for it if you want.
    – JNK
    Commented Feb 17, 2012 at 13:59

1 Answer 1

2

In order to implement what you want you need something functionally equivalent to tee or grep on unix. Windows does ship with a utility called find.exe that has similar functionality to grep, so you may be able to use that to extract relevant lines from your output file. As far as I am aware there is no equivalent to tee shipped with base windows builds.

This sort of thing is trivial in a unix shell scripting environment but can cause issues on a raw Windows build. If you can't implement what you want using find.exe, three basic approaches come to mind:

  1. cmd.exe actually has significant pipelining capabilities, although not as rich as a unix shell. You can get Win32 ports of tee and grep from the GNU Win32 collection, or from Microsoft Services for Unix (SFU). The GNU Win32 ones are better because they use msvcrt.dll and understand native Windows paths. SFU places an emulation layer over the base O/S in much the same way as cygwin, so it emulates /cygdrive/c style paths that behave in a more unix-like manner.

    An alternative to this would be to use WSH, powershell or even a small .Net console app to implement a small utility to read the output from the log file and present your prompt to the user.

  2. Reimplement the process in a way that allows the control to be automated. If you can agree business rules for this control it can be run automatically and simply log the result, generating a status report when the process has finished running.

  3. It seems quite strange that you are developing a server-side process that requires interactive input from an operator. If this process has to be run interactively, is there any reason why the interactive parts could not simply be run from somebody's PC? This would probably allow you considerably more flexibility in installing third party components or bespoke client-side code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.