【问题标题】:PLSQL dbms_output.put_line() is not working while taking input from userPLSQL dbms_output.put_line() 在接受用户输入时不起作用
【发布时间】:2019-12-06 14:06:58
【问题描述】:

在我的 PL/SQL 代码中,我必须在控制台上打印一些行,然后我必须从用户那里做出选择,但在我的情况下,它永远不会在控制台上打印任何内容并直接等待用户输入。

这是我的代码:

declare
cust_id Customer.cust_id%type;
cust_name Customer.name%type;
cust_purchase Customer.total_purchase%type;
cust_category varchar2(30);
input int := 1;

cursor cursor_customer is select * from Customer;

begin

dbms_output.put_line('Which one you want to use?');
dbms_output.put_line('1. PROCEDURE');
dbms_output.put_line('2. FUNCTION');

input := &t;

open cursor_customer;
loop
fetch cursor_customer into cust_id, cust_name, cust_purchase;
    exit when cursor_customer%notfound;
    if (input = 1) then
        proc_grade(cust_id,cust_name,cust_purchase,cust_category);
    elsif (input = 2) then
        cust_category := func_grade(cust_id,cust_name,cust_purchase);
    end if;
    dbms_output.put_line('Category is added for the client id '|| cust_id || ' is ' || cust_category);

end loop;

close cursor_customer;

end;
/

开始我写了三个 dbms_output.put_line() 语句,但是这些语句并没有直接执行它等待用户输入。

这是运行代码后的输出

输入 t 的值:

但是,当我删除接受输入“input := &t;”的语句时,所有输出语句都会起作用。

请谁能告诉我哪里错了。

【问题讨论】:

  • 你不能。 PL/SQL 主要是为数据库交互而标准化的,而不是为交互式用户输入/输出而设计的。使用 bash、python、windows batch 等顶部的包装语言/脚本来满足此类要求。
  • 您的作业是否真的说您必须从 PL/SQL 中“在控制台上打印一些行”?假设这只需要在 SQL*Plus 中运行是否安全?你已经用那个和 PL/SQL Developer 标记了这个问题——但是当你使用替换变量时,你真的是这个意思,还是 SQL Developer?你可能真的想要一些东西more like this?不过,这取决于您的确切任务是什么,以及您所学的内容...

标签: oracle plsql oracle11g sqlplus


【解决方案1】:

在整个过程完成之前,您不会看到 DBMS_OUTPUT 的结果;它不像你想象的那样“互动”。

因此,请在调用 PL/SQL 过程之前询问使用他们想要做什么,并将选择作为参数传递。


acc.sql文件:

accept par_deptno number prompt "Enter department number: "

set serveroutput on
set ver off
declare
  l_cnt number;
begin
  select count(*)
    into l_cnt
    from emp 
    where deptno = &par_deptno;

  dbms_output.put_line('There are ' || l_cnt || ' employees in that department');
end;
/

执行:

SQL> @acc
Enter department number: 10
There are 3 employees in that department

PL/SQL procedure successfully completed.

SQL>

【讨论】:

  • 所以你的意思是我必须将此 plsql 块转换为一个过程,然后我必须在另一个块中输入并将其传递给该过程。对吗?
  • 这是一种选择。另一种是将所有内容放入一个.SQL 文件(使用accept 获取参数值)并从SQL*Plus 命令提示符调用它。我已经修改了我的答案,向您展示如何做到这一点;看看吧。
  • 我已经试过了,你在回答中写了什么。但是通过使用接受我必须在哪里写三个 dbms_output 行?如果我在接受之前写,那么它会给我一个错误。
  • 你会使用 PROMPT 代替。
猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 2018-01-11
  • 2011-01-31
相关资源
最近更新 更多