Using cursors and temporary tables in MySQL stored procedures

Source: Internet
Author: User

Use the cursor and temporary table in the MySQL stored procedure:

  1. BEGIN
  2. DECLAREC_egidINTEGER;
  3. DECLAREDoneINT DEFAULT0;
  4. DECLAREMy_testCursorCURSOR FOR
  5. (SELECTCp. tb_electricGroup_idFROMTb_chargingPoleASCpWHERECp. tb_chargeStation_id = 12 );
  6. DECLARE CONTINUEHANDLERFOR NOTFOUNDSETDone = 1;
  7. DROP TABLEIf exists tmp_table;
  8. CREATE TEMPORARY TABLETmp_table (to create a temporary table, write it inDECLARENo syntax errors later
  9. Tmp_egidINTEGERSome table data formats have changed, such as datetimeTimestamp,BitIs 0, 1, and so on.
  10. );
  11. OPENMy_testCursor;
  12. Emp_loop: LOOP
  13. FETCHMy_testCursorINTOC_egid; the number of parameters required for each column of the cursor
  14. IF done = 1THENThe position of this sentence is very important. If it is not placed at the beginning, it may produce a Data repeat.
  15. LEAVE emp_loop;
  16. ENDIF;
  17. INSERT INTOTmp_table (tmp_egid)VALUES(C_egid); insert data into a temporary table
  18. ENDLOOP emp_loop;
  19. CLOSEMy_testCursor;
  20. Select*FromTmp_table;
  21. TRUNCATETmp_table;
  22. END

It is worth noting that declare continue handler for not found set done = 1

In a cursor loop, we use done 0 to determine whether to exit the loop. There is no problem in normal insert statements.

However, Handler is triggered when the select statement is used to find null in the cursor, And the done changes from 0 to 1.

Therefore, if the cursor contains a select statement, you can use other methods, such as the while loop. The following is an example of exiting the while loop by determining the length of the result set.

  1. SELECT COUNT(*)INTO@ PageCountFROMTmp_bwGetChargePoleCommunicationtable;
  2. SET@ Num = 0;
  3. OPENMy_testCursor;
  4. WHILE @ num <@ pageCount DO
  5. SET@ Num = @ num + 1;
  6. FETCHMy_testCursorINTOParam_id, param_ip, param_port, param_key;
  7. IF param_keyIS NULL THEN
  8. SELECTKiosk_keyINTO@ TmpParam_keyFROMTb_kioskWHEREKiosk_id = (SELECTTb_kiosk_idFROMTb_chargingPoleWHERECp_id = param_id );
  9. UPDATETmp_bwGetChargePoleCommunicationtableSETTmp_key = @ tmpParam_keyWHERETmp_id = param_id;
  10. ENDIF;
  11. ENDWHILE;
  12. CLOSEMy_testCursor;

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.