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;
ENDMsg 443, Level 16, State 15, Procedure UpdateParentHasChildByChildId, Line 25
在函数内的 'UPDATE' 中对带副作用的或依赖于时间的运算符的使用无效。
Msg 443, Level 16, State 15, Procedure UpdateParentHasChildByChildId, Line 27
在函数内的 'UPDATE' 中对带副作用的或依赖于时间的运算符的使用无效。