Petr Novák
Petr Novák

Reputation: 115

Mysql stored procedure if (select....) > variable then

I want to do something like:

if ((select count(id) from abc where ...) > variableINT) then .....

Why does this doesn´t work properly?

Upvotes: 0

Views: 4547

Answers (2)

Andy Chapman
Andy Chapman

Reputation: 41

Let's say you have a link table (mylinks) with links from source_id to target_id and you want to find only the targets that have more than 5 links. This will work in MySQL. Not sure about MS-SQL though.

select target_id, count(source_id) as num_links from mylinks group by target_id having num_links>5;

Upvotes: 0

giorashc
giorashc

Reputation: 13713

Do the following in your stored procedure (I assume you basic knowledge of declaring variables and writing queries) :

Declare a variable count_id : count_id NUMBER;

Then run your query : select count(id) into count_id from abc where ...

Then you can test that variable : if (count_id > ...) then .... end if

Upvotes: 3

Related Questions