Fix the last row of the MySQL cursor

Source: Internet
Author: User
Tags mysql tutorial
When using the MySQL cursor, I found that the last row was repeatedly read. Later I found that I should put the read data in the next loop to avoid repeated Code as follows:

When using the MySQL cursor, I found that the last row was repeatedly read. Later I found that I should put the read data in the next loop to avoid repeated Code as follows:

Homepage → Database Technology

Background:

Read News

Fix the last row of the MySQL cursor

[Date: 2011-07-20] Source: Linux Author: babaoqi [Font:]

When MySQL cursor is used, the last row is read repeatedly.

Later, we found that we should put the read data in the next loop to avoid duplication.

The Code is as follows:
DELIMITER $

USE 'test' $

Drop procedure if exists 'SP _ getalltablename' $

Create procedure 'SP _ getalltablename' (OUT strAllTableNames VARCHAR (10000 ))
BEGIN
DECLARE bEnd boolean default false;
DECLARE tbName VARCHAR (255 );

# Declare a cursor
DECLARE curTableNames cursor for select TABLE_NAME FROM information_schema.tables AS t WHERE t. table_schema = "test ";

# DECLARE CONTINUE HANDLER
Declare continue handler for sqlstate '000000' SET bEnd = TRUE;

SET strAllTableNames = "";

# Open a cursor
OPEN curTableNames;

# Retrieving data from all rows
# Obtain the first line of content
FETCH curTableNames INTO tbName;

# Loop start
REPEAT
SET strAllTableNames = CONCAT (strAllTableNames, tbName ,";");
# Obtain the content of the next row
FETCH curTableNames INTO tbName;
# Loop end
UNTIL bEnd end repeat;

# Closing a cursor
CLOSE curTableNames;

# Processed
SET strAllTableNames = CONCAT ("All table names:", strAllTableNames );

END $

DELIMITER;

CALL test. sp_getAllTableName (@ allNames );

SELECT @ allNames;

  • 0
  • Set CentOS to automatically start Oracle at startup

    Oracle Shared Pool optimization ideas

    MySQL tutorial

    Image Information

    View All comments in this article (0)

    Comments

    Latest Information

    Popular this week

    Introduction to Linux community-advertisement service-website map-help Info-contact us
    The articles published on this site (LinuxIDC) do not mean that they agree with the statement or description. They only provide more information and do not constitute any suggestions.


    Copyright©2006-2011 Linux community All rights reserved Zhejiang ICP backup No. 06018118

    ,

    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.