Oracle PL/SQL 打印字符串的每个字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29995683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle PL / SQL printing each letter of a string
提问by Computerphile
I need to print out the letters of the word 'Hello' each on it's own , I wrote this :
我需要单独打印出“你好”这个词的字母,我是这样写的:
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;
but it skips the first two letter and the out is
但它跳过前两个字母,输出是
l
l
o
any ideas what might be the problem? Thank you.
任何想法可能是什么问题?谢谢你。
回答by mahi_0707
Better to have Loop end limit For i in 1..v_length
This way you do not have to change the Loop
limit each time you run.
最好有循环结束限制 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.
回答by Gary_W
It can be done in a single select too:
它也可以在单个选择中完成:
SQL> select substr('hello', level, 1) Letter
2 from dual
3 connect by level <= length('hello');
L
-
h
e
l
l
o
SQL>
回答by Nancy Guruswamy
The script you have written is right.
你写的剧本是对的。
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;
output:-
输出:-
H
e
l
l
o
回答by Nick Pierpoint
Your script:
你的脚本:
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;
Outputs the following:
输出以下内容:
H
e
l
l
o
All looks fine for me.
对我来说一切都很好。
What output do you get?
你得到什么输出?