我通过SQL
通过TRANSACTION
通过一个体积适中的PDO
服务器$sql = "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
DECLARE @id [int];
//Check whether specific record exists with a SELECT
//If it doesn't, create records on two tables
//Check whether record exists again with another sSELECT (it will now) and assign the selected ID to @id
//Update records on the two tables where ID = @id
COMMIT TRANSACTION;";
,格式如下:
UPSERT
这很好,我可以整天fetch()
。但是,我想检索@id的值来更新调用对象以供以后使用,理想情况下无需编写另一个单独的选择查询,并且无法找到可靠的获取方法。
INSERTs
在不需要UPDATEs
时将其恢复(即记录存在,因此只调用INSERTs
),而不是在调用tests.sort(function(a, b) {
var diff = a.title.localeCompare(b.title);
return diff == 0 ? (a.modifiedDate ? a.modifiedDate.getTime() : Infinity) - (b.modifiedDate ? b.modifiedDate.getTime() : Infinity) : diff;
})
时。< / p>
答案 0 :(得分:1)
事实证明,经过大量的实验,问题是第一个SELECT
语句 - 当记录存在时,它返回一个值,当记录不存在时,它没有。添加第二个SELECT
最初似乎没有帮助,因为它是一个单独的结果行。因此,解决方案是在获取数组内容之前将结果集提前一个。
工作查询格式:
$sql = "SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
DECLARE @id int;
//Check whether specific record exists with a SELECT
//If it doesn't, create records on two tables
//Check whether record exists again with another SELECT (it will now) and assign the selected ID to @id
//Update records on the two tables where ID = @id
SELECT @id AS testID
COMMIT TRANSACTION;";
$stmt = $this->_db->prepare($sql);
$stmt->execute();
$stmt->nextRowset();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$testID = $result['testID'];
return $testID;