views:

138

answers:

2

Over the last couple of days I have tried to write an Stored procedure in MySQL and I have some truble getting it to work. Hope someone here can give me some input :)

The example I post is for asp.Net Membership provider to create a new user. I expect to send email and password to the DB and get an int return to verify that the userdeatils was written to the DB.

I use a MySQL DB 5.1 (I think) and write the SQL to a webinterface.

I got 2 sidequestions, can someone explain that too :): 1) I use a DELIMITER, but do not know what it does. 2) I am not sure if I have to do other things then to set autocommit = 0 to get transactions to work, or if I even have to do that.

I know that I could have used a IF / ELSE statement instead of a transaction, but would like to do it with one to find out how it works. (I expect to use it alot later)

The code I can not get to work:

DELIMITER //

CREATE DEFINER=`websharp_dk`@`%` PROCEDURE `CreateUser`(
    IN _username VARCHAR(100),
    IN _Password VARCHAR(100))
    RETURNS INT

    BEGIN
    SET autocommit = 0;
    DECLARE return_value INT;

    BEGIN TRY
     START TRANSACTION
       INSERT INTO User 
       (Email
       ,Password
       ,Failed_Password_Count
       ,Creation_Date)
       VALUES
       (_username
       ,_Password
       ,0
       ,Datetime.Now())
      SET return_value = 1;
     COMMIT;
    END TRY

    BEGIN CATCH
     ROLLBACK
     SET return_value = 0;
    END CATCH

    BEGIN FINALLY
    RETURN return_value;
    END FINALLY 
    END//
DELIMITER ;

Edit: The error message I get is:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT BEGIN SET autocommit = 0; DECLARE return_value INT; ' at line 4

+1  A: 

To get support for transactions, make sure you are using the InnoDB storage engine rather than the default MyISAM.

As far as that code itself, my first question would be, why are you wrapping that single query in a transaction? Also, what errors are you seeing?

phoebus
Looking up InnoDB now, thx. I just posted the error above, should have done that from the start, sorry
Jorn
Thx for the info about the DB engine. It seems like I got the InnoBD by luck :)
Jorn
+1  A: 

The delimiter redefines what sequence of characters you use to end an sql statement. The entire create procedure is one big statement and you need to tell MySQL where it ends with something (would normally be ';'). But since you have a bunch of other statements in the "body" (between BEGIN and END) of the create procedure statement that all need to be ended too you need to redefine the delimiter so you don't end the create procedure statement at the first ';'.

Without redefining the delimiter, MySQL would think that the create procedure statement looked like this and then begin a new statement:

CREATE DEFINER=`websharp_dk`@`%` PROCEDURE `CreateUser`(
    IN _username VARCHAR(100),
    IN _Password VARCHAR(100))
    RETURNS INT

    BEGIN
    SET autocommit = 0;

Using DELIMITER ; at the end of the script changes the delimiter back to ';' and is not needed although it's good practice to do so.

Andreas