我有一个名为GetLatestUsageFactor801UNR
的存储过程,该存储过程返回一个精度为18,10的十进制值,但是当我使用下面的代码时,我总是返回零,我该如何设置存储过程的结果在变量保持为零后进行选择时进入变量
DECLARE @UsageFactor801UNR decimal(18,10)
EXEC @UsageFactor801UNR = GetLatestUsageFactor801UNR
SELECT @UsageFactor801UNR
我正在使用SQL Server作为我的RDBMS
我的存储过程如下:
select TOP 1 TotalUsageFactor as '801 UNR Usage Factor'
from MarketMessage as a
inner join messagetype591 as b on a.MarketMessageID = b.MarketMessageID
inner join AdditionalAggregationInformation as c on b.MessageType591ID = c.MessageType591ID
inner join AdditionalAggregationData as d on c.AdditionalAggregationInformationID = d.AdditionalAggregationInformationID
where SettlementRunIndicator = 20
and LoadProfileCode = 801
and TimeOfUse = 'UNR'
order by SettlementDate desc
答案 0 :(得分:1)
通过当前执行,您将从存储过程的执行中获取返回值,该值是整数。
这里的一个选项是定义一个输出参数,以从您的语句中检索值:
-- Stored procedure
CREATE PROCEDURE [GetLatestUsageFactor801UNR]
@UsageFactor801UNR decimal(18, 10) OUTPUT
AS BEGIN
select TOP 1 @UsageFactor801UNR = TotalUsageFactor
from MarketMessage as a
inner join messagetype591 as b on a.MarketMessageID = b.MarketMessageID
inner join AdditionalAggregationInformation as c on b.MessageType591ID = c.MessageType591ID
inner join AdditionalAggregationData as d on c.AdditionalAggregationInformationID = d.AdditionalAggregationInformationID
where SettlementRunIndicator = 20
and LoadProfileCode = 801
and TimeOfUse = 'UNR'
order by SettlementDate desc
END
-- Execution
DECLARE @err int
DECLARE @UsageFactor801UNR decimal(18, 10)
EXECUTE @err = [GetLatestUsageFactor801UNR] @UsageFactor801UNR OUTPUT
IF @err = 0 BEGIN
PRINT 'OK'
PRINT @UsageFactor801UNR
END
ELSE BEGIN
PRINT 'Error'
END
另一个选择是将来自此存储过程的结果存储在表中。然后,您不需要输出参数:
-- Stored procedure
CREATE PROCEDURE [GetLatestUsageFactor801UNR]
AS BEGIN
select TOP 1 TotalUsageFactor AS UsageFactor801UNR
from MarketMessage as a
inner join messagetype591 as b on a.MarketMessageID = b.MarketMessageID
inner join AdditionalAggregationInformation as c on b.MessageType591ID = c.MessageType591ID
inner join AdditionalAggregationData as d on c.AdditionalAggregationInformationID = d.AdditionalAggregationInformationID
where SettlementRunIndicator = 20
and LoadProfileCode = 801
and TimeOfUse = 'UNR'
order by SettlementDate desc
END
-- Execution
DECLARE @UsageFactor801UNR decimal(18, 10)
CREATE TABLE #Temp (UsageFactor801UNR decimal(18, 10))
INSERT INTO #Temp (UsageFactor801UNR)
EXECUTE [GetLatestUsageFactor801UNR]
SELECT @UsageFactor801UNR = UsageFactor801UNR
FROM #Temp
PRINT @UsageFactor801UNR