Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to implement Transaction in a MySql Stored Procedure with Cursor ?

How to implement Transaction in a MySql Stored Procedure with Cursor ?

Scheduled Pinned Locked Moved Database
databasehelpmysqltutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fearless stallion
    wrote on last edited by
    #1

    Hi I am using a MySql 5.0 DB. am trying to implement a transaction inside a stored procedure which uses cursors. but the problem is the way the cursor closes it self. it throws an exception, i have written a handler for any sqlexception inside the procedure which simply rolls back, and because of the cursor , transaction gets rolled back without completing the task. Has anybody ever worked on MySql with transactions. Please Help. thanks

    KSS

    D 1 Reply Last reply
    0
    • F fearless stallion

      Hi I am using a MySql 5.0 DB. am trying to implement a transaction inside a stored procedure which uses cursors. but the problem is the way the cursor closes it self. it throws an exception, i have written a handler for any sqlexception inside the procedure which simply rolls back, and because of the cursor , transaction gets rolled back without completing the task. Has anybody ever worked on MySql with transactions. Please Help. thanks

      KSS

      D Offline
      D Offline
      DLM TD
      wrote on last edited by
      #2

      Hi, the following stored procedure will work properly with transactions and cursors: DELIMITER $$ DROP PROCEDURE IF EXISTS `testdb`.`TestCursorRollback` $$ CREATE PROCEDURE `testdb`.`TestCursorRollback` () BEGIN declare n int; DECLARE done INT DEFAULT 0; declare cur1 cursor for select col1 from hotels.a; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; start transaction; open cur1; repeat fetch cur1 into n; if not done then insert into testdb.b(col1) values (n*10); end if; UNTIL done END REPEAT; close cur1; rollback; /* or commit*/ insert into testdb.b(col1) values(200); END $$ DELIMITER ;

      FREE cross database comparison and synchronization tools DBTYP.NET - see database differences, for free (SQL Server, MySQL, Oracle)

      F 1 Reply Last reply
      0
      • D DLM TD

        Hi, the following stored procedure will work properly with transactions and cursors: DELIMITER $$ DROP PROCEDURE IF EXISTS `testdb`.`TestCursorRollback` $$ CREATE PROCEDURE `testdb`.`TestCursorRollback` () BEGIN declare n int; DECLARE done INT DEFAULT 0; declare cur1 cursor for select col1 from hotels.a; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; start transaction; open cur1; repeat fetch cur1 into n; if not done then insert into testdb.b(col1) values (n*10); end if; UNTIL done END REPEAT; close cur1; rollback; /* or commit*/ insert into testdb.b(col1) values(200); END $$ DELIMITER ;

        FREE cross database comparison and synchronization tools DBTYP.NET - see database differences, for free (SQL Server, MySQL, Oracle)

        F Offline
        F Offline
        fearless stallion
        wrote on last edited by
        #3

        Hi, Thanks for the reply but i was looking for another handler for the rollback part. Like : Declare Exit handler for SQLEXCEPTION ROLLBACK; I want my proc to roll back in case of any error. because once i find any error it will throw an exception to my application which can never rollback-ed thanks Siva

        kss

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups