oracle 函数中能调用存储过程吗
2016-01-15 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
oracle 函数中能调用存储过程,举例如下:
1、无参数存储过程的调用
--创建无参存储过程
CREATE OR REPLACE FUNCTION stu_proc RETURN VARCHAR2 IS
--声明语句段
v_name varchar2(20);
BEGIN
--执行语句段
SELECT o.sname INTO v_name FROM student o where o.id=1;
RETURN v_name;
END;
--调用无参存储过程
DECLARE
BEGIN
DBMS_OUTPUT.put_line('在PL/SQL中打印的结果:'||stu_proc);
END;
2、入参存储过程的调用
--创建入参存储过程
CREATE OR REPLACE FUNCTION stu_proc(v_id IN NUMBER) RETURN VARCHAR2 IS
--声明语句段
v_name varchar2(20);
BEGIN
--执行语句段
SELECT o.sname INTO v_name FROM student o where o.id=v_id;
RETURN v_name;
END;
--调用入参存储过程
DECLARE
BEGIN
DBMS_OUTPUT.put_line('在PL/SQL中打印的结果:'||stu_proc(1));
END;
展开全部
举个例子吧,你看看
--创建表
CREATE TABLE member_info(
memberid NUMBER,
memberlevel VARCHAR2(20),
memberdis NUMBER)
;
CREATE TABLE cost_info(
memberid NUMBER,
spend FLOAT,
COST FLOAT)
;
INSERT INTO member_info VALUES(1,'A',0.1);
INSERT INTO member_info VALUES(2,'A',0.2);
INSERT INTO member_info VALUES(3,'A',0.3);
INSERT INTO member_info VALUES(4,'A',0.4);
--创建函数
CREATE OR REPLACE FUNCTION function_mem(p_memeberid member_info.MEMBERID%TYPE,p_spend cost_info.SPEND%TYPE)
RETURN FLOAT
IS
v_cost cost_info.COST%TYPE;
v_memberdis member_info.MEMBERDIS%TYPE;
BEGIN
SELECT memberdis INTO v_memberdis FROM member_info WHERE memberid = p_memeberid;
v_cost := v_memberdis* p_spend;
RETURN v_cost;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END ;
--创建procedure
CREATE OR REPLACE PROCEDURE proc_mem(p_memeberid member_info.MEMBERID%TYPE,p_spend cost_info.SPEND%TYPE)
IS
v_cost cost_info.COST%TYPE;
BEGIN
SELECT function_mem(p_memeberid,p_spend) INTO v_cost FROM dual;
INSERT INTO cost_info VALUES(p_memeberid,p_spend,v_cost);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
dbms_output.put_line(SQLERRM);
END;
----执行procedure
BEGIN
proc_mem(1,1000);
END;
--创建表
CREATE TABLE member_info(
memberid NUMBER,
memberlevel VARCHAR2(20),
memberdis NUMBER)
;
CREATE TABLE cost_info(
memberid NUMBER,
spend FLOAT,
COST FLOAT)
;
INSERT INTO member_info VALUES(1,'A',0.1);
INSERT INTO member_info VALUES(2,'A',0.2);
INSERT INTO member_info VALUES(3,'A',0.3);
INSERT INTO member_info VALUES(4,'A',0.4);
--创建函数
CREATE OR REPLACE FUNCTION function_mem(p_memeberid member_info.MEMBERID%TYPE,p_spend cost_info.SPEND%TYPE)
RETURN FLOAT
IS
v_cost cost_info.COST%TYPE;
v_memberdis member_info.MEMBERDIS%TYPE;
BEGIN
SELECT memberdis INTO v_memberdis FROM member_info WHERE memberid = p_memeberid;
v_cost := v_memberdis* p_spend;
RETURN v_cost;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END ;
--创建procedure
CREATE OR REPLACE PROCEDURE proc_mem(p_memeberid member_info.MEMBERID%TYPE,p_spend cost_info.SPEND%TYPE)
IS
v_cost cost_info.COST%TYPE;
BEGIN
SELECT function_mem(p_memeberid,p_spend) INTO v_cost FROM dual;
INSERT INTO cost_info VALUES(p_memeberid,p_spend,v_cost);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
dbms_output.put_line(SQLERRM);
END;
----执行procedure
BEGIN
proc_mem(1,1000);
END;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当然可以。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |