title: SQL variable in IF exists
body: <p>I can't figure out the correct way to assign query output into a variable that i could later use in an INSERT clause.</p>
  
  <p>The error I get is  </p>
  
  <blockquote>
    <p>ERROR 1064 (42000): 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 @lennuid = (select lend_id from lend where     sihtpunkt=in_kuhu AND kuupaev=in_' at line 8</p>
  </blockquote>
  
  <p>I've tried looking at every guide on variables in SQL but none of them could help me solve this.</p>
  
  <p>Iam using the below  code:</p>
  
  <pre><code>CREATE PROCEDURE proov(
  in_kuhu varchar(50), in_nimi1 varchar(50), in_nimi2 varchar(50), in_adre varchar(50), in_telo varchar(20), in_email varchar(100), in_date date)
  BEGIN
  DECLARE viga INTEGER DEFAULT 0 ;
  DECLARE lennuid INT;
  START TRANSACTION;
  IF exists (
  SET @lennuid = (select lend_id from lend where sihtpunkt=in_kuhu AND kuupaev=in_date)) THEN
  INSERT INTO broneering
  (lend_id, bron_aeg, eesnim, perenimi, aadress, telefon, email)
  VALUES
  (lennuid, NOW(), in_nimi1, in_nimi2, in_adre, in_telo, in_email);
  ELSE
  set viga=1;
  SELECT 'Muudatus ebaonnestus ',viga;
  END IF;
  IF viga=0 then
  COMMIT;
  select 'korras';
  ELSE
  select 'tagasi';
  ROLLBACK;
  END IF;
  </code></pre>