------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsEvent')
BEGIN
CREATE TABLE [dbo].[ut_WartungsEvent]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[LfdNr] integer DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Uhrzeit] char(5) DEFAULT ' '
  ,[AuftragId] integer DEFAULT 0
  ,[MfrIdent] char(30) DEFAULT ' '
  ,[AuftragNr] char(20) DEFAULT ' '
  ,[Status] char(50) DEFAULT ' '
  ,[DownloadOK] tinyint DEFAULT 0
  CONSTRAINT [Pk_WartungsEvent]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsEvent_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsEvent_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsEvent_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsEvent_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsEvent_Keys')
  DROP TABLE [dbo].[ut_WartungsEvent_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [LfdNr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [Datum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [Uhrzeit] char(5) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'AuftragId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [AuftragId] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'MfrIdent')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [MfrIdent] char(30) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'AuftragNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [AuftragNr] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [Status] char(50) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'DownloadOK')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ADD [DownloadOK] tinyint NOT NULL DEFAULT 0
END
GO
IF 5 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ALTER COLUMN [Uhrzeit] char(5)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'MfrIdent')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ALTER COLUMN [MfrIdent] char(30)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'AuftragNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ALTER COLUMN [AuftragNr] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsEvent' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsEvent] ALTER COLUMN [Status] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsEvent')
  DROP VIEW [dbo].[WartungsEvent]
GO
CREATE VIEW [dbo].[WartungsEvent] AS
  SELECT * FROM [dbo].[ut_WartungsEvent]
GO
CREATE TABLE [dbo].[ut_WartungsEvent_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(18) NOT NULL
  CONSTRAINT [Pk_ut_WartungsEvent_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WartungsEvent_KeyDel] ON [dbo].[ut_WartungsEvent]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WartungsEvent_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_WartungsEvent_KeyUpd] ON [dbo].[ut_WartungsEvent]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_WartungsEvent_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_WartungsEvent_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([LfdNr],8,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_WartungsEvent_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([LfdNr],8,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_WartungsEvent] SET RecId = RecId
GO


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WartungsEvent')
  DROP PROCEDURE [dbo].[up_Add_WartungsEvent]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsEvent] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WartungsEvent])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsEvent] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_WartungsEvent')
  DROP PROCEDURE [dbo].[up_Upd_WartungsEvent]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsEvent]
  @RI integer
 ,@Parm_LfdNr integer
 ,@Parm_Datum datetime
 ,@Parm_Uhrzeit char(5)
 ,@Parm_AuftragId integer
 ,@Parm_MfrIdent char(30)
 ,@Parm_AuftragNr char(20)
 ,@Parm_Status char(50)
 ,@Parm_DownloadOK tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WartungsEvent] SET
     [LfdNr] = @Parm_LfdNr
    ,[Datum] = @Parm_Datum
    ,[Uhrzeit] = RTRIM(@Parm_Uhrzeit)
    ,[AuftragId] = @Parm_AuftragId
    ,[MfrIdent] = RTRIM(@Parm_MfrIdent)
    ,[AuftragNr] = RTRIM(@Parm_AuftragNr)
    ,[Status] = RTRIM(@Parm_Status)
    ,[DownloadOK] = @Parm_DownloadOK
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsEvent')
  DROP PROCEDURE [dbo].[up_Sel_WartungsEvent]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsEvent]
  @RI integer
AS
SELECT
     [LfdNr]
    ,[Datum]
    ,[Uhrzeit]
    ,[AuftragId]
    ,[MfrIdent]
    ,[AuftragNr]
    ,[Status]
    ,[DownloadOK]
  FROM [dbo].[ut_WartungsEvent]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WartungsEvent')
  DROP PROCEDURE [dbo].[up_Del_WartungsEvent]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsEvent]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WartungsEvent] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsEvent')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsEvent]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsEvent]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsEvent] ORDER BY RecId
    DECLARE  @CurrId  integer
    DECLARE  @NextId  integer
    --BEGIN TRANSACTION
    OPEN C
    SET @NextId = 1
    FETCH NEXT FROM C INTO @CurrId
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
      IF @CurrId <> @NextId
      UPDATE [dbo].[ut_WartungsEvent] SET RecId=@NextId WHERE CURRENT OF C
      SET @NextId = @NextId + 1
      FETCH NEXT FROM C INTO @CurrId
    END
    CLOSE C
    --COMMIT TRANSACTION
    DEALLOCATE C
  END

  IF (@bDbcc = 1)
  BEGIN
    DBCC DBREINDEX ( [ut_WartungsEvent] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsEvent')
  DROP PROCEDURE [dbo].[up_Append_WartungsEvent]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsEvent]
  @Parm_LfdNr integer
 ,@Parm_Datum datetime
 ,@Parm_Uhrzeit char(5)
 ,@Parm_AuftragId integer
 ,@Parm_MfrIdent char(30)
 ,@Parm_AuftragNr char(20)
 ,@Parm_Status char(50)
 ,@Parm_DownloadOK tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WartungsEvent])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsEvent] (
    RecId
   ,[LfdNr]
   ,[Datum]
   ,[Uhrzeit]
   ,[AuftragId]
   ,[MfrIdent]
   ,[AuftragNr]
   ,[Status]
   ,[DownloadOK]
     ) VALUES (
    @NeueId
   ,@Parm_LfdNr
   ,@Parm_Datum
   ,RTRIM(@Parm_Uhrzeit)
   ,@Parm_AuftragId
   ,RTRIM(@Parm_MfrIdent)
   ,RTRIM(@Parm_AuftragNr)
   ,RTRIM(@Parm_Status)
   ,@Parm_DownloadOK
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsEvent')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsEvent]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsEvent]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Uhrzeit]))
      +LTRIM(RTRIM([MfrIdent]))
      +LTRIM(RTRIM([AuftragNr]))
      +LTRIM(RTRIM([Status]))
    AS Data
  FROM     [dbo].[ut_WartungsEvent]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsEvent_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsEvent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsEvent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsEvent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsEvent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsEvent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WartungsEvent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsEvent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsEvent_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsEvent_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_WartungsEvent_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsEvent_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_WartungsEvent_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsEvent_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_WartungsEvent_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsEvent_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_WartungsEvent_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_WartungsEvent_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsEvent_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_WartungsEvent_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_WartungsEvent_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WartungsEvent_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_WartungsEvent_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_WartungsEvent_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WartungsEvent_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_WartungsEvent_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_WartungsEvent_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_WartungsEvent_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_WartungsEvent_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsEvent_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

