------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsPositionen')
BEGIN
CREATE TABLE [dbo].[ut_WartungsPositionen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[AnlagenId] integer DEFAULT 0
  ,[PosNr] integer DEFAULT 0
  ,[Menge] float DEFAULT 0
  ,[Einheit] integer DEFAULT 0
  ,[Lkz] char(2) DEFAULT ' '
  ,[ArtNr] char(18) DEFAULT ' '
  ,[Bez1] char(42) DEFAULT ' '
  ,[Bez2] char(42) DEFAULT ' '
  ,[Merkmal] char(42) DEFAULT ' '
  CONSTRAINT [Pk_WartungsPositionen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsPositionen_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsPositionen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsPositionen_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsPositionen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsPositionen_Keys')
  DROP TABLE [dbo].[ut_WartungsPositionen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPositionen' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] 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_WartungsPositionen' and sc.[name] = 'PosNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [PosNr] 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_WartungsPositionen' and sc.[name] = 'Menge')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [Menge] float 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_WartungsPositionen' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [Einheit] 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_WartungsPositionen' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [Lkz] char(2) 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_WartungsPositionen' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [ArtNr] char(18) 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_WartungsPositionen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [Bez1] char(42) 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_WartungsPositionen' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [Bez2] char(42) 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_WartungsPositionen' and sc.[name] = 'Merkmal')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ADD [Merkmal] char(42) NOT NULL DEFAULT ' '
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPositionen' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ALTER COLUMN [Lkz] char(2)
END
GO
IF 18 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPositionen' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ALTER COLUMN [ArtNr] char(18)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPositionen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ALTER COLUMN [Bez1] char(42)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPositionen' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ALTER COLUMN [Bez2] char(42)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPositionen' and sc.[name] = 'Merkmal')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPositionen] ALTER COLUMN [Merkmal] char(42)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsPositionen')
  DROP VIEW [dbo].[WartungsPositionen]
GO
CREATE VIEW [dbo].[WartungsPositionen] AS
  SELECT RecId
    ,[AnlagenId]
    ,[PosNr]
    ,[Menge]
    ,[Einheit]
    ,[Lkz]
    ,[ArtNr]
    ,[Bez1]
    ,[Bez2]
    ,[Merkmal]
  FROM [dbo].[ut_WartungsPositionen]
GO
CREATE TABLE [dbo].[ut_WartungsPositionen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(22) NOT NULL
  ,[Key01_Value] CHAR(42) NOT NULL
  ,[Key02_Value] CHAR(148) NOT NULL
  CONSTRAINT [Pk_ut_WartungsPositionen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WartungsPositionen_KeyDel] ON [dbo].[ut_WartungsPositionen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WartungsPositionen_Keys] /*WITH (SERIALIZABLE)*/
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_WartungsPositionen_Keys] /*WITH (SERIALIZABLE)*/
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value],[Key02_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([AnlagenId],8,0)+STR([PosNr],4,0)+STR(RecId,10))
     ,UPPER(STR([AnlagenId],8,0)+convert(char(2),[Lkz])+convert(char(18),[ArtNr])+STR([PosNr],4,0)+STR(RecId,10))
     ,UPPER(STR([AnlagenId],8,0)+convert(char(42),[Bez1])+convert(char(42),[Bez2])+convert(char(42),[Merkmal])+STR([PosNr],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_WartungsPositionen_Keys] /*WITH (SERIALIZABLE)*/ SET
    [Key00_Value] = UPPER(STR([AnlagenId],8,0)+STR([PosNr],4,0)+STR([RecId],10))
   ,[Key01_Value] = UPPER(STR([AnlagenId],8,0)+convert(char(2),[Lkz])+convert(char(18),[ArtNr])+STR([PosNr],4,0)+STR([RecId],10))
   ,[Key02_Value] = UPPER(STR([AnlagenId],8,0)+convert(char(42),[Bez1])+convert(char(42),[Bez2])+convert(char(42),[Merkmal])+STR([PosNr],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_WartungsPositionen', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_WartungsPositionen
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_WartungsPositionen() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_WartungsPositionen] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Sel_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsPositionen]
  @RI integer
AS
SELECT
     [AnlagenId]
    ,[PosNr]
    ,[Menge]
    ,[Einheit]
    ,[Lkz]
    ,[ArtNr]
    ,[Bez1]
    ,[Bez2]
    ,[Merkmal]
  FROM [dbo].[ut_WartungsPositionen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Sel2_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Sel2_WartungsPositionen]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[WartungsPositionen]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Append_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsPositionen]
  @Parm_AnlagenId integer
 ,@Parm_PosNr integer
 ,@Parm_Menge float
 ,@Parm_Einheit integer
 ,@Parm_Lkz char(2)
 ,@Parm_ArtNr char(18)
 ,@Parm_Bez1 char(42)
 ,@Parm_Bez2 char(42)
 ,@Parm_Merkmal char(42)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsPositionen()
      INSERT INTO [dbo].[ut_WartungsPositionen] (RecId
       ,[AnlagenId]
       ,[PosNr]
       ,[Menge]
       ,[Einheit]
       ,[Lkz]
       ,[ArtNr]
       ,[Bez1]
       ,[Bez2]
       ,[Merkmal]
         ) VALUES (@NeueId
       ,@Parm_AnlagenId
       ,@Parm_PosNr
       ,@Parm_Menge
       ,@Parm_Einheit
       ,RTRIM(@Parm_Lkz)
       ,RTRIM(@Parm_ArtNr)
       ,RTRIM(@Parm_Bez1)
       ,RTRIM(@Parm_Bez2)
       ,RTRIM(@Parm_Merkmal)
           )
      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_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Upd_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsPositionen]
  @RI integer
 ,@Parm_AnlagenId integer
 ,@Parm_PosNr integer
 ,@Parm_Menge float
 ,@Parm_Einheit integer
 ,@Parm_Lkz char(2)
 ,@Parm_ArtNr char(18)
 ,@Parm_Bez1 char(42)
 ,@Parm_Bez2 char(42)
 ,@Parm_Merkmal char(42)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WartungsPositionen] SET
     [AnlagenId] = @Parm_AnlagenId
    ,[PosNr] = @Parm_PosNr
    ,[Menge] = @Parm_Menge
    ,[Einheit] = @Parm_Einheit
    ,[Lkz] = RTRIM(@Parm_Lkz)
    ,[ArtNr] = RTRIM(@Parm_ArtNr)
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[Bez2] = RTRIM(@Parm_Bez2)
    ,[Merkmal] = RTRIM(@Parm_Merkmal)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsPositionen]
  @Param_RecId varchar(20)
 ,@Param_AnlagenId varchar(20)
 ,@Param_PosNr varchar(20)
 ,@Param_Menge varchar(20)
 ,@Param_Einheit varchar(20)
 ,@Param_Lkz varchar(2)
 ,@Param_ArtNr varchar(18)
 ,@Param_Bez1 varchar(42)
 ,@Param_Bez2 varchar(42)
 ,@Param_Merkmal varchar(42)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_WartungsPositionen] SET
     [AnlagenId] = dbo.itcfunc_float(@Param_AnlagenId)
    ,[PosNr] = dbo.itcfunc_float(@Param_PosNr)
    ,[Menge] = dbo.itcfunc_float(@Param_Menge)
    ,[Einheit] = dbo.itcfunc_float(@Param_Einheit)
    ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
    ,[ArtNr] = UPPER(RTRIM(@Param_ArtNr))
    ,[Bez1] = RTRIM(@Param_Bez1)
    ,[Bez2] = RTRIM(@Param_Bez2)
    ,[Merkmal] = RTRIM(@Param_Merkmal)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsPositionen] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsPositionen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Lkz]))
      +LTRIM(RTRIM([ArtNr]))
      +LTRIM(RTRIM([Bez1]))
      +LTRIM(RTRIM([Bez2]))
      +LTRIM(RTRIM([Merkmal]))
    AS Data
  FROM     [dbo].[ut_WartungsPositionen]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Add_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsPositionen] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsPositionen()
  INSERT INTO [dbo].[ut_WartungsPositionen] (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_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Del_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsPositionen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WartungsPositionen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsPositionen')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsPositionen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsPositionen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsPositionen] 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_WartungsPositionen] 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_WartungsPositionen] )
  END
GO


CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsPositionen_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_WartungsPositionen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsPositionen]
(AnlagenId,PosNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsPositionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsPositionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsPositionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsPositionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsPositionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WartungsPositionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsPositionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsPositionen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsPositionen_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_WartungsPositionen_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_First_WartungsPositionen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_Last_WartungsPositionen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WartungsPositionen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_Next_WartungsPositionen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsPositionen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_Next50_WartungsPositionen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WartungsPositionen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsPositionen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WartungsPositionen_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_WartungsPositionen_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_WartungsPositionen_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys] ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_WartungsPositionen_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_WartungsPositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_WartungsPositionen_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_WartungsPositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

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

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

CREATE INDEX [Ik_Key02] ON [dbo].[ut_WartungsPositionen_Keys]
( [Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_First_WartungsPositionen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_Last_WartungsPositionen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_WartungsPositionen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_Next_WartungsPositionen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsPositionen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_Next50_WartungsPositionen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_WartungsPositionen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsPositionen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_WartungsPositionen_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_WartungsPositionen_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys] ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_WartungsPositionen_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys] ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_WartungsPositionen_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_WartungsPositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_WartungsPositionen_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WartungsPositionen_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_WartungsPositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

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

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

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

