我在数据库中有一个表 TEST_TABLE ,我想使用存储过程在其中插入一些值。SP 完成后如何打印插入到 TEST_TABLE 中的行数??...我想使用 UTL_FILE 包执行此操作,并希望生成一个文本文件来监视数据负载。
问问题
430 次
1 回答
0
这是一个基于 HR 模式的示例——我将 DEPARTMENTS 表中的一些行插入到 DEPT_TEST 中。可以检索插入的行数,通过SQL%ROWCOUNT
它返回最后一个 DML 语句(调用 SQL%ROWCOUNT 之前执行的最后一个)处理的行数。
该过程本身有点愚蠢-它“模拟”了我认为您的行为-插入一些行,然后插入更多行,等等。我想UTL_FILE的用法是可以理解的(天哪, FOPEN或FWRITE会做什么?)。我正在将值写入位于我的 EXT_DIR 目录(实际上是 C:\TEMP)中的 TEST.TXT。
请注意 - 如果您还没有它 - 您应该首先创建目录(以 SYS 身份连接)并向将要使用它的用户(在我的情况下为 HR)授予 READ/WRITE 权限。
好的,那么,我们开始:
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>
但是,如果可以的话,我会建议另一种方法 - 登录到数据库。为此,您需要一个日志表和一个使用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.
现在,最初的“插入”过程如下所示:
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 选项,但是 - 最后,这取决于您。
于 2018-01-21T11:55:52.477 回答