将MySQL触发器重写为Postgres
伙计们 我在MySQL数据库中有一个触发器:将MySQL触发器重写为Postgres,mysql,sql,postgresql,stored-procedures,Mysql,Sql,Postgresql,Stored Procedures,伙计们 我在MySQL数据库中有一个触发器: CREATE DEFINER="root"@"127.0.0.1" TRIGGER `tai_actions_for_active_count` AFTER INSERT ON `actions` FOR EACH ROW BEGIN DECLARE l_isnn TINYTEXT; IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN SET l_isnn
CREATE DEFINER="root"@"127.0.0.1" TRIGGER `tai_actions_for_active_count` AFTER INSERT ON `actions` FOR EACH ROW BEGIN
DECLARE l_isnn TINYTEXT;
IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
SET l_isnn = IF(NEW.isnn is NULL, '*', NEW.isnn);
IF NOT NEW.action_type = 'CLOSE' THEN
INSERT INTO subscriptions.`statistics_active_count`(`service_id`, `operator_id`, `isnn`, `active_count`)
VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1)
ON DUPLICATE KEY UPDATE active_count = active_count + 1;
ELSE
SET @tai_actions_for_active_count = -1;
UPDATE subscriptions.`statistics_active_count` SET active_count = @tai_actions_for_active_count := active_count - 1
WHERE `service_id` = NEW.service_id AND `operator_id` = NEW.operator_id AND `isnn` = l_isnn;
IF @tai_actions_for_active_count = 0 THEN DELETE FROM subscriptions.`statistics_active_count` WHERE `active_count` = 0; END IF;
END IF;
END IF;
END
所以我需要重写它,使它在Postgres数据库中工作。由于存在重复密钥更新,我使用的是Postgres 9.5版本,其中UPSERT ON CONFLICT KEY DO UPDATE
所以我对SQL语言知之甚少,你能告诉我我做错了什么吗?这是Postgres PL代码:
DECLARE
l_isnn TEXT;
tai_actions_for_active_count INTEGER;
BEGIN
IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
IF NEW.isnn is NULL THEN
l_isnn := '*';
ELSE
l_isnn := NEW.isnn;
END IF;
IF NOT NEW.action_type = 'CLOSE' THEN
INSERT INTO "subscriptions.statistics_active_count"(service_id, operator_id, isnn, active_count)
VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1)
ON CONFLICT(active_count) DO UPDATE SET active_count = active_count + 1;
ELSE
tai_actions_for_active_count := -1;
UPDATE "subscriptions.statistics_active_count" SET active_count = active_count - 1
-- (tai_actions_for_active_count := active_count - 1)
WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
UPDATE "subscriptions.statistics_active_count" SET tai_actions_for_active_count = active_count
WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
IF tai_actions_for_active_count = 0 THEN DELETE FROM "subscriptions.statistics_active_count" WHERE active_count = 0; END IF;
END IF;
END IF;
RETURN NULL;
END;
因为我想测试这个触发器,所以得到了一个错误关系subscriptions.statistics\u active\u count不存在
你能帮我处理代码吗?我终于找到了解决方案。我想:
BEGIN
IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
IF NEW.isnn IS NULL THEN
l_isnn := '*';
ELSE
l_isnn := NEW.isnn;
END IF;
IF NOT NEW.action_type = 'CLOSE' THEN
BEGIN
INSERT INTO subscriptions.statistics_active_count (service_id, operator_id, isnn, active_count)
VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1);
EXCEPTION WHEN unique_violation THEN
UPDATE subscriptions.statistics_active_count SET active_count = active_count + 1
WHERE service_id = NEW.service_id and operator_id=NEW.operator_id;
END;
ELSE
tai_actions_for_active_count := -1;
WITH upd AS
(UPDATE subscriptions.statistics_active_count
SET active_count = active_count - 1
WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
RETURNING active_count)
SELECT *
FROM upd INTO tai_actions_for_active_count;
IF tai_actions_for_active_count = 0 THEN
DELETE FROM public.statistics_active_count
WHERE active_count = 0;
END IF;
END IF;
END IF;
END;
您之所以出现此错误,是因为统计信息\u活动\u计数可能不存在。我的意思是,你有一个名为subscriptions.statistics\u active\u count的表吗?或者在订阅模式中有一个名为statistics\u active\u count的表?若表和架构是小写的,只需删除引号,它就会运行。@user\u 0,我在订阅数据库中有统计数据\u active\u count表。我现在试图删除引号,但仍然是这个错误。如果订阅是当前数据库,只需将其从语法中删除即可。如果没有,请添加有关涉及的架构、db、foreigndatawrappers的一些信息。我已删除订阅,现在我有另一个错误:它说列引用活动计数在..nflicative\u count DO UPDATE SET active\u count=active\u cou…处不明确。。。。据我所知,我需要以某种方式从数据库中选择active_count?很感谢您尝试它,而不仅仅是说如何转换此触发器并期望其他人执行此操作。不过,我认为我们需要看看表格的定义。