Example of plsql block

Example 1 only includes the execution part of the pl/sql block

set serveroutput on -- open the output option
begin
dbms_output.put_line('hello, world!');
end;

related instructions:
dbms_output is a package provided by oracle (similar to the java development package ), the package contains some procedures, put_line is a procedure of the dbms_output package.

Without set serveroutput on this to turn on the output option, nothing in oracle shows up.

Actual display:
SQL> set serveroutput on;
SQL> begin
  2 dbms_output.put_line('hello,world!');
  3 end;
  4 /
hello,world!
PL/SQL procedure successfully completed

 


Example 2 pl/sql block containing definition part and execution part

declare
  v_ename varchar2(5);--define string variable
begin
  select ename into v_ename from emp where empno=&no;
  dbms_output.put_line('employee name:'||v_ename );
end;
/

Related instructions:
& means to receive the variable input from the console


Note:
1.ename into v_ename means to assign the value of ename to v_ename.
2. The function of || in the output statement is the same as that of + in java.
3. The no after the & in empno=&no; is actually optional, but it must be written. Its function is to assign the variable input in the console to which value. For example, &no means to assign the value input from the console to no.


SQL> declare
  2 -- define variable
  3 v_ename varchar2(10);
  4 begin
  5 -- execute part
  6 select ename into v_ename from emp where empno=&aa;
  7 -- display user name in console
  8 dbms_output.put_line('user The name is: '||v_ename);
  9 end;

The user name is: SCOTT
PL/SQL procedure successfully completed



-- also display the user's salary
declare
-- define variable
v_ename varchar2(10);
v_sal number(7,2);
begin
  -- execute part
  select ename,sal into v_ename ,v_sal from emp where empno=&aa; --Display username
  in console
  dbms_output.put_line('Username is: '||v_ename||' Salary: '||v_sal);
  end;

 


Example 3 includes the definition part, the execution part and the exception processing part.
In order to avoid the operation error of the pl/sql program and improve the robustness of the pl/sql, it is necessary to deal with the possible errors:

1) For example, in example 2 , If a non-existent employee number is entered, an exception should be handled

2) Sometimes an exception occurs, and I hope to use another logic to deal with the

relevant instructions: Oracle predefines some exceptions in advance, and no_data_found is an exception for not finding data .

SQL> declare
  2 --define variable
  3 v_ename varchar2(10);
  4 v_sal number(7,2);
  5 begin
  6 --execute part
  7 select ename,sal into v_ename,v_sal from emp where empno=&aa;
  8 -- Display the user name in the console
  9 dbms_output.put_line('User name is: '||v_ename||' Salary: '||v_sal);
 10 --Exception handling
 11 exception
 12 when no_data_found then
 13 dbms_output.put_line('The number The user does not exist, please re-enter!');
 14 end;
 15 /
The user with this number does not exist, please re-enter!
PL/SQL procedure successfully completed


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327033519&siteId=291194637