create function Func_IsSubCategory
--判断某商品分类是否属于另一商品分类的子孙分类
(
@targetCateId int, --子孙分类ID
@ancestorCateId int --祖宗分类ID
)
returns bit --返回值类型为布尔类型
as
begin
--定义用于临时保存商品分类的变量
declare @temCateId int;
--定义返回值变量
declare @exitsed bit;
--设置初始值为0
set @exitsed=0;
if(@targetCateId=@ancestorCateId)
begin
set @exitsed=1;
return @exitsed;
end
--循环判断
select @temCateId=parentCateId from CategoryInfo where Id=@targetCateId
while @temCateId is not null
begin
if(@temCateId=@ancestorCateId)
begin
set @exitsed=1;
break;
end
select @temCateId=parentCateId from CategoryInfo where Id=@temCateId
end
return @exitsed;
end
--判断某商品分类是否属于另一商品分类的子孙分类
(
@targetCateId int, --子孙分类ID
@ancestorCateId int --祖宗分类ID
)
returns bit --返回值类型为布尔类型
as
begin
--定义用于临时保存商品分类的变量
declare @temCateId int;
--定义返回值变量
declare @exitsed bit;
--设置初始值为0
set @exitsed=0;
if(@targetCateId=@ancestorCateId)
begin
set @exitsed=1;
return @exitsed;
end
--循环判断
select @temCateId=parentCateId from CategoryInfo where Id=@targetCateId
while @temCateId is not null
begin
if(@temCateId=@ancestorCateId)
begin
set @exitsed=1;
break;
end
select @temCateId=parentCateId from CategoryInfo where Id=@temCateId
end
return @exitsed;
end