PL/SQL programming basics (1)

 

 

 

Let me share some of Oracle's PL/SQL programming exercises. The following are answers written by individuals. Excuse me!

1. To write and run a simple PL/SQL block program. Requirements: Receive a certain name information XXX, and output and display " Hello XXX , today is: current date " .

 

 

or:

2. Practice the definition and use of various types of variables in PL/SQL .

Refer to the textbook p140 Experiment 1, take the table in scott mode as an example or take the user-defined table as an example to practice the definition and use of various types of variables in PL/SQL (including scalar variables , %type type variables, custom Record variables , %rowtype variables ). Record all the example PL/SQL statements.

(1) Use scalar variables.

 

 

 

(2) Use %type variables.

 

(3) Custom record variables.

 

 

 

(4) Use %rowtype variable.

   

3. Practice using basic flow control (loop) statements to write PL/SQL code to complete the specified data processing functions .

(1) Calculate the sum of even-numbered items between 1 and 100 using the LOOp cycle method (run result: 2550).

declare

       i number:=1;

       v_sum number:=0;

begin

       Loop

              exitwhen i>100;

              i:=i+1;

              ifmod(i,2)=0 then

                     v_sum: = v_sum + i;

              endif;

       end loop;

end;

/

 

 

(2) Use the WHILE loop to print out all integers within 50 that can be divisible by 3, and the number of the integers (run result: 16).

 

(2) Use the FOR loop to calculate and output S=1!+2!+…+10! (operation result: 4037913).

declare

     var_snumber: = 1;

     var_sumnumber: = 0;

Begin

     For a in 1..100loop

            For b in 1..a loop

                   var_s: = var_s * b;

            end loop;

            var_sum: = var_sum + var_s;

     end loop;

end;

/

 

 

 

4. Practice using basic flow control statements to write PL/SQL code to complete the specified data processing functions.

(1) Subject requirements: Use temporary variables (&e_no) to require users to enter the employee number, use the IF statement to determine whether the employee's position is'CLERK', and if so, increase the employee's salary by 1%.

Answer: declare

              v_e_no varchar2 (80): = & e_no;

       begin

              ifv_e_no=’ CLERK’then

                     update emp

                     set sal = sal * 1.01

                     where job =’CLERK’;

              endif;

       end;

       /

 

 

(2) Subject requirements: Use temporary variables (&d_no) to require the user to enter the department number. According to the entered department number, use the case judgment using the selector :

If it is department 10, change the employee subsidy of the department (corresponding to the field COMM in the EMP table) to 100; if it is department 20, change the employee subsidy of the department to 80; if it is department 30, change the department The employee's subsidy is changed to 50, otherwise the output "the department does not exist!"

Answer: declare

              v_d_no varchar2(20):=&d_no;

       begin

              casev_d_no

                     when 10 then  

                            updateemp

                            setcomm=100

                            wheredeptno=’10’;

 when20 then

                            updateemp

                            setcomm=80

                            wheredeptno=’20’;

when 30 then  

                            updateemp

                            setcomm=50

                            wheredeptno=’30’;

                     else

                            dbms_output.put_line('The department does not exist!');

              endcase;

       end;

       /

 

 

(3) Subject requirements: Use temporary variables (&e_no) to require the user to enter the employee number. According to the entered employee number, use the case judgment without using the selector :

If the employee's salary is less than 1000, the employee's subsidy (corresponding to the field COMM in the EMP table) is changed to 100; if the employee's salary is between 1000 and 2000, the employee's subsidy is changed to 80; if the employee's salary is greater than 2000, then Change the employee's subsidy to 50

 

Answer: declare

              v_e_no number:=&e_no;

       begin

              case

                     when v_e_no<1000 then

                            updateemp

                            setcomm=100

                            wheredeptno<1000;

                     when v_e_no<2000 and v_e_no>1000then

                            updateemp

                            setcomm=80

                            wheredeptno<2000 and dept>1000;

                     when v_e_no>2000 then

                            updateemp

                            setcomm=50

                            wheredeptno>2000;

              endcase;

       end;

 

 

      /

 

note:

          1) When using a for loop, the middle is ".." instead of "..."; 

          2) Different from other programming languages, in the pl/sql block statement, the assignment statement is ":=";

          3) When declaring variables, pay attention to the specification of identifier naming, otherwise errors may occur. For the specification of identifier naming , see: Click to open the link

 

 

 

 

Guess you like

Origin blog.csdn.net/VinWqx/article/details/79846428