【发布时间】:2015-10-20 11:16:20
【问题描述】:
我有以下函数循环遍历树结构以查看是否有任何子注释包含属性:
CREATE FUNCTION [dbo].[ufn_FamilyHasCar] ( @PersonId Integer ) RETURNS bit
as
BEGIN
DECLARE @Out bit = 0
Declare @Count int
Select @Count = count(*) from car WHERE owner_id = @PersonId and type = 2
if @Count = 0
begin
declare @tbl_temp table (personId int)
Declare @Id int
insert into @tbl_temp(personId) (select id from person where parent_id = @PersonId)
While (Select Count(*) From @tbl_temp) > 0
Begin
Select Top 1 @Id = personId From @tbl_temp
set @Out = dbo.ufn_FamilyHasCar(@Id)
if @Out = 1
break
Delete from @tbl_temp Where personId = @Id
End
end
else
set @Out = 1
RETURN @Out
END
GO
这似乎是我当前实现的瓶颈,所以我想问是否以及如何提高 udf 的性能?
【问题讨论】:
标签: sql sql-server recursion user-defined-functions