从插入语句的函数返回mysql错误消息

时间:2013-09-28 06:22:25

标签: mysql sql user-defined-functions

我在MySQL中有以下功能。 当一行成功插入行时,如何使函数返回GUID,如果“插入”出现错误,如何返回MySQL错误消息。

DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `fnInsertBusinessInfo`(
    BU_NAME VARCHAR(300),
    BU_PHONE VARCHAR(20),
    BU_TYPE VARCHAR(50) 
) RETURNS varchar(100) CHARSET latin1
BEGIN
DECLARE
    BU_ID VARCHAR(100);
    SET BU_ID := (SELECT UUID());

    INSERT INTO tb_business_info (BU_ID,BU_NAME,BU_PHONE,BU_TYPE) values (BU_ID,BU_NAME,BU_PHONE,BU_TYPE);

RETURN BU_ID;
END

我已经设法返回id但是如何在不成功的插入时返回错误消息?

1 个答案:

答案 0 :(得分:0)

DECLARE
    BU_ID VARCHAR(100);
        --modify SET BU_ID := (SELECT UUID());
    SELECT UUID() into BU_ID;


    INSERT INTO tb_business_info (BU_ID,BU_NAME,BU_PHONE,BU_TYPE) values (BU_ID,BU_NAME,BU_PHONE,BU_TYPE);




RETURN BU_ID;
END