我试图创建一个存储过程但是得到一个错误,任何人都可以告诉我的代码中有什么错误
_id
答案 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
语句,以便那些内部;
不会结束此过程。
您不需要BEGIN
和END
,因为IF
是一个声明。
填写f_name
和l_name
的测试需要先行。否则,它只会执行f_name
查询。