2013-05-12 41 views
0

直接在「into @gutsTVP」之後的左paren將其分解。
該錯誤是在第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並且慢得多。

是的,我知道可以重新考慮這一點,不要有一個領先的左派paren。
這是一個簡化的查詢。
需要能夠處理領先的左派paren。

下面的技巧將語法轉化爲工作。
但現在連接速度較慢,因爲它不知道PK。
不能有SID爲PK和接受空

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] 

回答

2

嘗試的CTE來代替:

DECLARE @gutsTVP AS TABLE (sID INT PRIMARY KEY); 
; WITH a AS (
     select [ftsIndexWordOnce].[sID] 
     from [ftsIndexWordOnce] with (nolock) 
     where [ftsIndexWordOnce].[wordID] in (1,2) 
    union 
     select [ftsIndexWordOnceB].[sID] 
     from [ftsIndexWordOnceB] with (nolock) 
     where [ftsIndexWordOnceB].[wordID] in (5,6) 
) 
, b AS (
     select [ftsIndexWordOnce].[sID] 
     from [ftsIndexWordOnce] with (nolock) 
     where [ftsIndexWordOnce].[wordID] in (9,10,11,12) 
    union 
     select [ftsIndexWordOnceB].[sID] 
     from [ftsIndexWordOnceB] with (nolock) 
     where [ftsIndexWordOnceB].[wordID] in (13,14,15,16) 
) 
insert into @gutsTVP 
SELECT * FROM a 
    intersect 
SELECT * FROM b; 

select [guts].[sID] from @gutsTVP as [guts] 
join [docSVsys] with (nolock) 
    on [docSVsys].[sID] = [guts].[sID] 
order by [docSVsys].[sParID], [docSVsys].[sID] 

還要注意的是,你可以放棄distinct小號,因爲他們已被union所默許的。

+0

我可以刪除獨特但它運行速度明顯慢 - 真的。這工作+1,但我想以線性方式建立該語法。我更新了我的工作。 – Paparazzi 2013-05-12 20:06:39

+0

看起來不錯。 parens有點棘手 - 如果沒有INSERT,原來的查詢就會變得語法正確。但是當一個開放paren緊跟在一個INSERT之後時,SQL Server需要一個列列表而不是一組值。 – 2013-05-12 20:17:21

+0

在進一步的測試中,這比我的黑客執行得更好 – Paparazzi 2013-05-12 21:01:10