MySQL-- stored procedures, local variables and user variables [@] (initial understanding)


      For more information about the database, please add attention yo ~ ~. For bloggers please contact Gabor main private letter or contact:
      QQ: 3327908431
      micro letter: ZDSL1542334210

        Preface: stored procedures, a fantastic way to learn not to learn MySQL stored procedure is not complete, then it really magical Where? Today's statement bloggers take you into the Secret stored procedures ... Ahem ... I admit that I have installed the *. Bloggers point out is understood as a function of self! Please follow my footsteps slowly coming ~~

1. What is a stored procedure

1.1 Definition and role of the stored procedure

        Or stored procedure is a set of multiple SQL statements, stored procedures can help us: 1, streamline operations, reduce redundant steps; 2, reduce errors during the operation, improve work efficiency. So stored procedure is useful, you should learn and use it as much as possible. Bloggers themselves understood as a self function, but with varying functions and from different is that it can be directly saved to be used directly in MySQL inside, while the function was run after the self or package before calling.

1.1 Establishment and retrieve stored procedure

delimiter //  #  实现两个整数相加的存储过程
create procedure test(in a int, in b int)  #创建存储过程
begin
   <你想要实现的SQL功能语句>
end //
delimiter ;
call test(2, 3); # 调用存储过程   

        Interpretation: 'delimiter //' symbol is a MySQL end set to '//'. Because MySQL default statement terminator symbol ';', and during storage in order to avoid SQL statement terminator conflicts need 'DELIMITER' change end character stored procedure, and to 'end //' end of the stored procedure. After completion of the stored procedure is defined using a 'delimiter;' restore the default terminator.
        Create a stored procedure: create procedure <存储过程名称> (in a int, in b int) # 输入参数a和b,且指定为整型In the begin ... end write your code inside the function statement. 'call' to call just to write stored procedures. Of course, if the stored procedure does not need to be deleted drop procedure <你创建存储过程的名称>. You create a stored procedure in your current database Stored Proceduresinside:
Here Insert Picture Description

2, local variables and user variables

2.1 Local variables

        It is a way to assign values ​​to variables, but it can only be declared and used in the subprogram, that is, begin ... end block of code, that functions in SQL statements that you want to achieve in. Assign it to a variable (remember only begin ... end inside use, from the mouth can not use):

a. 使用set语句为变量赋值:
declare test int;  # 介入一个赋值变量test
set test = 1 + 2# 对该变量进行传值
b. 使用selectinto为变量赋值:
declare test int# 介入一个赋值变量test
select 1 + 2 into test; # 对该变量进行传值

2.2 user variables

        The method is also a variable assignment, user variables and links related to a client-defined variables can not be seen or to use other clients, which is our common variable. When the client exits, all the variables connected clients will automatically released. Specifically, is to be assigned to a variable, it is not begin ... end block of code limit, local variables and intervention methods form different, it is "@var_name" form, do not need declare defined. Assigned to:

a. 使用SET语句为变量赋值:
set @test = 1 + 2;
select @test;
b. 使用selectinto为变量赋值:
select 1 + 2 into @testselect @test;

        Finished theoretical knowledge, is not subject to erosion at the point, where to find the pearls buried inside the earth?

3, section title

A Title: I Write the same function and sum function of a stored procedure to complete the addition of two numbers

delimiter //    # 法一 用户变量
create procedure sums(in a int,in b int)
begin
    select a+b into @sumvalue; #这里是用户变量,看“@”符号
    select @sumvalue;
end //
delimiter ;
call sums(32,32); # 调用存储过程
-- -----------------------------------------------------------------------------
delimiter //    # 法二 局部变量
create procedure sumss(in a int,in b int)
begin
	declare valus int
    select a+b into valus; #这里是局部变量,无“@”符号
    select valus;
end //
delimiter ;
call sumss(32,32); # 调用存储过程
# 结果为:64

Title II: Please write a stored procedure to complete the addition of two numbers by subtracting the third number

delimiter //   
create procedure texts(in a int,in b int,in c int)
begin
    select (a+b)/c into @sumvalue; #这里是用户变量,看“@”符号
    select @sumvalue;
end //
delimiter ;
call texts(2,4,6); # 调用存储过程
# 结果为:
1.00

Title three (OUT parameter): Please write a stored procedure, after completion of the addition of two numbers OUT to the third number

-- out类型参数
delimiter //
create procedure outtest(in a int, in b int, out c int)
begin
	select a+b into c;
	-- select c;
end //
delimiter ;
call outtest(3,3,@c);
# 结果为
6

        You can see, out parameters and assignment method is not the same as two or more parameters, the parameters need not be declared to be out and defined, just use out specified on it, in the out parameter is the final output.

4, the end of the paper eggs - easy moment

        Really who promises a good harvest Now, this is not just past National Day Well, the day before going to his house to see the Cubs and sister-in-law, so he went to bring them home with bright brother, sister-in-law saw at this time to breastfeed her child, sister-in-law called Cubs get dried fruit for us to eat, then it bright brother out of courtesy, and thus it came to him to touch the child's face, said: "! baby good boy, glowing skin nice, very nice," then my sister-in-law blushed and whispered: "the children fell asleep, face over there ..." and then I saw it, really good skin is very white ...

       Today to end here yo // each article has the end the egg - relaxed moment yo ~ plus interest to learn more about MySQL knowledge! Thank you for watching, I was Jetuser-data

Links: [https://blog.csdn.net/L1542334210]
CSND: L1542334210
Here Insert Picture Description
I wish you all success! Family fun!

Published 29 original articles · won praise 53 · views 30000 +

Guess you like

Origin blog.csdn.net/L1542334210/article/details/102385593