Skip to content
Advertisement

MySQL: Is it possible to set a variable outside of SELECT with SELECT as the only permission?

WHAT
I’m on MySQL 5.6 and my account only has permissions for SELECT and USAGE (as found using the SHOW GRANTS command).

Presumably because of this using DECLARE before SELECT returns a generic syntax error.

WHY
I want to use variables to both make the code easier to read and maintain. Below you can see variable @isOwn is needed in WHERE clause and the day differences could be shortened (note: DATEDIFF didn’t work for me in WHERE for some reason)

QUESTION
Being able to declare the variables before SELECT would simplify everything. Am I doing something wrong or can I not do this because of my minimal permissions?

CODE EXAMPLE

Advertisement

Answer

Use SET keyword instead of DECLARE. The latter is only needed in stored procedures and functions, to define local variables.

You need an user variable, which is different, for example it is not required to define it and it has no strict type. User variables have @ prefix, local variables don’t. But you can still access it, even from stored procedures/functions.

Also, as commented, you need to separate the two statements (SET and SELECT) with a delimiter ; (or perhaps call them as two statements on the same connection).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement