With your current execution you get the return value from execution of the stored procedure, which is an integer.
One option here is to define an output parameter to retrieve the value from your statement:
-- 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
Another option is to store the results from this stored procedure in a table. Then you don't need an output parameter:
-- 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