amin3d
یک شنبه 29 خرداد 1390, 19:22 عصر
به نام خدا ،با سلام ،فرضا اگر ما جدولی بصورت زیر داشته باشیم و بخواهیم عموهای یک شخص را با تابع پیاده سازی کنیم ، چه باید کرد. من از cursor بهره بردم ولی نمی دانم در خروجی چرا جواب نمی دهد.
info(id,name,se,pid)
USE [Karete-meli]
GO
/****** Object: UserDefinedFunction [dbo].[myuncle] Script Date:
06/19/2011 19:30:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[myuncle](@id int) returns @myuncle table(name
nvarchar(50)) as
begin
/* define a cursor for moving */
declare c cursor for
select name from info where pid=(
select pid from info where id=(select pid from info where id=@id)) and
se='male'
except
select name from info where id =(select pid from info where id=@id)
declare @uncle table(name nvarchar(50))
declare @i_uncle nvarchar
begin
open c
fetch next from c into @i_uncle
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM c into @i_uncle
set @i_uncle=@i_uncle
insert into @uncle values(@i_uncle)
END
end
close c
deallocate c
return
end
من برای فراخوانی این تابع که یک خروجی جدول برمی گرداند از دستور زیر استفاده کردم ؛ آیا روش کار مشکل دارد یا کد ؟
select * from dbo.myaunt(100)
info(id,name,se,pid)
USE [Karete-meli]
GO
/****** Object: UserDefinedFunction [dbo].[myuncle] Script Date:
06/19/2011 19:30:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[myuncle](@id int) returns @myuncle table(name
nvarchar(50)) as
begin
/* define a cursor for moving */
declare c cursor for
select name from info where pid=(
select pid from info where id=(select pid from info where id=@id)) and
se='male'
except
select name from info where id =(select pid from info where id=@id)
declare @uncle table(name nvarchar(50))
declare @i_uncle nvarchar
begin
open c
fetch next from c into @i_uncle
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM c into @i_uncle
set @i_uncle=@i_uncle
insert into @uncle values(@i_uncle)
END
end
close c
deallocate c
return
end
من برای فراخوانی این تابع که یک خروجی جدول برمی گرداند از دستور زیر استفاده کردم ؛ آیا روش کار مشکل دارد یا کد ؟
select * from dbo.myaunt(100)