1

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

3
  • be specific, is this file in CSV format? Also please rephrase your question title - it's way too generic. Commented Feb 4, 2011 at 7:26
  • you have asked another question which has far more detail and is to the point. I suggest you simply close this one as too vague and concentrate on your other one which is a lot better: stackoverflow.com/questions/4895725/… Commented Feb 4, 2011 at 8:28
  • I added your reply to the answers also to the question above, and rephrased the title a bit. Commented Feb 5, 2011 at 10:06

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

5 Comments

The task is to automate the process. So have to write a batch script to parse this file , The input will be the attached file and What I all finally need is only the sql query in a single line and output should be in file. The each input file will have an 4 Sql query. So each sql query need to be in new line.
@sudeep: so what you're asking for is an SQL interpreter and extraction engine to be written, which will handle the file you haven't attached and write the extracted data to a file? If that's the case, I don't think you fully understand the complexity of what you're proposing.
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
so what to write in the batch file so that it will take a text file as an input?
@sudeep, see my answer to your other question: stackoverflow.com/questions/4895725/…
0

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.