------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsTypenDaten')
BEGIN
CREATE TABLE [dbo].[ut_WartungsTypenDaten]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Ident] integer DEFAULT 0
  ,[LfdNr] integer DEFAULT 0
  ,[Bez] char(40) DEFAULT ' '
  ,[Sollwert] char(30) DEFAULT ' '
  ,[Zusatz] char(10) DEFAULT ' '
  CONSTRAINT [Pk_WartungsTypenDaten]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsTypenDaten_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsTypenDaten_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsTypenDaten_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsTypenDaten_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsTypenDaten_Keys')
  DROP TABLE [dbo].[ut_WartungsTypenDaten_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTypenDaten' and sc.[name] = 'Ident')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] ADD [Ident] 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_WartungsTypenDaten' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] 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_WartungsTypenDaten' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] 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_WartungsTypenDaten' and sc.[name] = 'Sollwert')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] 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_WartungsTypenDaten' and sc.[name] = 'Zusatz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] 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_WartungsTypenDaten' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] 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_WartungsTypenDaten' and sc.[name] = 'Sollwert')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] ALTER COLUMN [Sollwert] char(30)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTypenDaten' and sc.[name] = 'Zusatz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTypenDaten] ALTER COLUMN [Zusatz] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsTypenDaten')
  DROP VIEW [dbo].[WartungsTypenDaten]
GO
CREATE VIEW [dbo].[WartungsTypenDaten] AS
  SELECT * FROM [dbo].[ut_WartungsTypenDaten]
GO
CREATE TABLE [dbo].[ut_WartungsTypenDaten_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(22) NOT NULL
  CONSTRAINT [Pk_ut_WartungsTypenDaten_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WartungsTypenDaten_KeyDel] ON [dbo].[ut_WartungsTypenDaten]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WartungsTypenDaten_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WartungsTypenDaten')
  DROP PROCEDURE [dbo].[up_Add_WartungsTypenDaten]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsTypenDaten] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WartungsTypenDaten])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsTypenDaten] (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_WartungsTypenDaten')
  DROP PROCEDURE [dbo].[up_Upd_WartungsTypenDaten]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsTypenDaten]
  @RI integer
 ,@Parm_Ident integer
 ,@Parm_LfdNr integer
 ,@Parm_Bez char(40)
 ,@Parm_Sollwert char(30)
 ,@Parm_Zusatz char(10)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WartungsTypenDaten] SET
     [Ident] = @Parm_Ident
    ,[LfdNr] = @Parm_LfdNr
    ,[Bez] = RTRIM(@Parm_Bez)
    ,[Sollwert] = RTRIM(@Parm_Sollwert)
    ,[Zusatz] = RTRIM(@Parm_Zusatz)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsTypenDaten')
  DROP PROCEDURE [dbo].[up_Sel_WartungsTypenDaten]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsTypenDaten]
  @RI integer
AS
SELECT
     [Ident]
    ,[LfdNr]
    ,[Bez]
    ,[Sollwert]
    ,[Zusatz]
  FROM [dbo].[ut_WartungsTypenDaten]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WartungsTypenDaten')
  DROP PROCEDURE [dbo].[up_Del_WartungsTypenDaten]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsTypenDaten]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WartungsTypenDaten] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsTypenDaten')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsTypenDaten]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsTypenDaten]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsTypenDaten] 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_WartungsTypenDaten] 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_WartungsTypenDaten] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsTypenDaten')
  DROP PROCEDURE [dbo].[up_Append_WartungsTypenDaten]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsTypenDaten]
  @Parm_Ident integer
 ,@Parm_LfdNr integer
 ,@Parm_Bez char(40)
 ,@Parm_Sollwert char(30)
 ,@Parm_Zusatz char(10)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WartungsTypenDaten])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsTypenDaten] (
    RecId
   ,[Ident]
   ,[LfdNr]
   ,[Bez]
   ,[Sollwert]
   ,[Zusatz]
     ) VALUES (
    @NeueId
   ,@Parm_Ident
   ,@Parm_LfdNr
   ,RTRIM(@Parm_Bez)
   ,RTRIM(@Parm_Sollwert)
   ,RTRIM(@Parm_Zusatz)
       )
  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_WartungsTypenDaten')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsTypenDaten]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsTypenDaten]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Bez]))
      +LTRIM(RTRIM([Sollwert]))
      +LTRIM(RTRIM([Zusatz]))
    AS Data
  FROM     [dbo].[ut_WartungsTypenDaten]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsTypenDaten_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsTypenDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsTypenDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsTypenDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsTypenDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsTypenDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WartungsTypenDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsTypenDaten_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsTypenDaten_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsTypenDaten_Key00]
GO

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

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

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

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

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

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

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

