我正在尝试使存储过程调用AddCluster 女巫正在参与'title'和'alt'
案例1:
如果'标题'在数据库中,那么只返回“旧”行!
案例2:
如果“标题”不在数据库中,那么
在parmerer'title'和'alt'
上插入一行然后通过LAST_INSERT_ID()
选择新添加的行问题在于案例2,它只返回空!!
-- --------------------------------------------------------------------------------
-- AddCluster Group Routines
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`linkbay_dk`@`%` PROCEDURE `AddCluster`(in in_title varchar(45), in in_alt text)
BEGIN
/* check if 'in_title' is in db */
IF EXISTS
(
SELECT count(*) FROM Cluster
WHERE title=in_title
)
THEN
/* returns old Cluster there is in db */
SELECT * FROM Cluster WHERE title=in_title;
ELSE
INSERT INTO Cluster
(
`id`,
`create_at`,
`title`,
`alt`
)
VALUES
(
null,
NOW(),
in_title,
in_alt
);
/* returns the newly added Cluster */
SELECT * FROM Cluster WHERE id=LAST_INSERT_ID();
END IF;
END$$
答案 0 :(得分:0)
尝试INSERT而不指定id:
...
ELSE
INSERT INTO Cluster
(
`create_at`,
`title`,
`alt`
)
VALUES
(
NOW(),
in_title,
in_alt
);
/* returns the newly added Cluster */
...
确保Cluster.id
列为AUTO_INCREMENT
列,只有这样才能获得LAST_INSERT_ID()
的值。
答案 1 :(得分:0)
稍微更新一下。
我发现错误..这是count(*)
SELECT count(*) FROM Cluster WHERE title=in_title
应该是这样的:
SELECT * FROM Cluster WHERE title=in_title
请参阅漏洞更新存储过程here!