现在我有了这个MySQL存储过程,但是我如何检查该行是否已经存在呢?
CREATE DEFINER=`edmetrics`@`%` PROCEDURE `CreateTestJob`(JobLink varchar(300), StartTime datetime, Endtime datetime, Owner_info varchar(45), Engine_info varchar(100), TestSuiteId INT, TestSuiteCollectionId INT, Finished varchar(45), JenkinsBuild INT(100), JenkinsJobName varchar(100))
BEGIN
IF TestSuiteId = '' THEN
SET TestSuiteId = null;
END IF;
IF TestSuiteCollectionId = '' THEN
SET TestSuiteCollectionId = null;
END IF;
IF TestSuiteCollectionId != '' THEN
SET TestSuiteId = null;
END IF;
INSERT INTO TestJob (`id`,`JobLink`,`StartTime`,`Endtime`,`Owner`,`Engine`,`TestSuiteId`,`TestSuiteCollectionId`,`Finished`,`JenkinsBuild`,`JenkinsJobName`)
VALUES (NULL, JobLink, StartTime, Endtime, Owner_info, Engine_info, TestSuiteId, TestSuiteCollectionId, Finished, JenkinsBuild, JenkinsJobName);
SELECT LAST_INSERT_ID() AS LastInsertId;
END
因此,按照建议,我应该使用if exists
,但一个变量为空,然后将其设置为null,如果不是,那么就有一个变量。但这会使它有点困难,因为这行不通:
BEGIN
declare return_id int;
IF TestSuiteId = '' THEN
SET TestSuiteId = null;
END IF;
IF TestSuiteCollectionId = '' THEN
SET TestSuiteCollectionId = null;
END IF;
IF TestSuiteCollectionId != '' THEN
SET TestSuiteId = null;
END IF;
IF (EXISTS(SELECT
`JobLink`,
`StartTime`,
`Endtime`,
`Owner`,
`Engine`,
`TestSuiteId`,
`TestSuiteCollectionId`,
`Finished`,
`JenkinsBuild`,
`JenkinsJobName`
FROM
testreportingdebug.testjob
WHERE
`JobLink` = JobLink
AND `StartTime` = StartTime
AND `Endtime` = Endtime
AND `Owner` = Owner_info
AND `Engine` = Engine_info
AND `TestSuiteId` = TestSuiteId
AND `TestSuiteCollectionId` = TestSuiteCollectionId
AND `Finished` = Finished
AND `JenkinsBuild` = JenkinsBuild
AND `JenkinsJobName` = JenkinsJobName LIMIT 1))
THEN
SET return_id = -1;
ELSE
INSERT INTO TestJob (`id`,`JobLink`,`StartTime`,`Endtime`,`Owner`,`Engine`,`TestSuiteId`,`TestSuiteCollectionId`,`Finished`,`JenkinsBuild`,`JenkinsJobName`)
VALUES (NULL, JobLink, StartTime, Endtime, Owner_info, Engine_info, TestSuiteId, TestSuiteCollectionId, Finished, JenkinsBuild, JenkinsJobName);
SET return_id = LAST_INSERT_ID();
END IF;
SELECT return_id AS LastInsertId;
END
这将只工作,如果所有变量都设置,如果一个或多个是null
那么它不工作。因为你不能说varaible = null
,而只能说variable is null
和其他方式。我该如何解决这个问题?
1条答案
按热度按时间lzfw57am1#
您可以使用IF_EXIST命令进行检查。
谢谢你,谢谢。