Code
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER FUNCTION dbo.FUNC_GET_Edition(@area_id int,@level_num int =-1)
RETURNS @rt_table table(
ProductID int,
ProductName nvarchar(50)
)
AS
BEGIN
declare @tep_name varchar(50),
@tep_id int,
@sub_count int
declare c1 cursor for
(
select productid,productname from product where parentid = @area_id
)
if @level_num=0
begin
insert into @rt_table(ProductID,ProductName)
select productid,productname from product where productid = '000'
end
open c1
fetch c1 into @tep_id,@tep_name
select @sub_count = 0
while @@fetch_status>=0
begin
select @sub_count=count(*) from product where parentid = @tep_id
insert into @rt_table(ProductID,ProductName)values(@tep_id,@tep_name)
if @sub_count>0
begin
insert into @rt_table(ProductID,ProductName)
select ProductID,ProductName from dbo.FUNC_GET_Edition(@tep_id,1)
end
FETCH NEXT FROM c1 INTO @tep_id,@tep_name
end
close c1
deallocate c1
return
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER FUNCTION dbo.FUNC_GET_Edition(@area_id int,@level_num int =-1)
RETURNS @rt_table table(
ProductID int,
ProductName nvarchar(50)
)
AS
BEGIN
declare @tep_name varchar(50),
@tep_id int,
@sub_count int
declare c1 cursor for
(
select productid,productname from product where parentid = @area_id
)
if @level_num=0
begin
insert into @rt_table(ProductID,ProductName)
select productid,productname from product where productid = '000'
end
open c1
fetch c1 into @tep_id,@tep_name
select @sub_count = 0
while @@fetch_status>=0
begin
select @sub_count=count(*) from product where parentid = @tep_id
insert into @rt_table(ProductID,ProductName)values(@tep_id,@tep_name)
if @sub_count>0
begin
insert into @rt_table(ProductID,ProductName)
select ProductID,ProductName from dbo.FUNC_GET_Edition(@tep_id,1)
end
FETCH NEXT FROM c1 INTO @tep_id,@tep_name
end
close c1
deallocate c1
return
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO