CREATE TABLE LogMttoAlarmas ( 
Id BIGINT IDENTITY ( 1,1 ) NOT NULL,
IdTnal CHAR(2) NOT NULL,
TipoUnidad CHAR(1) DEFAULT ('V') NOT NULL,
NumUnidad VARCHAR(10) NOT NULL,
TipoInspec VARCHAR(3) DEFAULT ('MP') NOT NULL,
IdConc VARCHAR(4) NOT NULL,
NumParte VARCHAR(20) DEFAULT ('0') NOT NULL,
Descripcion VARCHAR(250),
IdSistema VARCHAR(4),
Sistema VARCHAR(50),
TipoMtto VARCHAR(50),
Fecha SMALLDATETIME NOT NULL,
TipoControl INT DEFAULT ((0)) NOT NULL,
Intervalo_km INT DEFAULT ((0)) NOT NULL,
Intervalo_dias INT DEFAULT ((0)) NOT NULL,
Intervalo_Horas INT DEFAULT ((0)) NOT NULL,
kmActual INT DEFAULT ((0)) NOT NULL,
kmRuta INT DEFAULT ((0)) NOT NULL,
kmOtro INT DEFAULT ((0)) NOT NULL,
kmUltMtto INT DEFAULT ((0)) NOT NULL,
kmUltMttoRuta INT DEFAULT ((0)) NOT NULL,
kmUltMttoOtro INT DEFAULT ((0)) NOT NULL,
FecUltMtto SMALLDATETIME,
kmRest INT DEFAULT ((0)) NOT NULL,
DiasRest INT DEFAULT ((0)) NOT NULL,
HorasRest INT DEFAULT ((0)) NOT NULL,
FecVence SMALLDATETIME,
ColorKm INT DEFAULT ((0)) NOT NULL,
ColorDias INT DEFAULT ((0)) NOT NULL,
ColorHoras INT DEFAULT ((0)) NOT NULL,
AcumHoras INT DEFAULT ((0)) NOT NULL,
HsUltMtto INT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_LogMttoAlarmas PRIMARY KEY NONCLUSTERED (Id),
 CONSTRAINT CK_LogMttoAlarmasIdConc CHECK ((len([IdConc])>(0))),
 CONSTRAINT CK_LogMttoAlarmasIdTnal CHECK ((len([IdTnal])>(0))),
 CONSTRAINT CK_LogMttoAlarmasNumParte CHECK ((len([NumParte])>(0))),
 CONSTRAINT CK_LogMttoAlarmasNumUnidad CHECK ((len([NumUnidad])>(0))),
 CONSTRAINT CK_LogMttoAlarmasTipoInspec CHECK ((len([TipoInspec])>(0))),
 CONSTRAINT CK_LogMttoAlarmasTipoUnidad CHECK ((len([TipoUnidad])>(0))))
GO
 CREATE CLUSTERED INDEX IX_LogMttoAlarmasNumUnidad ON LogMttoAlarmas(IdTnal,NumUnidad,IdConc)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsLogMttoAlarmas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsLogMttoAlarmas]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsLogMttoAlarmas] @pmIdTnal CHAR(2),@pmTipoUnidad CHAR(1),@pmNumUnidad VARCHAR(10),@pmTipoInspec VARCHAR(3),@pmIdConc VARCHAR(4),@pmNumParte VARCHAR(20),@pmDescripcion VARCHAR(250)
,@pmIdSistema VARCHAR(4),@pmSistema VARCHAR(50),@pmTipoMtto VARCHAR(50),@pmFecha SMALLDATETIME,@pmTipoControl INT,@pmIntervalo_km INT,@pmIntervalo_dias INT,@pmIntervalo_Horas INT,@pmkmActual INT,@pmkmRuta INT,@pmkmOtro INT
,@pmkmUltMtto INT,@pmkmUltMttoRuta INT,@pmkmUltMttoOtro INT,@pmFecUltMtto SMALLDATETIME,@pmkmRest INT,@pmDiasRest INT,@pmHorasRest INT,@pmFecVence SMALLDATETIME,@pmColorKm INT,@pmColorDias INT,@pmColorHoras INT,@pmAcumHoras INT,@pmHsUltMtto INT
 AS 
 INSERT INTO LogMttoAlarmas (IdTnal,TipoUnidad,NumUnidad,TipoInspec,IdConc,NumParte,Descripcion,IdSistema,Sistema,TipoMtto,Fecha,TipoControl,Intervalo_km,Intervalo_dias,Intervalo_Horas,kmActual,kmRuta,kmOtro,kmUltMtto
,kmUltMttoRuta,kmUltMttoOtro,FecUltMtto,kmRest,DiasRest,HorasRest,FecVence,ColorKm,ColorDias,ColorHoras,AcumHoras,HsUltMtto)
 VALUES (@pmIdTnal,@pmTipoUnidad,@pmNumUnidad,@pmTipoInspec,@pmIdConc,@pmNumParte,@pmDescripcion,@pmIdSistema,@pmSistema,@pmTipoMtto,@pmFecha,@pmTipoControl,@pmIntervalo_km,@pmIntervalo_dias,@pmIntervalo_Horas,@pmkmActual
,@pmkmRuta,@pmkmOtro,@pmkmUltMtto,@pmkmUltMttoRuta,@pmkmUltMttoOtro,@pmFecUltMtto,@pmkmRest,@pmDiasRest,@pmHorasRest,@pmFecVence,@pmColorKm,@pmColorDias,@pmColorHoras,@pmAcumHoras,@pmHsUltMtto)
GO
