Oracle10g下简单存储过程编写

来源:互联网 发布:php的curlpath 编辑:程序博客网 时间:2024/06/11 02:42

经过个把小时的折腾,写了一个简单的存储过程。一直都是改别人的存储过程,没有自己
写过,这样很不好。写了一个简单功能的:
有判断选择,循环遍历游标。

存储过程注意点:
1.注意其基本的语法。
2.关注游标,在10g中定义游标一般都使用sys_refcursor
而cursor只是用来在申明部分进行初始化,而sys_refcursor可以直接
使用。
3.%found %notfound %isopen等游标属性


/**
*@date:2009-06-16
*
*/
CREATE OR REPLACE PROCEDURE SP_TEST(STRFLAG      IN STRING,
                                    CUR_FCONSIGN out sys_refcursor,
                                    CUR_FEE      out sys_refcursor,
                                    INTCOUNT     OUT INTEGER) AS

  fcsgConsignId varchar2(200); --委托表ID
  fcsgConsignNo varchar2(200); --委托表NO
  i             integer;
 
  --定义中间游标
   cursor CUR_TEMP is
    SELECT fc.fcsg_consign_id, fc.fcsg_consign_no
      FROM FCONSIGN fc
     where rownum <= 5;
  
BEGIN

  --如果传的标志为1,则返回CUR_FCONSIGN
  IF STRFLAG = 0 THEN
    SELECT COUNT(*) INTO INTCOUNT FROM FCONSIGN;
    DBMS_OUTPUT.PUT_LINE(INTCOUNT);
    --返回fconsign表的所有数据
    open CUR_FCONSIGN for
      select fc.fcsg_consign_id, fc.fcsg_consign_no
        from fconsign fc
       where rownum <= 100;
 
  ELSIF STRFLAG = 1 then
    select count(*) into INTCOUNT from fexpense;
    dbms_output.put_line(INTCOUNT);
  --返回费用的数据fexpenses
    open CUR_FEE for
      select fe.fexp_expense_id, fe.fexp_bill_no
        from fexpense fe
       where rownum <= 5;
   END IF;


  --打开游标
  open CUR_TEMP;
 --把游标的某行值赋值给变量
  fetch CUR_TEMP
    into fcsgConsignId, fcsgConsignNo;
  dbms_output.put_line('first' || fcsgConsignId || fcsgConsignNo);
  dbms_output.put_line('------------------------');

  --打印委托表中的前一百条数据
  --遍历游标
  i := 1;
  while CUR_TEMP%found loop
    fetch CUR_TEMP
      into fcsgConsignId, fcsgConsignNo;
    dbms_output.put_line(i);
    dbms_output.put_line(fcsgConsignId || fcsgConsignNo);
    i := i + 1;
  end loop;

--关闭游标
  if CUR_TEMP%isopen then
    close CUR_TEMP;
  end if;


END SP_TEST;

凡事由简入难~~~~基础很重要,要不停的补充。

原创粉丝点击