关键词 SQL Server 存储过程

3
我是SQL Server的新手,但我必须编写一个存储过程,用于搜索特定表格中的关键词列表,并返回命中的行。我写了一个查询语句可以正常工作,但问题在于如果我需要修改关键词列表,就必须从头开始编写查询语句。
以下是查询语句:
SELECT * 
INTO [playground].[dbo].[New table name] 
FROM [playground].[dbo].[Main table]
WHERE [Document Type Description] LIKE 'Alcohol' 
   OR [Document Type Description] LIKE 'DSTV' 
   OR [Document Type Description] LIKE 'Entertainment' OR
[Document Type Description]like'Bday' OR
[Document Type Description]like'Birthday' OR
[Document Type Description]like'Bar' OR
[Document Type Description]like'Booze' OR
[Document Type Description]like'Catering' OR
[Document Type Description]like'Farewell' OR
[Document Type Description]like'Food' OR
[Document Type Description]like'Function' OR
[Document Type Description]like'Meals' OR
[Document Type Description]like'Year end functions' OR
[Document Type Description]like'Womens day' OR
[Document Type Description]like'Womans day' OR
[Document Type Description]like'Tuck shop' OR
[Document Type Description]like'Teambuilding' OR
[Document Type Description]like'Refreshment' OR
[Document Type Description]like'Liquor' OR
[Document Type Description]like'Lunch' OR
[Document Type Description]like'Water' OR
[Document Type Description]like'Bread' OR
[Document Type Description]like'Breakaway' OR
[Document Type Description]like'Canteen' OR
[Document Type Description]like'Gifts' OR
[Document Type Description]like'Glass' OR
[Document Type Description]like'Glasses' OR
[Document Type Description]like'Glassware' OR
[Document Type Description]like'Ticket' OR
[Document Type Description]like'Rugby' OR
[Document Type Description]like'Cricket' OR
[Document Type Description]like'Tea cups' OR
[Document Type Description]like'Tea' OR
[Document Type Description]like'Sugar bowl' OR
[Document Type Description]like'Sugar' OR
[Document Type Description]like'Soup bowls' OR
[Document Type Description]like'Side plate' OR
[Document Type Description]like'Serving tray' OR
[Document Type Description]like'Saucers' OR
[Document Type Description]like'Tray' OR
[Document Type Description]like'Non slip tray' OR
[Document Type Description]like'Milk' OR
[Document Type Description]like'Milk jug' OR
[Document Type Description]like'Mugs' OR
[Document Type Description]like'Dessert' OR
[Document Type Description]like'Dessert spoons' OR
[Document Type Description]like'Dinner set' OR
[Document Type Description]like'Jug' OR
[Document Type Description]like'Kent' OR
[Document Type Description]like'Knifes' OR
[Document Type Description]like'Knives' OR
[Document Type Description]like'Cooler boxes' OR
[Document Type Description]like'Crockery' OR
[Document Type Description]like'Christmas' OR
[Document Type Description]like'Coffee' OR
[Document Type Description]like'Popcorn machine' OR
[Document Type Description]like'Cooler' OR
[Document Type Description]like'Freezer' OR
[Document Type Description]like'Fridge' OR
[Document Type Description]like'Fan ' OR
[Document Type Description]like'Extraction fan' OR
[Document Type Description]like'Heaters' OR
[Document Type Description]like'Water cooler' OR
[Document Type Description]like'Washing machine' OR
[Document Type Description]like'Warmer' OR
[Document Type Description]like'Vacuum cleaner' OR
[Document Type Description]like'Urn' OR
[Document Type Description]like'Thermostat'

最终,我希望有一个支持读取关键字数组并让我选择在主表中搜索哪个列的SP。希望这样说得清楚。谢谢。

3
将这些字符串放入一个表中,并使用[Document Type Description]进行等值连接。您可以添加另一列来控制它们的包含。 - Alex K.
@AlexK。OP正在使用“like”而不是相等性。在SQL Server中是否有一种使用“like”比较语义来实现您所描述的方法? - Jiri Tousek
1
实际上,他正在寻找相等性(没有通配符,他说它按预期工作)- 是的,你可以使用JOIN T ON X LIKE '%' + Y + '%' - Alex K.
你需要“like”还是相等?如果你要使用“like”,在搜索字符串之前和之后添加%:“[Document Type Description] like '%Thermostat%'”。 - Zahiro Mor
1个回答

2

关于您的代码,有几点建议:

使用LIKE
您在查询中使用了LIKE语句,但实际上它执行的是相等性检查,因为您没有在LIKE语句中加入通配符。这会导致两个问题:

  1. 您可能得不到预期的结果。
  2. 您可能没有使用索引(这会加快速度),因为您使用了LIKE

首先,您需要决定是要检查相等性还是要搜索包含所寻找字符串的字段。

如果您想进行相等性检查,请使用:

... WHERE [Field] = 'value'

替代

... WHERE [Field] LIKE 'value'

相等性检查
有几种方法可以加快它们的速度。您可以将搜索项放入表格中,然后执行以下操作:

... WHERE [Field] in (SELECT Term FROM TableOfSearchTerms)

或者你甚至可以尝试将这两个表连接在一起。那么你就不需要任何WHERE子句了:

... FROM Table1 t1 INNER JOIN TableOfSearchTerms terms ON terms.Term = t1.[Field]

使搜索字段动态化
这并不容易。您可以创建一个动态SQL语句作为字符串,然后使用EXEC来执行它,但是您必须小心,不要引入问题(如SQL注入等)。

执行实际的LIKE
在这种情况下,您需要在语句中使用通配符,例如:

... WHERE [Field] LIKE `%searchterm%`

使用我上面提到的方法解决这个问题并不容易。在这种情况下(虽然我很难过),最简单的方法可能是组装一个包含查询的字符串,然后使用EXEC执行它。结果可能如下:

DECLARE @query NVARCHAR(max)
SET @query = "INSERT INTO ... WHERE ";

EXEC (@query)

您可以在搜索词表上使用游标,将所需的LIKE添加到WHERE子句中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接