【发布时间】: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