SELECT @billingMeterID = TOP 1 bmt.BillingMeterTypeID不起作用

时间:2012-06-29 11:02:41

标签: sql sql-server tsql

我想将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

3 个答案:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

SELECT @billingMeterID = TOP 1 bmt.BillingMeterTypeID

不起作用,因为它应该是

SELECT TOP 1 @billingMeterID = bmt.BillingMeterTypeID

此外,它不能用于子选择。