"MySQL" stored procedures, cursors, circular simple instances

Source: Internet
Author: User

Sometimes the SQL statement may not be able to achieve the desired data operation purposes, it may be necessary to write some method body, through the cycle of judgment and other operations to achieve the end. Then the implementation of this method in the database needs a stored procedure, the individual think a simple example with annotations can be easily rough to solve most of the problems, of course, to learn more about the words or to see the tutorial documentation, not much said, on the code:

[SQL]View PlainCopy
  1. CREATE PROCEDURE My_procedure () --Creating a stored procedure
  2. Begin --Start the stored procedure
  3. DECLARE my_id varchar (32); --Custom variable 1
  4. DECLARE my_name varchar (50); --Custom variable 2
  5. DECLARE done INT DEFAULT FALSE; --Custom control cursor loop variable, default false
  6. DECLARE my_cursor Cursor for ( SELECT ID, name from t_people); --Define a cursor and enter a result set
  7. DECLARE CONTINUE HANDLER for Don't FOUND SET done = TRUE; --Bind the control variable to the cursor, and the cursor loop end automatically goes true
  8. OPEN My_cursor; --Open cursor
  9. Myloop:loop --Starts the loop body, myloop the custom loop name, and ends the loop with
  10. FETCH my_cursor into my_id, my_name; --assigns the data order of the cursor currently reading rows to the custom variable
  11. If do then --determine if the loop continues
  12. LEAVE Myloop; --End Loop
  13. END IF;
  14. --The things you want to do, use custom variables directly in SQL
  15. UPDATE t_user SET c_name = my_name WHERE id = my_id and RTrim (LTrim (c_name)) = '; --left and right to go space
  16. COMMIT; --Commit a transaction
  17. END LOOP Myloop; --End the custom loop body
  18. CLOSE My_cursor; --Close cursor
  19. END; --End stored procedure

SQL Call Stored Procedure

[SQL]View PlainCopy
    1. --SQL execution stored procedure
    2. Call My_procedure ();

SQL Delete stored procedure

[SQL]View PlainCopy
    1. --SQL Delete stored procedure
    2. Drop procedure my_procedure;

End.

"MySQL" stored procedures, cursors, circular simple instances

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.