“进入@gutsTVP”之后的左括号打破了这一点。
错误在第 12 行的正确括号上。
如果删除它运行的第一个联合周围的第一组括号,但它返回错误的答案,因为然后在第一个联合之前应用了相交
DECLARE @gutsTVP AS TABLE ( sID INT PRIMARY KEY);
insert into @gutsTVP
(
select distinct [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (1,2)
union
select distinct [ftsIndexWordOnceB].[sID]
from [ftsIndexWordOnceB] with (nolock)
where [ftsIndexWordOnceB].[wordID] in (5,6)
)
intersect
(
select distinct [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (9,10,11,12)
union
select distinct [ftsIndexWordOnceB].[sID]
from [ftsIndexWordOnceB] with (nolock)
where [ftsIndexWordOnceB].[wordID] in (13,14,15,16)
)
select [guts].[sID] from @gutsTVP as [guts]
join [docSVsys] with (nolock)
on [docSVsys].[sID] = [guts].[sID]
order by [docSVsys].[sParID], [docSVsys].[sID]
包括最终连接,因为这是 TABLE 的目的。
如果我加入派生表,它不知道 PK 并且慢得多。
是的,我知道可以将其重新考虑为没有领先的左括号。
这是一个简化的查询。
需要能够处理前导左括号。
下面的技巧使语法起作用。
但现在加入速度较慢,因为它不知道 PK。
不能将 sID 作为 PK 并接受 null
DECLARE @gutsTVP AS TABLE ( sID INT PRIMARY KEY);
insert into @gutsTVP
select 0 -- real PK starts at 1
union
(
select distinct [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (1,2)
union
select distinct [ftsIndexWordOnceB].[sID]
from [ftsIndexWordOnceB] with (nolock)
where [ftsIndexWordOnceB].[wordID] in (5,6)
)
intersect
(
select distinct [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (9,10,11,12)
union
select distinct [ftsIndexWordOnceB].[sID]
from [ftsIndexWordOnceB] with (nolock)
where [ftsIndexWordOnceB].[wordID] in (13,14,15,16)
)
select [guts].[sID] from @gutsTVP as [guts]
join [docSVsys] with (nolock)
on [docSVsys].[sID] = [guts].[sID]
where [guts].[sID] > 0
order by [docSVsys].[sParID], [docSVsys].[sID]