oracle中运用存储过程调用函数
表A:会员信息表(会员号memberid,会员级别,享受折扣memberdis)表B:收银信息表(会员号memberid,本次消费额spend,实付金额cost)建一存储...
表A:会员信息表(会员号memberid,会员级别,享受折扣memberdis)
表B:收银信息表(会员号memberid,本次消费额spend,实付金额cost)
建一存储过程,向B中插入一条完整的数据,已知memberid和spend,同时通过memberid从表A中得出memberdis,cost=memberdis*spend。用函数实现计算cost,用存储过程实现调用函数向表B中插入一条完整的数据。
存储过程名proc_mem,包含两个输入参数memberid和spend,函数名function_mem 展开
表B:收银信息表(会员号memberid,本次消费额spend,实付金额cost)
建一存储过程,向B中插入一条完整的数据,已知memberid和spend,同时通过memberid从表A中得出memberdis,cost=memberdis*spend。用函数实现计算cost,用存储过程实现调用函数向表B中插入一条完整的数据。
存储过程名proc_mem,包含两个输入参数memberid和spend,函数名function_mem 展开
展开全部
我也来做,以下程序经过测试,正常获取cost值,插入收银信息表正常!!
--创建表
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;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么没人回答啊?嘿,我好久没用过oracle了,电脑上没有装oracle,所以也不知道这对不对?函数的这种创建和调用对不对,记不太清了,错了楼主见谅啊!
--函数,根据会员id和本次消费额,返回本次实付金额
create or replace function function_mem(mem_id in varchar,spend in number)
return number
is
re_spend number(11,2);
temp_spend number(5,2);
begin
select memberdis into temp_spend from 表A where memberid=mem_id;
re_spend := temp * temp_spend;
return (re_spend);
end;
--存储过程
create or replace procedure proc_mem(mem_id varchar,spend number)
as
begin
inser into 表B (memberid,spend,cost) values (memberid,spend,function_mem(memberid,spend));
end;
--函数,根据会员id和本次消费额,返回本次实付金额
create or replace function function_mem(mem_id in varchar,spend in number)
return number
is
re_spend number(11,2);
temp_spend number(5,2);
begin
select memberdis into temp_spend from 表A where memberid=mem_id;
re_spend := temp * temp_spend;
return (re_spend);
end;
--存储过程
create or replace procedure proc_mem(mem_id varchar,spend number)
as
begin
inser into 表B (memberid,spend,cost) values (memberid,spend,function_mem(memberid,spend));
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询