如何在 Oracle 中输入换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5812148/
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
How to enter newline character in Oracle?
提问by indolent
select col1, col2
into name1, name2
from table1
where col1=col;
m_sub := 'Subject ';
m_msg := 'Hello '||name||' ,'||/n||/n||'Your order has been placed.';
Error(34,33): PLS-00103: Encountered the symbol "/" when expecting one of the following: ( - + case mod new null avg count current max min prior sql stddev sum variance execute forall merge time timestamp interval date pipe
错误(34,33):PLS-00103:在期望以下之一时遇到符号“/”:( - + case mod new null avg count current max min before sql stddev sumvariance execute forall merge time timestamp interval date pipe
回答by Robert Love
Chr(Number)should work for you.
Chr(Number)应该适合你。
select 'Hello' || chr(10) ||' world' from dual
Remember different platforms expect different new line characters:
记住不同的平台需要不同的换行符:
- CHR(10) => LF, line feed (unix)
- CHR(13) => CR, carriage return (windows, together with LF)
- CHR(10) => LF,换行(unix)
- CHR(13) => CR,回车(windows,连同LF)
回答by Douglas Held
According to the Oracle PLSQL language definition, a character literal can contain "any printable character in the character set". https://docs.oracle.com/cd/A97630_01/appdev.920/a96624/02_funds.htm#2876
根据 Oracle PLSQL 语言定义,字符文字可以包含“字符集中的任何可打印字符”。https://docs.oracle.com/cd/A97630_01/appdev.920/a96624/02_funds.htm#2876
@Robert Love's answer exhibits a best practice for readable code, but you can also just type in the linefeed character into the code. Here is an example from a Linux terminal using sqlplus
:
@Robert Love 的回答展示了可读代码的最佳实践,但您也可以只在代码中输入换行符。这是一个来自 Linux 终端的示例,使用sqlplus
:
SQL> set serveroutput on
SQL> begin
2 dbms_output.put_line( 'hello' || chr(10) || 'world' );
3 end;
4 /
hello
world
PL/SQL procedure successfully completed.
SQL> begin
2 dbms_output.put_line( 'hello
3 world' );
4 end;
5 /
hello
world
PL/SQL procedure successfully completed.
Instead of the CHR( NN ) function you can also use Unicode literal escape sequences like u'\0085'
which I prefer because, well you know we are not living in 1970 anymore. See the equivalent example below:
除了 CHR( NN ) 函数,您还可以使用u'\0085'
我喜欢的Unicode 文字转义序列,因为您知道我们不再生活在 1970 年了。请参阅下面的等效示例:
SQL> begin
2 dbms_output.put_line( 'hello' || u'begin
dbms_output.put_line( 'hello' ||chr(13) || chr(10) || 'world' );
end;
0A' || 'world' );
3 end;
4 /
hello
world
PL/SQL procedure successfully completed.
For fair coverage I guess it is worth noting that different operating systems use different characters/character sequences for end of line handling. You've got to have a think about the context in which your program output is going to be viewed or printed, in order to determine whether you are using the right technique.
为了公平覆盖,我想值得注意的是,不同的操作系统使用不同的字符/字符序列来处理行尾。您必须考虑查看或打印程序输出的上下文,以确定您是否使用了正确的技术。
- Microsoft Windows: CR/LF or
u'\000D\000A'
- Unix (including Apple MacOS): LF or
u'\000A'
- IBM OS390: NEL or
u'\0085'
- HTML:
'<BR>'
- XHTML:
'<br />'
- etc. etc.
- Microsoft Windows:CR/LF 或
u'\000D\000A'
- Unix(包括 Apple MacOS):LF 或
u'\000A'
- IBM OS390:NEL 或
u'\0085'
- HTML:
'<BR>'
- XHTML:
'<br />'
- 等等等等