title: START TRANSACTION inside BEGIN ... END context or outside and LOOP syntax
body: <p>I have two questions about Compound-Statement and Transactions in MySQL.</p>
  
  <p>FIRST:</p>
  
  <p>There are two notes in MySQL Manual:</p>
  
  <blockquote>
    <p>Note</p>
    
    <p>Within all stored programs, the parser treats BEGIN [WORK] as the
    beginning of a BEGIN ... END block. To begin a transaction in this
    context, use START TRANSACTION instead.</p>
    
    <p>Note</p>
    
    <p>Within all stored programs (stored procedures and functions, triggers,
    and events), the parser treats BEGIN [WORK] as the beginning of a
    BEGIN ... END block. Begin a transaction in this context with START
    TRANSACTION instead.</p>
  </blockquote>
  
  <p>I can't understand what exactly is meant. They mean that I have to put <code>START TRANSACTION</code> instead of <code>BEGIN</code> or right after <code>BEGIN</code>?</p>
  
  <pre><code>// 1st variant:
  
  BEGIN
     START TRANSACTION
     COMMIT
  END
  
  
  // 2nd variant:
  
  START TRANSACTION
  COMMIT
  END
  </code></pre>
  
  <p>Which one is the right way, 1st variant or 2nd variant?</p>
  
  <p>SECOND:</p>
  
  <p>I don't want to create a Stored Procedure or Function. I just want to create a Compound-Statement Block with a loop inside it in the general flow, like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Is it possible such kind of structure? Because I have an error thrown:</p>
  
  <pre><code>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 ...
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Is it allowed to use <code>BEGIN...END</code> just in general flow without creating and using Stored Procedures or Functions?</li>
  <li><p>Is it allowed to use <code>BEGIN...END</code> inside of <code>START TRANSACTION...COMMIT</code> or I have to put <code>START TRANSACTION...COMMIT</code> inside of <code>BEGIN...END</code>?</p>
  
  <pre><code>BEGIN
     START TRANSACTION
     COMMIT
  END
  
  // vs.
  
  START TRANSACTION
     BEGIN
     END
  COMMIT
  </code></pre></li>
  <li><p>Do I by all means have to use <code>BEGIN...END</code> if I want to use only <code>LOOP</code>? May I just use <code>LOOP</code> syntax without starting <code>BEGIN...END</code>? The only example in the manual for <code>LOOP</code> is this:</p>
  
  <pre><code>  CREATE PROCEDURE doiterate(p1 INT)
       BEGIN
         label1: LOOP
           ... 
  </code></pre></li>
  </ol>