InterviewSolution
| 1. |
Solve : Use batch file to call sqlcmd? |
|
Answer» Env: MS Server 2008, MS SQL Server 2005 Good point Salmon Trout, you have to use ^% vs ^* No, to get a batch script to echo % you need to put %% Thanks for your efforts, but the problem remains...with no output from the query. Any advice that points me in the right direction would be appreciated. Do you get an output if you run the query manually? Have you checked that the batch is producing exactly the same query? Salmon, thanks for taking the time to help out. Quote Do you get an output if you run the query manually? Have you checked that the batch is producing exactly the same query? The SQL query runs without problem in MS SQL Mgmt Studio using the wildcard LIKE clause. The result that is returned there is correct. The DOS prompt produces exactly the query that it should but nothing is returned as output. No visible errors are returned when running the batch script in the DOS prompt. I've narrowed down the batch script and SQL Query to get rid of noise to make troubleshooting and testing easier, see code below: Code: [Select]@echo on setlocal enabledelayedexpansion :: There is one supplier with ID 41003898 in database, type is char set sql="SELECT SupplierID FROM Supplier WHERE SupplierID LIKE '4100389%%'" CALL sqlcmd -S server/s1 -U sa -P passw -d DB -Q%sql% -o output.txt -u -s "" -w180 output.txt endlocal If I change the SQL above and put LIKE '41003898' in the batch script it returns the correct result = one row in the output file.Since you have ECHO on, what happens if you put PAUSE at the end of the batch to halt it to give you time to copy the echoed sqlcmd line from the console and paste it into another console window, i.e. run it manually? I note you have "narrowed down" the batch; that is all very well, and I understand the reason, but you'd be surprised how often the cause of a script failing is somewhere in what has been removed "for clarity". For example it makes me uneasy to see you using a double colon (a broken label) as a comment. It might have been OK in MS-DOS, but in Windows NT family batch scripts this is a frowned-on practice (for example it breaks a script if it is within parentheses such as in a multiline FOR or IF structure.) It is undocumented, unsupported and should be avoided. Assuming sqlcmd is the Microsoft sqlcmd.exe, why are you using CALL to invoke it? This was embarrasing...so simple, yet so friggin difficult for me to solve on my own...thanks a bunch Salmon. Leaving out the CALL cmd solved it. A bit strange though that only the wildcard LIKE clause broke and nothing else... CALL is only for running one batch file from another and passing control back to the first when the second one finishes; you can use START for other types of executable but you can use the path and/or name of an exe directly. |
|