Stored procedure (Procedure), and the function (function)

Stored Procedures

definition

When the stored procedure in order to complete a specific set of functions sql statement, stored in the database compiled

Case

  1. Print hello world
create or replace procedure test_procedure1 as
begin
  dbms_output.put_line('hello world');
end;
-- test_procedure1存储过程名
-- 存储过程和函数中 as和is效果一样

-- 执行
begin
  test_procedure1;
end;
-- 客户端执行
exec test_procedure1;
  1. With variable
create or replace procedure test_procedure1 as
v_name varchar2(32);
begin
  v_name := '李四';
  dbms_output.put_line('名字:'||v_name);
end;
-- := 赋值

-- 执行
begin
  test_procedure1;
end;
  1. With parameters and variables
create or replace procedure test_procedure1(v_name in varchar2,age in number) as
  v_age number(10);
  out_name varchar2(32);
begin
  v_age  := age;
  out_name := v_name;
  dbms_output.put_line('名字:' || out_name||', 年龄:' || age);
end;
-- 参数中的in 代表输入
-- || 连接字符串
-- 参数不必写具体类型大小

-- 执行
begin
  test_procedure1('zs',22);
end;
  1. Parameter argument
create or replace procedure test_procedure1(v_name in varchar2,v_age in number) as
begin
  dbms_output.put_line('名字:' || v_name||', 年龄:' || v_age);
end;

-- 执行
declare
name varchar(10);
age number(2);
begin
  name := 'barry';
  age := 12;
  test_procedure1(v_name=>name,v_age => age); //test_procedure1(name,age);
end;
  1. in, out parameters
create or replace procedure test_procedure1(v_name out varchar2,v_age in number) as
begin
  dbms_output.put_line('年龄:' || v_age);
  select 'lisi' into v_name from dual;
end;
-- in代表输入,out用于输出,参数默认类型是in类型

-- 执行
declare
name varchar(10);
age number(2);
begin
  age := 12;
  test_procedure1(v_name=>name,v_age => age);
  dbms_output.put_line('name: '||name);
end;
  1. abnormal
create or replace procedure test_procedure1(v_age in out number) as
begin
  v_age := 10 / 0;
  dbms_output.put_line('年龄:' || v_age);
exception
  when others then
    dbms_output.put_line('error');
end;

-- 执行
declare
age number(2);
begin
  test_procedure1(v_age => age);
end;
  1. Judge
create or replace procedure test_procedure1(x in out number) as
begin
  if x = 0 then
    begin
      x := 0 - x;
    end;
  elsif x > 20 then
    x := 100;
  end if;
  dbms_output.put_line('x: ' || x);
end;

-- 注意 elsif

declare
  x int;
begin 
  x := 200;
  test_procedure1(x => x);
end;
  1. for in loop
create or replace procedure test_procedure1 as
i int;
begin
  for i in 0..10 loop
  begin 
  dbms_output.put_line(i);
  end;
  end loop;
end;

begin 
  test_procedure1;
end;
  1. delete
drop procedure 存储过程名

function

grammar structure

create [or replace] function 函数名 
([p1,p2...pn])
return datatype
is|as
--声明部分 
begin
-- 程序块
end

-- p1,p2入参列表
-- return datatype函数返回值类型

Case

  1. No-argument function
create or replace function test_function1 return varchar2
as 
begin 
  return to_char(sysdate, 'yyyy-MM-dd');
  end;

begin 
  dbms_output.put_line(test_function1);
end;
  1. There are function parameters
create or replace function test_function1(v_name in varchar2) return varchar2 as
  name varchar2(16);
begin
  name := v_name;
  return name;
end;

select test_function1('lisi') from dual;
  1. Delete function
drop function 函数名;

Stored procedures and functions difference

  1. Stored procedure to complete a given task or operation (e.g., insert, delete, etc.) in the database, the function returns specific data
  2. Stored procedure declaration with procedure,
  3. The stored procedure does not require the return type of the function must return type
  4. Stored procedure can be performed independently, can not function as an independent plsql execution, it must be used as part of an expression
  5. Stored procedures can return values ​​to the out and in / out, may be used in addition to the functions out, in / out, you may also be used to return values ​​return
  6. sql statement (DML or SELECT) stored procedure call is not available, and can function

Different scenarios

  1. If you not need to return multiple values ​​and return values, on the use of stored procedures; if only need to return a value, use the function
  2. Usually stored operation procedure for performing a specified function is generally used to calculate and return a value
  3. SQL can then call the internal functions to perform complex computational problems, but you can not call a stored procedure
Published 74 original articles · won praise 23 · views 40000 +

Guess you like

Origin blog.csdn.net/qxhly/article/details/100739313