Rishabh Bhargav Rishabh Bhargav -5 years ago 104
MySQL Question

To get input date from user in batch file

I am trying to run a SQL Query through Batch File and when I am trying to do so I am getting

invalid argument error

Below is the batch file with which I am trying to run my SQL SCRIPT



@echo ------BILLING REPORT---------

@echo Please enter billing report Date in (YYYY/MM/DD)

@echo off

::set /P D = Enter Date:

::Date %Date% > Billing_Data.sql


sqlcmd -S database\instance_name -E -i "C:\Users\rb54761\Deskto\rb54761\Billing_Data_For_Certs_New.sql" > Output.txt -v Date = '2016-07-01'

:: -o "C:\Users\rb54761\Desktop\rb54761\Output.txt"


COPY C:\Users\rb54761\Desktop\rb54761\Output.txt C:\Users\rb54761\Desktop

goto start

for this i need to thing
1. when user input the date, data will fetch according to that date
2. i want to add that date in where clause so that data will fetch accordingly
Below is the where clause where i want to enter the data

DateCompleted > CONVERT(DATETIME, '$(Date)', 102))

Answer Source

In you command line you use -v Date = '2016-07-01', and the single quotes are considered part of the sqlcmd variable. In your script you use '$(Date)', which becomes ''2016-07-01'', and the error should be fairly obvious.

Remove one set of quotes from either the command line or the script, and all should work.

If you want to quote the value in the command line to protect against poison characters or to include batch token delimiters like space or comma, then you must use double quotes: something like -v var = "Some value with spaces". In this case, the double quotes are not included in the value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download