dbaccess mydb sql_file.sql
Or if the SQL commands already include a database connection statement:
dbaccess - sql_file.sql
If you wish to enter SQL statements interactively without the menu, enter
dbaccess - -
Suppose you want to integrate a series of SQL statements within a UNIX shell script or Windows batch file. A simple method would be to pipe commands one at a time into dbaccess, e.g.:
echo select col1 from tab1 | dbaccess mydb
Or you could chain a series of SQL statements together to send to dbaccess. This is a typical methodwe'd use to create a self-contained bug reproduction. An engineer can then reproduce the problem byexecuting the shell script.
Here's a UNIX shell script that creates a database, inserts data, queries a table, and drops thedatabase:
#!/bin/shdbaccess - <<EOFcreate database test;create table t1(col1 serial, col2 char(20));insert into t1 values(0, "hello");select * from t1;close database;drop database test;EOF
Can this be done with a Windows batch file? Yes.
Here's the equivalent batch file:
echo create database test; ^create table t1(col1 serial, col2 char(20)); ^insert into t1 values(0, "hello"); ^select * from t1; ^close database; ^drop database test | dbaccess -Although it lacks the power of UNIX shells I can't help enjoying the simplicity of the Windows command language - 10 commands are easy to remember.
While I'm on the subject of integrating shell scripts with SQL, it's worth mentioningMarco Greco's Structured Query Scripting Language (SQSL). An open source project that provides a method of integrating SQL statements withreporting and formatting commands.