Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown system variable using DECLARE

Tags:

sql

mysql

I have the following MySQL Query.

DECLARE RecCount Int Default 0;
SET RecCount=(select count(*) from tables where WaiterID=1 and date(TDate)=date(now()));

When I execute it, I get the following error:

Error Code: 1193. Unknown system variable 'RecCount'

So, I googled 'declaring variables in MySQL.' It appears that everyone uses DECLARE:

  • First Result
  • Second Result
  • Third Result
  • Fourth Result
  • Fifth Result

I don't understand why my query doesn't execute.

I'm using MYSQL WORKBENCH 6.0 CE and I eventually want to execute this query in PHP.

EDIT:
I have used the default as 0. If I remove the default 0, AND change the query to what I've written below, it executes. But it still doesn't save the result like it should!

SET RecCount=(select count(*) from tables where WaiterID=1 and date(TDate)=date(now()));
SELECT RecCount;
like image 595
Jacques Koekemoer Avatar asked Oct 15 '25 02:10

Jacques Koekemoer


1 Answers

You have to run that code in a procedure or function.

If you use control statements you have to use them in a function or procedure.

MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE, and REPEAT constructs for flow control within stored programs.

Source

like image 57
juergen d Avatar answered Oct 17 '25 14:10

juergen d