0

I'd like to declare an int variable perform a count query and set the result to that variable, but mysql doesnt recognize the declared variable I've already tried this:

DECLARE itExists INT DEFAULT 0;
SELECT COUNT(*) FROM grades WHERE statId = 1 INTO itExists;
SELECT itExists AS cnt;

it always says:

Query: DECLARE itExists INT DEFAULT 0

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DECLARE itExists INT DEFAULT 0' at line 1

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0 sec
--------------------------------------------------

Query:  select count(*) from grades WHERE statId = 1 into itExists

Error Code: 1327
Undeclared variable: itExists

1 Answers1

0

You have to use session variables

SELECT COUNT(*) FROM grades WHERE statId = 1 INTO @itExists;
SELECT @itExists AS cnt;
nbk
  • 45,398
  • 8
  • 30
  • 47