刚刚遇到一个非常奇怪的服务器行为 - 即:
我得到了带有一个 TVP 参数的 TVF,如下所示
CREATE FUNCTION [BUD].[Foo] (@IdAgrs IdTable READONLY)
RETURNS TABLE
AS
RETURN
(
WITH recursive_CTE
AS
(
--> Select here is not important - only that TVP join goes here:
SELECT Id, IdParent, Number, 1 as 'lvl'
FROM Agreements ag
join @IdAgrs ig on ig.Id = ag.Id --> HERE
UNION ALL
SELECT a.Id, a.IdParent, a.Number
i.lvl + 1
FROM Agreements e
INNER JOIN recursive_CTE r ON e.IdParent = r.Id
)
select 1
from recursive_CTE
)
继承人TVP:
IF NOT EXISTS (select 1 from sys.table_types where name = 'IdTable')
CREATE TYPE IdTable AS TABLE (Id INT NOT NULL)
现在是“奇怪”的部分:
尝试在快速服务器版本上执行功能时 - 它冻结。我的意思是执行时间太长(我在等待 4 分钟后取消执行),而在完整版本上它没有问题(0s - 根本没有数据)。两者都是2008R2,相同的DB,相同的TVF,相同的TVP。TVF的执行如下
declare @IdAgrs IdTable
insert into @IdAgrs values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)
select * from BUD.Foo(@IdAgrs)
但!
当我单独执行 TVF 的主体时 - 它就像一个魅力(WTF?)
declare @IdAgrs IdTable
insert into @IdAgrs values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)
;WITH recursive_CTE
AS
(
--> Select here is not important - only that TVP join goes here:
SELECT Id, IdParent, Number, 1 as 'lvl'
FROM Agreements ag
join @IdAgrs ig on ig.Id = ag.Id --> HERE
UNION ALL
SELECT a.Id, a.IdParent, a.Number
i.lvl + 1
FROM Agreements e
INNER JOIN recursive_CTE r ON e.IdParent = r.Id
)
select 1
from recursive_CTE
我不知道为什么?!快递版是否有一些限制或什么?