我有这个程序:
DELIMITER //
CREATE PROCEDURE add_package(nam VARCHAR(255), pack_id INT, lib_id INT, descrip TEXT)
BEGIN
DECLARE ind INT;
DECLARE `_rollback` BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` = 1;
START TRANSACTION;
INSERT INTO java_master (name, description) VALUES (nam, descrip);
SET ind = (SELECT id FROM java_master WHERE name = nam AND description = descrip);
INSERT INTO java_package (master_id, parent_package_id, library_id) VALUES (ind, pack_id, lib_id);
IF `_rollback` THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END //
DELIMITER ;
我需要在中间创建set语句并返回id
表中的java_master
,即使description是空值...有关如何执行此操作的任何建议?
答案 0 :(得分:1)
由于您没有为“id”列提供值,我将假设您的id是一个自动递增值。
- 我想象你的桌子是什么样的? SHOW CREATE TABLE java_master;
CREATE TABLE java_master (id INTEGER PRIMARY KEY AUTO_INCREMENT, name CHAR(10), description VARCHAR(200));
- 插入null的示例
INSERT INTO java_master(name, description)
VALUES("class", NULL);
- 如果是这种情况,您应该使用LAST_INSERT_ID()来获取表的最后创建的自动递增值。 SET ind to this。
SELECT LAST_INSERT_ID();
如果你坚持这样做,那么我认为你可以这样做:
SELECT id FROM java_master WHERE name = nam AND IFNULL(description, "") = IFNULL(descrip, "");
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_last-insert-id
答案 1 :(得分:0)
使用Voidmain提供的答案,我的问题的解决方案与以下代码无关......
DELIMITER //
CREATE PROCEDURE add_package(nam VARCHAR(255), pack_id INT, lib_id INT, descrip TEXT)
BEGIN
DECLARE `_rollback` BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` = 1;
START TRANSACTION;
INSERT INTO java_master (name, description) VALUES (nam, descrip);
INSERT INTO java_package (master_id, parent_package_id, library_id) VALUES ((SELECT LAST_INSERT_ID()), pack_id, lib_id);
IF `_rollback` THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END //
DELIMITER ;