开发者

searching employee names on an @parameter in SQL [duplicate]

开发者 https://www.devze.com 2023-03-23 22:37 出处:网络
This question already has answers here:开发者_JS百科 Closed 11 years ago. Possible Duplicate: Parameterized Queries with LIKE and IN conditions
This question already has answers here: 开发者_JS百科 Closed 11 years ago.

Possible Duplicate:

Parameterized Queries with LIKE and IN conditions

I try in my VB.net application to search persons on characters put in a text box by a parameter in sql DB. Something like-

select * from employee where name like '@name%'

but it doesn't work!

Can somebody help me?


For a starts-with match:

SELECT * FROM employee WHERE name LIKE @name + '%'

For a contains match:

SELECT * FROM employee WHERE name LIKE '%' + @name + '%'

You want to combine the string '%' with the value of @name: currently you are using the string '@name%' which of course is not what you want to search for.


If you want to handle this in SQL you can also do the following:

select * from employee where name like @name +'%'

Edit: Kieren Johnson beat me to this answer and has explained further


you have to put the % at the end of your named parameter.

select * from employee where name like @name

addParameter("@name", "obama%") //this isn't actually a real function (because I don't know how you would call in in VB, but I think you see what i mean ;-))

also you should remove the ' around your named parameter

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号