Skip to content
Advertisement

SQL : Error 1064 in my query, i cannot find why

I’ve got this to create a trigger:

CREATE TRIGGER `create_account`
   AFTER INSERT
   ON
       `Client`
   FOR EACH ROW
BEGIN
   SET
       @lastID = (
           SELECT C.idClient
           FROM CLIENT as C
           ORDER BY C.idClient
               DESC
           LIMIT 1
       );
       SET 
       @iban = ('FR1330010000019111679074293');

   SELECT IF(
                  LENGTH(@iban) < 11,
                  CONCAT(
                          REPEAT('0', (11 - LENGTH(@iban))),
                          @iban
                      ),
                  @iban = @iban
              );
   INSERT INTO `Compte`(`idCompte`,
                        `numeroCompte`,
                        `soldeCompte`,
                        `idClientCompte`,
                        `statusCompte`)
   VALUES (NULL, @iban, '0', @lastId, '0');
END

And MySQL returns me

1064 – Syntax error near ” on line 14″

Someone have explanation?

Thank you all. I work to resolve it yet.

Advertisement

Answer

Did you wanted something like this ? If yes, then additional info: https://www.w3resource.com/mysql/control-flow-functions/if-function.php

The IF function does not need SELECT clause. I have tested it here: TEST

CREATE TRIGGER `create_account`
   AFTER INSERT
   ON
       `Client`
   FOR EACH ROW
BEGIN
   SET
       @lastID = (
           SELECT C.idClient
           FROM Client C
           ORDER BY C.idClient
               DESC
           LIMIT 1
       );
       SET 
       @iban = ('FR1330010000019111679074293');

   set @iban = IF(
                  LENGTH(@iban) < 11,
                  CONCAT(
                          REPEAT('0', (11 - LENGTH(@iban))),
                          @iban
                      ),
                  @iban
              );


   INSERT INTO Compte(idCompte,
                      numeroCompte,
                      soldeCompte,
                      idClientCompte,
                      statusCompte)
   VALUES (NULL, @iban, '0', @lastId, '0');

END;
/
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement