我试图将这个mssql触发器转换为mysql,因为它已经插入并删除了逻辑表,我无法使其工作。
任何帮助或建议都将受到高度赞赏:)
CREATE trigger [dbo].[UpdateBuildCacheScenarioCounts] on [dbo].[ScenarioResults] after insert, update, delete as
SET NOCOUNT ON
declare buildidcur cursor fast_forward for (select [BuildId] from inserted group by [BuildId]) UNION (select [BuildId] from deleted group by [BuildId])
open buildidcur
declare @buildid [Int]
fetch next from buildidcur into @buildid
while @@FETCH_STATUS = 0
begin
--shonky attempt at preventing deadlocks
update [Builds] set
CacheTotalCount = (select Count(1) from ScenarioResults with (nolock) where [BuildId] = @buildId),
CachePassCount = (select Count(1) from ScenarioResults with (nolock) where [BuildId] = @buildId and [IsPassed] = 1)
where [Id] = @buildid
-- original, incase we need to rollback the NOLOCK
-- update [Builds] set CacheTotalCount = (select Count(1) from ScenarioResults where [BuildId] = @buildId) where [Id] = @buildid
-- update [Builds] set CachePassCount = (select Count(1) from ScenarioResults where [BuildId] = @buildId and [Passed] = 1) where [Id] = @buildid
fetch next from buildidcur into @buildid
end
close buildidcur
deallocate buildidcur
SET NOCOUNT OFF
我试过这个但是没有为表ScenarioResults插入假值。
BEGIN
DECLARE buildid INT;
DECLARE done INT DEFAULT FALSE;
DECLARE buildidcur cursor FOR (select BuildId from ScenarioResults WHERE BuildId = NEW.BuildId group by BuildId ORDER BY CreatedAt DESC);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN buildidcur;
myloop: LOOP
FETCH buildidcur INTO buildid;
IF done THEN LEAVE myloop;
END IF;
UPDATE Builds SET
CacheTotalCount = (SELECT COUNT(1) FROM ScenarioResults WHERE BuildId = buildId),
CachePassCount = (SELECT COUNT(1) FROM ScenarioResults WHERE BuildId = buildId AND IsPassed = 1) WHERE Id = buildid;
END LOOP;
CLOSE buildidcur;
END
答案 0 :(得分:0)
触发器产生我期望的结果
DROP TRIGGER IF EXISTS T;
DELIMITER $$
CREATE TRIGGER T AFTER INSERT ON SCENARIORESULTS
FOR EACH ROW
BEGIN
DECLARE Vbuildid INT;
DECLARE VPASSED INT;
DECLARE done INT DEFAULT FALSE;
DECLARE buildidcur cursor FOR
(select BuildId from ScenarioResults WHERE BuildId = NEW.BuildId group by BuildId);# ORDER BY CreatedAt DESC);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN buildidcur;
myloop: LOOP
FETCH buildidcur INTO Vbuildid;
INSERT INTO DEBUG_TABLE(MSG,MSG2) VALUES (VBUILDID,VPASSED);
IF done THEN
LEAVE myloop;
END IF;
UPDATE Builds
SET CacheTotalCount = (SELECT COUNT(1) FROM ScenarioResults WHERE BuildId = VbuildId),
CachePassCount = (SELECT COUNT(1) FROM ScenarioResults WHERE BuildId = VbuildId AND IsPassed = 1) WHERE Id = Vbuildid;
END LOOP;
CLOSE buildidcur;
END $$
DELIMITER ;
DROP TABLE IF EXISTS SCENARIORESULTS,BUILDS;
CREATE TABLE BUILDS(ID INT, CACHETOTALCOUNT INT,CACHEPASSCOUNT INT);
CREATE TABLE SCENARIORESULTS (BUILDID INT, ISPASSED INT);
MariaDB [sandbox]> TRUNCATE TABLE BUILDS;
Query OK, 0 rows affected (0.22 sec)
MariaDB [sandbox]> TRUNCATE TABLE SCENARIORESULTS;
Query OK, 0 rows affected (0.25 sec)
MariaDB [sandbox]> INSERT INTO BUILDS VALUES (1,NULL,NULL),(2,NULL,NULL);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]> SELECT * FROM BUILDS;
+------+-----------------+----------------+
| ID | CACHETOTALCOUNT | CACHEPASSCOUNT |
+------+-----------------+----------------+
| 1 | NULL | NULL |
| 2 | NULL | NULL |
+------+-----------------+----------------+
2 rows in set (0.01 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> INSERT INTO SCENARIORESULTS VALUES (1,0);
Query OK, 1 row affected (0.02 sec)
MariaDB [sandbox]> SELECT * FROM BUILDS;
+------+-----------------+----------------+
| ID | CACHETOTALCOUNT | CACHEPASSCOUNT |
+------+-----------------+----------------+
| 1 | 1 | 0 |
| 2 | NULL | NULL |
+------+-----------------+----------------+
2 rows in set (0.00 sec)
MariaDB [sandbox]> INSERT INTO SCENARIORESULTS VALUES (1,1);
Query OK, 1 row affected (0.08 sec)
MariaDB [sandbox]> SELECT * FROM BUILDS;
+------+-----------------+----------------+
| ID | CACHETOTALCOUNT | CACHEPASSCOUNT |
+------+-----------------+----------------+
| 1 | 2 | 1 |
| 2 | NULL | NULL |
+------+-----------------+----------------+
2 rows in set (0.00 sec)
MariaDB [sandbox]> INSERT INTO SCENARIORESULTS VALUES (2,0);
Query OK, 1 row affected (0.03 sec)
MariaDB [sandbox]> SELECT * FROM BUILDS;
+------+-----------------+----------------+
| ID | CACHETOTALCOUNT | CACHEPASSCOUNT |
+------+-----------------+----------------+
| 1 | 2 | 1 |
| 2 | 1 | 0 |
+------+-----------------+----------------+
2 rows in set (0.00 sec)
MariaDB [sandbox]> INSERT INTO SCENARIORESULTS VALUES (2,1);
Query OK, 1 row affected (0.02 sec)
MariaDB [sandbox]> SELECT * FROM BUILDS;
+------+-----------------+----------------+
| ID | CACHETOTALCOUNT | CACHEPASSCOUNT |
+------+-----------------+----------------+
| 1 | 2 | 1 |
| 2 | 2 | 1 |
+------+-----------------+----------------+
2 rows in set (0.00 sec)
MariaDB [sandbox]> INSERT INTO SCENARIORESULTS VALUES (2,0);
Query OK, 1 row affected (0.02 sec)
MariaDB [sandbox]> SELECT * FROM BUILDS;
+------+-----------------+----------------+
| ID | CACHETOTALCOUNT | CACHEPASSCOUNT |
+------+-----------------+----------------+
| 1 | 2 | 1 |
| 2 | 3 | 1 |
+------+-----------------+----------------+
2 rows in set (0.00 sec)