Using Navicat to create stored procedures, triggers, and simple instances using cursors

Source: Internet
Author: User

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
  1. BEGIN
  2. #Routine body goes here ...
  3. declare n bigint;
  4. set n = 201121029684;
  5. While N <= 201121029694
  6. Do
  7. INSERT INTO users (student_id) values (n);
  8. Set n = n + 1;
  9. End While;
  10. 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
    1. 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
    1. Begin
    2. Update number
    3. Set num = (Select count (*) from users);
    4. 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
  1. BEGIN
  2. #Routine body goes here ...
  3. declare tmp bigint default 0;
  4. declare cur CURSOR for SELECT student_id from users; --Defining cursors
  5. /*
  6. 02000 one of the following exceptions occurred:
  7. The result of the subquery for the SELECT into statement or INSERT statement is an empty table.
  8. The number of rows identified in the UPDATE or DELETE statement of the search is zero.
  9. The cursor position referenced in the FETCH statement is after the last row in the result table.
  10. */
  11. declare CONTINUE HANDLER for SQLSTATE ' 02000 ' set tmp = 0;
  12. OPEN cur; --Open cursor
  13. FETCH cur into tmp; --the cursor goes down one step
  14. while (tmp! = 0)
  15. Do
  16. 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
  17. UPDATE Users
  18. SET student_id = tmp +
  19. WHERE student_id = tmp;
  20. FETCH cur into tmp;
  21. END while;
  22. CLOSE cur; --Close cursor
  23. 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

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.