简体   繁体   English

使用存储过程搜索字符串

[英]Use a stored procedure to search for a string

How can I pass paramenter into a stored procedure and use it to search the database?如何将参数传递给存储过程并使用它来搜索数据库?

I am trying to search the database for a business name which has the search term within it.我正在尝试在数据库中搜索包含搜索词的公司名称。

The following SP can be used to return all results:以下 SP 可用于返回所有结果:

BEGIN

SELECT company.businessname
FROM company;

END

Returns this result:返回此结果:

Better Cats Vetinary Practice
Appliance R'Us
The Beautiful Flower Company
Lovely Trips Away

However, if I use the 'searchtext' parameter which is passed in like this, and that parament is given the value of 'appliance' it should return one result, but returns nothing:但是,如果我使用像这样传入的 'searchtext' 参数,并且该参数被赋予 'appliance' 的值,它应该返回一个结果,但什么也不返回:

BEGIN

SELECT company.businessname
FROM company
WHERE company.businessname LIKE searchtext;
END

How can I get it to return the one result with 'appliance' in the business name?如何让它返回企业名称中带有“设备”的一个结果?

The solution to this appears to be to put the pass the named parameter including the %...eg searchtext = %appliance%, then the SP becomes:对此的解决方案似乎是传递包含 %...eg searchtext = %appliance% 的命名参数,然后 SP 变为:

SELECT company.businessname
FROM company
WHERE company.businessname like searchtext;

Then it returns the result expect.ed然后它返回结果 expect.ed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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