25. Save, retrieve, and run files using SQLPlus

Welcome to reprint, reprint please indicate the source: blog.csdn.net/notbaron/ar… \

SQL*Plus can hold, retrieve, and run scripts containing SQL*Plus commands and SQL statements.

Figure 1:

支那

支那

Enter the following SQL statement in SQL*Plus:

SQL> selectcustomer_id,first_name,last_name

 2  from customers

 3  where customer_id=1;

 

CUSTOMER_IDFIRST_NAME LAST_NAME

———– ———- ———-

           1 John      Brown

Use the SAVE command to keep the contents of the SQL*Plus buffer in a file named CUST_query.sql

SQL>save cust_query.sql

Created file cust_query.sql

You can view the cust_query.sql file.

Retrieve the contents of the cust_query.sql file using the GET command:

SQL> get cust_query.sql

 1  select customer_id,first_name,last_name

 2  from customers

  3*where customer_id=1

Then use/run the query

SQL> /

 

CUSTOMER_IDFIRST_NAME LAST_NAME

———– ———- ———-

           1 John      Brown

You can load and run the contents of the CUST_query.sql file in one step using the START command:

SQL>start cust_query.sql

CUSTOMER_ID FIRST_NAME LAST_NAME

———– ———- ———-

           1 John      Brown

 

You can EDIT the contents of the SQL*Plus buffer using the EDIT command:

SQL>EDIT

The EDIT command launches the default editor on the operating system. (Set the default edit to vi on the operating system by using the command: export EDITOR=vi, or write DEFINE_EDITOR=’vi’ to login.sql)

SQL> edit

Wrote file afiedt.buf

 1  selectcustomer_id,first_name,last_name

 2  from customers

  3*where customer_id=2

Then use/to run the query that you just modified:

SQL> /

 

CUSTOMER_IDFIRST_NAME LAST_NAME

———– ———- ———-

           2 Cynthia   Green

You can use the SPOOL command to copy the output from SQL*Plus into a file. Use SPOOL OFF to close the command.