mysql-Advanced declaration variables/stored Procedures

Source: Internet
Author: User

declaring variables

Setting Global variables

Set @a= ' a new variable ';

Variables used in functions and stored procedures Declear

Declear a int unsigned default 1;

This variable needs to be set to the type of the variable and only exists at begin. End of the paragraph

Select:  Into.. Assign the contents of a table directly to a specified variable

Select Name,bid into @a,@b from bank limit 1;

One thing to note is that the variable name cannot match the field name

Stored Procedures

stored procedures encapsulate a common set of operations so that different platforms can be shared

The stored procedure does not return a value, and cannot be called by the SQL statement, only call calls, and no result set is returned, execution executes

It is important to note that SQL statements are used in the storage process; This system default terminator to reset to another, or half the system in the process of the error recognition program to terminate and then error

Change the end of the command character to $

delimiter$+ Enter or shorthand \d $+ enter

Show all stored Procedures

Show procedure status;

To delete a specified stored procedure

drop procedure process name;

Stored Procedure Demo

0 \d $
1 CREATE PROCEDURE Yanshi (in arg tinyint)2 begin3 DeclareAge tinyintdefault0;4Set age=Arg;5 ifAge<20 Then6Select ' Number less than 20 ';7 ElseIfAge>20 Then8Select ' Number greater than 20 ';9 End if;Ten End One $ ACall procedure -Set @num =12$ -Call Yanshi (@num)$ theCall Yanshi (21) $

Determine which stage the number entered into the stored procedure belongs to

In-out inout three of parameters in the stored procedure

In can output a variable passed in from outside without changing the value of the passed-in variable

CREATE PROCEDURE a (in ID int)
Begin
Select ID;
Set id = 100;
End
$
Set @id =1$
Call A (@id) $//Output 1 is the value of the @id that comes in from the outside

Select $id $//Output 1 indicates that the passed-in value was not changed in the stored procedure

Out cannot output the value passed in from the outside will change the value passed into the variable

CREATE PROCEDURE B (out ID int)
Begin
Select ID;
Set id = 100;
End
$
Set @id =1$
Call B (@id) $//input NULL

Select @id $//Output 100

InOut is the ability to output incoming variables and change incoming variables.

Here's the time to test your computer's hardware performance.

Do you remember the Bank table of the year? He's the one who kept it. Then execute the following command:

CREATE PROCEDURE Addbank ()
Begin
declare i int default 0;
Set i = 5000000;
While i > 0 do
INSERT into bank (name) values (i);
Set i = i-1;
End while;
End
$

Call Addbank () $

Good luck

mysql-Advanced declaration variables/stored Procedures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.