Problems Making Simple Insert in Bank - Unexpected Token

Asked

Viewed 104 times

0

I’m having problems making entries in the database, someone could tell me what the possible problems?

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 sql: a comma or a closing Bracket was expected

  • It was resolved?

  • Yes, I answered my question to show the resolution.

2 answers

1

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

This is defined in official Mysql documentation:

The INSERT ... VALUES and INSERT ... SET [are] Forms of the statement Insert Rows based on explicitly specified values.

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

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

Without writing the 'VALUES'

See an example of your case working on SQL Fiddle

0

With the suggestions made to me, I could not solve the problem, but I managed to solve it in the following way:

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 playing for a variable, as follows:

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

Thank you to everyone who tried to help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.