PL / SQL匿名块来测试一个过程

时间:2011-12-11 07:01:20

标签: stored-procedures plsql

pL / SQL PROCEDURE

这是一个过程的代码,它将术语,lineno,组件名称,学生ID,分数作为输入并处理学生分数。

如果没有例外,该程序应将分数添加到分数表中。

CODE

CREATE OR REPLACE PROCEDURE score_details
(aterm     IN scores.Term%type,
alineno   IN scores.Lineno%type,
acompname IN scores.Compname%type,
asid      IN scores.sid%type,   
apoints   IN scores.points%type)

AS  sterm scores.Term%type;
slineno scores.Lineno%type;
scompname scores.compname%type;
ssid scores.sid%type;
spoints scores.points%type;

BEGIN

SELECT term,lineno,compname,sid,points
INTO sterm,slineno,scompname,ssid,spoints

FROM scores

WHERE aterm=term AND alineno=Lineno AND acompname=compname AND asid=sid AND apoints=points;

EXCEPTION
when no_data_found THEN
dbms_output.put_line('Invalid details');

ANONYMOUS BLOCK

以下是用于测试上述过程的匿名块的代码。

我无法得到正确的结果。请帮我解释一下代码。

ACCEPT   prompt 'pterm','plineno','pcompname','psid','ppoints'

DECLARE  pterm scores.Term%type;
plineno scores.Lineno%type;
pcompname scores.Compname%type;
psid scores.sid%type;
ppoints scores.points%type;

BEGIN
score_details(pterm,plineno,pcompname,psid,ppoints);

END

1 个答案:

答案 0 :(得分:2)

我将假设你故意错过了存储过程的大部分内容,因为它从表中取出一些值到一些局部变量中,然后对这些值没有任何作用。

我猜你在想知道为什么你在ACCEPT行中输入的值没有进入存储过程调用。

ACCEPT是一个SQL * Plus语句,可用于设置替换变量。以下示例创建名为colour的替换变量并显示结果。我输入了 Yellow 行:

SQL> ACCEPT &colour
Yellow
SQL> PROMPT Your favourite colour is &colour
Your favourite colour is Yellow

您还可以提示用户澄清您的要求:

SQL> ACCEPT colour PROMPT 'Enter a colour > '
Enter a colour > Yellow

并且您还可以在SQL中使用替换变量:

SQL> select '&colour' from dual;
old   1: select '&colour' from dual
new   1: select 'Yellow' from dual

'YELLO
------
Yellow

目前看来你的PL / SQL块有两件事情不太正确:

  1. 您的ACCEPT声明无效。你还没有完全正确的部分顺序(如果使用,PROMPT必须在变量名之后)。另外,我认为您不能在一个ACCEPT中设置多个替换变量。

  2. 您没有使用包含输入值的替换变量。

  3. 因此,我想你想要对PL / SQL块的调用看起来更像下面这样:

    ACCEPT pterm PROMPT 'Enter a term > '
    ACCEPT plineno PROMPT 'Enter a line number > '
    -- and similarly for the others.
    
    DECLARE
        pterm scores.Term%type := '&pterm';
        plineno scores.Lineno%type := '&plineno';
        pcompname scores.Compname%type := '&pcompname';
        psid scores.sid%type := '&psid';
        ppoints scores.points%type := '&ppoints';
    BEGIN
        score_details(pterm,plineno,pcompname,psid,ppoints);
    END;
    
相关问题