Oracle对操作系统文件的读写操作
在SQL*Plus中可以对操作系统中的文本文件进行简单的读写访问。事先将SQL语句或者PL/SQL块的代码存放在文本文件中,再把文本文件调
在SQL*Plus中可以对操作系统中的文本文件进行简单的读写访问。
例如,事先将SQL语句或者PL/SQL块的代码存放在文本文件中,再把文本文件调入缓冲区中,使之执行。
或者把当前缓冲区中的内容保存到一个文件中, 或者把SQL语句、PL/SQL块的执行结果保存到文件中。
1.读文件涉及的命令包括@、get、start等命令。
1.1 @命令的作用是将指定的文本文件的内容读到缓冲区中,并执行它。文本文件可以是本地文件,也可以是远程服务器上的文件。
如果是本地文件,@命令的命令的执行格式为:@文件名
这里的文件名要指定完整的路径,默认的扩展名是.sql,如果脚本文件使用了默认的扩展名,则在@命令中可以省略扩展名。
如果是远程文件,必须将它存放到一个web服务器上,并以HTTP或FTP方式访问。这时@命令的命令的执行格式为(以HTTP为例):@服务器/文件名
使用@命令读取文件时,文件中可以包含多条SQL语句,每条语句以分号结束;或者可以包含一个PL/SQL块。
文件被读入缓冲区中以后,SQL*Plus将按顺序执行文件中的代码,并将执行结果输出到显示器上。
例如,假设在/home/Oracle目录下有一个文件,名为a.sql,文件的内容为:
SELECT ename FROM emp WHERE empno=7902; SELECT dname FROM dept WHERE deptno=10;
现在希望通过@命令将这个文件读到缓冲区中,命令执行的执行格式如下:SQL> @/home/oracle /a
@命令还有一个用法,就是在启动SQL*Plus的同时,将指定的文件读入缓冲区并执行它。
这时@命令和文件名一起作为SQL*Plus的命令行参数,格式如下:sqlplus 用户名/口令 @文件名
注意,这种格式与以前提到的使用网络服务的格式是很相似的,
sqlplus 用户名/口令 @文件名
sqlplus 用户名/口令@网络服务名
但是仍然有区别,请注意观察:
sqlplus 用户名/口令@网络服务名 由于文件名和网络服务名都表现为字符串,所以单纯从名字上无法区分到底使用了文件名还是网络服务名。
二者的区别在于第一种格式中在用户名/口令之后有一个空格,这时将把后面的参数解释为一个文件,并把这个文件加载到缓冲区中。
在第二种格式中,用户名/口令之后没有空格,这时将后面的参数解释为网络服务名。
1.2 get命令的作用与@命令相似,但是它只是把文件加载到缓冲区中,并不直接执行。
get命令的的执行格式为:get 文件名 选项
其中文件名的默认扩展名为.sql,在get命令中可以省略。目前get命令只支持本地的操作系统文件。
可以使用的选项有两个:LIST和NOLIST。
其中LIST选项指定将文件的内容读到缓冲区的同时,还要在显示器上输出,,这是默认选项。
选项NOLIST使得文件的内容不在显示器上输出。
使用get命令时还要注意,在文本文件中只能包含一条SQL语句,而且不能以分号结束。也可以只包含一个PL/SQL块,块以分号结束。
在使用@和get命令时要注意这些格式上的差别。例如,假设在/home/oracle目录下有一个文件,名为b.sql,文件的内容为:
SELECT ename FROM emp WHERE empno=7902 现在先通过get命令把它读入缓冲区,然后执行/命令使之执行:
SQL> get /home/oracle/b 1* SELECT ename FROM emp WHERE empno=7902 SQL> / ENAME ---------- FORD
1.3 start命令与@命令是等价的,这里不再赘述。
2.写文件涉及的命令包括save和spool。
其中save命令用于将当前缓冲区中的内容写入一个操作系统文件,而spool命令用于将命令的执行结果输出到一个操作系统文件。
2.1 save命令的格式为:SQL>save 文件名 选项
其中选项指定以什么样的方式写文件。可以使用的选项有以下三个:
CREATE 如果文件不存在,则创建。否则,命令执行失败。
APPEND 如果文件不存在,则创建。否则,在文件末尾追加。
REPLACE 如果文件不存在,则创建。否则删除原文件,重新创建。
如果不指定完整的路径,则在当前目录下产生这个文件,文件的默认扩展名是.sql。
例如,假设当前缓冲区中有一条SELECT语句,使用save命令可以将这条语句写入文件:
SQL> list 1* SELECT * FROM emp
SQL> save /home/oracle/aa
2.2 spool命令利用假脱机技术,用于将SQL*Plus的输出写入到文件中,它有以下几种用法:
spool 得到当前spool的状态,默认为不可用。
spool 文件名 启动spool,并打开指定的文件。
spool off 关闭spool,并将SQL*Plus的输出写入文件中。
spool out 关闭spool,将SQL*Plus的输出写入文件中,并同时送往打印机。
如果在SQL*Plus中以命令行的方式执行spool命令,那么从执行spool命令并打开文件开始,此后的所有输出,包括错误信息,以及用户的键盘输入,都将写入指定的文件,直到遇到“spool off”或者“spool out”。
但是这些信息的写入是一次性完成,即在执行“spool off”或者“spool out”的一瞬间,这些信息才一次全部写入文件,包括最后执行的“spool off”或者“spool out”命令本身。
文件的默认扩展名为.LST,默认的路径是当前目录。
spool命令通常的用法是生成报表。
首先将精心设计的SQL语句存放在一个文件中,在产生输出的语句前后加上spool命令,然后将这个文件读到缓冲区中执行。
这样在写入的文件中只有命令执行的结果,而不包括SQL语句本身。
例如,假设当前目录下有一个文件,名为c.sql,它的内容为:
spool cc SELECT ename,sal FROM emp WHERE deptno=10; spool off 现在将这个文件读到缓冲区中,并使之执行,执行结果如下:
SQL> @c
ENAME SAL CLARK 2450 KING 5000 MILLER 1300
文件中SQL语句的执行结果显示在屏幕上,同时在当前目录下生成了文件cc.LST,文件的内容与屏幕上显示的结果完全一致。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.
