php sqlsrv 事务,sqlsrv_begin_transaction

sqlsrv_begin_transactionsqlsrv_begin_transaction

03/26/2018

本文内容

在指定的连接上开始事务。Begins a transaction on a specified connection. 当前事务包括指定连接上的所有语句,这些语句在调用 sqlsrv_begin_transaction 之后和调用 sqlsrv_rollback 或 sqlsrv_commit之前执行。The current transaction includes all statements on the specified connection that were executed after the call to sqlsrv_begin_transaction and before any calls to sqlsrv_rollback or sqlsrv_commit.

备注

默认情况下,Microsoft Drivers for PHP for SQL ServerMicrosoft Drivers for PHP for SQL Server 处于自动提交模式。The Microsoft Drivers for PHP for SQL ServerMicrosoft Drivers for PHP for SQL Server is in auto-commit mode by default. 这意味着,除非使用 sqlsrv_begin_transaction 开始事务。This means that all queries are automatically committed upon success unless they have been designated as part of an explicit transaction by using sqlsrv_begin_transaction.

备注

如果在连接上启动事务后调用 sqlsrv_begin_transaction ,但未通过调用 sqls_rvcommit 或 sqlsrv_rollback 完成,该调用将返回 false ,并且错误集合中将添加一个 Already in Transaction 错误。If sqlsrv_begin_transaction is called after a transaction has already been initiated on the connection but not completed by calling either sqlsrv_commit or sqlsrv_rollback, the call returns false and an Already in Transaction error is added to the error collection.

语法Syntax

sqlsrv_begin_transaction( resource $conn)

参数Parameters

$conn:与事务相关联的连接。$conn: The connection with which the transaction is associated.

返回值Return Value

布尔值:如果成功开始事务,则为 true 。A Boolean value: true if the transaction was successfully begun. 否则为 false。Otherwise, false.

示例Example

作为事务的一部分,以下示例将执行两次查询。The example below executes two queries as part of a transaction. 如果两次查询均成功完成,将提交事务。If both queries are successful, the transaction is committed. 如果任一查询失败或这两次查询都失败,将回滚事务。If either (or both) of the queries fail, the transaction is rolled back.

该示例中的第一次查询向 AdventureWorks 数据库的 Sales.SalesOrderDetail 表格中插入了一个新销售订单。The first query in the example inserts a new sales order into the Sales.SalesOrderDetail table of the AdventureWorks database. 该订单订购的是 5 套产品 ID 为 709 的产品。The order is for five units of the product that has product ID 709. 第二次查询将产品 ID 为 709 的产品库存量减少 5 套。The second query reduces the inventory quantity of product ID 709 by five units. 这些查询包含在一个事务中,因为这两次查询均必须成功完成,数据库才能准确反映订单状态和产品供应情况。These queries are included in a transaction because both queries must be successful for the database to accurately reflect the state of orders and product availability.

该示例假定已在本地计算机上安装了 SQL Server 和 AdventureWorks 数据库。The example assumes that SQL Server and the AdventureWorks database are installed on the local computer. 从命令行运行该示例时,所有输出都将写入控制台。All output is written to the console when the example is run from the command line.

/* Connect to the local server using Windows Authentication and

specify the AdventureWorks database as the database in use. */

$serverName = "(local)";

$connectionInfo = array( "Database"=>"AdventureWorks");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false )

{

echo "Could not connect.\n";

die( print_r( sqlsrv_errors(), true ));

}

/* Initiate transaction. */

/* Exit script if transaction cannot be initiated. */

if ( sqlsrv_begin_transaction( $conn ) === false )

{

echo "Could not begin transaction.\n";

die( print_r( sqlsrv_errors(), true ));

}

/* Initialize parameter values. */

$orderId = 43659; $qty = 5; $productId = 709;

$offerId = 1; $price = 5.70;

/* Set up and execute the first query. */

$tsql1 = "INSERT INTO Sales.SalesOrderDetail

(SalesOrderID,

OrderQty,

ProductID,

SpecialOfferID,

UnitPrice)

VALUES (?, ?, ?, ?, ?)";

$params1 = array( $orderId, $qty, $productId, $offerId, $price);

$stmt1 = sqlsrv_query( $conn, $tsql1, $params1 );

/* Set up and execute the second query. */

$tsql2 = "UPDATE Production.ProductInventory

SET Quantity = (Quantity - ?)

WHERE ProductID = ?";

$params2 = array($qty, $productId);

$stmt2 = sqlsrv_query( $conn, $tsql2, $params2 );

/* If both queries were successful, commit the transaction. */

/* Otherwise, rollback the transaction. */

if( $stmt1 && $stmt2 )

{

sqlsrv_commit( $conn );

echo "Transaction was committed.\n";

}

else

{

sqlsrv_rollback( $conn );

echo "Transaction was rolled back.\n";

}

/* Free statement and connection resources. */

sqlsrv_free_stmt( $stmt1);

sqlsrv_free_stmt( $stmt2);

sqlsrv_close( $conn);

?>

为了重点介绍事务行为,上面的示例中未包含一些建议的错误处理。For the purpose of focusing on transaction behavior, some recommended error handling is not included in the example above. 对于生产应用程序,建议应检查对 sqlsrv 函数的任何调用是否存在错误并进行相应处理。For a production application, it is recommended that any call to a sqlsrv function be checked for errors and handled accordingly.

备注

不要使用嵌入式 Transact-SQL 来执行事务。Do not use embedded Transact-SQL to perform transactions. 例如,不要以 Transact-SQL 查询的方式执行包含“BEGIN TRANSACTION”的语句来开始某一事务。For example, do not execute a statement with "BEGIN TRANSACTION" as the Transact-SQL query to begin a transaction. 使用嵌入式 Transact-SQL 执行事务时,无法保证出现预期的事务行为。The expected transactional behavior cannot be guaranteed when using embedded Transact-SQL to perform transactions.

另请参阅See Also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值