简体   繁体   中英

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 does 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 + '%' 

您将字符串连接起来:

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

尝试:

select * from sometable where somecolumn LIKE '%' + @department + '%'

您可以执行以下操作:

select * from sometable where somecolumn LIKE '%' + @department + '%'
select * from sometable 
where CHARINDEX(@department,somecolumn)>0 

You can also use the CHARINDEX function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM