简体   繁体   English

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

[英]Using a temporary table with sqlsrv_query function in PHP yields no results

I have a long query for SQL Server 2005 that I'm attempting to find the result set for using the sqlsrv_query function in PHP. 我有一个很长的SQL Server 2005查询,我试图找到在PHP中使用sqlsrv_query函数的结果集。 Here's my example query. 这是我的示例查询。 I've removed the variable declarations and SET statements as well as most of the WHEN conditions in the CASE statements for brevity. 为简洁起见,我删除了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

This query when run in SQL Server Management Studio display 19 rows of data as expected. 此查询在SQL Server Management Studio中运行时按预期显示19行数据。 However, when run via this PHP code I get no results. 但是,当通过这个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));

I've confirmed I can get test results to display when running simple queries via this method so it has something to do with how PHP is passing the query to SQL. 我已经确认通过此方法运行简单查询时可以显示测试结果,因此它与PHP如何将查询传递给SQL有关。

Any thoughts? 有什么想法吗?

It's been a while but in case someone is still having this problem: all you have to do is to call SET NOCOUNT ON at the beginning of the stored procedure. 已经有一段时间了,但万一有人仍然遇到这个问题:你所要做的就是在存储过程开始时调用SET NOCOUNT ON。 This will prevent the "rows affected" messages to be sent to the client and be mistaken for the output of the procedure. 这将防止“受影响的行”消息被发送到客户端并被误认为是过程的输出。

If it's anything like mysql_query() , you can't execute multiple queries in a single string with sqlsrv_query() . 如果它类似于mysql_query() ,则不能使用sqlsrv_query()在单个字符串中执行多个查询。 See the last post of this thread: http://sqlsrvphp.codeplex.com/discussions/35511 请参阅此主题的最后一篇文章: http//sqlsrvphp.codeplex.com/discussions/35511

So you should first try executing the queries in three different sqlsrv_query() statements. 因此,您应该首先尝试在三个不同的sqlsrv_query()语句中执行查询。

write before set nocount on; set nocount on;之前写set nocount on; like 喜欢

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM