Reputation: 93
how to write a batch script which will take a file as an input, then it will perform a sql query on that file and give a file as an output.
input will be a textfile which has 4 query in it. Now a batch file is to be written which will take 1 query at a time and execute it and output will be stored in a file. So there will be 4 seperate output file for 4 query
Upvotes: 1
Views: 6755
Reputation: 15232
You don't specify which sql server you are using, in this example I will use firebird. If you use a different sql server, you have to use the correct sql commandline tool and syntax. firebird uses isql.exe.
Asuming I have the following text file "input.sql" containing 4 sql commands:
select * from CUSTOMER;
select * from DEPARTMENT;
select * from EMPLOYEE;
select * from SALES;
Then this batchfile will execute each command using isql.exe, and creates a seperate output file for each command:
@echo off
set sql_exe="C:\Program Files\Firebird\Firebird_2_5\bin\isql.exe"
set sql_options=-u sysdba -p masterkey
set sql_db="C:\Program Files\Firebird\Firebird_2_5\examples\empbuild\EMPLOYEE.FDB"
set count=1
for /f "delims=" %%a in (input.sql) do (
echo %%a > temp.sql
call :processtemp_sql
)
goto :eof
:processtemp_sql
%sql_exe% %sql_options% -i temp.sql -o output%count%.txt %sql_db%
set /A count=%count%+1
goto :eof
:eof
at the end output1.txt..output4.txt are created. Each file contains the output of one sql command.
Upvotes: 2
Reputation: 1897
I think what you're looking for is Microsoft Log Parser 2.2. It allows you to execute SQL queries on a a number of file types, including logs, CSV and XML files.
Upvotes: 0
Reputation: 881123
You don't often perform SQL queries on a normal file, it generally requires a DBMS (database management system) at the other end for interpreting the SQL and extracting the relevant data.
Writing a batch file is relatively easy, be it a UNIX shell like bash
or the Windows cmd.exe
.
But, if you're thinking about implementing a full blown SQL query language that operates on text files (or any non-database file, really), that's probably going to take more then one question on Stack Overflow :-)
Perhaps you could flesh out your question with a little more detail, given that we may have misunderstood your requirements.
Upvotes: 0