在UPDATE之后SELECT不起作用

时间:2013-11-22 15:30:08

标签: php sql

我正在使用PHP向Sql Server发送查询。问题是,如果在UPDATE语句之后完成,我的“SELECT 1 AS updateResults”语句将不起作用。资源返回给PHP,但它没有任何行。

以下是我的查询的简化版本:

--SELECT 1 AS updateResults --if this is done before the UPDATE, a row gets returned

UPDATE theTable SET theValue = 'x' WHERE theRow = '5'

SELECT 1 AS updateResults --if this is done after the UPDATE, a row doesn't get returned

我的UPDATE查询有效并执行它应该执行的操作。

我也尝试过“SELECT'upformResults'= 1”格式并尝试放入;在每个陈述的最后,它没有任何区别。

有什么想法吗?

这是PHP代码:

$updateSQL = "BEGIN TRANSACTION

DECLARE @result1 int
DECLARE @result2 int

--SELECT 1 AS updateResults --if this is here, the row gets returned

UPDATE theTable
    SET endDate = '" . $endDate . "'
    WHERE permitYear = '" . $permitYear . "'

UPDATE theTable
    SET startDate = '" . $startDate . "'
    WHERE permitYear = '" . $nextYear . "'

--Test to make sure both records were saved
SELECT @result1 = permitYear
    FROM theTable
    WHERE permitYear = '" . $permitYear . "'
    AND endDate = '" . $endDate . "'

SELECT @result2 = permitYear
    FROM theTable
    WHERE permitYear = '" . $nextYear . "'
    AND startDate = '" . $startDate . "'

if ((@result1 > 0) AND (@result2 > 0))
    BEGIN
        COMMIT TRANSACTION
        SELECT 1 AS updateResults
    END
ELSE
    BEGIN
        ROLLBACK TRANSACTION
        SELECT 0 AS updateResults
    END";

$updateRS = sqlsrv_query($con, $updateSQL);

if (!is_resource($updateRS)) {
    //There was a problem. A resource wasn't returned. It never fails here
    exit;
}

if (!sqlsrv_has_rows($updateRS)) {
    echo ("fail for no rows returned");
    exit;
}

$updateARR = sqlsrv_fetch_array($updateRS);

//It makes it here if the SELECT is done before the UPDATE
if ($updateARR['updateResults'] == '1') {
    //success
}
else {
    //save problem
}

附加信息:如果我取出2个UPDATE语句,它将按预期运行并返回updateResults行。

4 个答案:

答案 0 :(得分:0)

你应该像这样查询选择

SELECT * FROM table1 WHERE 'updateResults' = 1

答案 1 :(得分:0)

我最终在两个SQL语句中而不是一个语句中执行此操作。这是我做的:

我从$ updateSQL中取出了SELECT x AS updateResults行:

$updateSQL = ...(same as the question, without the SELECT x AS updateResults lines)...

sqlsrv_query($con, $updateSQL);

$testSQL = "
DECLARE @result1 int
DECLARE @result2 int

SELECT @result1 = permitYear
FROM theTable
WHERE permitYear = '" . $permitYear . "'
    AND endDate = '" . $endDate . "'

SELECT @result2 = permitYear
FROM Transportation.dbo.PermitDate
WHERE permitYear = '" . $nextYear . "'
    AND startDate = '" . $startDate . "'

if ((@result1 > 0) AND (@result2 > 0))
    BEGIN
        SELECT 1 AS updateResults
    END
ELSE
    BEGIN
        SELECT 0 AS updateResults
    END
";

$testRS = sqlsrv_query($con, $testSQL);

if (!is_resource($testRS)) {
    //There was a problem. A resource wasn't returned
}

if (!sqlsrv_has_rows($testRS)) {
    //fail for no rows
}

$testARR = sqlsrv_fetch_array($testRS);

if ($testARR['updateResults'] == '1') {
    //success
}
else {
    //fail - the changes were not saved
}

答案 2 :(得分:0)

当他们成为交易的一部分时,您是否尝试颠倒SELECT和UPDATE语句的顺序?如果没有,我会尝试:

而不是:

COMMIT TRANSACTION
SELECT 1 AS updateResults

尝试:

SELECT 1 AS updateResults
COMMIT TRANSACTION

答案 3 :(得分:0)

我没有"设置NOCOUNT ON"所以SQL消息如"(1行(s)受影响)"在SELECT结果之前返回。我的代码应该是:

$updateSQL = "SET NOCOUNT ON
    BEGIN TRANSACTION
    ...

这样就会抑制SQL消息。

作为旁注,在我发现问题后,我注意到以下内容:即使我没有使用存储过程,我注意到在SQL Server Management Studio中创建存储过程时,它甚至将以下内容放在模板中:

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;