2016-07-18 43 views
2

我有兩個字段的TVP。基於表值參數的過濾表並將TVP值插入到選擇中

sentence_id是過濾器從表中選擇記錄,這完美的作品。 TVP還包含一個關鍵字。該TVP看起來是這樣的:

Create TYPE [dbo].[sentence_id_list2] AS TABLE(
    [sentence_id] [nvarchar](50) NOT NULL, 
    [keyword] [nvarchar](50) 
) 

我想通過該關鍵字在結果相同sentence_id所以它看起來是這樣的:

Sentence_Identifier,Keyword,Sentence 
123, curious, hello donna, i have a curious problem 

凡sentence_id從TVP傳遞是123,關鍵字很好奇。

這是我有的存儲過程,只是無法弄清楚如何在結果中包含關鍵字。

ALTER Procedure [dbo].[chat_Get_Sentences_Table_Value_Parameter] 
@sentence_keys [dbo].[sentence_id_list2] READONLY 
AS 
SELECT TOP (100) PERCENT dbo.chat_All_Records_Sentence_Details.Sentence_Identifier, 
dbo.chat_All_Records_Sentence_Details.Sentence, 

-- how do I write this to insert the keyword from the TVP into the select? 
(SELECT keyword FROM @sentence_keys) AS Keyword 

FROM dbo.chat_All_Records_Sentence_Details 
WHERE (dbo.chat_All_Records_Sentence_Details.Sentence_Identifier 
IN (SELECT sentence_id FROM @sentence_keys)) 

回答

2

而不是使用IN的simpy使用INNER JOIN的:

SELECT d.Sentence_Identifier, 
     d.Sentence, 
     sk.keyword 
FROM chat_All_Records_Sentence_Details AS d 
     INNER JOIN @sentence_keys AS sk 
      ON sk.sentence_id = d.Sentence_Identifier; 

我已刪除,因爲這TOP 100 PERCENT將無論如何優化掉,並且還使用別名,以便您的標識符沒有那麼長。

+0

感謝GarethD和提示,這很好! – Rob