使用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腳本。