SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP PROCEDURE IF EXISTS dbo.uspOsztondijHistory
GO
CREATE PROCEDURE dbo.uspOsztondijHistory(
    @pTanuloId int,
    @pTableId tinyint
)
WITH EXECUTE AS 'kreta_tech_user'
AS
DECLARE @TanevId INT,
        @OsztalyId INT;

SELECT @TanevId = C_ALTANEVID FROM T_TANULO WHERE ID = @pTanuloId;

SELECT 
     oh.ID
    ,oh.C_ENTITYID
    ,oh.C_TABLEID
    ,oh.C_ALTERATIONDATE
    ,oh.C_REASONID
    ,ISNULL(f.C_NYOMTATASINEV, '(rendszer)') as C_FELHASZNALONEV
    ,oh.C_PROPERTYNAME
    ,oh.C_ORIGINALVALUE
    ,oh.C_CURRENTVALUE
FROM T_OSZTONDIJHISTORY oh
LEFT JOIN T_FELHASZNALO f ON f.ID = oh.C_FELHASZNALOID
WHERE oh.C_TANEVID = @TanevId
AND oh.C_TANULOID = @pTanuloId
AND oh.C_TABLEID = @pTableId

UNION ALL

SELECT
     oh.ID
    ,oh.C_ENTITYID
    ,oh.C_TABLEID
    ,oh.C_ALTERATIONDATE
    ,oh.C_REASONID
    ,ISNULL(f.C_NYOMTATASINEV, '(rendszer)') as C_FELHASZNALONEV
    ,oh.C_PROPERTYNAME
    ,oh.C_ORIGINALVALUE
    ,oh.C_CURRENTVALUE
FROM T_OSZTONDIJHISTORY oh
LEFT JOIN T_FELHASZNALO f ON f.ID = oh.C_FELHASZNALOID
WHERE oh.C_TANEVID = @TanevId
AND oh.C_TANULOID = 0
AND oh.C_TABLEID = @pTableId
AND EXISTS(SELECT * FROM T_TANULOCSOPORT tcs WHERE tcs.C_OSZTALYCSOPORTID = oh.C_ENTITYID AND tcs.C_TANULOID = @pTanuloId)


