2

Just installed sql on ubuntu 21.04.

$ mysql -V
mysql  Ver 8.0.26-0ubuntu0.21.04.3 for Linux on x86_64 ((Ubuntu))

Wrote a test.sql file:

declare @a as int=4

On executing mysql> source /home/home/test.sql on the command line, the following error is returned:

ERROR 1064 (42000): 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 'declare @a as int=4' at line 1

adding begin and end to the script doesn't help either. What is the fix?

2
  • any reason you are using mysql instead of mariadb? Commented Aug 11, 2021 at 21:57
  • no particular reason. I do not know what is mariadb. Commented Aug 12, 2021 at 5:57

2 Answers 2

2

First you need to declare the variable

DECLARE @MyVariable INT;

and then you can set it

SET @MyVariable = 1;
Sign up to request clarification or add additional context in comments.

Comments

1

You can only use declared variables in stored procedure. In an ordinary SQL script, use user variables, which are names beginning with @. You don't specify a type, they can hold any type of value.

SET @a = 4;

3 Comments

thanks. this didn't return any error. How can I print the value of the variable? print @a is not working. Also, how to perform mathematical operations? @a=@a+1 does not work.
what is 'stored procedure'?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.