尝试在MySql中创建存储过程但收到错误

时间:2015-06-26 07:28:17

标签: mysql stored-procedures

我试图创建一个存储过程但是得到一个错误,任何人都可以告诉我的代码中有什么错误

_id

3 个答案:

答案 0 :(得分:1)

你可以试试这个,交配:

DELIMITER //
DROP PROCEDURE IF EXISTS new_pro //
CREATE PROCEDURE new_pro (
    IN f_name VARCHAR(100),
    IN l_name VARCHAR(100)
)
BEGIN
    CASE
        WHEN (f_name IS NOT NULL OR (f_name != '')) THEN
            SELECT * FROM tbl_contact_master WHERE first_name = f_name;
        WHEN (l_name IS NOT NULL OR (l_name != '')) THEN
            SELECT * FROM tbl_contact_master WHERE last_name = l_name;
        WHEN (
            (f_name IS NOT NULL OR (f_name != ''))
            AND (l_name IS NOT NULL OR (l_name != ''))
        ) THEN
            SELECT * FROM tbl_contact_master WHERE first_name = f_name AND last_name = l_name;  
    END CASE;
END //
DELIMITER ;

答案 1 :(得分:0)

首先添加分隔符并结束。像这样

DELIMITER //
    CREATE PROCEDURE new_pro
    (IN f_name VARCHAR(100),IN l_name VARCHAR(100))
    BEGIN
    IF (f_name != '')
     BEGIN
         SELECT * FROM tbl_contact_master
         WHERE first_name = f_name
     END
    ELSE IF (l_name != '')
     BEGIN
         SELECT * FROM tbl_contact_master
         WHERE last_name = l_name
     END
    ELSE IF (f_name != '' && l_name != '')
       BEGIN
         SELECT * FROM tbl_contact_master
         WHERE first_name = f_name AND last_name = l_name
       END
    END
    DELIMITER ;

答案 2 :(得分:0)

以下内容应该有效:

DELIMITER //
CREATE PROCEDURE new_pro
(IN f_name VARCHAR(100),IN l_name VARCHAR(100))
IF f_name != '' AND l_name != '' THEN
     SELECT * FROM tbl_contact_master
     WHERE first_name = f_name AND last_name = l_name;
ELSEIF f_name != '' THEN
     SELECT * FROM tbl_contact_master
     WHERE first_name = f_name;
ELSEIF l_name != '' THEN
     SELECT * FROM tbl_contact_master
     WHERE last_name = l_name;
END IF
//
DELIMITER ;

您错过了THEN语句中的IF个关键字。 SELECT正文中的IF语句需要以;结尾。您需要先使用DELIMITER语句,以便那些内部;不会结束此过程。

您不需要BEGINEND,因为IF是一个声明。

填写f_namel_name的测试需要先行。否则,它只会执行f_name查询。