DROP FUNCTION IF EXISTS dbo.fn_GetRecipePreparat;

--CREATE FUNCTION dbo.fn_GetRecipePreparat("RecipeID" uuid)
--RETURNS varchar(255) 
--AS
--$BODY$
--declare	_ret varchar(255);
--	_type int;		
-- Begin
--	select r.type_ 
--	from akuz.T_MEDICAL_RECORD r
--	into	_type
--	where r.MedicalRecordID = "RecipeID";

--	if _type = 607
--	then
--		select coalesce(T.NameLat || ' (' || M.NameLat || ')', M.NameLat)
--		from akuz.T_RECIPE_FEDERAL R
--		inner join 
--			akuz.T_MNN M 
--			on M.Code = R.Mnn
--		left join 
--			akuz.T_TRN T 
--			on T.Code = R.Trn
--		into _ret
--		where R.MedicalRecordID = "RecipeID";
--	elsif _type = 673
--	then
--		select case
--			   when (T.NameLat is null and M.NameLat is null) then coalesce(T2.NameLat || ' (' || M2.NameLat || ')', M.NameLat)
--			   else coalesce(T.NameLat || ' (' || M.NameLat || ')', M.NameLat)
--			  end
--		from akuz.T_RECIPE_REGIONAL R
--		left join 
--			akuz.T_MNN_REGIONAL M 
--			on R.MnnRegional = M.MnnRegionalID
--		left join 
--			akuz.T_TRN_REGIONAL T 
--			on R.TrnRegional = T.TrnRegionalID
--		left join AKUZ.T_MNN M2 on M2.Code = R.Mnn
--		left join AKUZ.T_TRN T2 on T2.Code = R.Trn
--		into _ret
--		where R.MedicalRecordID = "RecipeID";
--	end if;
--	return _ret;
	
--END $BODY$
--  LANGUAGE plpgsql volatile
--  COST 100;
--ALTER FUNCTION fn_getrecipepreparat(uuid)
--  OWNER TO postgres;