if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_GetUlyanovskReestrItem]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fn_GetUlyanovskReestrItem]
GO

--create function [dbo].[fn_GetUlyanovskReestrItem] (@UlyanovskReestrItemID uniqueidentifier)
--returns uniqueidentifier
--as
--begin

--return (
--    select
--    (
--        select top 1 wpi.WarrantyProgramItemID
--        from [AKUZ].T_WARRANTY_PROGRAM_ITEM wpi with(nolock)
--        join [AKUZ].T_WARRANTY_PROGRAM_GROUP wpg with(nolock) on wpi.WarrantyProgramGroup = wpg.WarrantyProgramGroupID
--        where wpi.Code = uri.KodPgg and wpg.WarrantyProgram = urm.WarrantyProgram
--    )
--    from [AKUZ].T_ULYANOVSK_REESTR_ITEM uri with(nolock)
--    join [AKUZ].T_ULYANOVSK_REESTR_MAIN urm with(nolock) on uri.UlyanovskReestrMain = urm.ReestrID
--    where uri.UlyanovskReestrItemID = @UlyanovskReestrItemID
--)
--end
--GO