kev
kev

Reputation: 7

SQL: Working with variables

I need this to return a boolean value if the following condition is met but I keep getting syntax errors

declare @OverLimit bit = 0  
declare @var int

set @var = Select count(pkid)
             From Clicks

If @var > 720,000 then
  @OverLimit = 1

Select @OverLimit

Upvotes: 0

Views: 592

Answers (5)

garths
garths

Reputation: 1

Since you didn't list specific errors, here are some lines I would recommend fixing.

  • Change from:

    set @var = Select count(pkid) from Clicks
    

    To:

    set @var = (Select count(pkid) from Clicks)
    
  • remove the comma from 720,000

  • remove "then" from If statement and insert "Set":

    If @var > 720000
       set @OverLimit = 1
    

Upvotes: 0

Cade Roux
Cade Roux

Reputation: 89661

Try this:

declare @OverLimit bit = 0
declare @var int

SELECT @var = count(pkid) From Clicks

If @var > 720000 SET @OverLimit = 1

Select @OverLimit

Or alternatively:

SELECT CASE WHEN COUNT(pkid) > 720000 THEN CAST(1 AS bit) ELSE CAST(0 AS bit) END AS Overlimit
FROM Clicks

Upvotes: 1

Nicholas Carey
Nicholas Carey

Reputation: 74267

You might try reading the SQL manual, and the error messages and dealing with the underlying syntax issues:

declare @OverLimit bit
set     @Overlimit = 0

declare @var int
select  @var = count(pkid) From Clicks

If @var > 720000 set @OverLimit = 1

select @OverLimit

Upvotes: 1

Joon
Joon

Reputation: 2147

Here is a version with the syntax errors rectified:

declare @OverLimit bit = 0 declare @var int

set @var = (Select count(pkid) From Clicks)

If @var > 720000 SET @OverLimit = 1

Select @OverLimit

The problems were:

The set - select statement needed brackets

The comma is reserved (removed it)

Upvotes: 1

Andiih
Andiih

Reputation: 12413

Wishing I had access to SQL as I write this to test: but isn't this wrong

set @var = Select count(pkid) From Clicks

try

 Select @var=count(pkid) From Clicks

Upvotes: 0

Related Questions