Sql 使用变量和TOP子句发出

Sql 使用变量和TOP子句发出,sql,tsql,sql-server-2005,Sql,Tsql,Sql Server 2005,尝试运行以下代码时,我遇到以下错误: TOP子句中的行数必须是整数 @constCnt变量声明为SMALLINT,@thresh声明为十进制(6,4)。当我执行select(floor((@constCnt*(1+@thresh)))时,我得到一个返回的不带小数的整数值 有什么办法可以解决这个问题吗 select top (@constCnt) * into #temp from ( select top (floor((@constCnt*(1

尝试运行以下代码时,我遇到以下错误:

TOP子句中的行数必须是整数

@constCnt变量声明为SMALLINT,@thresh声明为十进制(6,4)。当我执行
select(floor((@constCnt*(1+@thresh)))
时,我得到一个返回的不带小数的整数值

有什么办法可以解决这个问题吗

    select top (@constCnt) * 
    into #temp
    from (
            select top (floor((@constCnt*(1+@thresh)))) pt.*,
                inLast = CASE WHEN lh.code IS NULL THEN 0 ELSE 1 END
            from #pretemp pt
            left join #last lh
            on lh.code = pt.code
            order by em desc ) a    
    order by inlast desc, emr desc, code

尝试强制转换变量:

select top (cast(@constCnt as int)) *
...
declare @constCnt2 int = floor((@constCnt*(1+@thresh)))

可以通过定义另一个变量来实现这一点:

select top (cast(@constCnt as int)) *
...
declare @constCnt2 int = floor((@constCnt*(1+@thresh)))
然后在子查询中使用它

select top (@constCnt) *
into #temp
from (select top (@constCnt2) pt.*,
             inLast = CASE WHEN lh.code IS NULL THEN 0 ELSE 1 END
      from #pretemp pt left join
           #last lh
           on lh.code = pt.code
      order by em desc
     ) a
order by inlast desc, emr desc, code

你试过将(floor(@constCnt*(1+@thresh))转换为INT吗?是的,不起作用。编辑:确认,我设置它的方式出现错误--看起来它现在正在工作。谢谢这不管用。我收到以下错误:[“CAST”附近的语法不正确,应为“AS”。]编辑:语法混乱…现在似乎正在工作。谢谢