Oracle常用命令9(函数)

函数:

CREATE [OR REPLACE] FUNCTION

  <function name> [(param1,param2)]

RETURN <datatype>  IS|AS

  [local declarations]

BEGIN

  Executable Statements;

  RETURN result;

EXCEPTION

  Exception handlers;

END;

 

函数的调用

1

作为pl/sql的一部分

begin

        变量 := 函数名(参数列表)

End;

 

2 作为sql语句的一部分

Select 函数名(参数列表) from dual;

 

例:根据订单编号 返回 订单总金额(根据订单明细中的商品价格、数量)

create or replace function getOrderMoney(v_id number)

return number

is

       totalmoney number(7);

begin

       select sum( D_QUANTITY * D_PRICE ) into totalmoney from orderDetail where O_ID = v_id;

       return totalmoney;

end;

 

--测试函数:根据订单编号 返回 订单总金额

select getOrderMoney(1) from dual;

posted @ 2011-03-29 14:07  liuqun  阅读(217)  评论(0编辑  收藏  举报