我需要自己打印出"Hello"这个词的字母,我写道:
Declare
c1 Number:=1;
c2 Varchar2(5);
begin
for c1 in 1..5
loop
select substr('Hello' , c1 , 1 ) into c2 from dual;
dbms_output.put_line(c2);
end loop;
end;
Run Code Online (Sandbox Code Playgroud)
但是它会跳过前两个字母,然后输出
l
l
o
Run Code Online (Sandbox Code Playgroud)
任何想法可能是什么问题?谢谢.
最好有循环结束限制 For i in 1..v_length这种方式Loop每次运行时都不必更改限制.
Create or replace procedure print_string( IN_string IN varchar2 )
AS
v_length number(10);
v_out varchar2(20);
Begin
v_length := length(IN_string);
for i in 1..v_length
Loop
v_out := substr(IN_string,i,1) ;
DBMS_OUTPUT.PUT_LINE(v_out);
End loop;
DBMS_OUTPUT.PUT_LINE('Text printed: ' || IN_string);
End;
-- Procedure created.
BEGIN
print_string('Hello');
END;
-- Output:
H
e
l
l
o
Text printed: Hello
Statement processed.
Run Code Online (Sandbox Code Playgroud)
它也可以在单个选择中完成:
SQL> select substr('hello', level, 1) Letter
2 from dual
3 connect by level <= length('hello');
L
-
h
e
l
l
o
SQL>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13000 次 |
| 最近记录: |