-- получаем короткое строковое представление адреса по коду
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetExtraAddressShortView]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetExtraAddressShortView]
GO
create function GetExtraAddressShortView(@AddressID integer)
returns nvarchar(4000)
as
begin
  declare @res nvarchar(4000)
  set @res = ''
  declare @FFlat nvarchar(50)
  declare @FEntrance nvarchar(50) 
  select 
    @FFlat = Flat,
    @FEntrance = Entrance
    from AddressExtraParts
    where ExtraPartID = @AddressID

  if @FFlat <> ''
    set @res = @res + ' кв. ' + @FFlat
  if @FEntrance <> ''
    set @res = @res + ' п. ' + @FEntrance

  return @res
end