我有一个有两个领域的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
如果从TVP传入的sentence_id为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))
答案 0 :(得分:2)
而不是使用IN
简单地使用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
,因为无论如何都会对其进行优化,并且还使用了别名,以便您的标识符不会那么长。