错误:引用计数器作为分配目标-PL/SQL [英] ERROR: Reference the counter as the target of an assignment - PL/SQL
问题描述
我对SQL比较陌生. 我正在尝试通过这段代码打印一个简单的图案
I'm relatively new to SQL. I'm trying to print a simple pattern through this code
declare
n number(2):=5;
temp number(2):=n;
begin
for a in 1..5
a:=a+1;loop
for b in 1..temp loop
b:=b+1;
dbms_output.put_line(' ');
temp:=temp-1;
end loop;
for c in 1..2*a-1 loop
c:=c+1;
dbms_output.put_line('*');
end loop;
end loop;
end;
/
我不断收到此错误:
PLS-00103: Encountered the symbol "A" when expecting one of the following:
* & - + / at loop mod remainder rem <an exponent (**)> ||
multiset
我知道Oracle不允许将计数器作为分配的目标,这就是为什么我在第6行不断出现错误的原因,但是即使声明另一个全局变量并在其中分配增量语句,也无法使它工作它,但也不起作用. 请帮忙. 谢谢!
I understand Oracle doesn't allow to reference the counter as the target of an assignment which is why I keep getting error at line 6 but I'm unable to make it work even by declaring another global variable and assigning increment statement in it but it doesn't work either. Please help. Thanks!
推荐答案
修改先前的答案,实际上是给您Pascal的三角形,您曾提到您尝试在评论中添加它:
Modifying the previous answers to actually give you Pascal's triangle, which you mentioned you were attempting in a comment:
set serveroutput on format wrapped
declare
n number(2):=5;
begin
for a in 1..n loop
for b in 1..n-a loop
dbms_output.put(' ');
end loop;
for c in 1..2*a-1 loop
dbms_output.put('*');
end loop;
dbms_output.new_line;
end loop;
end;
/
*
***
*****
*******
*********
PL/SQL procedure successfully completed.
您的两个dbms_output.put_line
调用都只需为dbms_output.put
,因为这是将每个*
单独打印在一行上.但是您确实需要在每次a
循环之后都进行换行,因此我在此末尾添加了dbms_output.newline
.您还在b
循环内减小temp
,这意味着在a
循环周围第二次为(n-1)
而不是零.但实际上您根本不需要单独的temp
变量,因为该变量始终与(n-a)+1
相同,并且+1
只是在每行上都留有多余的空间. (我假设您只想稍后在某个位置更改n
的值,所以我也创建了a
循环1..n
).使用n := 8
:
Both your dbms_output.put_line
calls needed to be just dbms_output.put
, as that was printing each *
on a line on its own. But you do need a line break after each time around the a
loop, so I've added a dbms_output.newline
at the end of that. You were also decrementing temp
inside the b
loop, which meant it was zero instead of (n-1)
for the second time around the a
loop; but you don't really need a separate temp
variable at all as that is always the same as (n-a)+1
and the +1
just puts an extra space on every line. (I also made the a
loop 1..n
as I assume you want to change the value of n
later in one place only). With n := 8
:
*
***
*****
*******
*********
***********
*************
***************
重要的是,尽管您也必须set serveroutput on format wrapped
,否则将丢弃在b
循环中生成的前导空格.
Crucially though you also have to set serveroutput on format wrapped
, otherwise the leading spaces you're generating in the b
loop are discarded.
您也可以在普通SQL中执行此操作,尽管您需要提供两次5
或使用绑定或替换变量:
You can also do this in plain SQL, though you need to supply the 5
twice, or use a bind or substitution variable:
select lpad(' ', 5 - level, ' ') || rpad('*', (level * 2) - 1, '*') as pascal
from dual
connect by level <= 5
PASCAL
------------------------------
*
***
*****
*******
*********
您的b
和c
循环实际上只是在手动执行lpad
.
Your b
and c
loops are just doing a manual lpad
really.
这篇关于错误:引用计数器作为分配目标-PL/SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!