searching employee names on an @parameter in SQL [duplicate]
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
精彩评论