select IDENTITY(int, 1,1) AS ID_Num,* into #temp from 表
select * from #temp where ID_Num>10 and ID_Num<=20

解决方案 »

  1.   

    执行第一句报错如下:
    [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'IDENTITY'.
    执行第二句报错如下:
    [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name '#temp'.
      

  2.   

    USE PUBS
    GO
    --------------------------------------------------------------------------
    SELECT
        (SELECT COUNT(1) FROM authors a2 where a2.au_id <= a1.au_id) AS ROWNUM,
        *
    FROM authors a1
    --------------------------------------------------------------------------
    SELECT
        COUNT(a1.au_id),
        a1.au_id,
        a1.au_lname,
        a1.au_fname,
        a1.phone,
        a1.address,
        a1.city,
        a1.state,
        a1.zip,
        a1.contract
    FROM authors a1, authors a2
    WHERE a1.au_id <= a2.au_id -- >=
    GROUP BY a1.au_id,
             a1.au_lname,
             a1.au_fname,
             a1.phone,
             a1.address,
             a1.city,
             a1.state,
             a1.zip,
             a1.contract
    ORDER BY a1.au_id DESC -- ASC
    --------------------------------------------------------------------------
    可以看得出来,第二种写法比较烦,要是这个表有255个字段,那……
    这两种写法都有缺点
    就是不能按照你取数据的顺序进行编号,
    而且表(视图)中得有可以比较的关键字,一定是关键字,UNIQUE也不行!
    上面,你把<=和>=互换一次,或把ASC和DESC互换一次,结果顺序就会相反一次
    且和那个作为排序根据的关键字段要么顺序相同,要么相反所以,前面说的有很大的优势,只不过多用了一个临时表
    有分两步走,一步生成所需的带行号的临时表,另一步是从临时表里取数据
    也仅此而已
    在这给出的方法只是给大家一个选择而已
      

  3.   

    create table #temp(rowno int identity(1,1),c1,c2,....)  --字段类型,顺序要和你的查询的表结构一致
    go
    insert into #temp(c1,c2,...) select * from 表
    goselect * from temp
    godrop table #temp
    go