网友经验谈之Oracle外部程序触发小结
在论坛里曾经看过一个帖子,有位用户问了这么一个问题:oracle里一个表插入一条数据,提交后,如何让外部程序收到这个消息?
当时没有留意,现在想想好像应该可以总结一下了。举例:
1、过程中:
...
declare
myexceptexception;
inserted_count1number;
inserted_count2number;
...
begin
insertintotablevalues(...);
inserted_count1:=sql%rowcount;
...--可以判断inserted_count1是否大于0来确定是否插入了数据
commit;
inserted_count2:=sql%rowcount;--这里inserted_count2为0
...--事实上,这里一定是提交成功才会执行,否则会跳到exception段
yourprocess(yourpara);--调用你自己的过程或语句跟别的过程通信
...
exception
whenothersthen---你可以根据sqlcode自己去查具体的提交失败原因
...
yourprocess_for_fail(yourpara)--调用你自己的过程跟别的过程通信
...
end;
2、直接外部操作,建触发器:
createorreplacetriggertableafterinsertforeachrow
...
begin
...
yourprocess(yourprara);
...
end;
当然,2的方法这只能更总到提交前,如果要监视提交后状态,你可以在操作表建立标志位或专门建个表,操作表发生变化就通过trigger更新变化信息到状态表,扫描状态表就知道是否commit;成功了
我想这位用户应该通过trigger来调用javasource来实现插入数据后来通知外部程序来做一些事情:
使用javasource的例子如下:
SQL>createorreplaceandcompilejavasourcenamedHelloWorld
2as
3publicclassHelloWorld{
4publicstaticvoiddb_run(){
5System.out.println("HelloWorld");
6}
7}
8/
Javacreated.
SQL>createorreplaceprocedurerun_helloworld
2aslanguagejava
3name'HelloWorld.db_run()';
4/
Procedurecreated.
SQL>setserveroutputonsize5000
SQL>calldbms_java.set_output(5000);
Callcompleted.
SQL>
SQL>execrun_helloworld;
HelloWorld
PL/SQLproceduresuccessfullycompleted.