存储过程 Mysql 开始 结束

Stored procedure Mysql BEGIN END

我正在编写一个简单的存储过程来获取数据并将其输入 table

CREATE DEFINER = 'gunsdb-3939c99d-COPY-39393fb6'@'%'
PROCEDURE `gunsdb-3939c99d-COPY-39393fb6`.sp_entity_save(IN p_entity_id INT, IN p_entity_type_id INT, IN p_title_id INT, IN p_first_name VARCHAR(255), IN p_last_name VARCHAR(255), IN p_street_line_1 VARCHAR(255), IN p_street_line_2 VARCHAR(255), IN p_street_line_3 VARCHAR(255), IN p_town VARCHAR(255), IN p_county VARCHAR(255), IN p_postcode VARCHAR(255), IN p_email VARCHAR(255), IN p_telephone INT, IN p_company_name VARCHAR(500), IN p_company_desc VARCHAR(500), IN p_website VARCHAR(500), IN p_rfd_number INT, IN p_issuing_force VARCHAR(255), IN p_concent VARCHAR(255))
BEGIN    
INSERT INTO tbl_entity (
entity_id,
entity_type_id,
title_id,
first_name,
last_name,
street_line_1,
street_line_2,
street_line_3,
town,county,
postcode,
email,
telephone,
company_name,
company_desc,
website,
rfd_number,
issuing_force,
concent
)

VALUES
(
p_entity_id,
p_entity_type_id,
p_title_id,
p_first_name,
p_last_name,
p_street_line_1 , 
p_street_line_2,
p_street_line_3 ,
p_town, @county , 
p_postcode,
p_email ,
p_telephone,
p_company_name , 
p_company_desc, 
p_website, 
p_rfd_number , 
p_issuing_force , 
p_concent
)
ON DUPLICATE KEY UPDATE
    entity_type_id = p_entity_type_id, 
    title_id = p_title_id, 
    first_name = p_first_name, 
    last_name = p_last_name, 
    street_line_1 = p_street_line_1,
    street_line_2 = p_street_line_2,
    street_line_2 = p_street_line_2,
    town = p_town,
    county= p_county,
    postcode = p_postcode,
    email = p_email,
    telephone = p_telephone,
    company_name = p_company_name,
    company_desc = p_company_desc,
    website = p_website,
    rfd_number = p_rfd_number,
    issuing_force = p_issuing_force,
    concent = p_concent  
END

以上是抛出错误,但只要删除 BEGIN 和 END,它就可以正常工作,我不明白为什么。

我尝试添加 DELIMITER,但似乎遇到了同样的问题:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 77 SQL1.sql 77 3

我正在使用 dbforge,但我也尝试过在 Navicat 中遇到同样的问题。

您是否使用了正确的分隔符?在您的代码中,插入末尾没有分隔符。
您应该在插入的末尾使用定界符。

delimiter //
CREATE 
PROCEDURE sp_entity_save(IN p_entity_id INT, IN p_concent VARCHAR(255))
BEGIN    
INSERT INTO tbl_entity (
entity_id,
concent
)
VALUES
(
p_entity_id,
p_concent
)
ON DUPLICATE KEY UPDATE
    -- delimiter here
    concent = p_concent;
-- delimiter here
END //