CREATE OR REPLACE AVG_MARK
(
CROLL_NUM OUT TEST_TBL.ROLL_NUM%type,
CNAME OUT TEST_TBL.NAME%type,
CAVG_SALARY OUT TEST_TBL.SALARY%type
CRANK OUT INTEGER
)
AS
CURSOR C1 IS
SELECT ROLL_NUM, NAME, SALARY, RANK() OVER ORDER BY SALARY DESC) AS RNK
FROM TEST_TBL;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO CROLL_NUM , CNAME , CAVG_SALARY , CRANK;
DBMS_OUTPUT.PUT_LINE(CROLL_NUM ||','|| CNAME ||','|| CAVG_SALARY ||','|| CRANK);
EXIT WHEN C1%NOTFOUND
END LOOP;
CLOSE C1;
END;
内部选择查询返回5条记录
我可以编译上面的proc,但执行它时说 “调用AVG_MARK的参数号码或类型错误”
在打开Proc并执行它时,将连接到数据库并显示记录,但最后一条记录会重复两次。
请就此问题提出建议
答案 0 :(得分:1)
不确定如何将值传递给过程。我创建了一个像你一样的样本表和一些记录,它对我有用。我在程序中改变的只是修复了一些语法错误并在EXIT WHEN
之后立即放置FETCH
。这就是为什么你看到重复记录的原因。
CREATE OR replace PROCEDURE avg_mark (croll_num OUT test_tbl.roll_num%TYPE,
cname OUT test_tbl.name%TYPE,
cavg_salary OUT test_tbl.salary%TYPE,
crank OUT INTEGER)
AS
CURSOR c1 IS
SELECT roll_num,
name,
salary,
RANK()
over (
ORDER BY salary DESC) AS RNK
FROM test_tbl;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO croll_num, cname, cavg_salary, crank;
EXIT WHEN c1%NOTFOUND;
dbms_output.put_line(croll_num
||','
|| cname
||','
|| cavg_salary
||','
|| crank);
END LOOP;
CLOSE c1;
END;
/
这是调用。
DECLARE
v_croll_num test_tbl.roll_num%TYPE;
v_cname test_tbl.name%TYPE;
v_cavg_salary test_tbl.salary%TYPE;
v_crank INTEGER;
BEGIN
avg_mark(v_croll_num, v_cname, v_cavg_salary, v_crank);
END;
/
<强> O / P 强>
1,Kaushik,1000000000,1
2,Rhodes,1000,2