Oracle database is a collection of data which treated as a unit. Oracle Database is the first database designed for enterprise grid computing which supports to connect hardware and software components on demand to meet the changing needs of businesses.
- Command-line user interface
- Windows Graphical User Interface (GUI)
- iSQL*Plus web-based user interface
In this article, I am focusing on how can we interact with the Oracle database using the Command-line user interface.
How to connect to the Oracle database using the SQL*Plus command line?
Sample connection String
sqlplus 'admin/admin123@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=orclmdb.o2.com)(Port=1521))(CONNECT_DATA=(SERVICE_NAME=EMP)))'
How to retrieve information in CSV format using the SQL*Plus command line?
How to export data from Oracle Database using SQL*Plus command line?
set colsep ,
set headsep off
set pagesize 0
set trimspool on
set linesize #
set numwidth #
Let's get an brief idea about these setting parameter.
- colsep : the separator character used to split the columns. For a .csv file, this is a simple comma.
- Headsep: the separator character for the header row. In this example we are not outputting the header row and because of that its value is set as off.
- Pagesize: Specifies the number of lines per page. In this Pagesize value is set to 0 since the output is send to a file
- Trimspool: use to remove trailing whitespace.
- Linesize : the # value should be the total number of output columns in the resulting query.
- numwidth : is the column width (number of character spaces) used when outputting numeric values.
As the next step, we need to insert the spool command. The spool command is used to store the output of any query to the specified file. Since I need to save the Employee table content into a CSV file named employee I added the following command.
You can save the file content and exit by pressing Esc and type :wq! And Enter.
Now you can execute that script by running the following command in the SQL*Plus command line.
@employee
As the result of that execution, you could find a file named employee-data.csv in your working directory with all the content in the employee table.
No comments:
Post a Comment