USE Maindatabase
GO
CREATE TRIGGER InsUpdTov ON dbo.SprTovar 
FOR INSERT, UPDATE 
AS
BEGIN
declare @Pnt int

 BEGIN
  SELECT @Pnt =Pnt FROM SprTovar
  print 'ÏÍÒ'+str(@pnt)
    EXEC tovGlKodName  @Pnt

END
END
GO

CREATE TRIGGER triggerDeleteSprTovarCalc
ON sprtovar
FOR DELETE
AS 
DECLARE @PNT_DELETED INT
IF (SELECT COUNT(*) FROM deleted) > 0
BEGIN
   SELECT @PNT_DELETED = pnt FROM deleted	
--   IF (SELECT COUNT(*) FROM technolog.dbo.tbSyrie WHERE pnt = @PNT_DELETED) > 0	
--   BEGIN
--     RAISERROR ('Can''t be deleted because tbSyrie contains such TOVAR',16,2)
--     IF @@TRANCOUNT > 0
--     BEGIN  
--       ROLLBACK TRAN	
--     END
--     RETURN	
--   END
   IF (SELECT COUNT(*) FROM deleted) > 0
   BEGIN
    SELECT @PNT_DELETED = pnt FROM deleted	
    IF (SELECT COUNT(*) FROM technolog.dbo.tbProd WHERE pnt = @PNT_DELETED) > 0	
    BEGIN
     RAISERROR ('Can''t be deleted because tbProd contains such TOVAR',16,2)
     IF @@TRANCOUNT > 0
     BEGIN  
       ROLLBACK TRAN	
     END
     RETURN	
    END
   END
   IF (SELECT COUNT(*) FROM deleted) > 0
   BEGIN
    SELECT @PNT_DELETED = pnt FROM deleted	
    IF (SELECT COUNT(*) FROM maindatabase.dbo.docstring WHERE pnt = @PNT_DELETED) > 0	
    BEGIN
     RAISERROR ('Can''t be deleted because docstring table contains such TOVAR',16,2)
     IF @@TRANCOUNT > 0
     BEGIN  
       ROLLBACK TRAN	
     END
     RETURN	
    END
   END
END





