------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsMessDaten')
BEGIN
CREATE TABLE [dbo].[ut_WartungsMessDaten]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[AnlagenId] integer DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[LfdNr] integer DEFAULT 0
  ,[Inbetriebnahme] tinyint DEFAULT 0
  ,[Bez] char(40) DEFAULT ' '
  ,[Sollwert] char(30) DEFAULT ' '
  ,[Messwert] char(30) DEFAULT ' '
  ,[Zusatz] char(10) DEFAULT ' '
  CONSTRAINT [Pk_WartungsMessDaten]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsMessDaten_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsMessDaten_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsMessDaten_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsMessDaten_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsMessDaten_Keys')
  DROP TABLE [dbo].[ut_WartungsMessDaten_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMessDaten' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ADD [AnlagenId] 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_WartungsMessDaten' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] 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_WartungsMessDaten' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] 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_WartungsMessDaten' and sc.[name] = 'Inbetriebnahme')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ADD [Inbetriebnahme] tinyint 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_WartungsMessDaten' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ADD [Bez] char(40) 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_WartungsMessDaten' and sc.[name] = 'Sollwert')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ADD [Sollwert] 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_WartungsMessDaten' and sc.[name] = 'Messwert')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ADD [Messwert] 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_WartungsMessDaten' and sc.[name] = 'Zusatz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ADD [Zusatz] char(10) NOT NULL DEFAULT ' '
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMessDaten' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ALTER COLUMN [Bez] char(40)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMessDaten' and sc.[name] = 'Sollwert')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ALTER COLUMN [Sollwert] char(30)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMessDaten' and sc.[name] = 'Messwert')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ALTER COLUMN [Messwert] char(30)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMessDaten' and sc.[name] = 'Zusatz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMessDaten] ALTER COLUMN [Zusatz] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsMessDaten')
  DROP VIEW [dbo].[WartungsMessDaten]
GO
CREATE VIEW [dbo].[WartungsMessDaten] AS
  SELECT RecId
    ,[AnlagenId]
    ,[Datum]
    ,[LfdNr]
    ,[Inbetriebnahme]
    ,[Bez]
    ,[Sollwert]
    ,[Messwert]
    ,[Zusatz]
  FROM [dbo].[ut_WartungsMessDaten]
GO
CREATE TABLE [dbo].[ut_WartungsMessDaten_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(32) NOT NULL
  CONSTRAINT [Pk_ut_WartungsMessDaten_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WartungsMessDaten_KeyDel] ON [dbo].[ut_WartungsMessDaten]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WartungsMessDaten_Keys] /*WITH (SERIALIZABLE)*/
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_WartungsMessDaten_KeyUpd] ON [dbo].[ut_WartungsMessDaten]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_WartungsMessDaten_Keys] /*WITH (SERIALIZABLE)*/
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_WartungsMessDaten_Keys] /*WITH (SERIALIZABLE)*/
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([AnlagenId],8,0)+CONVERT(char(10),[Datum],120)+STR([LfdNr],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_WartungsMessDaten_Keys] /*WITH (SERIALIZABLE)*/ SET
    [Key00_Value] = UPPER(STR([AnlagenId],8,0)+CONVERT(char(10),[Datum],120)+STR([LfdNr],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_WartungsMessDaten', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_WartungsMessDaten
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_WartungsMessDaten() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_WartungsMessDaten] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Sel_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsMessDaten]
  @RI integer
AS
SELECT
     [AnlagenId]
    ,[Datum]
    ,[LfdNr]
    ,[Inbetriebnahme]
    ,[Bez]
    ,[Sollwert]
    ,[Messwert]
    ,[Zusatz]
  FROM [dbo].[ut_WartungsMessDaten]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Sel2_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Sel2_WartungsMessDaten]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[WartungsMessDaten]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Append_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsMessDaten]
  @Parm_AnlagenId integer
 ,@Parm_Datum datetime
 ,@Parm_LfdNr integer
 ,@Parm_Inbetriebnahme tinyint
 ,@Parm_Bez char(40)
 ,@Parm_Sollwert char(30)
 ,@Parm_Messwert char(30)
 ,@Parm_Zusatz char(10)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsMessDaten()
      INSERT INTO [dbo].[ut_WartungsMessDaten] (RecId
       ,[AnlagenId]
       ,[Datum]
       ,[LfdNr]
       ,[Inbetriebnahme]
       ,[Bez]
       ,[Sollwert]
       ,[Messwert]
       ,[Zusatz]
         ) VALUES (@NeueId
       ,@Parm_AnlagenId
       ,@Parm_Datum
       ,@Parm_LfdNr
       ,@Parm_Inbetriebnahme
       ,RTRIM(@Parm_Bez)
       ,RTRIM(@Parm_Sollwert)
       ,RTRIM(@Parm_Messwert)
       ,RTRIM(@Parm_Zusatz)
           )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END
  SELECT @NeueId AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Upd_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsMessDaten]
  @RI integer
 ,@Parm_AnlagenId integer
 ,@Parm_Datum datetime
 ,@Parm_LfdNr integer
 ,@Parm_Inbetriebnahme tinyint
 ,@Parm_Bez char(40)
 ,@Parm_Sollwert char(30)
 ,@Parm_Messwert char(30)
 ,@Parm_Zusatz char(10)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WartungsMessDaten] SET
     [AnlagenId] = @Parm_AnlagenId
    ,[Datum] = @Parm_Datum
    ,[LfdNr] = @Parm_LfdNr
    ,[Inbetriebnahme] = @Parm_Inbetriebnahme
    ,[Bez] = RTRIM(@Parm_Bez)
    ,[Sollwert] = RTRIM(@Parm_Sollwert)
    ,[Messwert] = RTRIM(@Parm_Messwert)
    ,[Zusatz] = RTRIM(@Parm_Zusatz)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsMessDaten]
  @Param_RecId varchar(20)
 ,@Param_AnlagenId varchar(20)
 ,@Param_Datum varchar(10)
 ,@Param_LfdNr varchar(20)
 ,@Param_Inbetriebnahme varchar(10)
 ,@Param_Bez varchar(40)
 ,@Param_Sollwert varchar(30)
 ,@Param_Messwert varchar(30)
 ,@Param_Zusatz varchar(10)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_WartungsMessDaten] SET
     [AnlagenId] = dbo.itcfunc_float(@Param_AnlagenId)
    ,[Datum] = dbo.itcfunc_datetime(@Param_Datum)
    ,[LfdNr] = dbo.itcfunc_float(@Param_LfdNr)
    ,[Inbetriebnahme] = dbo.itcfunc_integer(@Param_Inbetriebnahme)
    ,[Bez] = RTRIM(@Param_Bez)
    ,[Sollwert] = RTRIM(@Param_Sollwert)
    ,[Messwert] = RTRIM(@Param_Messwert)
    ,[Zusatz] = RTRIM(@Param_Zusatz)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsMessDaten] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsMessDaten]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Bez]))
      +LTRIM(RTRIM([Sollwert]))
      +LTRIM(RTRIM([Messwert]))
      +LTRIM(RTRIM([Zusatz]))
    AS Data
  FROM     [dbo].[ut_WartungsMessDaten]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Add_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsMessDaten] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsMessDaten()
  INSERT INTO [dbo].[ut_WartungsMessDaten] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Del_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsMessDaten]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WartungsMessDaten] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsMessDaten')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsMessDaten]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsMessDaten]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsMessDaten] 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_WartungsMessDaten] 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_WartungsMessDaten] )
  END
GO


CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsMessDaten_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_WartungsMessDaten'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsMessDaten]
(AnlagenId,Datum,LfdNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsMessDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsMessDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMessDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsMessDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsMessDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WartungsMessDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMessDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMessDaten_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMessDaten_Key00]
GO

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

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_WartungsMessDaten_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_WartungsMessDaten_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_WartungsMessDaten_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsMessDaten_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

