| |
【赛迪网-it技术报道】使用ruby脚本调用oracle存储过程的示例:
1.首先创建oracle存储过程test:
sql> create or replace procedure test(p1 in varchar2,p2 out varchar2 ) is
2 begin
3 select p1||' procedure executed!' into p2 from dual ;
4 end;
5 /
procedure created
|
2.再写ruby脚本,调用存储过程test,脚本内容如下,将脚本保存为:call_proc_test.rb:
require 'dbi'
db_read_str = 'begin test(?, ?); end;'
dbh = dbi.connect('dbi:oci8:tnsdbname', 'username', 'password')
sth_db = dbh.prepare(db_read_str)
sth_db.bind_param(1, 'test:',''*50) # allow for up to 50 chars
sth_db.bind_param(2, ' ' * 100) # allow for up to 100 chars
sth_db.execute
str = sth_db.func(:bind_value, 2)
puts str
dbh.disconnect
|
3.检查ruby语法错误:
c:\>ruby -cw call_proc_test.rb
syntax ok
c:\>
|
4.最后执行ruby脚本:
c:\>ruby call_proc_test.rb
test: procedure executed!
c:\>
|
注释:如果是windows环境下,大家也可以通过双击call_proc_test.rb 文件来运行ruby脚本。
|
|