vbNewbie
vbNewbie

Reputation: 3345

use result of sql select in if else

I am trying to construct a query where I check the number of rows returned from a sql select. For example, I want to check that if the number of rows returned from a query are greater than 3, then do something else do nothing

if @@rowcount(select clientId from Clients group by clientId) > 3
   PRINT 'WARNING'

Any ideas appreciated

Upvotes: 1

Views: 594

Answers (3)

teenboy
teenboy

Reputation: 368

Hope this helps

[EDITED]

DECLARE @Cnt AS INT
select @Cnt = COUNT(clientId) from Clients group by clientId
if @Cnt > 3
PRINT 'WARNING'

Upvotes: 1

Aaron Bertrand
Aaron Bertrand

Reputation: 280351

DECLARE @Count INT = (SELECT COUNT(DISTINCT ClientId) FROM Clients);

IF @Count > 3
    PRINT 'WARNING';

Upvotes: 3

AJC
AJC

Reputation: 1893

Try:

case
    when (select count(*) from table where condition) > 3 Then
    else
end

Hope this helps...

Upvotes: 4

Related Questions