oracle中print,oracle数据库存储过程的运用之print_table

create or replace

procedure print_table

( p_query in varchar2,

p_date_fmt in varchar2 default 'dd-mon-yyyy hh24:mi:ss' )

-- this utility is designed to be installed ONCE in a database and used

-- by all. Also, it is nice to have roles enabled so that queries by

-- DBA's that use a role to gain access to the DBA_* views still work

-- that is the purpose of AUTHID CURRENT_USER

AUTHID CURRENT_USER

is

l_theCursor integer default dbms_sql.open_cursor;

l_columnValue varchar2(4000);

l_status integer;

l_descTbl dbms_sql.desc_tab;

l_colCnt number;

l_cs varchar2(255);

l_date_fmt varchar2(255);

-- small inline procedure to restore the sessions state

-- we may have modified the cursor sharing and nls date format

-- session variables, this just restores them

procedure restore

is

begin

if ( upper(l_cs) not in ( 'FORCE','SIMILAR' ))

then

execute immediate

'alter session set cursor_sharing=exact';

end if;

if ( p_date_fmt is not null )

then

execute immediate

'alter session set nls_date_format=''' || l_date_fmt || '''';

end if;

dbms_sql.close_cursor(l_theCursor);

end restore;

begin

-- I like to see the dates print out with times, by default, the

-- format mask I use includes that. In order to be "friendly"

-- we save the date current sessions date format and then use

-- the one with the date and time. Passing in NULL will cause

-- this routine just to use the current date format

if ( p_date_fmt is not null )

then

select sys_context( 'userenv', 'nls_date_format' )

into l_date_fmt

from dual;

execute immediate

'alter session set nls_date_format=''' || p_date_fmt || '''';

end if;

-- to be bind variable friendly on this ad-hoc queries, we

-- look to see if cursor sharing is already set to FORCE or

-- similar, if not, set it so when we parse -- literals

-- are replaced with binds

if ( dbms_utility.get_parameter_value

( 'cursor_sharing', l_status, l_cs ) = 1 )

then

if ( upper(l_cs) not in ('FORCE','SIMILAR'))

then

execute immediate

'alter session set cursor_sharing=force';

end if;

end if;

-- parse and describe the query sent to us. we need

-- to know the number of columns and their names.

dbms_sql.parse( l_theCursor, p_query, dbms_sql.native );

dbms_sql.describe_columns

( l_theCursor, l_colCnt, l_descTbl );

-- define all columns to be cast to varchar2's, we

-- are just printing them out

for i in 1 .. l_colCnt loop

if ( l_descTbl(i).col_type not in ( 113 ) )

then

dbms_sql.define_column

(l_theCursor, i, l_columnValue, 4000);

end if;

end loop;

-- execute the query, so we can fetch

l_status := dbms_sql.execute(l_theCursor);

-- loop and print out each column on a separate line

-- bear in mind that dbms_output only prints 255 characters/line

-- so we'll only see the first 200 characters by my design...

while ( dbms_sql.fetch_rows(l_theCursor) > 0 )

loop

for i in 1 .. l_colCnt loop

if ( l_descTbl(i).col_type not in ( 113 ) )

then

dbms_sql.column_value

( l_theCursor, i, l_columnValue );

dbms_output.put_line

( rpad( l_descTbl(i).col_name, 30 )

|| ': ' ||

substr( l_columnValue, 1, 200 ) );

end if;

end loop;

dbms_output.put_line( '-----------------' );

end loop;

-- now, restore the session state, no matter what

restore;

exception

when others then

restore;

raise;

end;

SQL> set serverout on size 100000

SQL> select * from a;

ID COL

------ -----

1 AA

2 bb

3 cc

SQL> exec print_table('select * from a');

ID : 1

COL : AA

-----------------

ID : 2

COL : bb

-----------------

ID : 3

COL : cc

-----------------

PL/SQL procedure successfully completed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,可以实现从 A 数据库读取表数据分批插入到 B 数据库: ```python import cx_Oracle import threading # A 数据库连接信息 A_HOST = 'localhost' A_PORT = 1521 A_SID = 'ORCL' A_USER = 'user' A_PASSWORD = 'password' # B 数据库连接信息 B_HOST = 'localhost' B_PORT = 1521 B_SID = 'ORCL' B_USER = 'user' B_PASSWORD = 'password' # 分批读取的大小 BATCH_SIZE = 1000 def read_from_a(start): try: conn_a = cx_Oracle.connect(f'{A_USER}/{A_PASSWORD}@{A_HOST}:{A_PORT}/{A_SID}') cursor_a = conn_a.cursor() cursor_a.execute(f"SELECT * FROM table_a WHERE id >= {start} ORDER BY id ASC") rows = cursor_a.fetchmany(BATCH_SIZE) while rows: insert_to_b(rows) start += BATCH_SIZE rows = cursor_a.fetchmany(BATCH_SIZE) except cx_Oracle.Error as error: print('Error:', error) finally: cursor_a.close() conn_a.close() def insert_to_b(rows): try: conn_b = cx_Oracle.connect(f'{B_USER}/{B_PASSWORD}@{B_HOST}:{B_PORT}/{B_SID}') cursor_b = conn_b.cursor() cursor_b.executemany("INSERT INTO table_b VALUES (:1, :2, :3)", rows) conn_b.commit() except cx_Oracle.Error as error: print('Error:', error) finally: cursor_b.close() conn_b.close() if __name__ == '__main__': start = 0 threads = [] for i in range(10): t = threading.Thread(target=read_from_a, args=(start,)) threads.append(t) t.start() start += BATCH_SIZE for t in threads: t.join() ``` 这个示例代码定义了两个函数,`read_from_a()` 和 `insert_to_b()`,分别用于从 A 数据库读取数据和向 B 数据库插入数据。在 `read_from_a()` 函数,使用 `fetchmany()` 方法分批读取数据,并调用 `insert_to_b()` 函数插入到 B 数据库。在主函数创建 10 个线程,每个线程从 A 数据库读取一批数据并插入到 B 数据库。 需要注意的是,这个示例代码仅作为参考,具体实现还需要根据实际场景进行调整,例如需要根据数据量大小和系统资源等情况确定分批大小,还需要进行线程间同步和互斥等处理。同时,对于数据库连接等资源,也需要进行合理的管理和释放,以避免资源浪费和泄漏等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值