Create a stored procedure and trigger
1. Create a table
First, create two tables (users table and number table). The specific design is as follows:
2. Stored Procedure
Write a stored procedure and insert data into the users table. The creation procedure is as follows:
The Code is as follows:
[SQL]View plaincopy
- Begin
- # Routine body goes here...
- Declare n bigint;
- Set n = 201121029684;
- While n <= 201121029694
- Do
- Insert into users (student_id) values (N );
- Set n = n + 1;
- End while;
- End
After executing the stored procedure, you can see the following data in the users table:
The entire stored procedure is completed. Of course, this is just an extremely simple example and is only for reference.
3. triggers
Before writing a trigger, we first clear the users data.
[SQL]View plaincopy
- Truncate table users;
Now we have two tables. What I want to do is, When I insert data into users, the number also changes accordingly:
For example, an initial data entry in the number table, id = 1, num = 0;
When I insert a piece of data into users, the num FIELD IN THE NUMBER table is triggered to add 1, that is, the number of records.
Next we will implement this small feature.
Right-click the users table and select the design table.
Select trigger options
Code:
[SQL]View plaincopy
- Begin
- UPDATE Number
- Set num = (select count (*) from users );
- End
After saving, add new data to the users table, and check the data in number. You will find that the data in the number table has also changed. Try it on your own!
PS: the stored procedure needs to be executed by the programmer. The trigger, as the name suggests, is automatically triggered.
4. Use a cursor
First, clear the data in the users table (of course, you can do it without clearing it), and then fill in the data in it, as shown in:
What I want to do now is to add the student_id field to 100. This example shows how to use the cursor.
Create a stored procedure. For more information about how to create a stored procedure, see the preceding steps. The Stored Procedure Code is as follows:
[SQL]View plaincopy
- Begin
- # Routine body goes here...
- Declare TMP bigint default 0;
- Declare cur cursor for select student_id from users; -- defines the cursor
- /*
- 02000 one of the following exceptions occurs:
- The results of the select into statement or the insert statement subquery are empty tables.
- The number of lines identified in the update or delete statement is zero.
- The cursor referenced in the fetch statement is placed after the last row of the result table.
- */
- Declare continue handler for sqlstate '200' set TMP = 0;
- Open cur; -- open the cursor
- Fetch cur into TMP; -- move the cursor down
- While (TMP! = 0)
- Do
- Select TMP; -- print the TMP, and you will find that TMP is like a pointer. It points to the first row at the beginning, and the cursor points to the next row of records.
- Update users
- Set student_id = TMP + 100
- Where student_id = TMP;
- Fetch cur into TMP;
- End while;
- Close cur; -- close the cursor
- End
When you execute the above stored procedure, you will find that the data in users has changed as you wish.
Of course, this function can be solved directly through loops. Here I will simply show the usage of the cursor, which is conducive to a perceptual knowledge of the cursor.
That's all ~
Create a stored procedure and trigger