I have two questions about Compound-Statement and transactions in MySQL.
FIRST:
There are two notes in the MySQL manual:
Note
Inside all saved programs, the parser considers BEGIN [WORK] as the beginning of the BEGIN ... END block. To start a transaction in this context, use START TRANSACTION instead.
Note
In all stored programs (stored procedures and functions, triggers, and events), the parser considers BEGIN [WORK] as the beginning of BEGIN ... END. Start the transaction in this context with START Instead, the transaction.
I donβt understand what exactly is meant. Do they mean that I have to put START TRANSACTION instead of BEGIN or right after BEGIN ?
// 1st variant: BEGIN START TRANSACTION COMMIT END // 2nd variant: START TRANSACTION COMMIT END
Which one is correct, 1st option or 2nd option?
SECOND:
I do not want to create a stored procedure or function. I just want to create a Compound-Statement block with a loop inside it in a common thread, for example:
USE 'someDb'; START TRANSACTION ... create table statement ... insert statement // now I want to implement some insert/select statements using loop, I do as follows: DELIMITER $ BEGIN SET @n = 1, @m = 2; lab1: LOOP ... some insert, select statements here END LOOP lab1; END $ DELIMITER ; END COMMIT
Is such a structure possible? Because I have an error:
Query: BEGIN SET @n = 1, @m = 2; lab1: LOOP SELECT ... Error Code: 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 'SET @n = 1, @m = 2; lab1: LOOP SELECT ...
My questions:
- Is it allowed to use
BEGIN...END only in a common thread without creating and using stored procedures or functions? Is it allowed to use BEGIN...END inside START TRANSACTION...COMMIT or do I need to put START TRANSACTION...COMMIT inside BEGIN...END ?
BEGIN START TRANSACTION COMMIT END // vs. START TRANSACTION BEGIN END COMMIT
Should I use BEGIN...END if I want to use only LOOP ? Is it possible to use LOOP syntax without running BEGIN...END ? The only example in the manual for LOOP is the following:
CREATE PROCEDURE doiterate(p1 INT) BEGIN label1: LOOP ...
loops mysql stored-procedures transactions
Green 04 Sep 2018-12-12T00: 00Z
source share