Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从PDO SQL Server事务返回值_Php_Sql Server_Pdo - Fatal编程技术网

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
Server
TRANSACTION

$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;
相关的: