Oracle PL/SQL/PL SQL/DBMS OUTPUT — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 09:59, 26 мая 2010

Concatenate string and number

 
SQL>
SQL> set serveroutput on
SQL> declare
  2    myNumber number := 1;
  3  begin
  4    myNumber := 1 + 1;
  5    dbms_output.put_line( "1 + 1 = " || to_char( myNumber ) || "!" );
  6  exception
  7    when others then
  8      dbms_output.put_line( "We encountered an exception!" );
  9  end;
 10  /
1 + 1 = 2!
PL/SQL procedure successfully completed.
SQL>
SQL>



DBMS_OUTPUT package

SQL>
SQL> -- DBMS_OUTPUT package
SQL>
SQL>
SQL>
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
  2     DBMS_OUTPUT.PUT_LINE("Hello");
  3     DBMS_OUTPUT.PUT_LINE("there");
  4  END;
  5  /
Hello
there
PL/SQL procedure successfully completed.
SQL>
SQL>



Print out value using DBMS_OUTPUT.PUT_LINE

SQL>
SQL> --Basic loop
SQL>
SQL>
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    x NUMBER := 1;
  3  BEGIN
  4    LOOP
  5      DBMS_OUTPUT.PUT_LINE("This loop has executed"||TO_CHAR(x)||" time(s)");
  6      x := x +1;
  7      EXIT WHEN x > 5;
  8    END LOOP;
  9  END;
 10
 11
 12  /
This loop has executed1 time(s)
This loop has executed2 time(s)
This loop has executed3 time(s)
This loop has executed4 time(s)
This loop has executed5 time(s)
PL/SQL procedure successfully completed.



Use dbms_output.put_line to display varchar2 type value

 
SQL>
SQL> set serverout on
SQL>
SQL> declare
  2      myText varchar2(100);
  3    begin
  4      myText := "Hello, World!";
  5      dbms_output.put_line(myText);
  6    exception
  7      when others then
  8        dbms_output.put_line("We encountered an exception!");
  9        raise;
 10    end;
 11    /
Hello, World!
PL/SQL procedure successfully completed.
SQL>