要看你實際需求,有時可以找到變通的方法,如函數中實現top n,
函數中不能: exec('select top '+@num +....),但可以用以下方法處理
--函数的方法( zjcxc(邹建)):
Create function testfn(@counts int)
returns @temptable table(storeid int,storename varchar(50))
as
begin
declare @r table(id int identity(1,1),storeid int,storename varchar(50))
insert @r select * from stores
insert @temptable select storeid,storename
from @r where id<=@counts
return
end
go
--如果storeid 是主键,可以用:
Create function testfn(@counts int)
returns @temptable table(storeid int,storename varchar(50))
as
begin
insert @temptable
select storeid,storename
from stores a
where (select sum(1) from stores where storeid<=a.storeid)<=@counts
return
end
go