0

我有以下功能:

DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(adapter_id int(10), view_id int(10),name varchar(255)) RETURNS TINYINT(1)
BEGIN
    DECLARE retOK TINYINT DEFAULT 0;
    IF (SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) = 0 THEN
        INSERT INTO `tables` (`adapter_id`,`view_id`,`name`) VALUES (adapter_id, view_id, name);
        SET retOK = 1;
    END IF;
    RETURN retOK;
END;
$$
DELIMITER ;

当我调用函数插入新行时

SELECT saveTableRow(3,1,'Text');

我得到结果 '0' 并且没有保存新行。

4

2 回答 2

0

这可能是名称冲突问题。列的名称与您的参数名称相同。您需要更改与列名称不同的参数名称。例如,

DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(
       _adapter_id int(10), 
       _view_id int(10),
       _name varchar(255)) 
RETURNS TINYINT(1)
BEGIN
    DECLARE retOK TINYINT DEFAULT 0;
    IF (SELECT COUNT(*) 
        FROM `tables` 
         WHERE `adapter_id`=_adapter_id AND 
               `view_id`=_view_id AND
               `name`=_name ) = 0 THEN
        INSERT INTO `tables` (`adapter_id`,`view_id`,`name`) 
        VALUES (_adapter_id, _view_id, _name);
        SET retOK = 1;
    END IF;
    RETURN retOK;
END;
$$
DELIMITER ;
于 2013-09-20T12:38:00.377 回答
0

更改 if 如下,请尝试:

IF ((SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) < 1)
于 2013-09-20T12:39:40.440 回答