如何在SQL Server中向FormsOf函数传递参数

4
我的问题与这个类似:我在使用FormsOf(INFLECTIONAL)进行全文搜索时遇到了参数问题。这个查询没有返回任何结果。
DECLARE @SearchWord nvarchar(4000)
SET @SearchWord = 'tax'
SELECT listing_id, RANK, name, address, city, zip, heading, phone 
FROM listings a, 
FREETEXTTABLE(listings, *, 'FormsOf(INFLECTIONAL, @SearchWord')
WHERE [KEY] = a.listing_id
ORDER BY RANK DESC, name

但这个可以正常工作

DECLARE @SearchWord nvarchar(4000)
SET @SearchWord = 'tax'
SELECT listing_id, RANK, name, address, city, zip, heading, phone 
FROM listings a, 
FREETEXTTABLE(listings, *, 'FormsOf(INFLECTIONAL, tax')
WHERE [KEY] = a.listing_id
ORDER BY RANK DESC, name

I found this question here

1个回答

3
你正在搜索 "@SearchWord" 而不是 "tax"!
尝试使用以下查询:
DECLARE @SearchWord nvarchar(max)
SET @SearchWord = 'tax'

DECLARE @SearchString nvarchar(max)
SET @SearchString = 'FormsOf(INFLECTIONAL, "' + @SearchWord + '")'

SELECT listing_id, RANK, name, address, city, zip, heading, phone 
FROM listings a, 
FREETEXTTABLE(listings, *, @SearchString)
WHERE [KEY] = a.listing_id
ORDER BY RANK DESC, name

有没有想过这个会对 SQL 注入攻击的易感性/免疫性如何?如 http://stackoverflow.com/questions/2176440/parameters-in-the-formsof-function-and-sql-injection。 - Andrew M
1
@Andrew M:这段代码是否容易受到注入攻击取决于@SearchWord的来源。我认为程序员需要了解SQL注入,每个关于SQL的问题都讨论它没有太多意义。 - Andomar
1
很抱歉,我并不是在批评,只是想更好地理解参数@SearchString的动态生成方式以及它是否能提供与更简单的参数化查询相同的保护机制。我对从字符串中调用FormsOf函数的语法也是新手。 - Andrew M

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