Problems doing simple insert in the bank - Unexpected Token

0

I'm having trouble inserting into the database, could anyone tell me what the possible problems are?

SQL 1:

INSERT INTO data_inicio (data_sys, descricao)
VALUES (NOW(),'Data de inicio'); 

Error in sql: Unexpected token near 'Start date'

SQL 2:

INSERT INTO curso_desconto (id_curso,id_desconto) VALUES((SELECT 
MAX(id) FROM curso), '1');

Error in this sql: comma or a closing bracket was expected

    
asked by anonymous 19.04.2018 / 16:08

2 answers

1

Using the INSERT INTO <<table_name>> VALUES ((...)) syntax you should provide literal values, rather than functions like NOW() .

This is defined in the official MySql documentation :

  

The INSERT ... VALUES and INSERT ... SET [are] forms of the statement insert rows based on explicitly specified values.

Already to insert the result of select, also known as 'INSERT SELECT' , syntax it would be like this :

INSERT INTO <<table_name>> [(column_name1, column_name2, ...)] SELECT 5, NOW()

Without writing 'VALUES'

See an example of your case working in SQL Fiddle

    
19.04.2018 / 17:03
0

With the suggestions made to me, I could not solve the problem, but I managed to solve it like this:

Instead of using now () I used CURRENT_TIMESTAMP, getting 1 query like this:

INSERT INTO data_inicio (data_sys, descricao)
VALUES (CURRENT_TIMESTAMP,'Data de inicio'); 

In the second case, I was able to solve by taking the maximum id by playing for a variable, as follows:

SELECT @max := MAX(curso.id)  FROM curso;
INSERT INTO curso_desconto (id_curso,id_desconto) VALUES(@max, '1');

Thanks to everyone who tried to help!

    
20.04.2018 / 14:56