------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Mengeneinheiten')
BEGIN
CREATE TABLE [dbo].[ut_Mengeneinheiten]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nr] char(4) DEFAULT ' '
  ,[PrtNr] char(4) DEFAULT ' '
  ,[Name] char(6) DEFAULT ' '
  ,[Suchen] char(6) DEFAULT ' '
  CONSTRAINT [Pk_Mengeneinheiten]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mengeneinheiten_KeyDel')
  DROP TRIGGER [dbo].[tr_Mengeneinheiten_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mengeneinheiten_KeyUpd')
  DROP TRIGGER [dbo].[tr_Mengeneinheiten_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Mengeneinheiten_Keys')
  DROP TABLE [dbo].[ut_Mengeneinheiten_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mengeneinheiten' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ADD [Nr] 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_Mengeneinheiten' and sc.[name] = 'PrtNr')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ADD [PrtNr] 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_Mengeneinheiten' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ADD [Name] char(6) 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_Mengeneinheiten' and sc.[name] = 'Suchen')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ADD [Suchen] char(6) 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_Mengeneinheiten' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ALTER COLUMN [Nr] char(4)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mengeneinheiten' and sc.[name] = 'PrtNr')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ALTER COLUMN [PrtNr] char(4)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mengeneinheiten' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ALTER COLUMN [Name] char(6)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mengeneinheiten' and sc.[name] = 'Suchen')
BEGIN
  ALTER TABLE [dbo].[ut_Mengeneinheiten] ALTER COLUMN [Suchen] char(6)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Mengeneinheiten')
  DROP VIEW [dbo].[Mengeneinheiten]
GO
CREATE VIEW [dbo].[Mengeneinheiten] AS
  SELECT * FROM [dbo].[ut_Mengeneinheiten]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Mengeneinheiten')
  DROP PROCEDURE [dbo].[up_Add_Mengeneinheiten]
GO
CREATE PROCEDURE [dbo].[up_Add_Mengeneinheiten] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Mengeneinheiten])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mengeneinheiten] (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_Mengeneinheiten')
  DROP PROCEDURE [dbo].[up_Upd_Mengeneinheiten]
GO
CREATE PROCEDURE [dbo].[up_Upd_Mengeneinheiten]
  @RI integer
 ,@Parm_Nr char(4)
 ,@Parm_PrtNr char(4)
 ,@Parm_Name char(6)
 ,@Parm_Suchen char(6)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Mengeneinheiten] SET
     [Nr] = RTRIM(@Parm_Nr)
    ,[PrtNr] = RTRIM(@Parm_PrtNr)
    ,[Name] = RTRIM(@Parm_Name)
    ,[Suchen] = RTRIM(@Parm_Suchen)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Mengeneinheiten')
  DROP PROCEDURE [dbo].[up_Sel_Mengeneinheiten]
GO
CREATE PROCEDURE [dbo].[up_Sel_Mengeneinheiten]
  @RI integer
AS
SELECT
     [Nr]
    ,[PrtNr]
    ,[Name]
    ,[Suchen]
  FROM [dbo].[ut_Mengeneinheiten]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Mengeneinheiten')
  DROP PROCEDURE [dbo].[up_Del_Mengeneinheiten]
GO
CREATE PROCEDURE [dbo].[up_Del_Mengeneinheiten]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Mengeneinheiten] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Mengeneinheiten')
  DROP PROCEDURE [dbo].[up_Reorg_Mengeneinheiten]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Mengeneinheiten]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Mengeneinheiten] 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_Mengeneinheiten] 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_Mengeneinheiten] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Mengeneinheiten')
  DROP PROCEDURE [dbo].[up_Append_Mengeneinheiten]
GO
CREATE PROCEDURE [dbo].[up_Append_Mengeneinheiten]
  @Parm_Nr char(4)
 ,@Parm_PrtNr char(4)
 ,@Parm_Name char(6)
 ,@Parm_Suchen char(6)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Mengeneinheiten])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mengeneinheiten] (
    RecId
   ,[Nr]
   ,[PrtNr]
   ,[Name]
   ,[Suchen]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Nr)
   ,RTRIM(@Parm_PrtNr)
   ,RTRIM(@Parm_Name)
   ,RTRIM(@Parm_Suchen)
       )
  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_Mengeneinheiten')
  DROP PROCEDURE [dbo].[up_TrSel_Mengeneinheiten]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Mengeneinheiten]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nr]))
      +LTRIM(RTRIM([PrtNr]))
      +LTRIM(RTRIM([Name]))
      +LTRIM(RTRIM([Suchen]))
    AS Data
  FROM     [dbo].[ut_Mengeneinheiten]
  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_Mengeneinheiten'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Primary')
CREATE INDEX [Ik_Primary] ON [dbo].[ut_Mengeneinheiten]
(Nr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_First_Mengeneinheiten_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_Last_Mengeneinheiten_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_Prior_Mengeneinheiten_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_Next_Mengeneinheiten_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_Prior50_Mengeneinheiten_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_Next50_Mengeneinheiten_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_FindL_Mengeneinheiten_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Mengeneinheiten_Primary')
  DROP PROCEDURE [dbo].[up_FindF_Mengeneinheiten_Primary]
GO

------------------------------------------------------------
/* ersten Datensatz aus Mengeneinheiten in Folge Primary ermittln */
CREATE PROCEDURE [dbo].[up_First_Mengeneinheiten_Primary] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten] ORDER BY Nr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Mengeneinheiten in Folge Primary ermittln */
CREATE PROCEDURE [dbo].[up_Last_Mengeneinheiten_Primary] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten] ORDER BY Nr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Nr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Mengeneinheiten_Primary] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nr FROM [dbo].[ut_Mengeneinheiten] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNr
  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_Mengeneinheiten]
                   WHERE Nr = @oldNr AND RecId > @oldRecId
                   ORDER BY Nr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE Nr > @oldNr
                   ORDER BY Nr,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 Nr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Mengeneinheiten_Primary] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nr FROM [dbo].[ut_Mengeneinheiten] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNr
  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_Mengeneinheiten]
                   WHERE Nr = @oldNr AND RecId < @oldRecId
                   ORDER BY Nr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE Nr < @oldNr
                   ORDER BY Nr 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 Nr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Mengeneinheiten_Primary] @xKey char(128),@xLen integer AS

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Mengeneinheiten'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key_Suchen')
CREATE INDEX [Ik_Key_Suchen] ON [dbo].[ut_Mengeneinheiten]
(Suchen,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_First_Mengeneinheiten_Key_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Last_Mengeneinheiten_Key_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Prior_Mengeneinheiten_Key_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Next_Mengeneinheiten_Key_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Prior50_Mengeneinheiten_Key_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Next50_Mengeneinheiten_Key_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_FindL_Mengeneinheiten_Key_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Mengeneinheiten_Key_Suchen')
  DROP PROCEDURE [dbo].[up_FindF_Mengeneinheiten_Key_Suchen]
GO

------------------------------------------------------------
/* ersten Datensatz aus Mengeneinheiten in Folge Key_Suchen ermittln */
CREATE PROCEDURE [dbo].[up_First_Mengeneinheiten_Key_Suchen] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten] ORDER BY Suchen,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Mengeneinheiten in Folge Key_Suchen ermittln */
CREATE PROCEDURE [dbo].[up_Last_Mengeneinheiten_Key_Suchen] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten] ORDER BY Suchen DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Suchen,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Mengeneinheiten_Key_Suchen] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSuchen char(6)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Suchen FROM [dbo].[ut_Mengeneinheiten] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSuchen
  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_Mengeneinheiten]
                   WHERE Suchen = @oldSuchen AND RecId > @oldRecId
                   ORDER BY Suchen,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE Suchen > @oldSuchen
                   ORDER BY Suchen,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 Suchen DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Mengeneinheiten_Key_Suchen] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSuchen char(6)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Suchen FROM [dbo].[ut_Mengeneinheiten] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSuchen
  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_Mengeneinheiten]
                   WHERE Suchen = @oldSuchen AND RecId < @oldRecId
                   ORDER BY Suchen DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE Suchen < @oldSuchen
                   ORDER BY Suchen 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 Suchen,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Mengeneinheiten_Key_Suchen] @xKey char(128),@xLen integer AS

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE SUBSTRING(Suchen,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Suchen,RecId
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE Suchen = SUBSTRING(@xKey,1,6)
      ORDER BY Suchen,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE SUBSTRING(Suchen,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Suchen DESC,RecId DESC
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE Suchen = SUBSTRING(@xKey,1,6)
      ORDER BY Suchen DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Mengeneinheiten'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key_PrtNr')
CREATE INDEX [Ik_Key_PrtNr] ON [dbo].[ut_Mengeneinheiten]
(PrtNr,Nr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_First_Mengeneinheiten_Key_PrtNr]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_Last_Mengeneinheiten_Key_PrtNr]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_Prior_Mengeneinheiten_Key_PrtNr]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_Next_Mengeneinheiten_Key_PrtNr]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_Prior50_Mengeneinheiten_Key_PrtNr]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_Next50_Mengeneinheiten_Key_PrtNr]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_FindL_Mengeneinheiten_Key_PrtNr]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Mengeneinheiten_Key_PrtNr')
  DROP PROCEDURE [dbo].[up_FindF_Mengeneinheiten_Key_PrtNr]
GO

------------------------------------------------------------
/* ersten Datensatz aus Mengeneinheiten in Folge Key_PrtNr ermittln */
CREATE PROCEDURE [dbo].[up_First_Mengeneinheiten_Key_PrtNr] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten] ORDER BY PrtNr,Nr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Mengeneinheiten in Folge Key_PrtNr ermittln */
CREATE PROCEDURE [dbo].[up_Last_Mengeneinheiten_Key_PrtNr] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten] ORDER BY PrtNr DESC,Nr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrtNr,Nr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Mengeneinheiten_Key_PrtNr] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrtNr char(4)
  DECLARE @oldNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrtNr,Nr FROM [dbo].[ut_Mengeneinheiten] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrtNr,@oldNr
  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_Mengeneinheiten]
                   WHERE PrtNr = @oldPrtNr AND Nr = @oldNr AND RecId > @oldRecId
                   ORDER BY PrtNr,Nr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE PrtNr = @oldPrtNr AND Nr > @oldNr
                   ORDER BY PrtNr,Nr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE PrtNr > @oldPrtNr
                   ORDER BY PrtNr,Nr,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 PrtNr DESC,Nr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Mengeneinheiten_Key_PrtNr] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrtNr char(4)
  DECLARE @oldNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrtNr,Nr FROM [dbo].[ut_Mengeneinheiten] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrtNr,@oldNr
  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_Mengeneinheiten]
                   WHERE PrtNr = @oldPrtNr AND Nr = @oldNr AND RecId < @oldRecId
                   ORDER BY PrtNr DESC,Nr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE PrtNr = @oldPrtNr AND Nr < @oldNr
                   ORDER BY PrtNr DESC,Nr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
                   WHERE PrtNr < @oldPrtNr
                   ORDER BY PrtNr DESC,Nr 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 PrtNr,Nr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Mengeneinheiten_Key_PrtNr] @xKey char(128),@xLen integer AS

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

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

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE PrtNr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Nr,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY PrtNr,Nr,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE PrtNr = SUBSTRING(@xKey,1,4) AND Nr = SUBSTRING(@xKey,5,4)
      ORDER BY PrtNr,Nr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE PrtNr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Nr,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY PrtNr DESC,Nr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mengeneinheiten]
      WHERE PrtNr = SUBSTRING(@xKey,1,4) AND Nr = SUBSTRING(@xKey,5,4)
      ORDER BY PrtNr DESC,Nr DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

