A little understanding of Mysql stored procedures and cursors

I recently learned the database language sql, and learned about stored procedures and cursors. I didn't listen to it at all in class, so it can be said that I was confused all the way. Fortunately, there is an after-school experiment. However, the error reporting in cmd is often extremely rough, and it will only tell you what is wrong nearby (sometimes it is still wrong). The task of the teacher, I feel that it is necessary to write a record.

The whole cmd deep space black background, don't enter for the faint of heart.

First, give the structure of the table I defined myself:

 Briefly, this is an employee information form, where empno is the employee number, ename is the employee name, job is the job name, heredate is the working time, salary is the salary, comm is the subsidy, deptno is the unit number, and mgr is the employee number of the boss. The data is all random, don't take it seriously.

In my understanding , stored procedures are actually functions that are often said in programming languages. Students who have learned other languages ​​are familiar with functions, but the name is really tall. The essence is to pass in the value, and then calculate and pass the value. come out.

A real question is posted directly below:

This question is based on the input empno and then you write a stored procedure, and then calculate the annual income of the number in it, and then output the answer. The idea is very simple, that is, find the corresponding employee according to the employee number, and then find his monthly salary and subsidy (I take it as a monthly subsidy), and multiply it by the amount of one year to get the annual income.

So the question is, how to write this stored procedure? The keyword of the stored procedure is precedence. We create a new procedure. Then we said that the stored procedure is a function. You have to give it to the person who needs to calculate it. Therefore, add an in before the empno in the parameter list to indicate that it is an input. Because you still need to output the value, I don’t know if there is return in sql, but I can mark the variable in the parameter list as out to indicate output, which is similar to the reference parameter passing in other languages, as follows;

create procedure cal_all_salary(in empno_in int,out all_salary double)
begin
    过程体
end

 cal_all_salary is the name of this process. There is an in in front of empno_in, which means it is an input. There is an out in front of all_salary, which means it is an output. Of course, this parameter needs to be passed in by yourself, and then begin and end are the beginning and end of the process. sign.

How to write in the process is nothing more than reassigning all_salary, you need to use the select...into variable from... method, this variable is what needs to be assigned.

select (comm+salary)*12 into all_salary from eemp where empno=empno_in;

Of course, this is not a complete one, because it also involves the end and start characters of the process, the completeness is as follows:

drop procedure if exists cal_all_salary;
delimiter //
create procedure cal_all_salary(in empno_in int,out all_salary double)
begin
select (comm+salary)*12 into all_salary from eemp where empno=empno_in;
end;
//
delimiter ;

Because it is written in Notepad, there is no indentation, and the format is more hip. The first sentence is mainly to avoid duplication of names, if there is duplication, it will be overwritten. The delimiter string indicates that this string is used as the terminator. I use // more often, which is set to avoid confusion with the ; in the process body.

Then test, you can use set @ variable name to define a variable:

set @ans=0;
cal_all_salary(1,@ans);
select @ans;

 Effect:

So the storage itself is to complete a specific task and store the method to complete this task, so the functionality is relatively clear, which facilitates the management of the database.

Next is the cursor . In my opinion, the cursor is a loop, which packs the qualified ones into a set, and then takes them out one by one for judgment and calculation.

The usage format of the cursor is as follows: (copied from others)

DECLARE 迭代变量名1
.
.
.
DECLARE 游标名字 CURSOR FOR 查询语句;
open 游标名
while/repeat..
fetch 游标名 into var_name...
close 游标名

Roughly the above steps. First of all, I will pack several useful attributes into the cursor, the format is {[attribute 1, attribute 2...], [attribute 1, attribute 2...]...}, each cursor points to a row , with multiple attribute values, and then I use the loop to take them out one by one and do what I want to do. The fetch here is to take the value, and then assign it to the corresponding variable. The thing to watch out for is opening and closing.

One last question:

The code is as follows, it should be implemented correctly

 

drop procedure if exists avg_from_dept;
delimiter //
create procedure avg_from_dept(in dept_code int,out ans double)
begin
declare now_salary double;
declare no_data int default 0;
declare sum double default 0;
declare dept_cursor cursor for select salary from eemp where deptno=dept_code;
declare exit handler for not found set no_data = 1; 
open dept_cursor;
repeat
fetch dept_cursor into now_salary;
set sum=sum+now_salary;
set ans=sum/(select count(*) from eemp where deptno=dept_code);
until no_data=1
end repeat;
close dept_cursor;
end;
//
delimiter ;

set @dept_in=1;
set @ans=0;
call avg_from_dept(@dept_in,@ans);
select @ans;

It's all in Notepad, poorly formatted...

Guess you like

Origin blog.csdn.net/weixin_60360239/article/details/128155051