-- 3.5.40 Power
--Добавляем процедуры, которые есть в Procedures, но нет в Patches.

-- получаем список айдишников владельцев
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetOwnersIDsList]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetOwnersIDsList]
GO
create function GetOwnersIDsList(@ObjectID integer)
returns nvarchar(4000)
as
begin
  declare @res nvarchar(4000)
  declare @sourceid integer
  select @sourceid = dbo.GetOwnersSource(@ObjectID)
  set @res = ''
  select @res = @res + 	
    case IsIndividual
      when 1 then convert(varchar, Individuals.IndividualID)
      else convert(varchar, Организация.Код_Организации)
    end + ','
  from  PowerObjectOwners
  inner join LegalEntities on LegalEntities.LegalEntityID = PowerObjectOwners.LegalEntityID
  left join Организация on Код_организации = LegalEntities.LegalEntityID
  left join Individuals on IndividualID = LegalEntities.LegalEntityID
  where PowerObjectID = @sourceid  
  return @res
end
GO
-- получаем список айдишников пользователей
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersIDsList]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetUsersIDsList]
GO
create function GetUsersIDsList(@ObjectID integer)
returns nvarchar(4000)
as
begin
  declare @res nvarchar(4000)
  declare @sourceid integer
  select @sourceid = dbo.GetUsersSource(@ObjectID)
  set @res = ''
  select @res = @res + 	
    case IsIndividual
      when 1 then convert(varchar, Individuals.IndividualID)
      else convert(varchar, Организация.Код_Организации)
    end + ','
  from  PowerObjectOwners
  inner join LegalEntities on LegalEntities.LegalEntityID = PowerObjectOwners.LegalEntityID
  left join Организация on Код_организации = LegalEntities.LegalEntityID
  left join Individuals on IndividualID = LegalEntities.LegalEntityID
  where PowerObjectID = @sourceid  
  return @res
end
GO