-- dbo.ET_TabVyroba_Palety_D
CREATE TRIGGER dbo.ET_TabVyroba_Palety_D ON dbo._TabVyroba_Palety
WITH EXECUTE AS CALLER
FOR DELETE
AS
BEGIN
 SET NOCOUNT ON

-- !! trigger neupravujte, bude stejne pregenerovan pri startu pluginu PluginHDCRTN !!

 DECLARE
  @id INT
  
 DECLARE c CURSOR LOCAL FOR
  SELECT d.ID
   FROM deleted d
 OPEN c
 WHILE (1=1)
  BEGIN
   FETCH NEXT FROM c INTO @id
   IF (@@FETCH_STATUS<>0) BREAK

   IF OBJECT_ID(N'dbo._TabVyroba_PaletyUkonceni', N'U') IS NOT NULL
    DELETE FROM dbo._TabVyroba_PaletyUkonceni WHERE IDPalety=@id

   IF OBJECT_ID(N'dbo._TabVyroba_PaletyPohybOZ', N'U') IS NOT NULL
    DELETE FROM dbo._TabVyroba_PaletyPohybOZ WHERE IDPalety=@id

  END
 CLOSE c
 DEALLOCATE c  



-- cisteni 
 IF EXISTS(SELECT 1 FROM dbo._TabVyroba_Palety)
   BEGIN
    SELECT @id=MAX(ID) FROM dbo._TabVyroba_Palety
    DBCC CHECKIDENT(_TabVyroba_Palety, RESEED, @id)
   END
  ELSE
   TRUNCATE TABLE dbo._TabVyroba_Palety

END
