MySQL存储过程语法错误

时间:2014-10-03 05:43:52

标签: mysql stored-procedures syntax-error

我对MySQL(以及一般的数据库)非常陌生,并且负责制作一个存储过程,接受varchar作为参数,用作从表的2列中搜索关键字。< / p>

我不熟悉语法,搜索互联网并没有像所希望的那样富有成效。这就是我想出的:

DELIMITER //

DROP PROCEDURE IF EXISTS proc_Search
CREATE PROCEDURE proc_Search (IN word varchar(100))
BEGIN
SELECT Section.Name 
FROM Section inner join DetailsMats ON Section.NumSection=DetailsMats.NumSection 
WHERE DetailsMats.TechName or DetailsMats.GenName LIKE %word%;
END//

只是想知道我是否在这里做了一些令人作呕的错误,或者我是否走上了正确的道路。谢谢!

3 个答案:

答案 0 :(得分:0)

似乎应该是这样的:

SELECT * 
FROM MyTable 
WHERE (Column1 LIKE '%keyword1%' OR Column2 LIKE '%keyword1%') 
      AND (Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%');

答案 1 :(得分:0)

尝试使用concat来转义%字符。    快乐的编码:)

DROP PROCEDURE IF EXISTS proc_Search CREATE PROCEDURE proc_Search (IN word varchar(100)) BEGIN SET @sql = CONCAT('SELECT Section.Name FROM Section inner join DetailsMats ON Section.NumSection=DetailsMats.NumSection WHERE DetailsMats.TechName or DetailsMats.GenName LIKE "',job_no, '%"'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; END $$

答案 2 :(得分:0)

我在您的代码中看到两个错误:

  1. //
  2. 之后缺少分隔符DROP PROCEDURE ...
  3. LIKE表达式必须是字符串。我使用CONCAT('%', word, '%')来获取word'%'
  4. 的字符串

    更正后的代码:

    DELIMITER //
    
    DROP PROCEDURE IF EXISTS proc_Search //  -- delimiter here
    
    CREATE PROCEDURE proc_Search (IN word varchar(100))
    BEGIN
    SELECT Section.Name 
    FROM Section inner join DetailsMats ON Section.NumSection=DetailsMats.NumSection 
    WHERE DetailsMats.TechName or DetailsMats.GenName LIKE CONCAT('%', word, '%');
    END
    //