Postgres LIKE 运算符问题
假设我有一个函数,它接受一个参数,
create function(i_param VARCHAR)
我想在表中选择一个 WHERE col_name LIKE 'i_param%'
我尝试这样做 LIKE i_param||'%' 但我认为它实际上返回字符串
'i_param'
而不是放入函数中的值。
它不会给我任何错误,但返回零行。如何对输入参数执行 LIKE
?
对这个东西还很陌生,所以任何帮助都会很棒!谢谢!
So let's say I have a function that takes in one parameter
create function(i_param VARCHAR)
I want to do a select one the table WHERE col_name LIKE 'i_param%'
I tried doing LIKE i_param||'%'
but I think it literally returns the string 'i_param'
not the value put into the function.
It doesn't give me any errors but returns zero rows. How would I do a LIKE
on the input parameter?
Pretty new to this stuff, so any help would be great! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,从 9.2 开始,您可以按照自己的方式做事。在以前的版本中,您应该会收到错误消息。
您没有收到错误的事实表明您对
i_param
的使用与列名称发生冲突。因此,我建议为输入变量添加前缀(我在工作中使用“in_”)。例如,如果您:
那么以下内容将不会生成错误:
然而,这实际上是将 foo 列与 bar 列进行比较。以下内容会给您带来错误:
我强烈建议避免与参数同名的变量。这会导致很多地方出现问题。
As a note, starting in 9.2, you can do things the way you are. In previous versions, you should get an error instead.
The fact that you didn't get an error suggests that your use of
i_param
is in colliding with a column name. I recommend prefixing input variables for this reason (I use "in_" in my work).For example, if you:
Then the following will not generate an error:
However this will in fact be comparing the column foo against the column bar. The following will give you errors:
I highly recommend avoiding variables with the same names as arguments. That leads to problems in many places.