------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Statusfelder')
BEGIN
CREATE TABLE [dbo].[ut_Statusfelder]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[KdNr] char(15) DEFAULT ' '
  ,[PrjNr] char(8) DEFAULT ' '
  ,[StatId] char(10) DEFAULT ' '
  ,[ArtLkz] char(2) DEFAULT ' '
  ,[ArtNr] char(18) DEFAULT ' '
  ,[StWertI] integer DEFAULT 0
  ,[StWertD] float DEFAULT 0
  ,[StText] char(50) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  CONSTRAINT [Pk_Statusfelder]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Statusfelder_KeyDel')
  DROP TRIGGER [dbo].[tr_Statusfelder_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Statusfelder_KeyUpd')
  DROP TRIGGER [dbo].[tr_Statusfelder_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Statusfelder_Keys')
  DROP TABLE [dbo].[ut_Statusfelder_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'KdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [KdNr] char(15) 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_Statusfelder' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [PrjNr] char(8) 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_Statusfelder' and sc.[name] = 'StatId')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [StatId] char(10) 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_Statusfelder' and sc.[name] = 'ArtLkz')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [ArtLkz] 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_Statusfelder' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] 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_Statusfelder' and sc.[name] = 'StWertI')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [StWertI] 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_Statusfelder' and sc.[name] = 'StWertD')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [StWertD] 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_Statusfelder' and sc.[name] = 'StText')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [StText] 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_Statusfelder' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [BelArt] 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_Statusfelder' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ADD [BelNr] char(8) NOT NULL DEFAULT ' '
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'KdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [KdNr] char(15)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'StatId')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [StatId] char(10)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'ArtLkz')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [ArtLkz] char(2)
END
GO
IF 18 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [ArtNr] char(18)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'StText')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [StText] char(50)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Statusfelder' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_Statusfelder] ALTER COLUMN [BelNr] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Statusfelder')
  DROP VIEW [dbo].[Statusfelder]
GO
CREATE VIEW [dbo].[Statusfelder] AS
  SELECT * FROM [dbo].[ut_Statusfelder]
GO
CREATE TABLE [dbo].[ut_Statusfelder_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(73) NOT NULL
  ,[Key01_Value] CHAR(38) NOT NULL
  ,[Key02_Value] CHAR(56) NOT NULL
  ,[Key03_Value] CHAR(56) NOT NULL
  CONSTRAINT [Pk_ut_Statusfelder_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Statusfelder_KeyDel] ON [dbo].[ut_Statusfelder]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Statusfelder_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Statusfelder_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value],[Key02_Value],[Key03_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(15),[KdNr])+convert(char(8),[PrjNr])+convert(char(10),[StatId])+convert(char(2),[ArtLkz])+convert(char(18),[ArtNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR(RecId,10))
     ,UPPER(convert(char(10),[StatId])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR(RecId,10))
     ,UPPER(convert(char(10),[StatId])+convert(char(18),[ArtNr])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR(RecId,10))
     ,UPPER(convert(char(10),[StatId])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(18),[ArtNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Statusfelder_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(15),[KdNr])+convert(char(8),[PrjNr])+convert(char(10),[StatId])+convert(char(2),[ArtLkz])+convert(char(18),[ArtNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(10),[StatId])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([RecId],10))
   ,[Key02_Value] = UPPER(convert(char(10),[StatId])+convert(char(18),[ArtNr])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([RecId],10))
   ,[Key03_Value] = UPPER(convert(char(10),[StatId])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(18),[ArtNr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Statusfelder')
  DROP PROCEDURE [dbo].[up_Add_Statusfelder]
GO
CREATE PROCEDURE [dbo].[up_Add_Statusfelder] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Statusfelder])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Statusfelder] (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_Statusfelder')
  DROP PROCEDURE [dbo].[up_Upd_Statusfelder]
GO
CREATE PROCEDURE [dbo].[up_Upd_Statusfelder]
  @RI integer
 ,@Parm_KdNr char(15)
 ,@Parm_PrjNr char(8)
 ,@Parm_StatId char(10)
 ,@Parm_ArtLkz char(2)
 ,@Parm_ArtNr char(18)
 ,@Parm_StWertI integer
 ,@Parm_StWertD float
 ,@Parm_StText char(50)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Statusfelder] SET
     [KdNr] = RTRIM(@Parm_KdNr)
    ,[PrjNr] = RTRIM(@Parm_PrjNr)
    ,[StatId] = RTRIM(@Parm_StatId)
    ,[ArtLkz] = RTRIM(@Parm_ArtLkz)
    ,[ArtNr] = RTRIM(@Parm_ArtNr)
    ,[StWertI] = @Parm_StWertI
    ,[StWertD] = @Parm_StWertD
    ,[StText] = RTRIM(@Parm_StText)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Statusfelder')
  DROP PROCEDURE [dbo].[up_Sel_Statusfelder]
GO
CREATE PROCEDURE [dbo].[up_Sel_Statusfelder]
  @RI integer
AS
SELECT
     [KdNr]
    ,[PrjNr]
    ,[StatId]
    ,[ArtLkz]
    ,[ArtNr]
    ,[StWertI]
    ,[StWertD]
    ,[StText]
    ,[BelArt]
    ,[BelNr]
  FROM [dbo].[ut_Statusfelder]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Statusfelder')
  DROP PROCEDURE [dbo].[up_Del_Statusfelder]
GO
CREATE PROCEDURE [dbo].[up_Del_Statusfelder]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Statusfelder] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Statusfelder')
  DROP PROCEDURE [dbo].[up_Reorg_Statusfelder]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Statusfelder]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Statusfelder] 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_Statusfelder] 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_Statusfelder] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Statusfelder')
  DROP PROCEDURE [dbo].[up_Append_Statusfelder]
GO
CREATE PROCEDURE [dbo].[up_Append_Statusfelder]
  @Parm_KdNr char(15)
 ,@Parm_PrjNr char(8)
 ,@Parm_StatId char(10)
 ,@Parm_ArtLkz char(2)
 ,@Parm_ArtNr char(18)
 ,@Parm_StWertI integer
 ,@Parm_StWertD float
 ,@Parm_StText char(50)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Statusfelder])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Statusfelder] (
    RecId
   ,[KdNr]
   ,[PrjNr]
   ,[StatId]
   ,[ArtLkz]
   ,[ArtNr]
   ,[StWertI]
   ,[StWertD]
   ,[StText]
   ,[BelArt]
   ,[BelNr]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_KdNr)
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_StatId)
   ,RTRIM(@Parm_ArtLkz)
   ,RTRIM(@Parm_ArtNr)
   ,@Parm_StWertI
   ,@Parm_StWertD
   ,RTRIM(@Parm_StText)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
       )
  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_Statusfelder')
  DROP PROCEDURE [dbo].[up_TrSel_Statusfelder]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Statusfelder]
AS
  SELECT   RecId,
      LTRIM(RTRIM([KdNr]))
      +LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([StatId]))
      +LTRIM(RTRIM([ArtLkz]))
      +LTRIM(RTRIM([ArtNr]))
      +LTRIM(RTRIM([StText]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
    AS Data
  FROM     [dbo].[ut_Statusfelder]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_Statusfelder_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_First_Statusfelder_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_Last_Statusfelder_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Statusfelder_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_Next_Statusfelder_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_Statusfelder_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Statusfelder_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Statusfelder_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Statusfelder_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Statusfelder_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_Statusfelder_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_First_Statusfelder_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_Last_Statusfelder_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Statusfelder_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_Next_Statusfelder_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_Statusfelder_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_Next50_Statusfelder_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Statusfelder_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Statusfelder_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Statusfelder_Key01]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key02] ON [dbo].[ut_Statusfelder_Keys]
( [Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_First_Statusfelder_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_Last_Statusfelder_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Statusfelder_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_Next_Statusfelder_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_Statusfelder_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_Next50_Statusfelder_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_FindL_Statusfelder_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Statusfelder_Key02')
  DROP PROCEDURE [dbo].[up_FindF_Statusfelder_Key02]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key03] ON [dbo].[ut_Statusfelder_Keys]
( [Key03_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_First_Statusfelder_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_Last_Statusfelder_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_Prior_Statusfelder_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_Next_Statusfelder_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_Statusfelder_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_Next50_Statusfelder_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_FindL_Statusfelder_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Statusfelder_Key03')
  DROP PROCEDURE [dbo].[up_FindF_Statusfelder_Key03]
GO

CREATE PROCEDURE [dbo].[up_First_Statusfelder_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Statusfelder_Keys] ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Statusfelder_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Statusfelder_Keys] ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Statusfelder_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Statusfelder_Keys]
  WHERE [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_Statusfelder_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Statusfelder_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Statusfelder_Keys]
  WHERE [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_Statusfelder_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

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

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

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

