简体   繁体   English

SQL存储过程字符串通过传递参数搜索并使用“LIKE”查询

[英]SQL Stored Procedure String Search via Passed Parameter and Using 'LIKE' query

I am trying to create a stored procedure that will search my table using the parameter passed by the user as it calls the SP. 我正在尝试创建一个存储过程,它将使用用户在调用SP时传递的参数来搜索我的表。 I would like to use the 'LIKE' query for this. 我想使用'LIKE'查询。 So far, this is what I got. 到目前为止,这就是我得到的。

DROP PROCEDURE IF EXISTS `SEARCH_STUDENT`$$
CREATE
    PROCEDURE `test`.`SEARCH_STUDENT`(_textInput VARCHAR(10))
    BEGIN

    SELECT * FROM records WHERE `lastname` LIKE '%'+_textInput+'%';
    END$$

but im getting this error: 但我得到这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@Search varchar(10);
    SET @Search=_textInput;
    SELECT * FROM records WHERE `last' at line 10

What I get from google are samples with fixed string to be searched. 我从谷歌获得的是带有固定字符串的样本。 Mine depends on what the user will input in the textbox. 我的取决于用户将在文本框中输入的内容。 Thanks a lot for any help in advanced. 非常感谢您提供的任何帮助。

I got it now. 我现在明白了。 Thanks to this link. 感谢这个链接。

http://www.sitepoint.com/forums/showthread.php?620319-MySQL-Stored-Procedure-LIKE-and-a-string-variable-the-combination-don-t-work http://www.sitepoint.com/forums/showthread.php?620319-MySQL-Stored-Procedure-LIKE-and-a-string-variable-the-combination-don-t-work

The sql CONCAT function is the answer. sql CONCAT函数就是答案。 Here's my code now: 这是我现在的代码:

CREATE
    PROCEDURE `test`.`SEARCH_STUDENT`(_textInput VARCHAR(10))
    BEGIN

    -- sELECT * FROM records WHERE `lastname` LIKE '%' + _textInput;
    SELECT * FROM records WHERE `lastname` LIKE CONCAT('%',_textInput,'%');

    END$$
CREATE PROCEDURE `test`.`SEARCH_STUDENT`(in _textInput VARCHAR(10))
BEGIN
    -- SELECT * FROM records WHERE `lastname` LIKE '%' + _textInput;
    SELECT * FROM records WHERE `lastname` LIKE CONCAT('%',_textInput,'%');
END$$

put in 投入

\`test\`.\`SEARCH_STUDENT\`(in _textInput VARCHAR(10))

other wise out will not show properly 其他明智的将无法正常显示

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

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