一个不错的存储过程,用到了不少知识:
自定义table类型,游标,异常。
这样的存储过程却是能够解决开发中的很多问题,但是问题来了....
phoneNum_message_collection := t_dept_phoneNum_message_table(); --实例对象
调用 phoneNum_message_collection
怎么调用啊,目前来讲没有找到答案。大家群策群力吧!
CREATE OR REPLACE TYPE t_dept_phoneNum_message_type AS OBJECT(
cell_phone varchar2(30) , --根据业务需要要获取的cell_phone 号码
message varchar2(1000) --组织获取短信内容
)
create or replace type t_dept_phoneNum_message_table as table of t_dept_phoneNum_message_type
---特别注意,上面的命令一定需要和下面的存储过程分开执行。决不可一起执行!
create or replace procedure pro_borrow_alarm_first(phoneNum_message_collection out t_dept_phoneNum_message_table) is
cellphonenumber VARCHAR2(30);
message varchar2(1000);
sp_id_real VARCHAR2(10); --配件Id
sp_id_prefix VARCHAR2(10);
sp_id_suffix VARCHAR2(10);
startTime varchar2(100);
productNo varchar2(100);
ownername varchar2(100);
i BINARY_INTEGER := 0; --for 遍历CUR_SP_IDS 中需要一个同步变量。 SP不是Int类型,不能用。
CURSOR CUR_SP_IDS IS --声明显式游标
select id from T_BNS_SP sp where sp_state='借出' ; --查询所有借出的工单号
SP CUR_SP_IDS%ROWTYPE; --定义游标变量,该变量的类型为基于游标CUR_SP_IDS的记录
begin
phoneNum_message_collection := t_dept_phoneNum_message_table(); --实例对象
for SP in CUR_SP_IDS loop
sp_id_real:= SP.id;
sp_id_prefix:= SP.id||',';
sp_id_suffix:= ','||SP.id;
BEGIN
select sm.cell_phone,sm.start_time,sm.product_no ,sm.ower_name into cellphonenumber,startTime,productNo,ownername from T_BNS_SP_BORROW_MAIN sm where sm.sp_id=sp_id_real and (sysdate - sm.start_time)>30 and rownum = 1;
EXCEPTION
WHEN no_data_found THEN
GOTO point1; --这是循环跳转应用
END;
i:=i+1;
message := '您好:'||ownername || '请尽快归还您在'|| startTime ||'借的备件'||productNo ;
phoneNum_message_collection.EXTEND;--如果不加执行的时候会报ORA-06533,实际上集合没有存储空间
--phoneNum_message_collection(i):=user_id;
phoneNum_message_collection(i) := t_dept_phoneNum_message_type(cellphonenumber,message);
dbms_output.put_line(cellphonenumber);
dbms_output.put_line(message);
cellphonenumber:= null;
startTime:=null;
productNo:=null;
--end if;
<<point1>> --这是断点设置
NULL;
end loop;
end ;
//oracle_cs = (OracleCallableStatement)oracle_conn.prepareCall("{call pro_borrow_alarm_first(?)}");
//oracle_cs.registerOutParameter(1,OracleTypes.JAVA_OBJECT);
//oracle_cs.registerOutParameter(1, OracleTypes.STRUCT, "t_dept_phoneNum_message_table".toUpperCase());
//cstmt.registerOutParameter(1, OracleTypes.ARRAY, "person_table_type.toUpperCase()");
//cstmt.execute();
//ARRAY array = oracle_cs.getARRAY(1);
//oracle_cs.registerOutParameter(1, OracleTypes.ARRAY, "t_dept_phoneNum_message_table".toUpperCase());
//oracle_cs.execute();
//ARRAY array = (ARRAY) oracle_cs.getObject(1);
//System.out.println(array.length());
//ResultSet rs = array.getResultSet();
// Object[] attr = struct.getAttributes();
// STRUCT struct = (STRUCT) oracle_cs.getObject(1);
// Object[] attr = struct.getAttributes();
// System.out.println(struct.longValue());
以上是我乱贴的一些代码,都是从网上找的,总结的。不过我这里没有测试成功,还望高手们指教