如何在存储过程中找到哪个唯一索引失败?
我创建了一个包含单列主键和三个单列唯一索引的表:
CREATE TABLE tableName (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
col1 VARCHAR(50) NOT NULL,
col2 VARCHAR(50) NOT NULL,
col3 VARCHAR(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX id_UNIQUE (id ASC),
UNIQUE INDEX col1_UNIQUE (col1 ASC),
UNIQUE INDEX col2_UNIQUE (col2 ASC),
UNIQUE INDEX col3_UNIQUE (col3 ASC)
) ENGINE = InnoDB;
我创建了一个存储过程来处理错误'1062':'重复条目%s'为密钥%d':
DELIMITER $$
CREATE PROCEDURE insertRecord (
col1Value VARCHAR(50),
col2Value VARCHAR(50),
col3Value VARCHAR(50),
OUT idValue INT,
OUT errColName VARCHAR(50)
) BEGIN
DECLARE EXIT HANDLER FOR 1062 BEGIN
SET errColName = 'Insert Column Name Here';
END;
INSERT tableName SET
col1 = col1Value,
col2 = col2Value,
col3 = col3Value;
SET idValue = LAST_INSERT_ID();
END;
$$
DELIMITER ;
我使用不同的数据调用存储过程5次以返回各种结果:
第一次INSERT尝试成功
-- Initialize parameters
SET @col1Val = 'MySQL',
@col2Val = 'Is',
@col3Val = 'Cool',
@Id = NULL,
@ErrColName = NULL;
-- 1st Insert Attempt
CALL insertRecord(@col1Val, @col2Val, @col3Val, @Id, @ErrColName);
-- Expected result: @Id = 1, @ErrColName = NULL
SELECT @Id '@Id', @ErrColName '@ErrColName';
第二次INSERT尝试失败,因为col1不是唯一的
-- Re-initialize parameters
SET @col1Val = 'MySQL', -- Intended to generate an error
@col2Val = 'Is',
@col3Val = 'Cool',
@Id = NULL,
@ErrColName = NULL;
-- 2nd Insert Attempt
CALL insertRecord(@col1Val, @col2Val, @col3Val, @Id, @ErrColName);
-- Expected result: @Id = NULL, @ErrColName = 'col1'
SELECT @Id '@Id', @ErrColName '@ErrColName';
第3次INSERT尝试失败,因为col2不是唯一的
-- Re-initialize parameters
SET @col1Val = 'SQL',
@col2Val = 'Is', -- Intended to generate an error
@col3Val = 'Cool',
@Id = NULL,
@ErrColName = NULL;
-- 3rd Insert Attempt
CALL insertRecord(@col1Val, @col2Val, @col3Val, @Id, @ErrColName);
-- Expected result: @Id = NULL, @ErrColName = 'col2'
SELECT @Id '@Id', @ErrColName '@ErrColName';
第4次INSERT尝试失败,因为col3不是唯一的
-- Re-initialize parameters
SET @col1Val = 'SQL',
@col2Val = 'For',
@col3Val = 'Cool', -- Intended to generate an error
@Id = NULL,
@ErrColName = NULL;
-- 4th Insert Attempt
CALL insertRecord(@col1Val, @col2Val, @col3Val, @Id, @ErrColName);
-- Expected result: @Id = NULL, @ErrColName = 'col3'
SELECT @Id '@Id', @ErrColName '@ErrColName';
第五次INSERT尝试成功
-- Re-initialize parameters
SET @col1Val = 'SQL',
@col2Val = 'For',
@col3Val = 'Life',
@Id = NULL,
@ErrColName = NULL;
-- 5th Insert Attempt
CALL insertRecord(@col1Val, @col2Val, @col3Val, @Id, @ErrColName);
-- Expected result: @Id = 5, @ErrColName = NULL
SELECT @Id '@Id', @ErrColName '@ErrColName';
在存储过程中我需要做什么才能在存储过程中找到哪个唯一索引失败?
提前致谢。
答案 0 :(得分:1)
我在Which unique key is hit with my insert?下找到了答案。但这并不理想,但解决了这个问题。
以下是应用的解决方案:
...
DECLARE EXIT HANDLER FOR 1062 BEGIN
-- Check if col1Value is already in use
IF EXISTS (
SELECT 1
FROM tableName
WHERE col1 = col1value
LIMIT 1
) THEN
SET errColName = 'Col1';
-- Check if col2Value is already in use
ELSEIF EXISTS (
SELECT 1
FROM tableName
WHERE col2 = col2value
LIMIT 1
) THEN
SET errColName = 'Col2';
-- Check if col3Value is already in use
ELSEIF EXISTS (
SELECT 1
FROM tableName
WHERE col3 = col3value
LIMIT 1
) THEN
SET errColName = 'Col3';
END IF;
END;
...