Simple Single-store process-loop a select result set

Source: Internet
Author: User

Abstract: This article describes how to create, call, and use a stored procedure. I believe that mastering the cursor will be helpful to you. If you have any shortcomings, please advise.
 
Navigation: 1. Create and call a stored procedure
Ii. Use of cursors
Iii. Example
Iv. Supplement
 
Note:
1. Two data tables used:
 
From_data

To_data

2. Example requirement: insert the select result set of the table from_data into the table to_data cyclically;
 
Pseudocode: while LOOP select id, name from_data
 
Insert into to_data (id, name) value (from_data.id, from_data.name)
 
End
 
3. Environment: mysql
 
 
 
1. Create and call a stored procedure
 
We create a stored procedure named add_test.
 
1. Check whether add_test exists.
SQL code
Drop procedure if exists add_test;
2. Create
 
 
SQL code
Create procedure add_test ()
(
# [In | out | inout] datatype


A int;
B int;
)
Begin
# SQL statement;



End;
 
3. Call
 
SQL code
Call add_test (1, 2

);

 
The above is the basic creation method. Note the following points:
1. "()" after add_test is required during creation and calling.
2. If "in", "out", and "inout" are not explicitly specified for the MySQL stored procedure parameters, the default value is "in", and the default value cannot be specified for the parameters.
3. When multiple SQL statements are included, the begin end keyword is required. A semicolon (;) must be added at the end of each statement in begin end.
4. DECLARE the variable in begin end and use the keyword DECLARE, for example:
 
SQL code
Begin

# Declare a name variable with the varchar type (remember the semicolon)
Name varchar (32 );
End;
 
 
Ii. Use of cursors
 
1. Define a cursor
SQL code
/*
Defines the CURSOR Keyword: CURSOR.
Cursor_name,
Cursor cursor_name current pointer record
Is a multi-row result set of a table from_data.
*/
DECLARE cursor_name cursor for select id, name

From from_data;
 
 
 
2. Open the cursor
 
SQL code
# Keywords: OPEN
OPEN cursor_name;
 
3. Obtain the cursor
 
SQL code
# Declare two variables
DECLARE a int

;
DECLARE B varchar (32)

;

/*
FETCH obtains the record of the current pointer of the cursor and transmits the record to the specified variables a and B.
*/
FETCH cursor_name INTO a, B;
 
Note: (1. This is very important. We will explain in detail how to use it in the following loop examples,
(2) Note that the number of variables must be the same as the number of fields returned by the MySQL cursor and the type. For details, see the red dot in step 2 and 3,
Id of type a and name of type B
 
4. Close the cursor
 
SQL code
CLOSE cursor_name;
 
 
The above is the common usage of cursors. I have already explained the key part in each step, so I will not talk about it. Now let's look at the example:
 
Iii. Example
 
SQL code
Drop procedure if exists add_test;
# Add_test


Create procedure add_test ()

BEGIN
# Defining variables


DECLARE a int;
DECLARE B VARCHAR (30 );

# This change is dispensable. To give a prompt after the storage function is successfully executed, you will know it when you run it.


DECLARE str VARCHAR (300 );
DECLARE x int;

# This is used to process the cursor reaching the last row


DECLARE

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.