我有这个使用 pg_query_params 在 php 中运行的 sql。但是,如果我尝试将其变成与 BEGIN 的交易;和承诺;我收到以下错误:Query failed: ERROR: cannot insert multiple commands into a prepared statement
是否可以在同一个查询中准备包含多个语句的事务?或者我应该如何修改它以进行交易?
谢谢!
WITH usr1 AS (
SELECT id FROM users WHERE mongoid = $1
),
usr2 AS (
INSERT INTO users (mongoid, shopid, idinshop, attributes)
SELECT $1, $2, $3, $4
WHERE NOT EXISTS (SELECT 1 FROM usr1)
RETURNING id
),
ses1 AS (
UPDATE sessions
SET traffic=$7, counts=$8
WHERE mongoid=$5
RETURNING id
),
ses2 AS (
INSERT INTO sessions (mongoid, shopid, userid, session, traffic, counts)
SELECT $5, $2, (SELECT id FROM usr1 NATURAL FULL OUTER JOIN usr2),
$6, $7, $8
WHERE NOT EXISTS (SELECT 1 FROM ses1)
RETURNING id
)
INSERT INTO events (shopid, sessionid, userid, type, attributes, mongoid)
VALUES (
$2,
(SELECT id FROM usr1 NATURAL FULL OUTER JOIN usr2),
(SELECT id FROM ses1 NATURAL FULL OUTER JOIN ses2),
$9, $10, $11
);
然后我运行:pg_query_params($db, $sql, array(...))