:SetVar Procedure "BKRG::UpdateBook"
:r $(DropProcedureScript_AUX)
create procedure [$(Procedure)]
   @ID            uniqueidentifier,
   @ISBN          $(dt_String_AUX) = null,
   @Name          $(dt_String_AUX) = null,
   @ReleaseYear   int = null,
   @Description   $(dt_String_AUX) = null,
   @AuthorId      uniqueidentifier,
   @PublisherId   uniqueidentifier,
   @SeriesId      uniqueidentifier,
   @CategoryId    uniqueidentifier,
   @GenreId       uniqueidentifier,
   @Status        uniqueidentifier  = null out,
   @Message       $(dt_Message_AUX) = null out
AS
/*==================================================================================================
# BookRegistry app. | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# BOOK DATA CORRECTION:
#
# === Usage: ===
# exec @RetValue = [BKRG::UpdateBook] <Book ID>, ...
#                                     @Status=@Status out, @Message=@Message out
--------------------------------------------------------------------------------------------------*/
-- commstat_NotFound
:SetVar Status_AllreadyExists_ISBN "'A72713DF-A0B0-409C-8139-E8F657BA4279'"
--:SetVar Status_ "''"
/*================================================================================================*/
$(BEGIN_AUX)
----------------------------------------------------------------------------------------------------
select @Status=null, @Message=null
----------------------------------------------------------------------------------------------------
if @ID is null or @AuthorId is null or @PublisherId is null or @SeriesId is null or @CategoryId is null or @GenreId is null
   exec [AUX:RaiseIncorrectProcedureCall] N'$(Procedure)'
if @ID = $(guid_Empty_AUX)
   exec [AUX:RaiseIncorrectProcedureCall] N'$(Procedure)',
      'Empty GUID for @ID-parameter (book ID).'
----------------------------------------------------------------------------------------------------
set @ISBN = dbo.[BKRG:PrepareStrParam]( @ISBN )
if @ISBN is not null and exists(
   select * from [Books] (nolock) where [ID]<>@ID and [ISBN]=@ISBN )
begin
   select @Status=$(Status_AllreadyExists_ISBN), @Message=dbo.[AUX:LocalizedText](
      'Another book with such ISBN ('+@ISBN+') allready exists in the database.',
      N'Другая книга с тем же ISBN ('+@ISBN+') уже присутствует в базе данных.' )
   RETURN 1
end
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

update [Books]
   set
      [ISBN]         = dbo.[BKRG:PrepareStrParam]( @ISBN ),
      [Name]         = dbo.[BKRG:PrepareStrParam]( @Name ),
      [ReleaseYear]  = @ReleaseYear,
      [Description]  = dbo.[AUX:EmptyAsNull](RTRIM( @Description )),
      [AuthorId]     = @AuthorId,
      [PublisherId]  = @PublisherId,
      [SeriesId]     = @SeriesId,
      [CategoryId]   = @CategoryId,
      [GenreId]      = @GenreId
   where
      [ID] = @ID
if @@ROWCOUNT = 0
begin
   select @Status=$(commstat_NotFound), @Message=dbo.[AUX:LocalizedText](
      'Updated book record is not found.',
      N'Обновляемая запись книги не обнаружена.' )
   RETURN 1
end

----------------------------------------------------------------------------------------------------
$(END_AUX)
/*================================================================================================*/
GO
