0

我想将 SELECT TOP 1...... 的结果分配给我的 SQL Server 存储过程中的局部变量。请在下面找到存储过程的部分:

SELECT 
(
    SELECT @billingMeterID =  TOP 1 bmt.BillingMeterTypeID
    FROM SDP_BillingMeterType bmt
    JOIN SDP_Series s ON s.SeriesID = bmt.SeriesID
    WHERE s.EngineID = SDP_PricePointBase.EngineID
) AS BillingMeterTypeID,                
dbo.fn_GetTotalStreetCost(EngineID, ProgramID, ConsumableTypeID, @billingMeterID) AS StreetCost,
[PricePointBaseID] AS PricePointBaseID, 
(SELECT TOP 1 CountryID FROM SDP_Country) AS CountryID,-- Only US is considered for now
dbo.fn_GetTotalStreetCost(EngineID, ProgramID, ConsumableTypeID, @billingMeterID) * 0.5 AS StatedCost
@dummyCost AS StreetCostUSD,
@dummyCost AS StreetCostEURO,
@dummyCost AS StatedCostUSD,
@dummyCost AS StatedCostEURO,
@exchangeRateSourceID AS ExchangeRateSourceID -- populating dummy value as the source is unknown at present
FROM SDP_PricePointBase
4

3 回答 3

1
SELECT @billingMeterID =  TOP 1 bmt.BillingMeterTypeID
                FROM SDP_BillingMeterType bmt
                JOIN SDP_Series s ON s.SeriesID = bmt.SeriesID
                Join DP_PricePointBase on s.EngineID = SDP_PricePointBase.EngineID

SELECT       @billingMeterID AS BillingMeterTypeID,                
            dbo.fn_GetTotalStreetCost(EngineID, ProgramID, ConsumableTypeID,  
             @billingMeterID) AS StreetCost,
            [PricePointBaseID] AS PricePointBaseID, 
            (SELECT TOP 1 CountryID FROM SDP_Country) AS CountryID,
             -- Only US is considered for now
            dbo.fn_GetTotalStreetCost(EngineID, ProgramID, ConsumableTypeID, 
            @billingMeterID) * 0.5 AS StatedCost
            @dummyCost AS StreetCostUSD,
            @dummyCost AS StreetCostEURO,
            @dummyCost AS StatedCostUSD,
            @dummyCost AS StatedCostEURO,
            @exchangeRateSourceID AS ExchangeRateSourceID 
            -- populating dummy value as the source is unknown at present                                 
            FROM SDP_PricePointBase
于 2012-06-29T11:13:09.093 回答
0
SELECT @billingMeterID = TOP 1 bmt.BillingMeterTypeID

不起作用,因为它应该是

SELECT TOP 1 @billingMeterID = bmt.BillingMeterTypeID

它也不能在子选择中使用。

于 2012-06-29T13:22:44.677 回答
0

您需要在单独的语句中将变量设置为将使用它的选择。

于 2012-06-29T11:08:49.183 回答