我对 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 有关。
有什么想法吗?