SQL:使用变量

时间:2011-01-03 19:39:50

标签: sql sql-server tsql sql-server-2008

如果满足以下条件但我不断收到语法错误

,我需要这个返回一个布尔值
declare @OverLimit bit = 0  
declare @var int

set @var = Select count(pkid)
             From Clicks

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

Select @OverLimit

5 个答案:

答案 0 :(得分:1)

以下是修正了语法错误的版本:

declare @OverLimit bit = 0 declare @var int

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

If @var > 720000 SET @OverLimit = 1

Select @OverLimit

问题是:

set - select语句需要括号

保留逗号(删除它)

答案 1 :(得分:1)

您可以尝试阅读SQL手册,错误消息并处理基础语法问题:

declare @OverLimit bit
set     @Overlimit = 0

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

If @var > 720000 set @OverLimit = 1

select @OverLimit

答案 2 :(得分:1)

试试这个:

declare @OverLimit bit = 0
declare @var int

SELECT @var = count(pkid) From Clicks

If @var > 720000 SET @OverLimit = 1

Select @OverLimit

或者:

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

答案 3 :(得分:0)

希望我在编写测试时能够访问SQL:但这不是错误的

set @var = Select count(pkid) From Clicks

 Select @var=count(pkid) From Clicks

答案 4 :(得分:0)

由于您未列出特定错误,因此我建议修改一些行。

  • 改变自:

    set @var = Select count(pkid) from Clicks
    

    要:

    set @var = (Select count(pkid) from Clicks)
    
  • 从720,000

  • 中删除逗号
  • 从If语句中删除“then”并插入“Set”:

    If @var > 720000
       set @OverLimit = 1