在MSSQL中更新表时出现死锁

时间:2015-10-22 13:26:12

标签: php sql sql-server

我需要删除" Content"中的所有外部链接。列。

<?php

require_once('phpQuery-onefile.php');

$connectionInfo = array( "Database"=>"database", "UID"=>"user", "PWD"=>"pass");
$conn = sqlsrv_connect( 'server', $connectionInfo);

if( $conn ) {

}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

$query = "SELECT ArticleId, Content FROM Articles WHERE Content LIKE '%href%';";
$stmt=sqlsrv_query($conn,$query);

if (!$stmt) {
    die(print_r(sqlsrv_errors(), true));
}


while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {    
    $cont_old = $row['Content'];
    $ch = false;
    $dom = phpQuery::newDocumentHTML($cont_old, 'cp1251');
    foreach ($dom->find('a') as $a) {
        $a = pq($a);
        if (strpos($a->attr('href'), '/biblio/') === false) {
            $ch = true;
            print "link = " . iconv('CP1251', 'CP866//IGNORE', $a->attr('href') . " -> " . $a->html()) ."\n";
            $a = pq($a);
            $a->replaceWith($a->html());
        }
    }
    $cont = $dom->htmlOuter();


    if ($ch) {
        $sql2 = 'UPDATE Articles SET content = ? WHERE ArticleId = ?';
        $stmt2 = sqlsrv_prepare($conn, $sql2, array(&$cont, &$row['ArticleId']));
        if( sqlsrv_execute( $stmt2 ) === false ) {
            print "id={$row['ArticleId']} ERROR: ";
            print_r( sqlsrv_errors());
            print "\n";
        } else {
            print "updated id={$row['ArticleId']}\n";
        }

    }
}

sqlsrv_close($conn);

print "END\n";

抛出错误:

  

[Microsoft] [SQL Server Native Client 10.0] [SQL Server]事务   (进程ID 60)在锁定时死锁通讯缓冲区|   通用的waitable对象资源与另一个进程一直存在   被选为死锁受害者。重新运行该交易。

0 个答案:

没有答案