lundi 12 août 2019

MySQL IF THEN AS Syntax

I'm trying to populate a table via stored procedure as such:

SET @resultsCount = (SELECT COUNT(*) FROM tableA);
SET @i = 0;  

WHILE @i < @resultscount DO
  SET @revNum = 0;
  INSERT INTO tableB (Name, revNumber, Meta)
  SELECT
    name AS Name,
    (IF @i=0 THEN  
      SELECT @revNum;
    ELSE IF <condition> THEN
      SET @revNum = @revNum +1;
      SELECT @revNum;
    ELSE
      SELECT @revNum;  
    END IF) AS revNumber,
    meta AS Meta
  FROM tableA;
  SET @i = @i + 1;
END WHILE; 

However, the above IF statement fails due to syntax errors. I have tried to use the IF function, or case statements instead, but they seem to only allow a single statement to be executed after each condition, and I need to do a SET and then a SELECT in two separate statements.

Aucun commentaire:

Enregistrer un commentaire