I was given a huge batch file(windows) which when I run it generates the following error:
VALUES was unexpected at this time.
I manage to remove a great part of it and break it down to the following code(read a text file and insert data to a sql server table:
@echo off
setlocal enabledelayedexpansion
set "input_file=data.txt"
set "server=166.444.44.44"
set "database=MYDB"
set "table=Test"
set "sql_file=insert_data.sql"
rem Delete old SQL file if it exists
if exist %sql_file% del %sql_file%
rem Read the input file and generate insert statements
for /f "tokens=1,2,3,4 delims=, eol=;" %%A in (%input_file%) do (
echo INSERT INTO %table% (Name, Age, Income, Address) VALUES ('%%A', %%B, %%C, '%%D'); >> %sql_file%
)
rem Execute the SQL file using sqlcmd
sqlcmd -S %server% -d %database% -E -i %sql_file%
rem Cleanup
del %sql_file%
endlocal
Even with this I’m getting ” VALUES was unexpected at this time.”
I did a lot of research but so far no luck.