- 存储过程
概念:存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象。
sql语句:sql中的存储过程及相关介绍:CREATE PROCEDURE [拥有者.]存储过程名[;程序编号][(参数#1,…参数#1024)][WITH{RECOMPILE | ENCRYPTION | RECOMPILE, ENCRYPTION}][FOR REPLICATION]AS 程序行其中存储过程名不能超过128个字。每个存储过程中最多设定1024个参数(SQL Server 7.0以上版本),参数的使用方法如下:@参数名数据类型[VARYING] [=内定值] [OUTPUT]每个参数名前要有一个“@”符号,每一个存储过程的参数仅为该程序内部使用,参数的类型除了IMAGE外,其他SQL Server所支持的数据类型都可使用。[内定值]相当于我们在建立数据库时设定一个字段的默认值,这里是为这个参数设定默认值。[OUTPUT]是用来指定该参数是既有输入又有输出值的,也就是在调用了这个存储过程时,如果所指定的参数值是我们需要输入的参数,同时也需要在结果中输出的,则该项必须为OUTPUT,而如果只是做输出参数用,可以用CURSOR,同时在使用该参数时,必须指定VARYING和OUTPUT这两个语句。实例:create or replace procedure add_sal(dept_id number,temp_num out number) is cursor sal_cursor is select employee_id,salary,hire_date from employees where department_id = dept_id; v_1 number(4,2) := 0; begin temp_num := 0; for c in sal_cursor loop if to_char(c.hire_date,'yyyy') < '1995' then v_1 := 0.05; elsif to_char(c.hire_date,'yyyy') < '1995' then v_1 := 0.03; else v_1 := 0.01; end if; update employees set salary = salary*(1+v_1) where employee_id = c.employee_id; temp_num := temp_num + c.salary*v_1; end loop; dbms_output.put_line(temp_num); end; 调用方法: declare v_temp number(10) := 0; begin add_sal(80,v_temp); end;
- 存储函数:
概念:有返回值的存储过程。
实例: helloworld create or replace function hello_world(v_log varchar2) return varchar2 is begin return 'helloworld '||v_log; end; 调用: declare v_hell varchar2 := 'nihao'; begin hello_world(v_hell); end;
实例2:
create or replace function get_sal(dept_id number,total out number) return number is v_sumsal number(10) := 0; cursor salary_cursor is select salary from employees where department_id = dept_id; begin total := 0; for c in salary_cursor loop v_sumsal := v_sumsal + c.salary; total := total + 1; end loop; return v_sumsal; end;
调用: declare v_dept number := 80; v_num number := 0; begin
end;实例3:get_sal(v_dept,v_num);
create or replace function get_sysDate return date is v_date date; begin v_date := sysdate; return v_date; end;
oracle存储函数和存储过程
最新推荐文章于 2024-04-25 00:04:23 发布