Oracle DBMS_输出不使用游标获取输出中的任何行
Oracle DBMS_输出不使用游标获取输出中的任何行,oracle,plsql,sqlplus,Oracle,Plsql,Sqlplus,DBMS\u输出。在使用光标时,PUT\u行不返回数据,如下面的代码所示 我使用布尔值来比较表中的金额 SET SERVEROUTPUT ON; DECLARE --declaration of variable x_id test.saa.id%TYPE; x_acctname test.saa.acctname%TYPE; x_curbal test.saa.balamt%TYPE; x_sid tes
DBMS\u输出。在使用光标时,PUT\u行
不返回数据,如下面的代码所示
我使用布尔值来比较表中的金额
SET SERVEROUTPUT ON;
DECLARE
--declaration of variable
x_id test.saa.id%TYPE;
x_acctname test.saa.acctname%TYPE;
x_curbal test.saa.balamt%TYPE;
x_sid test.saa.sid%TYPE;
--setting of the boolean value default to null
b_lowamount BOOLEAN := false;
--declaration of cursor
CURSOR custbal IS
SELECT id,acctname,bal_amt,sid
FROM
test.saa WHERE ROWNUM <= 1000;
BEGIN
--checking cursor is open or not
IF NOT ( custbal%isopen ) THEN
OPEN custbal;
END IF;
LOOP
FETCH custbal INTO
x_id,
x_acctname,
x_curbal,
x_sid;
EXIT WHEN custbal%notfound;
CONTINUE WHEN custbal%found;
--begin another
BEGIN
b_lowamount := ( x_curbal < 10 );
IF b_lowamount THEN
dbms_output.put_line('The customer having '|| x_acctname|| ' with sol_id '|| x_sid|| 'with balance RS. '|| x_curbal);
ELSE
dbms_output.put_line('The customer having '|| x_acctname|| ' with sol_id '|| x_sid|| 'with balance RS. '|| x_curbal);
END IF;
END;
END loop;
end;
打开服务器输出;
声明
--变量声明
x_id test.saa.id%类型;
x_acctname test.saa.acctname%类型;
x_路缘试验。saa.平衡%类型;
x_sid test.saa.sid%类型;
--将布尔值默认设置为null
b_lowamount布尔值:=false;
--游标声明
光标custbal是
选择id、帐户名、余额、sid
从…起
test.saa其中ROWNUM未显示输出的原因是因为继续
——这意味着转到下一个循环的开始
相反,您的过程可以写成:
DECLARE
--declaration of variable
x_id test.saa.id%TYPE;
x_acctname test.saa.acctname%TYPE;
x_curbal test.saa.balamt%TYPE;
x_sid test.saa.sid%TYPE;
--setting of the boolean value default to null
b_lowamount BOOLEAN := FALSE;
--declaration of cursor
CURSOR custbal IS
SELECT id,
acctname,
bal_amt,
sid
FROM test.saa
WHERE rownum <= 1000;
BEGIN
OPEN custbal;
LOOP
FETCH custbal
INTO x_id,
x_acctname,
x_curbal,
x_sid;
EXIT WHEN custbal%NOTFOUND;
b_lowamount := (x_curbal < 10);
IF b_lowamount
THEN
dbms_output.put_line('The customer having ' || x_acctname || ' with sol_id ' || x_sid || 'with balance RS. ' || x_curbal);
ELSE
dbms_output.put_line('The customer having ' || x_acctname || ' with sol_id ' || x_sid || 'with balance RS. ' || x_curbal);
END IF;
END LOOP;
CLOSE custbal;
END;
/
(我保留了IF
语句,即使两个分支都输出相同的字符串-我认为这是一个错误,您的意思是输出不同的文本,取决于余额是否小于10?如果这不重要,您可以去掉IF
语句,而只输出结果。)
游标for循环的好处在于,不需要声明变量来将值返回到中(该记录是作为in()
语句的一部分隐式创建的),也不需要处理游标的打开和关闭。它还使您的代码更简单,因此-IMO-更易于理解和维护。在删除退出时条件后,非常感谢您,但它在dbms_输出的所有行中都返回了相同的表记录。替代方法工作正常。
。您究竟为什么要删除退出时子句?这意味着你将进入一个无限循环!这样做之后,我进入了无限循环,但是dbms_输出的所有记录都返回sameThat,这并不奇怪;如果您没有获取行,但由于没有n exit条件,仍然在循环,那么在获取最后一行后,变量中的值不会更改。简言之,把退出时条款放回!!!伟大的人!
BEGIN
FOR rec IN (SELECT id,
acctname,
bal_amt,
sid
FROM test.saa
WHERE rownum <= 1000)
LOOP
IF rec.bal_amt < 10
THEN
dbms_output.put_line('The customer having ' || rec.acctname || ' with sol_id ' || rec.sid || 'with balance RS. ' || rec.curbal);
ELSE
dbms_output.put_line('The customer having ' || rec.acctname || ' with sol_id ' || rec.sid || 'with balance RS. ' || rec.curbal);
END IF;
END LOOP;
END;
/