Skip to content
Home » Oracle Database » How to Execute SQL Statements in One CMD Line

How to Execute SQL Statements in One CMD Line

There're 4 ways to run SQL in just one command line.

Run One Statement

If your query contains just only one statement, you can do this:

C:\Users\ed>echo select name from v$database; | sqlplus scott/tiger@orcl

When the query is finished, the command will exit sqlplus automatically.

If your query contains several statements in a file, you can execute it (e.g. run_check.sql) like the following commands:

Run SQL File

The syntax is directly supported by sqlplus.

C:\Users\ed>sqlplus sys/password@primdb_scan as sysdba @run_check.sql

By this method, you have to add "exit" in your SQL script to make itself leave sqlplus.

Dump SQL File

Here we dump the entire file into sqlplus command.

C:\Users\ed>sqlplus scott/tiger@orcl < run_check.sql

This will exit sqlplus automatically.

Output SQL File

Here we print the entire file and pipe the output into sqlplus command.

C:\Users\ed>echo @run_check.sql | sqlplus scott/tiger@orcl

This will exit sqlplus automatically.

Leave a Reply

Your email address will not be published. Required fields are marked *