ROLLBACK TO SAVEPOINT
CASE可以吗?我的查询是
BEGIN;
SAVEPOINT my_savepoint;
INSERT INTO DPoint (uuid) VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214')
ON CONFLICT (uuid) DO NOTHING;
WITH
ins1 AS (INSERT INTO Point (latitude, longitude, srid)
VALUES (37.251667, 14.917222, 4326) RETURNING id),
ins2 as (INSERT INTO SPoint (idPt, uuiddpt)
VALUES ((SELECT id FROM ins1), '5547f4b7-00b3-4aac-8ceb-c9ca163a0214') RETURNING id),
ins3 as (INSERT INTO Distance (idSpt, uuiddpt)
VALUES ((SELECT id FROM ins2), '5547f4b7-00b3-4aac-8ceb-c9ca163a0214'))
INSERT INTO DPointTS (uuid, type, name, idPoint)
VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214', NULL, NULL, (SELECT id FROM ins1));
SELECT CASE WHEN
(SELECT uuid FROM DPoint
WHERE uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214' )
is not NULL THEN ROLLBACK TO SAVEPOINT my_savepoint END;
COMMIT;
我的想法是:
再次尝试插入 DPoint.uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214' 时,无需插入 Point、SPoint、Distance、DPointTS。所以我想在事务中将这些插入回滚到 my_savepoint。也许知道我必须以什么方式重写我的代码?
编辑:
SELECT uuid IS NULL AS is_not_uuid FROM DPoint WHERE uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214';
\gset
\if :is_not_uuid
\echo 'insert row to DPoint'
INSERT INTO DPoint (uuid) VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214');
...
my INSERT query
\endif
我在没有 SAVEPOINT 的情况下更新我的策略 - 如果 SELECT 查询返回TRUE
我评估所有插入。我以什么方式执行查询,仅在命令行中?在 DataGRIP 中尝试 console.sql 时,它会引发错误 - 它会诚实地执行所有行,INSERT INTO DPoint (uuid)...
如果该点已经存在,则会失败。我想以一种方式执行语句