如何在 Oracle 10g 中使用 UTL_FILE 打印在 table 中插入的行数?
How do I print number of rows inserted in a table using UTL_FILE in Oracle 10g?
我在数据库中有一个 table TEST_TABLE,我想使用存储过程在其中插入一些值。 SP 完成后如何打印插入 TEST_TABLE 中的行数??...我想使用 UTL_FILE 包执行此操作并希望生成一个文本文件来监视数据加载。
这是一个基于 HR 架构的示例 - 我将部门 table 中的一些行插入到 DEPT_TEST 中。可以通过 SQL%ROWCOUNT
检索插入的行数,其中 returns 最后一个 DML 语句处理的行数(调用 SQL%ROWCOUNT 之前最后执行的)。
程序本身有点愚蠢 - 它"simulates" 我认为你的做法 - 插入一些行,然后插入更多行,等等。UTL_FILE 用法是可以理解的,我想(哎呀,FOPEN 或 FWRITE 会做什么?)。我正在将值写入位于我的 EXT_DIR 目录(实际上是 C:\TEMP)的 TEST.TXT。
请注意 - 如果您还没有它 - 您应该首先创建目录(以 SYS 身份连接)并授予 READ/WRITE 权限给将要使用它的用户(在我的例子中是 HR)。
好的,我们开始吧:
SQL> show user
USER is "SYS"
SQL> create directory ext_dir as 'c:\temp';
Directory created.
SQL> grant read, write on directory ext_dir to hr;
Grant succeeded.
SQL> connect hr/hr@xe
Connected.
SQL> create table dept_test as select * From departments where 1 = 2;
Table created.
SQL> declare
2 l_file utl_file.file_type;
3 begin
4 l_file := utl_file.fopen('EXT_DIR', 'test.txt', 'w');
5
6 insert into dept_test (department_id, department_name, manager_id, location_id)
7 select department_id, department_name, manager_id, location_id
8 from departments
9 where department_id between 0 and 150;
10 utl_file.put_line(l_file, to_char(sysdate, 'hh24:mi:ss') ||
11 ': Inserted ' || sql%rowcount || ' rows');
12
13 insert into dept_test (department_id, department_name, manager_id, location_id)
14 select department_id, department_name, manager_id, location_id
15 from departments
16 where department_id between 151 and 300;
17 utl_file.put_line(l_file, to_char(sysdate, 'hh24:mi:ss') ||
18 ': Inserted ' || sql%rowcount || ' rows');
19
20 utl_file.fclose(l_file);
21 end;
22 /
PL/SQL procedure successfully completed.
SQL> $type c:\temp\test.txt
12:32:51: Inserted 15 rows
12:32:51: Inserted 12 rows
SQL>
但是,如果可以的话,我会建议另一种方法 - 登录数据库。为此,您需要一个日志 table 和一个使用 autonomous_transaction
pragma 的过程(以便您可以提交插入,而不影响调用者执行的事务)。
SQL> CREATE TABLE a1_log
2 (
3 id NUMBER,
4 datum DATE,
5 desc VARCHAR2 (200)
6 );
Table created.
SQL> CREATE SEQUENCE seqlog START WITH 1 INCREMENT BY 1;
Sequence created.
SQL> CREATE OR REPLACE PROCEDURE a1_p_log (par_description IN VARCHAR2)
2 IS
3 PRAGMA AUTONOMOUS_TRANSACTION;
4 BEGIN
5 INSERT INTO a1_log
6 SELECT seqlog.NEXTVAL, SYSDATE, par_description FROM DUAL;
7
8 COMMIT;
9 END a1_p_log;
10 /
Procedure created.
现在,最初的 "inserting" 过程如下所示:
SQL> begin
2 insert into dept_test (department_id, department_name, manager_id, location_id)
3 select department_id, department_name, manager_id, location_id
4 from departments
5 where department_id between 0 and 150;
6 a1_p_log('Inserted ' || sql%rowcount || ' rows');
7
8 insert into dept_test (department_id, department_name, manager_id, location_id)
9 select department_id, department_name, manager_id, location_id
10 from departments
11 where department_id between 151 and 300;
12 a1_p_log('Inserted ' || sql%rowcount || ' rows');
13 end;
14 /
PL/SQL procedure successfully completed.
SQL> col description format a40
SQL> alter session set nls_date_format = 'hh24:mi:ss';
Session altered.
SQL> select * From a1_log;
ID DATUM DESCRIPTION
---------- -------- ----------------------------------------
1 12:52:08 Inserted 15 rows
2 12:52:08 Inserted 12 rows
SQL>
在我看来,这是一个更简单的选项,我建议您使用它而不是 UTL_FILE 选项,但是 - 最后,这取决于您。
我在数据库中有一个 table TEST_TABLE,我想使用存储过程在其中插入一些值。 SP 完成后如何打印插入 TEST_TABLE 中的行数??...我想使用 UTL_FILE 包执行此操作并希望生成一个文本文件来监视数据加载。
这是一个基于 HR 架构的示例 - 我将部门 table 中的一些行插入到 DEPT_TEST 中。可以通过 SQL%ROWCOUNT
检索插入的行数,其中 returns 最后一个 DML 语句处理的行数(调用 SQL%ROWCOUNT 之前最后执行的)。
程序本身有点愚蠢 - 它"simulates" 我认为你的做法 - 插入一些行,然后插入更多行,等等。UTL_FILE 用法是可以理解的,我想(哎呀,FOPEN 或 FWRITE 会做什么?)。我正在将值写入位于我的 EXT_DIR 目录(实际上是 C:\TEMP)的 TEST.TXT。
请注意 - 如果您还没有它 - 您应该首先创建目录(以 SYS 身份连接)并授予 READ/WRITE 权限给将要使用它的用户(在我的例子中是 HR)。
好的,我们开始吧:
SQL> show user
USER is "SYS"
SQL> create directory ext_dir as 'c:\temp';
Directory created.
SQL> grant read, write on directory ext_dir to hr;
Grant succeeded.
SQL> connect hr/hr@xe
Connected.
SQL> create table dept_test as select * From departments where 1 = 2;
Table created.
SQL> declare
2 l_file utl_file.file_type;
3 begin
4 l_file := utl_file.fopen('EXT_DIR', 'test.txt', 'w');
5
6 insert into dept_test (department_id, department_name, manager_id, location_id)
7 select department_id, department_name, manager_id, location_id
8 from departments
9 where department_id between 0 and 150;
10 utl_file.put_line(l_file, to_char(sysdate, 'hh24:mi:ss') ||
11 ': Inserted ' || sql%rowcount || ' rows');
12
13 insert into dept_test (department_id, department_name, manager_id, location_id)
14 select department_id, department_name, manager_id, location_id
15 from departments
16 where department_id between 151 and 300;
17 utl_file.put_line(l_file, to_char(sysdate, 'hh24:mi:ss') ||
18 ': Inserted ' || sql%rowcount || ' rows');
19
20 utl_file.fclose(l_file);
21 end;
22 /
PL/SQL procedure successfully completed.
SQL> $type c:\temp\test.txt
12:32:51: Inserted 15 rows
12:32:51: Inserted 12 rows
SQL>
但是,如果可以的话,我会建议另一种方法 - 登录数据库。为此,您需要一个日志 table 和一个使用 autonomous_transaction
pragma 的过程(以便您可以提交插入,而不影响调用者执行的事务)。
SQL> CREATE TABLE a1_log
2 (
3 id NUMBER,
4 datum DATE,
5 desc VARCHAR2 (200)
6 );
Table created.
SQL> CREATE SEQUENCE seqlog START WITH 1 INCREMENT BY 1;
Sequence created.
SQL> CREATE OR REPLACE PROCEDURE a1_p_log (par_description IN VARCHAR2)
2 IS
3 PRAGMA AUTONOMOUS_TRANSACTION;
4 BEGIN
5 INSERT INTO a1_log
6 SELECT seqlog.NEXTVAL, SYSDATE, par_description FROM DUAL;
7
8 COMMIT;
9 END a1_p_log;
10 /
Procedure created.
现在,最初的 "inserting" 过程如下所示:
SQL> begin
2 insert into dept_test (department_id, department_name, manager_id, location_id)
3 select department_id, department_name, manager_id, location_id
4 from departments
5 where department_id between 0 and 150;
6 a1_p_log('Inserted ' || sql%rowcount || ' rows');
7
8 insert into dept_test (department_id, department_name, manager_id, location_id)
9 select department_id, department_name, manager_id, location_id
10 from departments
11 where department_id between 151 and 300;
12 a1_p_log('Inserted ' || sql%rowcount || ' rows');
13 end;
14 /
PL/SQL procedure successfully completed.
SQL> col description format a40
SQL> alter session set nls_date_format = 'hh24:mi:ss';
Session altered.
SQL> select * From a1_log;
ID DATUM DESCRIPTION
---------- -------- ----------------------------------------
1 12:52:08 Inserted 15 rows
2 12:52:08 Inserted 12 rows
SQL>
在我看来,这是一个更简单的选项,我建议您使用它而不是 UTL_FILE 选项,但是 - 最后,这取决于您。