Bat file for reading in CSV to SQL

March 7, 2009 at 05:07:06
Specs: WINDOWS XP
Hi

I need a batch file that will read in a csv and pass the info to a SQL statment called datanew.sql.
A example csv file is below:-

1,1234,1
2,4321,2
3,3333,9

the SQL Statments would look like this:-
insert into AA_cigs (code,productid,desciption) values (1,(select productid from AA_bars where barcode='1234'),1);
insert into AA_cigs (code,productid,desciption) values (2,(select productid from AA_bars where barcode='4321'),2);
insert into AA_cigs (code,productid,desciption) values (3,(select productid from AA_bars where barcode='3333'),9);

The statment is the same each time. I had it working with one line using this code
@Echo Off > datanew.sql
For /F "tokens=1-3 delims=,. " %%a in (data.csv) Do (
Echo insert into AA_CIGS ^(code,productid,desciption^) values ^(%%a,^(select productid from AA_BARS where barcode='%%b'^),%%c^); > datanew.sql)

but cannot get it to do all lines of the CSV.
Any help would be great.


See More: Bat file for reading in CSV to SQL

Report •


#1
March 7, 2009 at 06:51:10
echo text>filename.txt
means create/overwrite filename.txt and write string "text" to the file

echo text>>filename.txt
means append string "text" to the end of file. creating the file if not exist.


Report •

#2
March 7, 2009 at 14:22:04
Thanks for that I had a blonde moment there works a treat now.

Report •

Related Solutions


Ask Question