PLSQL基础语法六-函数

函数语法:

/*CREATE [OR REPLACE] FUNCTION <函数名>
    (<参数1>,[方式1]<数据类型1>,<参数2>,[方式2]<数据类型2>...)
    RETURN<表达式>
 IS|AS
  PL/SQL程序体;*/  --其中必须要有一个RETURN子句

例子:

/*******************************
函数和存储过程基本差不多,唯一的区别,函数有返回值,函数的参数只有in
函数一般是为了得到一个结果,而存储过程则更倾向于做成某项复杂的业务处理.
*********************************/
--练习1:生成helloworld的简单函数
create or replace function helloFun return varchar2 is
begin
  return 'helloworld';
end helloFun;
--测试
begin
 dbms_output.put_line(helloFun());
  end;
--练习2:计算年龄*12(接收一个参数)
create or replace function yearSal(v_sal number) return number is
begin
 return v_sal*12;
end yearSal; 
--测试 
declare
 v_sal number;  
begin
 select age into v_sal from student where id = 1;
 dbms_output.put_line('年龄*12='||yearSal(v_sal));
end;


你可能感兴趣的:(oracle,plsql)