-- dbo.ET__TabVyroba_Objednavky_D
CREATE TRIGGER dbo.ET__TabVyroba_Objednavky_D ON dbo._TabVyroba_Objednavky
WITH EXECUTE AS CALLER
FOR DELETE
AS
BEGIN
 SET NOCOUNT ON

 DECLARE
  @id INT


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

   DELETE FROM dbo._TabVyroba_Objednavky_Pol WHERE IDHlava=@id

  END
 CLOSE c
 DEALLOCATE c


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



END
