31

How do I print a new line in PL/SQL? I'm after something similar to '\n' in the C language.

Example:

begin

    dbms_output.put_line('Hi, good morning friends');

end;

I need the output is like this:

hi,
good 
morning
friends
durron597
  • 31,968
  • 17
  • 99
  • 158
user1252398
  • 1,069
  • 7
  • 22
  • 29

8 Answers8

41

You can concatenate the CR and LF:

chr(13)||chr(10)

(on windows)

or just:

chr(10)

(otherwise)

dbms_output.put_line('Hi,'||chr(13)||chr(10) ||'good' || chr(13)||chr(10)|| 'morning' ||chr(13)||chr(10) || 'friends');
A.B.Cade
  • 16,735
  • 1
  • 37
  • 53
41

In PL/SQL code, you can use: DBMS_OUTPUT.NEW_LINE;

9

Most likely you need to use this trick:

dbms_output.put_line('Hi' || chr(10) || 
                     'good' || chr(10) || 
                     'morning' || chr(10) || 
                     'friends' || chr(10));
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
6
dbms_output.put_line('Hi,');
dbms_output.put_line('good');
dbms_output.put_line('morning');
dbms_output.put_line('friends');

or

DBMS_OUTPUT.PUT_LINE('Hi, ' || CHR(13) || CHR(10) || 
                     'good' || CHR(13) || CHR(10) ||
                     'morning' || CHR(13) || CHR(10) ||
                     'friends' || CHR(13) || CHR(10) ||);

try it.

Jaime Yule
  • 981
  • 1
  • 11
  • 20
5

Pass the string and replace space with line break, it gives you desired result.

select replace('shailendra kumar',' ',chr(10)) from dual;
Nazik
  • 8,696
  • 27
  • 77
  • 123
Shailendra
  • 51
  • 1
  • 1
2
  begin

        dbms_output.put_line('Hi, '||CHR(10)|| 'good'||CHR(10)|| 'morning' ||CHR(10)|| 'friends');

    end;
Maddy
  • 3,726
  • 9
  • 41
  • 55
0
SET SERVEROUTPUT ON;
DECLARE
   TYPE MESSAGE IS TABLE OF VARCHAR2 (100);
   MESSAGE_   MESSAGE;
   PROCEDURE write_
   IS
      counter   INT := 0;
   BEGIN
      MESSAGE_ :=
         MESSAGE ('Hi,',
                  'good',
                  'morning',
                  'friends');
      FOR i IN 1 .. MESSAGE_.COUNT
      LOOP
         counter := counter + 1;
         MESSAGE_ (counter) := MESSAGE_ (i);
         DBMS_OUTPUT.PUT_LINE (MESSAGE_ (counter));
      END LOOP;
   END;
BEGIN
   write_;
END;
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 05 '23 at 05:36
0

If you prefer the C approach, you can use the replace function to replace c-style escape sequences in your string literal.

This should not be seen as a widely recommended solution.

declare
  c_lf constant char(1) := chr(10);
begin
  dbms_output.put_line(replace('hi,\ngood\nmorning\nfriends','\n',c_lf));
end;
/
alexeionin
  • 514
  • 4
  • 8