------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonStatus')
BEGIN
CREATE TABLE [dbo].[ut_MareonStatus]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Status] char(4) DEFAULT ' '
  ,[Wert] char(2) DEFAULT ' '
  ,[Descrip] char(40) DEFAULT ' '
  CONSTRAINT [Pk_MareonStatus]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonStatus_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonStatus_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonStatus_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonStatus_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonStatus_Keys')
  DROP TABLE [dbo].[ut_MareonStatus_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonStatus' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_MareonStatus] ADD [Status] char(4) 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_MareonStatus' and sc.[name] = 'Wert')
BEGIN
  ALTER TABLE [dbo].[ut_MareonStatus] ADD [Wert] 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_MareonStatus' and sc.[name] = 'Descrip')
BEGIN
  ALTER TABLE [dbo].[ut_MareonStatus] ADD [Descrip] char(40) NOT NULL DEFAULT ' '
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonStatus' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_MareonStatus] ALTER COLUMN [Status] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonStatus' and sc.[name] = 'Wert')
BEGIN
  ALTER TABLE [dbo].[ut_MareonStatus] ALTER COLUMN [Wert] char(2)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonStatus' and sc.[name] = 'Descrip')
BEGIN
  ALTER TABLE [dbo].[ut_MareonStatus] ALTER COLUMN [Descrip] char(40)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonStatus')
  DROP VIEW [dbo].[MareonStatus]
GO
CREATE VIEW [dbo].[MareonStatus] AS
  SELECT * FROM [dbo].[ut_MareonStatus]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonStatus')
  DROP PROCEDURE [dbo].[up_Add_MareonStatus]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonStatus] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonStatus])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonStatus] (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_MareonStatus')
  DROP PROCEDURE [dbo].[up_Upd_MareonStatus]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonStatus]
  @RI integer
 ,@Parm_Status char(4)
 ,@Parm_Wert char(2)
 ,@Parm_Descrip char(40)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonStatus] SET
     [Status] = RTRIM(@Parm_Status)
    ,[Wert] = RTRIM(@Parm_Wert)
    ,[Descrip] = RTRIM(@Parm_Descrip)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonStatus')
  DROP PROCEDURE [dbo].[up_Sel_MareonStatus]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonStatus]
  @RI integer
AS
SELECT
     [Status]
    ,[Wert]
    ,[Descrip]
  FROM [dbo].[ut_MareonStatus]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonStatus')
  DROP PROCEDURE [dbo].[up_Del_MareonStatus]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonStatus]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonStatus] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonStatus')
  DROP PROCEDURE [dbo].[up_Reorg_MareonStatus]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonStatus]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonStatus] 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_MareonStatus] 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_MareonStatus] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonStatus')
  DROP PROCEDURE [dbo].[up_Append_MareonStatus]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonStatus]
  @Parm_Status char(4)
 ,@Parm_Wert char(2)
 ,@Parm_Descrip char(40)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonStatus])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonStatus] (
    RecId
   ,[Status]
   ,[Wert]
   ,[Descrip]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Status)
   ,RTRIM(@Parm_Wert)
   ,RTRIM(@Parm_Descrip)
       )
  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_MareonStatus')
  DROP PROCEDURE [dbo].[up_TrSel_MareonStatus]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonStatus]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Status]))
      +LTRIM(RTRIM([Wert]))
      +LTRIM(RTRIM([Descrip]))
    AS Data
  FROM     [dbo].[ut_MareonStatus]
  WHERE    RecId > 0
  ORDER BY RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_MareonStatus'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonStatus]
(Status,Wert,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonStatus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonStatus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonStatus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonStatus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonStatus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonStatus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonStatus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonStatus_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonStatus_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus MareonStatus in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_MareonStatus_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus] ORDER BY Status,Wert,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus MareonStatus in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_MareonStatus_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus] ORDER BY Status DESC,Wert DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Status,Wert,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_MareonStatus_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(4)
  DECLARE @oldWert char(2)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,Wert FROM [dbo].[ut_MareonStatus] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldWert
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
                   WHERE Status = @oldStatus AND Wert = @oldWert AND RecId > @oldRecId
                   ORDER BY Status,Wert,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
                   WHERE Status = @oldStatus AND Wert > @oldWert
                   ORDER BY Status,Wert,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
                   WHERE Status > @oldStatus
                   ORDER BY Status,Wert,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Status DESC,Wert DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_MareonStatus_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(4)
  DECLARE @oldWert char(2)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,Wert FROM [dbo].[ut_MareonStatus] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldWert
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
                   WHERE Status = @oldStatus AND Wert = @oldWert AND RecId < @oldRecId
                   ORDER BY Status DESC,Wert DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
                   WHERE Status = @oldStatus AND Wert < @oldWert
                   ORDER BY Status DESC,Wert DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
                   WHERE Status < @oldStatus
                   ORDER BY Status DESC,Wert DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Status,Wert,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_MareonStatus_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE SUBSTRING(Status,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Status,Wert,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE Status = SUBSTRING(@xKey,1,4)
      ORDER BY Status,Wert,RecId
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE Status = SUBSTRING(@xKey,1,4) AND SUBSTRING(Wert,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Status,Wert,RecId
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE Status = SUBSTRING(@xKey,1,4) AND Wert = SUBSTRING(@xKey,5,2)
      ORDER BY Status,Wert,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Status DESC,Wert DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_MareonStatus_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE SUBSTRING(Status,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Status DESC,Wert DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE Status = SUBSTRING(@xKey,1,4)
      ORDER BY Status DESC,Wert DESC,RecId DESC
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE Status = SUBSTRING(@xKey,1,4) AND SUBSTRING(Wert,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Status DESC,Wert DESC,RecId DESC
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_MareonStatus]
      WHERE Status = SUBSTRING(@xKey,1,4) AND Wert = SUBSTRING(@xKey,5,2)
      ORDER BY Status DESC,Wert DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

