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

时间:2012-11-14 01:39:47

标签: php sql-server

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

有什么想法吗?

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:1)

如果它与mysql_query()类似,则无法在sqlsrv_query()的单个字符串中执行多个查询。请参阅此主题的最后一篇文章:http://sqlsrvphp.codeplex.com/discussions/35511

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

答案 2 :(得分: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