﻿DROP FUNCTION IF EXISTS fnGetRendszerbeallitasSelectedValue;  
GO
CREATE FUNCTION fnGetRendszerbeallitasSelectedValue (@beallitasTipus int, @intezmenyId int, @tanevId int)
  RETURNS int
BEGIN  

  DECLARE @json nvarchar(max)

  SELECT @json = JSON_QUERY(C_ERTEK)    
  FROM T_RENDSZERBEALLITAS_OSSZES
  WHERE C_BEALLITASTIPUS = @beallitasTipus
    AND C_INTEZMENYID = @intezmenyId
    AND C_TANEVID = @tanevId
	AND TOROLT = 'F'
    
  RETURN (select TOP(1) [Value] FROM OPENJSON( @json, '$.Options' ) WITH ([Value] int '$.Value', [Selected] NVARCHAR(25) '$.Selected') where Selected = 'true')
END;
GO