4

我对 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 有关。

有什么想法吗?

4

3 回答 3

8

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

于 2013-08-08T06:17:36.067 回答
2

如果它类似于mysql_query(),则不能使用 . 在单个字符串中执行多个查询sqlsrv_query()。请参阅此线程的最后一篇文章:http: //sqlsrvphp.codeplex.com/discussions/35511

sqlsrv_query()因此,您应该首先尝试在三个不同的语句中执行查询。

于 2012-11-14T02:05:21.283 回答
0

写之前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
于 2015-11-04T17:34:02.263 回答