How to pass parameter to stored procedure
I am using SQL Server 2008 Enterprise on Windows Server 2008 Enterprise. In the stored procedure, I need to pass parameter to a select-where-like statement. For example,
@department is store procedure input parameter, its type is varchar(20),
select * from sometable where somecolumn LIKE '%@department%'
but seems my statement above d开发者_开发问答oes not work, any ideas how to pass parameter @department to like statement?
thanks in advance, George
select * /*But don't use * in production!*/
from sometable
where somecolumn
LIKE '%' + @department + '%'
You concatenate the strings:
select * from sometable where somecolumn LIKE '%' + @department + '%'
It's a variable, it goes outside the quotes.
select * from sometable where somecol like '%' + @department + '%'
Or, more preferably, add the %s to the variable and just use it directly
try:
select * from sometable where somecolumn LIKE '%' + @department + '%'
You could do something like this:
select * from sometable where somecolumn LIKE '%' + @department + '%'
select * from sometable
where CHARINDEX(@department,somecolumn)>0
You can also use the CHARINDEX function.
精彩评论