ORDER BY 子句
指定结果集的排序。除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
-----------
create function GetFixRowID (@intNum int,@strType varchar(2))
returns int  --这是第2行
as
begin
  declare @intOutID  int
  set @intOutID=(select top 1 T.id from (select top @intNum a.id from t1 a where  
                 a.ftype=@strtype order by a.id) T order by T.id desc )   --这是第6行。
  return (@intoutid)
end
goselect * from t1 where id<=GetFixRowID(9,ftype) order by ftype,id
go

解决方案 »

  1.   

    create function GetFixRowID (@intNum int,@strType varchar(2))
    returns int  --这是第2行
    as
    begin
      declare @intOutID  int
      set @intOutID=(select top 1 T.id from (select top @intNum a.id from t1 a where  
                     a.ftype=@strtype order by a.id --这儿) T order by T.id desc )   --这是第6行。                               
      return (@intoutid)
    end
    goselect * from t1 where id<=GetFixRowID(9,ftype) order by ftype,id
    go
      

  2.   

    select a.type,b.name
    from a join b on a.id=b.a_id
    where b.id in(select top 9 id from b where a_id=a.id)
      

  3.   

    --下面是数据测试--测试数据
    declare @a table(id int,type varchar(10))
    declare @b table(id int identity(1,1),a_id int,name varchar(10))insert into @a
    select 1,'水果'
    union all select 2,'蔬菜'
    union all select 3,'水产类'insert into @b
    select 1,'香蕉'
    union all select 1,'苹果'
    union all select 2,'芹菜'
    union all select 1,'苹果a'
    union all select 2,'芹菜b'
    union all select 1,'苹果c'
    union all select 2,'芹菜d'
    union all select 1,'苹果e'
    union all select 2,'芹菜f'
    union all select 1,'苹果g'
    union all select 2,'芹菜h'
    union all select 1,'苹果1'
    union all select 2,'芹菜2'
    union all select 1,'苹果3a'
    union all select 2,'芹菜4b'
    union all select 1,'苹果5c'
    union all select 2,'芹菜6d'
    union all select 1,'苹果7e'
    union all select 2,'芹菜8f'
    union all select 1,'苹果9g'
    union all select 2,'芹菜0h'
    union all select 1,'苹果aa'
    union all select 2,'芹菜bb'
    union all select 1,'苹果acc'
    union all select 2,'芹菜bdd'
    union all select 1,'苹果cee'
    union all select 2,'芹菜dff'
    union all select 1,'苹果egg'
    union all select 2,'芹菜fhh'
    union all select 1,'苹果gkk'
    union all select 2,'芹菜hll'--查询处理
    select *
    from @a a join @b b on a.id=b.a_id
    where b.id in(select top 9 id from @b where a_id=a.id)/*--测试结果id          type       id          a_id        name       
    ----------- ---------- ----------- ----------- ---------- 
    1           水果         1           1           香蕉
    1           水果         2           1           苹果
    1           水果         4           1           苹果a
    1           水果         6           1           苹果c
    1           水果         8           1           苹果e
    1           水果         10          1           苹果g
    1           水果         12          1           苹果1
    1           水果         14          1           苹果3a
    1           水果         16          1           苹果5c
    2           蔬菜         3           2           芹菜
    2           蔬菜         5           2           芹菜b
    2           蔬菜         7           2           芹菜d
    2           蔬菜         9           2           芹菜f
    2           蔬菜         11          2           芹菜h
    2           蔬菜         13          2           芹菜2
    2           蔬菜         15          2           芹菜4b
    2           蔬菜         17          2           芹菜6d
    2           蔬菜         19          2           芹菜8f(所影响的行数为 18 行)
    --*/
      

  4.   

    --如果要排序:
    select a.type,b.name --如果显示所有字段,将a.type,b.name改为*
    from a join b on a.id=b.a_id
    where b.id in(select top 9 id from b where a_id=a.id)
    order by a.id,b.id
      

  5.   

    shuiniu(飞扬的梦):
    谢谢回复。我加上order by 之后,报告的错误还是跟刚才一样的。zjcxc(邹建) :
    谢谢回复。我已经明白怎么取每类的开头9行。我的问题是我的函数为什么错了。
    问题还没有解决。
      

  6.   

    函数中不能完成你的工作.top 后面不能跟变量,必须是指定的数字.而函数又不支持exec
      

  7.   

    未在下面的列表中列出的语句不能用在函数主体中。 赋值语句。
    控制流语句。
    DECLARE 语句,该语句定义函数局部的数据变量和游标。
    SELECT 语句,该语句包含带有表达式的选择列表,其中的表达式将值赋予函数的局部变量。
    游标操作,该操作引用在函数中声明、打开、关闭和释放的局部游标。只允许使用以 INTO 子句向局部变量赋值的 FETCH 语句;不允许使用将数据返回到客户端的 FETCH 语句。
    INSERT、UPDATE 和 DELETE 语句,这些语句修改函数的局部 table 变量。
    EXECUTE 语句调用扩展存储过程。 
      

  8.   

    --想出了另一个办法,将函数改成这样就可以了.
    create function GetFixRowID (
    @Num int,
    @Type varchar(2))
    returns int
    as
    begin
    declare @ID  int
    select @id=id from t1 a
    where  ftype=@type 
    and(select sum(1) from t1 where ftype=@type and id<=a.id)=@num
    return (@id)
    end
    goselect * from t1 where id<=dbo.GetFixRowID(9,ftype) order by ftype,id
    go
      

  9.   

    zjcxc(邹建) :谢谢。我明白了,top后不能跟变量。调用函数的时候一定要加上dbo.限定吗?什么场合要加上dbo.的限定?麻烦再给讲讲吧。另外,你写的函数我试了一下,其中where条件中的=@num要改成<=@num就完全正确了。
      

  10.   

    调用 函数是时候一定要加dbo