﻿DROP PROCEDURE IF EXISTS uspDeleteDictionaryItemBase
GO

CREATE PROCEDURE uspDeleteDictionaryItemBase
   @pFelhasznaloId int
  ,@pId int
  ,@pIntezmenyId int
  ,@pTanevId int
AS BEGIN
  SET NOCOUNT ON
  
  DECLARE 
     @sql nvarchar(MAX) = ''
    ,@TypeCodeName nvarchar(100) = (
      SELECT dt.C_CODENAME
      FROM T_DICTIONARYITEMBASE_OSSZES dib
      INNER JOIN T_DICTIONARYTYPE_OSSZES dt ON dib.C_DICTIONARYTYPEID = dt.ID AND dt.C_TANEVID = dib.C_TANEVID
        AND dt.TOROLT = 'F'
      WHERE dib.ID = @pId 
        AND dib.C_TANEVID = @pTanevId 
        AND dib.C_INTEZMENYID = @pIntezmenyId 
        AND dib.TOROLT = 'F')

SET @sql = N'
  UPDATE T_DICTIONARYITEMBASE_OSSZES
    SET
       TOROLT = ''T''
      ,SERIAL = SERIAL + 1
      ,MODIFIER = @pFelhasznaloId
      ,LASTCHANGED = GETDATE()
    WHERE
    ID = @pId AND C_TANEVID = @pTanevId AND C_INTEZMENYID = @pIntezmenyId

    UPDATE T_'+@TypeCodeName+'_OSSZES
    SET
       TOROLT = ''T''
      ,MODIFIER = @pFelhasznaloId
      ,LASTCHANGED = GETDATE()
    WHERE
    ID = @pId AND C_ALTANEVID = @pTanevId AND C_ALINTEZMENYID = @pIntezmenyId AND TOROLT = ''F'''

  EXEC sys.sp_executesql @sql, N'
     @pId int
    ,@pTanevId int
    ,@pIntezmenyId int
    ,@pFelhasznaloId int'
    ,@pId
    ,@pTanevId
    ,@pIntezmenyId 
    ,@pFelhasznaloId

END
GO