Data export in Oracle (3)

If I don't want to open the DOS interface to input commands to export data now, what is the simple and convenient way to achieve it?

I will describe an automated operation method in this article, simply put: I put the data script to be queried on a mini on the desktop, and directly export the data result to a text document by double-clicking the shortcut icon. The following is the specific operation to achieve this step, be sure to read it carefully!

1. First, create a notepad on the desktop, and then enter the command of the operating system in the text:

sqlplus /nolog @data.sql
exit

As shown below:

Comment out the two commands:

  • sqlplus is an interactive command-line tool for executing SQL statements or PL/SQL code.
  • /nolog is a parameter of sqlplus, which means to start sqlplus in "no login" mode, that is, not connecting to any database instance, and not specifying any user name and password. The purpose of starting sql*plus here is to use /nolog instead of scott/tiger for security reasons. I don't want others to see the user's password.
  • The "@" symbol can be used to execute a SQL script file, followed by the path and name of the script file. So @data.sql means that the Oracle script file data.sql in the unified directory will be run immediately after sql*plus starts.
  • "exit" means to launch the DOS window. If the exit command is not written in the script, the DOS window will stay on the desktop after executing all the commands. It looks very unsightly. In other words, it is not professional enough, hahaha! ! !

Therefore, the meaning of the sqlplus /nolog @data.sql command is to start sqlplus but not log in to the database, but to read the SQL startup statement from the data.sql file and execute it without connecting to the database .

This method is usually used to execute a series of SQL statements or PL/SQL codes to complete a certain operation (such as data import, data backup, etc.), and can also be used to perform some initialization operations.

Notice:

If data.sql contains statements to connect to the database (such as CONNECT or ALTER SESSION, etc.), these statements will not be successfully executed when using the sqlplus /nolog @data.sql command. At this time, you need to use the sqlplus user name/password command to connect to the database, and then use the @@ or START command to execute the script.

2. Save the text you just edited, and then click File→Save As in the dialog box, choose to save to the previously created D disk SQL folder, and enter the file name in the drop-down box corresponding to the file name, because I want to export dept here The data of the department table, so it can be named: DeptDownLoad.bat , and finally click Save. As shown below:

Remark:

.bat is the suffix name of a batch file under the Windows system. A batch file is a text file containing a series of operation commands, which can be used to automate some common tasks or operating systems or applications.

Batch files are composed of a series of commands and parameters. In the files, loops, conditional statements, variables, etc. can be used to realize complex operation processes. Batch files usually end in .bat and can be run directly by double-clicking .

Batch files can be used for a wide range of purposes, such as automating backup and cleanup operations, performing periodic system maintenance tasks, automatically deploying assemblies or configuration files, and so on. When certain commands or operations need to be executed repeatedly, batch files can make these operations more efficient and convenient.

Notice:

Batch files can be run in the command prompt window. Improperly written commands in the file may cause harm to the system. Therefore, when writing batch files, you need to pay attention to safety and correctness to avoid data loss or system failure caused by wrong operations. collapse.

3. In order for the system to automatically open the data.sql file, I need to enter the D:\SQL folder, then right-click the data.sql script file , and select the [Edit] command in the pop-up shortcut menu, as shown in the figure:

4. Enter the SQL*PLUS command in the first and last lines of the original command script:

connect scott/tiger    --第一行输入
.
.
.
exit                   --最后一行输入

The details are shown in the figure below:

Remark:

The 'connect scott/tiger' command is the command used in Oracle Database to connect to a database instance. Where 'scott' and 'tiger' are the username and password used to authenticate the database login. In actual use, we can replace it with other valid user names and passwords to complete the connection to the database.

Notice:

  1. In order for the connection to succeed, the command must be permitted by the privileges of the executor. In addition, in some versions of the Oracle database, the SYSTEM and SYS users are disabled by default to directly log in to the Oracle instance through SQLPLUS, so this command may prompt that the connection cannot be made. If you need to log in as a SYSTEM or SYS user, you need additional authorization or use other methods to log in, such as connecting with a DBA user and switching the login user through the su command. We can also specify the connection information of the database instance by configuring the tnsnames.ora file, so that we can directly use tools such as sqlplus and Oracle client to connect to the database without using the connect command to manually enter the user name and password.
  2. The exit command must be added, otherwise the SQL*PLUS interface will remain on the desktop, so that other users will clearly see our user password.

The last thing is to save the script file you just edited!

5. In order for us to know whether we have successfully exported the data in the future, it is recommended to delete the existing data.txt data file first.

6. Right-click the DOS batch file DeptDownLoad.bat, and select [Send to] → [Desktop Shortcut] in the pop-up shortcut menu, as shown in the figure:

We send the icon of the file to the main desktop of the computer, as shown in the following figure:

7. In order to show that we are professional, directly modify the icon on the desktop just now. The operation steps are: right-click the desktop icon, select [Properties] command in the shortcut menu, and a dialog box will pop up, as shown in the figure below:

 8. Click the [Change Icon] button to open the [Change Icon] dialog box, select an icon you like, here I choose a [ × ], as shown in the figure below:

 Click [OK], then click [Apply], and finally [OK] to complete the icon replacement, as shown in the following figure:

9. In order to display more professionally, I changed the file name again. Right-click the icon, select [Rename], I named it [ Delete Library and Run ] ( it is not recommended to do this in daily life, the naming must be standardized to meet your work needs ), as shown in the following figure:

10. In the end, it looks quite professional in this way, and the rows are full! ! ! Now double-click the [Delete Library and Run] icon to export the data of the dept department table in the Oracle database.

11. In order to verify whether my previous execution successfully exported the data results, I entered the D:\SQL folder and found a data.txt file, as shown in the figure below:

Then double-click to open it, check the data content inside, and make sure it is the result I need, then the data export is a perfect success. The result is shown in the figure below:

 Summarize:

Through the description of the above steps, it can be clearly found that this method can export the data results we often need more quickly and conveniently, without entering the DOS interface to execute the spool command script.

The next article will describe how to import the output data results into the excel table? Stay tuned!

Guess you like

Origin blog.csdn.net/m0_71406734/article/details/131019759