:SetVar Procedure "BKRG::GetWeatherForecastSummaries"
:r $(DropProcedureScript_AUX)
create procedure [$(Procedure)]
   @DateFrom      datetime2,
   @SimulateError bit = 0,
   @Status        uniqueidentifier  = null out,
   @Message       $(dt_Message_AUX) = null out
AS
/*==================================================================================================
# BookRegistry app. | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# OBTAINMENT OF WEATHER FORECAST SUMMARIES (BOOK-REGISTRY APP.):
#
# Random week forecast is returned (7 day-rows), starting from specified date.
#
# === Usage: ===
# exec @RetValue = [BKRG::GetWeatherForecastSummaries]
#    <Date from>, @Status=@Status out, @Message=@Message out
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# === Result-set: ===
# [Date]          datetime2            not null,
# [Summary]       $(dt_ShortName_AUX)  not null,
# [Temperature]   int                  not null
# ___ Order: ___
# [Date]
--------------------------------------------------------------------------------------------------*/
-- commstat_ArtificialError
--:SetVar Status_ "''"
/*================================================================================================*/
$(BEGIN_AUX)
----------------------------------------------------------------------------------------------------
select @Status=null, @Message=null
----------------------------------------------------------------------------------------------------
/*
if @DateFrom is null
   exec [AUX:RaiseNullParameter] N'$(Procedure)', 'DateFrom'
if @SimulateError is null
   exec [AUX:RaiseNullParameter] N'$(Procedure)', 'SimulateError'
*/
if @DateFrom is null or @SimulateError is null
   exec [AUX:RaiseNullParameter] N'$(Procedure)', '@DateFrom'
----------------------------------------------------------------------------------------------------
/*
waitfor delay '00:00:03'
--set @Status='E6A63477-82E5-4E76-84F5-B9FAAA78F839'
set @Message = dbo.[AUX:LocalizedText](
   'Access to weather database has been denied.',
   'Доступ к БД прогноза погоды отвергнут.' )
RETURN 1
*/
----------------------------------------------------------------------------------------------------

declare @SUMMARIES table
(
   [No]              int                  primary key,
   [RandomWeight]    float                not null,
   [Summary.EN]      $(dt_ShortName_AUX)  not null,
   [Summary.RU]      $(dt_ShortName_AUX)  not null,
   [Temperature]     $(dt_MPDecimal_AUX)  not null
)
insert @SUMMARIES
values
(  1, RAND(), 'Chilly'    , 'холодно'  , -8 - RAND()*30 ),
(  2, RAND(), 'Bracing'   , 'бодро'    , -3 - RAND()*5  ),
(  4, RAND(), 'Freezing'  , 'заморозки',  1 - RAND()*4  ),
(  3, RAND(), 'Cool'      , 'прохладно',  2 + RAND()*8  ),
(  5, RAND(), 'Mild'      , 'умеренно' , 10 + RAND()*5  ),
(  6, RAND(), 'Warm'      , 'тепло'    , 16 + RAND()*10 ),
(  7, RAND(), 'Balmy'     , 'комфортно', 20 + RAND()*5  ),
(  8, RAND(), 'Hot'       , 'жарко'    , 30 + RAND()*10 ),
(  9, RAND(), 'Sweltering', 'душно'    , 28 + RAND()*10 ),
( 10, RAND(), 'Scorching' , 'знойно'   , 30 + RAND()*10 )
--( , RAND(), '', '', ),

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

declare @NoFrom int, @NoTo int
set @NoFrom = dbo.[AUX:IntRound.Float]( 1 + RAND()*3 )
set @NoTo = @NoFrom + 6

create table #RESULT_SUMMARIES
(
   [No]           int                  identity primary key,
   [Summary.EN]   $(dt_ShortName_AUX)  not null,
   [Summary.RU]   $(dt_ShortName_AUX)  not null,
   [Temperature]  int                  not null
)
insert #RESULT_SUMMARIES ( [Summary.EN], [Summary.RU], [Temperature] )
   select [Summary.EN], [Summary.RU], [Temperature] from @SUMMARIES
   where [No] between @NoFrom and @NoTo
   order by [RandomWeight]

--delete #RESULT_SUMMARIES -- for test purpose (empty result list will cause error)
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ( @SimulateError = 1 )
begin
   select @Status=$(commstat_ArtificialError), @Message=dbo.[AUX:LocalizedText](
      'Weather forecast request has failed (simulated error from DB).',
      N'Запрос погоды потерпел неудачу (искусственная ошибка БД).' )
   RETURN 1
end
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if dbo.[AUX:ActiveLanguage]() = $(num_LanguageId_English_AUX) goto IN_ENGLISH
if dbo.[AUX:ActiveLanguage]() = $(num_LanguageId_Russian_AUX) goto IN_RUSSIAN
----------------------------------------------------------------------------------------------------
IN_ENGLISH:
select DATEADD(day,[No]-1,@DateFrom) 'Date', [Summary.EN] 'Summary', [Temperature]
from #RESULT_SUMMARIES order by [No]
goto CHECK_RSET_ROW_COUNT
IN_RUSSIAN:
select DATEADD(day,[No]-1,@DateFrom) 'Date', [Summary.RU] 'Summary', [Temperature]
from #RESULT_SUMMARIES order by [No]
goto CHECK_RSET_ROW_COUNT
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CHECK_RSET_ROW_COUNT:
if @@ROWCOUNT = 0
begin
   select @Status=null, @Message=dbo.[AUX:LocalizedText](
      'Forecast summary result-list is empty.',
      N'Пустой список результатов погоды.' )
   RETURN 1
end
----------------------------------------------------------------------------------------------------
$(END_AUX)
/*================================================================================================*/
GO
/*
declare @DateFrom datetime2, @RetValue int, @Status uniqueidentifier, @Message nvarchar(max)
set @DateFrom = GETDATE()
exec @RetValue = [BKRG::GetWeatherForecastSummaries] @DateFrom, @Status=@Status out, @Message=@Message out
if @RetValue<>0 select @RetValue 'RetValue', @Status 'Status', @Message 'Message'
*/
