Search code examples
sqlsql-serverbatch-filesqlcmd

Why does database query using sqlcmd produce just empty file while same query works in Management Studio?


I have made recently a huge MSSQL script which was working until the server environment changed and some queries are not anymore allowed. So I had to make a .bat file which executes this query from command line.

I get no error or something else. I just get a file with no entry. But I receive a lot of entries if I use the code of the query in the Management Studio.

Does somebody see where is the mistake in my command line?

I inserted some new lines for reading the code better. Everything except command PAUSE is on one line in the batch file.

EDIT: I figured out that the problem is in the last WHERE clause in the LIKE operater. If I take out the LIKE operater it works. It has nothing todo with the % caracter. it is effective the LIKE operater. Does anybody know how to fix that?

sqlcmd -S connection\string -U user -P password -d dbName -s";" -Q "SET NOCOUNT ON; 
SELECT [per_nummer] as EmployeeID, 
[per_id] as System_nr, 
[per_pid] as PID, 
[per_anrede] as Gender, 
[per_vname] as Vorname, 
[per_name] as Name, 
[per_telEx] as Telefon, 
[per_email] as Email, 
[per_instradierungHauptort] as Instradierung, 
[per_gebnr] as Gebaeudenummer, 
(SELECT mobileTelephoneNumber FROM [dbName].[dbo].[import_zuko_GLDAP_DUMP] WHERE UserID = per_pid ) as Mobile, 
[per_business_area] as Business_Area, 
(SELECT csgdivision FROM [dbName].[dbo].[import_zuko_GLDAP_DUMP] WHERE UserID = per_pid ) as Division,
NULL as Bereich, 
(SELECT csgCompany FROM [dbName].[dbo].[import_zuko_GLDAP_DUMP] WHERE UserID = per_pid ) as Firma,
[per_sprache] as Korespondenzsprache,
(SELECT roomnumber FROM [dbName].[dbo].[import_zuko_GLDAP_DUMP] WHERE UserID = per_pid ) as Bueronummer,
[per_floor] as Etage,
(SELECT convert(varchar, convert(date,[per_eintrittsdatum]), 104)) as Eintritt_per,
(SELECT convert(varchar, convert(date,[per_austrittsdatum]), 104)) as Austritt_per,
CONVERT(VARCHAR(10), GETDATE(), 104) AS letzte_mutation,
per_lm as Linemanager,
(SELECT TOP 1 per_pid FROM [dbName].[dbo].[person] WHERE per_nummer = master_table.per_lm) AS lm_pid,
(SELECT convert(varchar, convert(date,[per_lm_von]), 104)) as lm_von,
(SELECT convert(varchar, convert(date,[per_lm_bis]), 104)) as lm_bis,
NULL FROM [dbName].[dbo].[person] as master_table
WHERE
[per_nummer] IS NOT NULL AND per_pidStatus = 'A' AND
([per_pid] LIKE('A%')OR [per_pid] LIKE('F%')OR [per_pid] LIKE('W%'))"
-w 1000 -W  -o "\\servername\G$\path\to\file.csv"
PAUSE

Solution

  • Different default options

    Do you have read carefully sqlcmd Utility documentation by Microsoft?

    There is at top the important information:

    Because different default options may apply, you might see different behavior when you execute the same query in SQL Server Management Studio in SQLCMD Mode and in the sqlcmd utility.

    Therefore the empty file could be caused by different default options.

    Space between option and value of option

    Further the documentation contains at top:

    Currently, sqlcmd does not require a space between the command line option and the value. However, in a future release, a space may be required between the command line option and the value.

    Double quotes inside an argument string are often problematic, see answer on Why double quotes should be always only at beginning and end of an argument string?

    Therefore I suggest to follow the advice of Microsoft and change -s";" to -s ";" with a space charcter after -s as done for all other options, too.

    Escaping percentage sign

    Do you have tried to escape each % in the query string with an additional % and using therefore 3 times %% in the query string?

    Command line interpreter cmd.exe could interpret the string between two % as a reference to an environment variable and as there is no such environment variable, removes everything between two % from the command line.

    Update: It turned out that indeed the not escaped percentage signs resulted in a wrong query string and therefore in an empty results file.

    Command with path and extension

    General hint:
    It is always advisable to specify in batch files commands like sqlcmd with full name which means with path and file extension as this makes the execution of the application independent on the values of the environment variables PATH and PATHEXT.