在 PHP 中使用带有 sqlsrv_query 函数的临时表不会产生任何结果

Posted

技术标签:

【中文标题】在 PHP 中使用带有 sqlsrv_query 函数的临时表不会产生任何结果【英文标题】:Using a temporary table with sqlsrv_query function in PHP yields no results 【发布时间】:2012-11-14 01:39:46 【问题描述】:

我有一个关于 SQL Server 2005 的长查询,我试图找到在 php 中使用 sqlsrv_query 函数的结果集。这是我的示例查询。为简洁起见,我删除了 CASE 语句中的变量声明和 SET 语句以及大部分 WHEN 条件。

DECLARE @TotalTable TABLE (
    SourceName varchar(50),
        GrossRevenue float,
        TotalOrderCount int,
        FBAOrderCount int,
        PostageExpense float,
        MarketplaceFees float,
        PickupExpense float,
        COGS float,
        AvgOrder float
    );

INSERT @TotalTable(SourceName,GrossRevenue,TotalOrderCount,FBAOrderCount,PostageExpense,MarketplaceFees,PickupExpense,COGS,AvgOrder)
SELECT 
    cl.Name,
    SUM(oi.Price) + SUM(oi.NativeShippingFee),
    COUNT(*) AS 'Order Count',
    SUM(CASE WHEN o.ActualPostage = '0.00' THEN 1 ELSE 0 END) AS 'FBA Orders',
    SUM(o.ActualPostage) AS 'Postage Expense',
    CAST(SUM(CASE 
        WHEN o.ActualPostage = '0.00' AND mcd.VisibleName = 'Amazon US' THEN --FBA order on Amazon
            (pt.weight * @FBAWeightCost) + @FBAPickPackCost + (oi.Price * @AmzComm) + @AmzVcf
        ELSE 
            0
        END) AS DECIMAL(10,2)) AS 'Marketplace Fees',
    CASE
        WHEN cl.Name = 'Source - Monroe County Recycler' THEN 
            (@PickupMonroeCounty * @TimesToMonroeCounty) * @JohnPerMile
        ELSE
            0
    END AS 'Pickup Expense',
    CASE 
        WHEN cl.Name = 'Source - Monroe County Recycler' THEN 
            @TotalCostMonroeCounty
        ELSE 
            0
    END AS 'COGS',
    CAST(ROUND(SUM(oi.Price) / COUNT(*),2) AS DECIMAL(10,2)) AS 'Avg Order'
FROM 
    [Order] o
JOIN [OrderItem] oi ON oi.OrderId = o.Id
JOIN [InventoryItem] ii ON ii.Id = oi.InventoryItemId
JOIN [ProductsTraits] pt ON pt.ASIN = ii.ASIN
JOIN [Classifier] clr ON clr.InventoryItemId = ii.Id
JOIN [Classification] cl ON cl.Id = clr.ClassificationId
JOIN [MarketConfigurationData] mcd ON mcd.MarketHandle = o.MarketHandle
WHERE cl.Name LIKE 'Source - %' AND o.Status = '2'
GROUP BY cl.Name
ORDER BY SUM(oi.Price) + SUM(oi.NativeShippingFee) DESC

SELECT
    SourceName,
    GrossRevenue,
    TotalOrderCount,
    FBAOrderCount,
    PostageExpense,
    MarketplaceFees,
    COGS,
    PickupExpense,
    AvgOrder,
    GrossRevenue - PostageExpense - MarketplaceFees - COGS - PickupExpense AS 'Profit'
FROM @TotalTable
ORDER BY 'Profit' DESC

此查询在 SQL Server Management Studio 中运行时按预期显示 19 行数据。但是,当通过此 PHP 代码运行时,我没有得到任何结果。

$sSql = <<<EOD
THIS IS WHERE THAT LONG QUERY STRING GOES
EOD;

$oConn = connectDb(SERVER,USER,PASSWORD);
$result = sqlsrv_query($oConn,$sSql) or die(print_r(sqlsrv_errors(), true));
print_r(sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC));

我已确认通过此方法运行简单查询时可以显示测试结果,因此它与 PHP 如何将查询传递给 SQL 有关。

有什么想法吗?

【问题讨论】:

【参考方案1】:

已经有一段时间了,但如果有人仍然遇到这个问题:您所要做的就是在存储过程的开头调用 SET NOCOUNT ON。这将防止“受影响的行”消息被发送到客户端并被误认为是过程的输出。

【讨论】:

我从 PHP 5.6 迁移到 7(正在处理中),并从 mysql_connect/FreeTDS 迁移到 sqlsrv 以及一些确实有效但现在无效的查询。特别是具有多个语句或选择/插入#TABLES 的查询以及查询它们。这解决了很多问题。【参考方案2】:

如果它类似于mysql_query(),则不能使用sqlsrv_query() 在单个字符串中执行多个查询。见本帖最后一篇:http://sqlsrvphp.codeplex.com/discussions/35511

所以你应该首先尝试在三个不同的sqlsrv_query() 语句中执行查询。

【讨论】:

谢谢。我会测试一下。【参考方案3】:

写在set nocount on;之前 喜欢

    SET NOCOUNT ON; DECLARE @TotalTable TABLE (
    SourceName varchar(50),
        GrossRevenue float,
        TotalOrderCount int,
        FBAOrderCount int,
        PostageExpense float,
        MarketplaceFees float,
        PickupExpense float,
        COGS float,
        AvgOrder float
    );

INSERT @TotalTable(SourceName,GrossRevenue,TotalOrderCount,FBAOrderCount,PostageExpense,MarketplaceFees,PickupExpense,COGS,AvgOrder)
SELECT 
    cl.Name,
    SUM(oi.Price) + SUM(oi.NativeShippingFee),
    COUNT(*) AS 'Order Count',
    SUM(CASE WHEN o.ActualPostage = '0.00' THEN 1 ELSE 0 END) AS 'FBA Orders',
    SUM(o.ActualPostage) AS 'Postage Expense',
    CAST(SUM(CASE 
        WHEN o.ActualPostage = '0.00' AND mcd.VisibleName = 'Amazon US' THEN --FBA order on Amazon
            (pt.weight * @FBAWeightCost) + @FBAPickPackCost + (oi.Price * @AmzComm) + @AmzVcf
        ELSE 
            0
        END) AS DECIMAL(10,2)) AS 'Marketplace Fees',
    CASE
        WHEN cl.Name = 'Source - Monroe County Recycler' THEN 
            (@PickupMonroeCounty * @TimesToMonroeCounty) * @JohnPerMile
        ELSE
            0
    END AS 'Pickup Expense',
    CASE 
        WHEN cl.Name = 'Source - Monroe County Recycler' THEN 
            @TotalCostMonroeCounty
        ELSE 
            0
    END AS 'COGS',
    CAST(ROUND(SUM(oi.Price) / COUNT(*),2) AS DECIMAL(10,2)) AS 'Avg Order'
FROM 
    [Order] o
JOIN [OrderItem] oi ON oi.OrderId = o.Id
JOIN [InventoryItem] ii ON ii.Id = oi.InventoryItemId
JOIN [ProductsTraits] pt ON pt.ASIN = ii.ASIN
JOIN [Classifier] clr ON clr.InventoryItemId = ii.Id
JOIN [Classification] cl ON cl.Id = clr.ClassificationId
JOIN [MarketConfigurationData] mcd ON mcd.MarketHandle = o.MarketHandle
WHERE cl.Name LIKE 'Source - %' AND o.Status = '2'
GROUP BY cl.Name
ORDER BY SUM(oi.Price) + SUM(oi.NativeShippingFee) DESC

SELECT
    SourceName,
    GrossRevenue,
    TotalOrderCount,
    FBAOrderCount,
    PostageExpense,
    MarketplaceFees,
    COGS,
    PickupExpense,
    AvgOrder,
    GrossRevenue - PostageExpense - MarketplaceFees - COGS - PickupExpense AS 'Profit'
FROM @TotalTable
ORDER BY 'Profit' DESC

【讨论】:

以上是关于在 PHP 中使用带有 sqlsrv_query 函数的临时表不会产生任何结果的主要内容,如果未能解决你的问题,请参考以下文章

sqlsrv_query 不返回行,长查询没有错误

sqlsrv_query 执行不返回结果,但在 sql manager 中执行代码时结果存在

在 PHP 中选择并插入 BLOB 数据

使用php将sql server数据转换为json

PHP sql查询不返回表格内容

PHP 输出MSSQL2005/2008数据DateTime字段类型问题