IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GetEhrRus5o5e88vhc6]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_GetEhrRus5o5e88vhc6]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Anatoliy Pokus
-- Create date: 2021-06-08
-- Description:	Возвращает справочник EHR_RUS_5O5E88VHC6
-- =============================================
CREATE FUNCTION [dbo].[fn_GetEhrRus5o5e88vhc6] 
(
	@Code int
)
RETURNS uniqueidentifier
AS
BEGIN
	if(len(@Code)> 2)
	begin
		select @Code = e.code from VCLib.T_ENUMS e where e.EnumID = @Code
	end 
	RETURN 
	(
		Select Top 1 e.EhrRusID 
		From EHR.T_EHR_RUS_5O5E88VHC6 e 
		Where e.IS_DUL = 1 and e.CodeEHR =		(case 												   
												   when @Code = 2	then	'1'
												   when @Code = 3	then	'6'
												   when @Code = 4	then	'27'
												   when @Code = 5	then	'9'
												   when @Code = 6	then	'11'
												   when @Code = 7	then	'12'
												   when @Code = 8	then	'25'
												   when @Code = 9	then	null
												   when @Code = 10	then	null
												   when @Code = 11	then	null
												   when @Code = 12	then	'14'
												   when @Code = 13	then	'3'
												   when @Code = 14	then	'7'
												   when @Code = 15	then	'7'
												   when @Code = 16	then	'10'
												   when @Code = 17	then	'5'
												   when @Code = 18	then	'2'
												   when @Code = 19	then	'29'
												   when @Code = 20	then	null
												   when @Code = 21	then	'18'
												   when @Code = 22	then	'17'
												   when @Code = 23	then	'32'
												   when @Code = 24	then	'13'
												   when @Code = 25	then	null
												   when @Code = 26	then	'39'
												   when @Code = 27	then	null
												   when @Code = 99	then	null
												   end)
	)
END
