关键字SQL Server存储过程
我对SQL Server相当陌生,但我必须编写一个存储过程,该过程将使用关键字列表搜索特定表,并假设返回找到命中的行,我编写了一个查询,该查询可以工作,但问题是当我必须修改关键字列表时,我必须从头开始编写查询 查询如下关键字SQL Server存储过程,sql,sql-server,stored-procedures,keyword-search,Sql,Sql Server,Stored Procedures,Keyword Search,我对SQL Server相当陌生,但我必须编写一个存储过程,该过程将使用关键字列表搜索特定表,并假设返回找到命中的行,我编写了一个查询,该查询可以工作,但问题是当我必须修改关键字列表时,我必须从头开始编写查询 查询如下 SELECT * INTO [playground].[dbo].[New table name] FROM [playground].[dbo].[Main table] WHERE [Document Type Description] LIKE 'Alcohol'
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来读取关键字数组,并让我选择在主表中搜索哪个表列
希望这有意义
提前感谢您关于您的代码的一些想法 使用类似
的
您放置查询的方式实际上是执行相等检查,因为您在
LIKE
语句中没有放置通配符。这导致两个问题:
LIKE
... WHERE [Field] = 'value'
而不是
... WHERE [Field] LIKE 'value'
相等性检查有两种方法可以加快速度。您可以将搜索词放在一个表中,然后执行以下操作:
... WHERE [Field] in (SELECT Term FROM TableOfSearchTerms)
... WHERE [Field] LIKE `%searchterm%`
DECLARE @query NVARCHAR(max)
SET @query = "INSERT INTO ... WHERE ";
EXEC (@query)
或者您甚至可以尝试将两个表连接在一起。那么您根本不需要WHERE
子句:
... FROM Table1 t1 INNER JOIN TableOfSearchTerms terms ON terms.Term = t1.[Field]
使搜索字段动态化这并不容易。您可以创建一个动态SQL语句作为字符串,然后使用
EXEC
执行该语句,但必须小心不要引入问题(如SQL注入等)
执行实际的,如
在这种情况下,您需要在语句中使用通配符,如下所示:
... WHERE [Field] in (SELECT Term FROM TableOfSearchTerms)
... WHERE [Field] LIKE `%searchterm%`
DECLARE @query NVARCHAR(max)
SET @query = "INSERT INTO ... WHERE ";
EXEC (@query)
用我上面所说的来解决这个问题并不容易。在这种情况下(尽管我很难说),组装一个包含查询的字符串并使用EXEC
执行可能是最简单的。结果可能如下所示:
... WHERE [Field] in (SELECT Term FROM TableOfSearchTerms)
... WHERE [Field] LIKE `%searchterm%`
DECLARE @query NVARCHAR(max)
SET @query = "INSERT INTO ... WHERE ";
EXEC (@query)
您可以使用搜索词表上的光标将所需的
LIKE
s添加到WHERE
子句中。将这些字符串放在一个表中,并与[Document Type Description]相等连接。您可以添加另一列来控制它们的包含。@AlexK。OP使用的是与类似的,而不是相等。有没有一种方法可以实现您在SQL Server中使用like
比较语义所描述的操作?实际上,他正在寻找相等(没有通配符,他说它可以按预期工作)-是的,您可以在X上像“%”+Y+“%”一样加入T
-您需要“like”还是相等?如果要在搜索字符串前后使用like-add%[Document Type Description]如“%contractor%”