Php 从PDO SQL Server事务返回值
我正在通过Php 从PDO SQL Server事务返回值,php,sql-server,pdo,Php,Sql Server,Pdo,我正在通过PDO以以下格式传递一个大小适中的SQLServerTRANSACTION: $sql = "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; DECLARE @id [int]; //Check whether specific record exists with a SELECT //If it doesn't, create r
PDO
以以下格式传递一个大小适中的SQL
ServerTRANSACTION
:
$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;";
这很好,我可以整天愉快地插入。但是,我想检索@id的值来更新调用对象以供以后使用,理想情况下不需要编写另一个单独的select查询,并且还没有找到可靠的方法来获取它
fetch()
在不需要插入
时(即记录存在,因此只调用更新
),而不是在调用插入
时,将其带回来。经过大量实验后,发现问题是第一条选择
语句-当记录存在时,它返回一个值,当记录不存在时,它就不存在了。添加第二个SELECT
起初似乎没有帮助,因为它是一个单独的结果行。因此,解决方案是在获取数组内容之前将resultset提前1
工作查询格式:
$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;
相关的: