继续请教高手关于函数中使用游标的问题
CREATE FUNCTION get_names (@id int)
RETURNS TABLE
AS
RETURN
(
SELECT GivenName,FamilyName from Patients where PatientNum=@id
);
GO
select * from dbo.get_names(9)
这是取出PatientNum=9的记录,里面有N条满足要求
现在想加一个参数 取出前5条,
不能用TOP,只能用游标做,大家请帮我改改,要交作业了,多谢多谢
------解决方案--------------------CREATE FUNCTION get_names (@id int)
RETURNS @return TABLE (GivenName ?类型, FamilyName ?类型)
AS
begin
declare @GivenName ?类型, @FamilyName ?类型
declare @i int
set @i = 0
declare curTest cursor for SELECT GivenName,FamilyName from Patients where PatientNum=@id
open curTest
fetch next from curTest into @GivenName, @FamilyName
while @@fetch_status = 0
begin
insert @return select @GivenName, @FamilyName
fetch next from curTest into @GivenName, @FamilyName
set @i = @i + 1
if @i = 5 break
end
close curTest
deallocate curTest
RETURN
end
GO