PLSQL-存储过程

一、in参数

	create or replace procedure raise_salary
	(p_id in emp_empno%type)	--相当于形参
	is
	begin
		update emp set sal=sal+500
		where empno=p_id;
	end;
	--调用存储过程1
	begin
		raise_salary(2296);	--调用存储过程时给参数赋值
	end;
	--调用过程2
	declare
		v_empno emp.empno%type:=2296;
	begin
		raise_salary(v_empno);
	end;

二、out参数

	create or replace procedure raise_salary1
	(
		p_id in emp.empno%type,	--入参
		p_name out emp.ename%type, --出参
		p_sal out emp.sal%type --出参
	)
	is
	begin
		select ename,sal into p_name,p_sal
		from emp
		where empno=p_id;	--入参,和数据库中的值匹配
	end;
	--调用存储过程
	declare
		v_name emp.ename%type;
		v_sal emp.sal%type;
	begin
		raise_salary1(2296,v_name,v_sal);
		dbms_output.put_line(v_name||' '||v_sal);
	end;

三、in out参数:既可以作为入参也可以作为出参

	create or replace procedure raise_salary2
	(
		p_id in emp.empno%type,
		p_name out emp.ename%type,
		p_sal in out emp.sal%type
	)
	is
	begin
		update emp set sal = sal+p_sal --p_sal作为入参使用
		where empno=p_id;
		
		select ename,sal into p_name,p_sal	--p_sal作为出参使用
		from emp
		where empno=p_id;
	end;
	--调用存储过程
	declare
		v_empno emp.empno%type:=2296;
		v_name emp.ename%type;
		v_sal emp.sal%type:=500;
	begin
		raise_salary2(v_empno,v_name,v_sal);
		dbms_output.put_line(v_name||' '||v_sal);
	end;

四、处理异常

	create or replace procedure raise_exception
	(
		p_id out emp.empno%type
	)
	is
	begin
		select empno into p_id
		from emp
		where empno = 99;
		--异常处理
		exception
			when no_data_found then
				dbms_output.put_line('没有数据输出');
	end;
	--调用存储过程
	declare
		v_empno emp.empno%type;
	begin
		raise_exception(v_empno);
		dbms_output.put_line(v_empno);
	end;

五、删除存储过程

	drop procedure raise_salary2;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值