tags:

views:

933

answers:

5

Is there any way to have a Windows batch file directly input SQL statements without calling a script? I want the batch file to login to SQL and then enter in the statements directly.

EDIT: I'm using Oracle v10g

+2  A: 

For a single command you can use this trick:

echo select * from dual; | sqlplus user/pw@db
Dave Costa
+2  A: 

Short answer: No. Batch files by themselves can't do this.

Long answer: You may be able to come close, depending on which kind of database server you're using, and what the capabilities the commandline client provides.

What kind of database server are you using? Oracle, mySql, Sybase, Microsoft, Terradata, ???

For example, with a Sybase database, you can use the isql commandline client to run from a batch file:

isql -S server -D database -U user -P password -i script
Bevan
+2  A: 

To run something on SQL server 2005/2008, you could use sqlcmd command line utility. sqlcmd -h prints the list of switches.

Mehrdad Afshari
+1  A: 

You could use sqlcmd (for sql server) or System.Data.Odbc.OdbcCommand from powershell.

Thuglife
A: 

The odbc command of the outwit tool suite allows you to run select statements on any database for which an appropriate ODBC data source has been defined.

Diomidis Spinellis