我有一个存储过程
CREATE PROCEDURE `DeleteAttachmentsByIDs`(IN p_ids nvarchar(1024), IN p_modifiedBy varchar(100), IN p_modifiedDate datetime)
BEGIN
update attachments set
`IsDeleted` = 1
, `ModifiedBy` = p_modifiedBy
, `ModifiedDate` = p_modifiedDate
where
id IN (p_ids);
END
现在我打电话给
CALL `DeleteAttachmentsByIDs`('12479,12480,12481', 'admin', '2019-04-02 15:32:30.558178')
出现错误
Error Code: 1292. Truncated incorrect DOUBLE value: '12479,12480,12481'
附件表的创建表命令为
CREATE TABLE `attachments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ObjectID` int(11) DEFAULT NULL,
`ObjectName` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`Category` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`ContentType` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`ContentLength` bigint(20) DEFAULT NULL,
`FileName` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`Data` longblob,
`Description` varchar(500) CHARACTER SET utf8 DEFAULT NULL,
`CreatedBy` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`CreatedDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ModifiedBy` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`ModifiedDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`IsDeleted` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `Index1` (`id`,`ObjectID`,`ObjectName`,`IsDeleted`)
) ENGINE=InnoDB AUTO_INCREMENT=12482 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我认为这可能是由需要数组的' in '条件引起的,但是传入了字符串。我认为我需要将字符串拆分为数组以使' in ”的作品。那是正确的,我该怎么做?
答案 0 :(得分:2)
在这种情况下,您可以使用FIND_IN_SET
:
CREATE PROCEDURE `DeleteAttachmentsByIDs` (IN p_ids NVARCHAR(1024), IN p_modifiedBy VARCHAR(100), IN p_modifiedDate DATETIME)
BEGIN
UPDATE attachments SET
`IsDeleted` = 1,
`ModifiedBy` = p_modifiedBy,
`ModifiedDate` = p_modifiedDate
WHERE FIND_IN_SET(id, p_ids) > 0 AND id <> 0; -- AND id <> 0 to support safe-mode.
END
另一个解决方案可以是,将字符串与LIKE
进行比较:
CREATE PROCEDURE `DeleteAttachmentsByIDs` (IN p_ids NVARCHAR(1024), IN p_modifiedBy VARCHAR(100), IN p_modifiedDate DATETIME)
BEGIN
UPDATE attachments SET
`IsDeleted` = 1,
`ModifiedBy` = p_modifiedBy,
`ModifiedDate` = p_modifiedDate
WHERE CONCAT(',', p_ids, ',') LIKE CONCAT('%,', id, ',%') AND id <> 0; -- AND id <> 0 to support safe-mode.
END
默认情况下,MySQL Workbench使用安全模式。因此,您可以添加AND id <> 0
来抑制MySQL Workbench引发的错误。在这里阅读有关安全模式的更多信息:MySQL error code: 1175 during UPDATE in MySQL Workbench