Turn: sqlplus non-interactive use

Non-interactive use of sqlplus

 

 

 

1 Problems with the sqlplus interactive interface

sqlplus is the most important official command line client software for the ORACLE database. It is an essential tool for DBA, and it can be used to complete almost all management tasks. However. The interactive interface of sqlplus is not very friendly, and there is no historical command record function when inputting commands. The default output is even more unsightly.

In terms of ease of use, sqlplus is indeed a lot worse than mysqlclient.

Since there are so many inconveniences in the sqlplus interactive mode. Then it is better to use it directly in non-interactive mode, and then use the shell toolset provided by the operating system to assist in completing more complex tasks. sqlplus itself supports non-interactive use, and supports it well, in line with the general Unix design philosophy: read commands from standard input. Write the result to standard output .

2 Two non-interactive ways of using sqlplus

In non-interactive mode, sqlplus can provide sql commands from two places: one is through external files, but through standard input.

 

2.1 Via external command file

sqlplus username/password@serverIP/侦听服务名 @命令文件名称

An example is given below. 
The command file name is 1.sql. The content is as follows:

select count(*) from dba_objects;
exit;
  • 1
  • 2

The run command is:

sqlplus sys/***@172.16.2.190/xgdb.db001.xigang @1.sql

At this point, the result will be output when the operation is completed. 
such an external command file. A separate physical file needs to be created to store the commands. Suitable for high-volume administrative tasks that are frequently used.

2.2 Via standard input

Since sqlplus reads commands from standard input, it can pipe commands to it.

 

echo 命令 | sqlplus username/password@serverIP/侦听服务名

This method does not need to include exit; in the command, because sqlplus automatically exits after running. Here is an example:

echo 'select count(*) from dba_objects;' | sqlplus sys/***@172.16.2.190/xgdb.db001.xigang as sysdba

After this operation, the result is output to the screen. Because the sql statement is the parameter of the bash command. And the entire command line can be called repeatedly through bash's convenient history. It is also convenient to use the up and down arrows to repeatedly use the sql statement that has been run.

3 Use pipelines for possible processing

Since sqlplus outputs the results to the standard output, you can use the pipeline and word processing tools such as sed and awk to get the desired output.

echo -e 'set pagesize 0\nselect table_name,owner from dba_tables;' | sqlplus -S sys/***@172.16.2.190/xgdb.db001.xigang as sysdba | awk '{printf("%-10d%-30s%-20s\n",NR,$1,$2);}'

4 Points to pay attention to

When using an external file, the commands in the file are exactly the same as for interactive use, no need to worry.

  • When using echo + pipe input, avoid bash metacharacters. Be sure to use single or double quotes to protect the sql command.

    When there are single quotes in the sql command. Double quote protection should be selected. Such as

    echo  "insert into t1(name) values('奥巴马');" | sqlplus ...
    
  • Because $ is also treated as a metacharacter in double quotes. So it needs to be escaped

    echo "select * from v\$nls_valid_values where parameter='LANGUAGE';" | sqlplus ...
    
  • In addition, sqlplus does not agree to mix the sqlplus command and the sql command on one line.

    And the sql command must end with ;. This must use the echo -e option to enable the interpretation of "\n" as a newline.

    for example

    echo -e 'set pagesize 0\nset linesize 100\nselect * from dba_objects;' | sqlplus ...
    

5 A simple script

Although the above command line method can conveniently call the executed SQL statement through the history function of bash, there are a lot of other contents besides the SQL statement in the command line, which is cumbersome. so. It is convenient to write a small script (the file name is sql), such as the following:

#!/bin/bash -
if [ $# -ne 1 ]
then
    echo "Usage: $0 'SQL statement'"
else
    SQL="set pagesize 0\nset linesize 300\n$1"
    echo -e "$SQL"
    echo -e "$SQL" | sqlplus -S sys/***@172.16.2.190/xgdb.db001.xigang as sysdba
fi
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

It is called like the following.

sql 'select * from dba_objects;'

It looks so clean. And it will not write down ORACLE's account information in the history command, which is relatively safe.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326163260&siteId=291194637