Syntax error in MySQL script

1

I created a simple script in MySQL .... the idea is to create a procedure that shows the data of the last record of a specific table.

This would be the code of the script:

DROP PROCEDURE IF EXISTS mostrarUltimaAlta;

DELIMITER $$
CREATE PROCEDURE mostrarUltimaAlta ()

BEGIN

SELECT id, nombre_completo, correo FROM USUARIOS WHERE id = (select max(id));

END;

$$
DELIMITER;

The idea is to make an initial drop to eliminate some similar procedure already created.

Then with delimiter I create the content of script ...

during the procedure I make a select to show the desired fields of the last inserted record.

Finally the procedure ends ...

Once I finish I save the code of the script and I leave MySQL ... and from prompt I try to launch the script:

 usuario@ubuntu: mysql -u root -p DB_MENSAJES <script.sql

But when I run it, it gives me this error:

  

ERROR 1064 (42000) at line 12: 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 'DELIMITER' at line 1

I can not see what is wrong with the code so that the delimiter does not work ... can you help me? thanks !!

    
asked by Daniel Martinez 02.12.2016 в 17:05
source

1 answer

1

You are missing a space between DELIMITER and ; :

DELIMITER ;

But also, you will notice that the SQL query will not give you the expected result. You have to add the FROM in the subquery to give you the correct results:

SELECT id, nombre_completo, correo 
  FROM USUARIOS 
 WHERE id = (select max(id)
               from usuarios);

Or, you can avoid the subquery by using LIMIT :

select id, nombre_completo, correo
  from usuarios
 order by id desc
 limit 1;
    
answered by 02.12.2016 / 17:10
source