--1.直接查询
select * from 表 where [index] between 3 and 5

解决方案 »

  1.   

    --2.写成存储过程
    create proc p_qry 
    @begin int,
    @end int
    as
    select * from 表 where [index] between @begin and @end
    go--调用
    exec p_qry
      

  2.   

    --上面的调用应该是:
    exec p_qry 3,5--3.写成函数
    create function f_qry(
    @begin int,
    @end int
    )returns tables
    as
    return(select * from 表 where [index] between @begin and @end)
    go--调用
    select * from f_qry(3,5)
      

  3.   

    create proc P_A @a int,@b int
    as 
    set nocount on
    declare @sql varchar(8000)set @sql='select index ,name ,sex from tb where index between '+@a+' and '+ @b+''
    exec(@sql)go
      

  4.   

    为什么要用@begin,@end啊
    是游标吗?代码中怎么来调用存储过程
      

  5.   

    参数嘛,你不定义参数怎么传递你要查询的值进去?和执行普通sql语句一样
    exec p_qry 3,5
      

  6.   

    //存储过程写完后,在哪看到效果啊你要用exec p_qry 3,5
    把它運行起來
      

  7.   

    写成函数时候
    returns tables
    提示无法找到tables的数据类型
    请问是什么原因啊,谢谢
      

  8.   

    多写了一个s
    returns tables改为
    returns table
      

  9.   

    不好意思,可能我的表达有错误,我要实现的是某一段的值,不是字段里的值,而是返回表中中实际的如第3行到第5行的值,并不以字段index来判断
      

  10.   

    select top 5 * from table where not in (select top 2 from table order by index ) order by index
      

  11.   

    --简单地说,按 index 顺序来取的话
    create function f_qry(
    @begin int,
    @end int
    )returns table
    as
    return(
    select * from 表 a
    where (select count(*) from 表 where [index]<=a.[index])
    between @begin and @end)
    go--调用
    select * from f_qry(3,5)
      

  12.   

    index只是表中的不能重复的用户编号,我要实现先将表中的数据按index排序后,然后取排序后表中的,实际的某一段数据,如第3行好第5行间的数据,与index无关
      

  13.   

    老大们都说的很清楚了哦
    你排序后是不是按照index从小到大排列的?
    如果不是的话,可以这样查:
    --m-n行的数据
    select top n-m+1 * from 表 where index not in(select top m-1 index from 表)
      

  14.   

    将数据排序后复制到1临时表
    在临时表里增加自增列字段[id]
    取临时表里[id]3-5的数据
      

  15.   

    to zjcxc(邹建):谢谢,我用了你的函数后,发现返回的值为空,这是怎么回事啊
      

  16.   

    select top 5 * from table where not in (select top 2 from table order by index ) order by index
      

  17.   

    funsuzhou(羡米奇)(努力ing,成为专家中!) 请问具体如何实现啊。谢谢
      

  18.   

    insert into TempTable1 select * from 表 order by [index]
    alter table TempTable1 add [id] int identity如果显示[id]也没关系的话
    select * from TempTable1 where [id] between 3 and 5如果不要显示[id]
    select a.字段A,a.字段B....from TempTable1 a where  a.[id] between 3 and 5
      

  19.   

    davorsuker39(大狐狸) :谢谢,关键我在程序中调用存储过程的时候,传参数给存储过程来得到返回值,光sql的话,参数在存储过程中好象不能跟在top的后面
      

  20.   

    谢谢,funsuzhou(羡米奇)(努力ing,成为专家中!) 
    这可以在存储过程中如何并在一起啊
      

  21.   

    select * into TempTable1 from 表 order by [index]
    alter table TempTable1 add [id] int identity如果显示[id]也没关系的话
    select * from TempTable1 where [id] between 3 and 5如果不要显示[id]
    select a.字段A,a.字段B....from TempTable1 a where  a.[id] between 3 and 5drop table TempTable1
      

  22.   

    create function f_qry(
    @begin int,
    @end int
    )returns table
    as
    select * into TempTable1 from 表 order by [index]
    alter table TempTable1 add [id] int identity
    return(select * from TempTable1 where [id] between 3 and 5) --如果显示[id]也没关系的话
    drop table TempTable1
    go--调用
    select * from f_qry(3,5)
      

  23.   

    create function f_qry(
    @begin int,
    @end int
    )returns table
    as
    select * into TempTable1 from 表 order by [index]
    alter table TempTable1 add [id] int identity
    return(select a.字段A,a.字段B....from TempTable1 a where  a.[id] between 3 and 5) --如果不要显示[id]
    drop table TempTable1
    go--调用
    select * from f_qry(3,5)
      

  24.   

    --测试--测试数据
    create table tb([index] int,name varchar(10),sex varchar(10))
    insert tb select 1,'jj','F'
    union all select 3,'dd','M'
    union all select 2,'ll','F'
    union all select 4,'rr','F'
    union all select 8,'aa','M'
    union all select 6,'xx','M'
    union all select 7,'pp','F'
    union all select 5,'zz','F'
    go--原来写的函数,按index顺序
    create function f_qry(
    @begin int,
    @end int
    )returns table
    as
    return(
    select * from tb a
    where (select count(*) from tb where [index]<=a.[index])
    between @begin and @end)
    go--调用
    select * from f_qry(3,5)/*--测试结果index       name       sex        
    ----------- ---------- ---------- 
    3           dd         M
    4           rr         F
    5           zz         F(所影响的行数为 3 行)
    --*/
    go--查询的函数2,按表中的记录顺序
    create function f_qry1(
    @begin int,
    @end int
    )returns @re table([index] int,name varchar(10),sex varchar(10))
    as
    begin
    declare @t table(id int identity,[index] int,name varchar(10),sex varchar(10))
    insert @t select * from tb
    insert @re select [index],name,sex from @t
    where id between @begin and @end
    return
    end
    go--调用
    select * from f_qry1(3,5)/*--测试结果index       name       sex        
    ----------- ---------- ---------- 
    2           ll         F
    4           rr         F
    8           aa         M(所影响的行数为 3 行)
    --*/
    go--删除测试
    drop table tb
    drop function f_qry,f_qry1