在查询中使用RegEx的String参数 [英] Use String parameter for RegEx in query
问题描述
在我的查询中(数据库是一个sql server)我使用RegEx来执行这样的select命令:
In my query (the database is a sql server) I use a RegEx for a select command like this:
SELECT * FROM test WHERE id LIKE '1[2,3]'
(此查询经过测试并返回我想要的数据)
(This query is tested and returns the data I want)
我想为此RegEx使用参数。为此,我将iReport中的Paramter $ P {id}
定义为字符串,值为1 [2,3]
。
I want to use a paramter for this RegEx. For that I definded the Paramter in iReport $P{id}
as a string and the value is "1[2,3]"
.
在我的查询中,我现在使用这个参数:
In my query I use now this parameter like this:
SELECT * FROM test WHERE id LIKE $P{id}
结果我得到了一个空白页面。我认为问题是参数的值是用定义的。但是''我得到一个编译错误,该参数不是一个字符串。
As result I get a blank page. I think the problem is that the value of the parameter is defined with " ". But with ' ' I get a compiler error that the paramter isn't a string.
我希望有人可以帮助我。
I hope someone can help me.
推荐答案
LIKE
适用于文本值,而不适用于数值。由于 id
是数字使用,如下所示:
LIKE
applies to text values, not to numeric values. Since id
is numeric use something like this:
SELECT * FROM test WHERE id IN (12, 13)
参数
SELECT * FROM test WHERE id IN ($P!{id_list})
并为参数提供逗号分隔的id列表。 bang(!)确保参数将按原样插入,不带字符串分隔符。
and supply a comma separated list of ids for the parameter. The bang (!) makes sure that the parameter will be inserted as-is, without string delimiters.
Btw: LIKE(Transact-SQL)使用通配符,而不是正则表达式。
Btw: LIKE (Transact-SQL) uses wildcards, not regex.
您仍然可以使用 LIKE
,因为存在隐式转换从数值类型到T-SQL中的文本,但这将导致(表或索引)扫描,其中 IN
子句可以利用索引。
You can still use LIKE
since there exists an implicit conversion from numeric types to text in T-SQL, but this will result in a (table or index) scan, where as the IN
clause can take advantage of indexes.
这篇关于在查询中使用RegEx的String参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!