-- dbo.ET_TabVyroba_Palety_IU
CREATE TRIGGER dbo.ET_TabVyroba_Palety_IU ON dbo._TabVyroba_Palety
WITH EXECUTE AS CALLER
FOR INSERT, UPDATE
AS
BEGIN
 SET NOCOUNT ON

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

 DECLARE
  @id INT,
  @idVPr INT,
  @idKmen INT



 DECLARE c CURSOR LOCAL FAST_FORWARD FOR
  SELECT i.ID, i.IDPrikaz, i.IDKmen
   FROM inserted i
 OPEN c
 WHILE (1=1)
  BEGIN
   FETCH NEXT FROM c INTO @id, @idVPr, @idKmen
   IF (@@FETCH_STATUS<>0) BREAK

   IF (@idKmen IS NULL)
    BEGIN
     SELECT @idKmen=IDTabKmen FROM dbo.TabPrikaz WHERE ID=@idVPr
     UPDATE dbo._TabVyroba_Palety SET IDKmen=@idKmen WHERE ID=@id
    END


  END
 CLOSE c
 DEALLOCATE c

END
