mariadb declare variable syntax error
Asked Answered
H

4

7

Im using mariadb with heidisql to execute sql:

DECLARE @AccountID INT;

Insert Into accounts(first_name, mi, last_name, email, is_admin, is_enabled, date_created) Values('testfirstname', 'a', 'testlastname', '[email protected]', 1, 1, NOW());

set @AccountID = Last_Insert_Id();

I keep getting an error:

QL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '@AccountID INT' at line 1 */

I was looking at the usage of Declare but it says that its used in a Function. Ive tried with a BEGIN/END before the DECLARE and after with the same error.
I new to the mysql syntax where this would work in sql server. so if anyone can let me know what i got wrong it would be appreciated.

Thanks

Horticulture answered 12/1, 2014 at 1:38 Comment(0)
C
2

Remove the DECLARE. It is not neccessary.

Convince answered 12/1, 2014 at 1:41 Comment(2)
why at their official website they still use "DECLARE" https://mariadb.com/kb/en/library/while/Adamok
removing the DECLARE keyword doens't solve the problem at all for me.Kosel
T
10

I'm using mariadb version 10.0.19

The above problem is described and has an easy solution
here: Delimiters in the mysql Client

in short:

DELIMITER //
create function .. as usual
BEGIN
.. as usual
END
//
DELIMITER ;

( The accepted solution did not worked ! )

Tracheid answered 10/6, 2015 at 20:23 Comment(0)
C
2

Remove the DECLARE. It is not neccessary.

Convince answered 12/1, 2014 at 1:41 Comment(2)
why at their official website they still use "DECLARE" https://mariadb.com/kb/en/library/while/Adamok
removing the DECLARE keyword doens't solve the problem at all for me.Kosel
P
1

Just Ignore the DECLARE statement, and execute the Insert.

Insert Into accounts(first_name, mi, last_name, email, is_admin, is_enabled, date_created) Values('testfirstname', 'a', 'testlastname', '[email protected]', 1, 1, NOW());

set @AccountID = Last_Insert_Id();
SELECT @AccountID;

Will get you the last inserted Id of the "accounts" table.

Pious answered 7/1, 2022 at 18:57 Comment(0)
S
0

I had the same problem I created my stored procedure in design

enter image description here enter image description here

I wrote my stored procedure of function in BEGIN and END and it worked

Segregate answered 6/9, 2022 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.