Creating stored procedures and triggers
1. Build a table
First, two tables (the Users table and the number table) are built, specifically designed as:
2. Stored Procedures
Write a stored procedure that inserts data into the users table, creating the following process:
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 data in the Users table as follows:
The entire stored procedure is written, which is, of course, a very simple example and is for reference only.
3. Trigger
Before we write the trigger, we'll erase the data from users.
[SQL]View Plaincopy
- Truncate table users;
Now that we have two tables, the thing I'm going to do is that when I insert data into users, the number changes as well:
For example, the number table has an initial data, id=1,num=0;
When I insert a piece of data into users, it triggers the NUM field in the Number table to add 1, which is how many users are logged.
Let's implement this little feature.
Right-click the Users table, select 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 then look at the data in number, you will magically find that the data in the number table has changed, try it yourself!
PS: Stored procedures require programmers to execute their own, triggers, as the name implies, automatically triggered.
4. Cursor use
First, I'll empty the data in the Users table (of course not emptying it), and then populate it with data, as shown in:
What I want to do now is to add 100 to the student_id field, and this example simply shows the use of a downstream label.
Create a stored procedure that references the steps above. 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; --Defining cursors
- /*
- 02000 one of the following exceptions occurred:
- The result of the subquery for the SELECT into statement or INSERT statement is an empty table.
- The number of rows identified in the UPDATE or DELETE statement of the search is zero.
- The cursor position referenced in the FETCH statement is after the last row in the result table.
- */
- declare CONTINUE HANDLER for SQLSTATE ' 02000 ' set tmp = 0;
- OPEN cur; --Open cursor
- FETCH cur into tmp; --the cursor goes down one step
- while (tmp! = 0)
- Do
- Select tmp; --Print tmp out, you will find that TMP is like a pointer, starting at the first line, the cursor one step, pointing to the next row of records
- UPDATE Users
- SET student_id = tmp +
- WHERE student_id = tmp;
- FETCH cur into tmp;
- END while;
- CLOSE cur; --Close cursor
- END
By executing the stored procedure above, you will find that the data in users has changed as you wish.
Of course, this function directly with the loop can be solved, I am just a simple demonstration of the use of a downstream standard, conducive to the cursor has a perceptual knowledge.
That ' s all~
Original: http://blog.csdn.net/cqnuztq/article/details/9735245
Using Navicat to create stored procedures, triggers, and simple instances using cursors