0

I am trying to execute below query but mysql says Syntax error: unexpected '@my_var' (at text suffix)

SET @my_var = 1;
INSERT INTO table1(id, myvar)
SELECT * FROM (LAST_INSERT_ID(), @my_var) AS tmp

How can I use declared variable in above query?

2
  • The syntax of your SELECT statement appears to be off. There is no table name after the FROM keyword. Commented Dec 10, 2015 at 5:46
  • @TimBiegeleisen It is valid. If I feed static value instead of @my_var it works. e.g. SELECT * FROM (LAST_INSERT_ID(), 1) AS tmp but I need to feed dynamic value using variable. Commented Dec 10, 2015 at 5:49

1 Answer 1

1

What you want to select, that you have putted in FROM.

In FROM there must be a table_name and not a column_name or function call.

Sign up to request clarification or add additional context in comments.

1 Comment

I just forgot to put SELECT, so the correct way is like this SELECT * FROM (SELECT LAST_INSERT_ID(), @my_var) AS tmp silly me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.