Mysql experience Sharing: Stored procedures _mysql

Source: Internet
Author: User
Use test;

DROP table if exists T8;
CREATE TABLE T8 (S1 int,primary KEY (S1));

drop procedure if exists handlerdemo;

DELIMITER $$
CREATE PROCEDURE handlerdemo ()
BEGIN
declare xx int default 4;
DECLARE oh_no condition for sqlstate ' 23000 ';
#DECLARE CONTINUE HANDLER for SQLSTATE ' 23000 ' SET @X2 =1;
Declare exit handler for Oh_no set @info = ' violates PRIMARY key constraint ';
SET @x=1;
INSERT into T8 VALUES (1);
SET @x=2;
INSERT into T8 VALUES (1);
SET @x=3;
End

Calling stored procedures

/* Call the stored procedure * *
Handlerdemo ();

/* View call Stored procedure results
/SELECT @x, @info;

Experience:

1, Statement Terminator

Perhaps MySQL is a stored procedure, a custom function as a statement, therefore, stored procedures in a number of statements with ";" separated, in order to avoid conflicts, you need to redefine the Terminator with delimiter.

You can typically define a new terminator before the stored procedure starts, such as

Delimiter//

After the stored procedure has finished writing, restore the definition: delimiter;

But you can also write delimiter $$ before the stored procedure starts, but you can't write another statement after the stored procedure. As the above example

2, variable

MySQL variables, like SQL Server, are shaped like @x, but need not be declared to be used directly.

In a stored procedure, a variable is not required to use @, but to declare. and declare the header to be placed in the stored procedure (? ), as in this case, or an error. It's strange, on the one hand, sometimes variables need not be declared to use, on the other hand, sometimes to limit the position of the declaration, it is confusing, seems to be a bit casual.

A variable within a stored procedure that is scoped to a stored procedure. But those with @ variables seem to span sessions and connections, and look like global variables? As the above example.

3. Conditions and treatment

Defines the condition to be invoked for processing. As the above example:

DECLARE oh_no condition for sqlstate ' 23000 ';
#DECLARE CONTINUE HANDLER for SQLSTATE ' 23000 ' SET @X2 =1;
Declare exit handler for Oh_no set @info = ' violates PRIMARY key constraint ';

Related Article

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.