能否在Select top n * From TableName中用变量的方法不确定这个n常量值。谢谢

解决方案 »

  1.   


    select top (@n) * from tb
      

  2.   

    --> 测试数据:@te
    declare @te table(id varchar(14),[成交量] int)
    insert @te
    select '玉米期货合约',333 union all
    select '大豆',222 union all
    select '黄金',333 union all
    select '玉米期货合约',222 union all
    select '大豆',333  --如何查询并列第一的现象 
    --常规方法1:
    select top 1 * from @te order  by [成交量] desc 
    --或方法2
    select  top 1  id,SUM([成交量]) as  总成交量  from  @te group by id order by 总成交量 desc
    select  top 1   with ties id,SUM([成交量]) as  总成交量 ,COUNT(1) from  @te group by id order by 总成交量 desc
    --或
    --SQL 2008 动态前N列
    declare @n int 
    set @n = 3  
    select TOP(@n) * from @te  
      

  3.   

    declare @n int
    set @n=[参数]select top(@n) from tb
      

  4.   

    declare @n int
    set @n=[参数]select top(@n)* from tb
      

  5.   

    不好意思各位,我司现在还是用MSSQL2000版本
      

  6.   

    他差5分,双星sql2000 不行,不支持!
      

  7.   

    declare @n int
    set @n=[参数]exec ('select top '+@n+' * from tb')
      

  8.   

    因为我写的那个存储过程相对比较复杂,不能使用动态字符串文法。
    其中有一段主要用MSSQL2000完成某个表中的数据行的打印页码,根据用户需求设计每页纸打印多少行。
    有字段:
    单据编号、单据日期、货品编号、数量、页码