日期:2014-05-17 浏览次数:20638 次
ALTER FUNCTION [dbo].[UpdateParentHasChildByChildId]
(
@childId nvarchar(50),
@oldParentId nvarchar(50), -- -1或者空或者 null 表示不存入
@type int
)
RETURNS int
AS
BEGIN
declare @parentId nvarchar(50);
declare @childCnt int;
if(@type=1)
begin
if(@oldParentId='' or @oldParentId = '-1' or @oldParentId is null)--表示更新
begin
if(@parentId is not null)
begin
select @childCnt=count(1) from tbCompany where TopComID=@oldParentId and CompanyID<>@childId;
if(@childCnt>0)
update tbCompany set HasChild=1 where CompanyID=@oldParentId;
else
update tbCompany set HasChild=0 where CompanyID=@oldParentId;
end
end
else
begin
select @parentId=cast(CompanyID as nvarchar(50)) from tbCompany where TopComID=@childId;
if(@parentId is not null)
begin
select @childCnt=count(1) from tbCompany where TopComID=@parentId;
/*if(@childCnt>0)
update tbCompany set HasChild=1 where CompanyID=@parentId;
else
update tbCompany set HasChild=0 where CompanyID=@parentId;
*/
end
end
end
return @childCnt;
END
if(@childCnt>0)
update tbCompany set HasChild=1 where CompanyID=@oldParentId;
else
update tbCompany set HasChild=0 where CompanyID=@oldParentId;