USE [IHSecurity]
GO
/****** Object:  Trigger [dbo].[ti_datos]    Script Date: 09/26/2016 11:18:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER trigger [dbo].[ti_datos] on [dbo].[Datos]
  for INSERT
  as
/* ERwin Builtin Wed Jul 15 16:50:05 1998 */
/* INSERT trigger on Datos */
/* default body for ti_datos */
begin
  declare  @numrows int,
                @nnullcnt int,
                @nullcnt int,
                @validcnt int,
                @insid_datos tinyint,
                @errno   int,
                @errmsg  varchar(255),
                @maxkey int,
                @valkey int 
  select @numrows = @@rowcount
   if is_srvrolemember('sysadmin') = 0
          begin
               select @errno = 30005,
                         @errmsg = 'Acceso Denegado'
               goto error
          end
      /*si la tabla no esta registrada en syswinfor no se puede insertar*/
    if not exists(select * from syswinfor where desc_tabla='DATOS')
         begin
             select @errno  = 30005,
                      @errmsg = 'Datos incoherentes'
             goto error
         end
       
      select @maxkey=max(id_datos) from inserted 
       select @valkey=k.valor_llave from syswinfor k where k.desc_tabla='DATOS'
       if @maxkey>@valkey
          begin
             select @errno  = 30005,
                      @errmsg = 'Datos incoherentes'
             goto error
          end

/* ERwin Builtin Wed Jul 15 16:50:05 1998 */
  /* Juegos_Datos R/54 Datos ON CHILD INSERT RESTRICT */
  if
    /* %ChildFK(" or",update) */
    update(id_juego)
  begin
     if not exists(select * from inserted i1  inner join juegos_datos jd
                                                      on i1.id_juego=jd.id_juego)
    
    begin
      select @errno  = 30002,
             @errmsg = 'Cannot INSERT "Datos" because "Juegos_Datos" does not exist.'
      goto error
    end
  end

/* ERwin Builtin Wed Jul 15 16:50:05 1998 */
  /* Modulo R/25 Datos ON CHILD INSERT RESTRICT */
  if
    /* %ChildFK(" or",update) */
    update(id_modulo)
  begin
      if not exists(select * from inserted i1  inner join modulo jd
                                                      on i1.id_modulo=jd.id_modulo)

    begin
      select @errno  = 30002,
             @errmsg = 'Cannot INSERT "Datos" because "Modulo" does not exist.'
      goto error
    end
  end
/*Valida que este servidor exista*/
 --if update(servidor)
    --begin
    --if exists(select * from inserted i1 where i1.servidor not in(select srvnetname from master..sysservers))
    --       begin
    --               select @errno  = 30002,
    --                        @errmsg = 'El nombre del servidor no esta registrado'
    --                goto error
    --       end            
    --end
    insert icstermserv select server.id_server,i1.id_datos,servs.id_serv,0,0 from inserted i1, icsservers server , icsservices servs 
                                                                                                                    where i1.id_modulo = 1    
  return
error:
    raiserror @errno @errmsg
    rollback transaction
end