------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_PosRecherche')
BEGIN
CREATE TABLE [dbo].[ut_PosRecherche]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[RefKey] char(6) DEFAULT ' '
  ,[PosRecId] integer DEFAULT 0
  CONSTRAINT [Pk_PosRecherche]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_PosRecherche_KeyDel')
  DROP TRIGGER [dbo].[tr_PosRecherche_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_PosRecherche_KeyUpd')
  DROP TRIGGER [dbo].[tr_PosRecherche_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_PosRecherche_Keys')
  DROP TABLE [dbo].[ut_PosRecherche_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_PosRecherche' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] 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_PosRecherche' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] 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_PosRecherche' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] ADD [BelNr] 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_PosRecherche' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] ADD [RefKey] 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_PosRecherche' and sc.[name] = 'PosRecId')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] ADD [PosRecId] integer NOT NULL DEFAULT 0
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_PosRecherche' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_PosRecherche' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] 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_PosRecherche' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] ALTER COLUMN [BelNr] char(8)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_PosRecherche' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_PosRecherche] ALTER COLUMN [RefKey] char(6)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'PosRecherche')
  DROP VIEW [dbo].[PosRecherche]
GO
CREATE VIEW [dbo].[PosRecherche] AS
  SELECT * FROM [dbo].[ut_PosRecherche]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_PosRecherche')
  DROP PROCEDURE [dbo].[up_Add_PosRecherche]
GO
CREATE PROCEDURE [dbo].[up_Add_PosRecherche] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_PosRecherche])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_PosRecherche] (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_PosRecherche')
  DROP PROCEDURE [dbo].[up_Upd_PosRecherche]
GO
CREATE PROCEDURE [dbo].[up_Upd_PosRecherche]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_PosRecId integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_PosRecherche] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[RefKey] = RTRIM(@Parm_RefKey)
    ,[PosRecId] = @Parm_PosRecId
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_PosRecherche')
  DROP PROCEDURE [dbo].[up_Sel_PosRecherche]
GO
CREATE PROCEDURE [dbo].[up_Sel_PosRecherche]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[RefKey]
    ,[PosRecId]
  FROM [dbo].[ut_PosRecherche]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_PosRecherche')
  DROP PROCEDURE [dbo].[up_Del_PosRecherche]
GO
CREATE PROCEDURE [dbo].[up_Del_PosRecherche]
  @RI integer
AS
  DELETE FROM [dbo].[ut_PosRecherche] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_PosRecherche')
  DROP PROCEDURE [dbo].[up_Reorg_PosRecherche]
GO
CREATE PROCEDURE [dbo].[up_Reorg_PosRecherche]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_PosRecherche] 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_PosRecherche] 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_PosRecherche] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_PosRecherche')
  DROP PROCEDURE [dbo].[up_Append_PosRecherche]
GO
CREATE PROCEDURE [dbo].[up_Append_PosRecherche]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_PosRecId integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_PosRecherche])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_PosRecherche] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[RefKey]
   ,[PosRecId]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_RefKey)
   ,@Parm_PosRecId
       )
  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_PosRecherche')
  DROP PROCEDURE [dbo].[up_TrSel_PosRecherche]
GO
CREATE PROCEDURE [dbo].[up_TrSel_PosRecherche]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([RefKey]))
    AS Data
  FROM     [dbo].[ut_PosRecherche]
  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_PosRecherche'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_PosRecherche]
(PrjNr,BelArt,BelNr,RefKey,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_First_PosRecherche_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_Last_PosRecherche_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_Prior_PosRecherche_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_Next_PosRecherche_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_PosRecherche_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_Next50_PosRecherche_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_FindL_PosRecherche_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_PosRecherche_Key00')
  DROP PROCEDURE [dbo].[up_FindF_PosRecherche_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus PosRecherche in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_PosRecherche_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche] ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus PosRecherche in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_PosRecherche_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche] ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_PosRecherche_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,RefKey FROM [dbo].[ut_PosRecherche] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldRefKey
  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_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND RefKey = @oldRefKey AND RecId > @oldRecId
                   ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND RefKey > @oldRefKey
                   ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr > @oldPrjNr
                   ORDER BY PrjNr,BelArt,BelNr,RefKey,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 PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_PosRecherche_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,RefKey FROM [dbo].[ut_PosRecherche] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldRefKey
  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_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND RefKey = @oldRefKey AND RecId < @oldRecId
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND RefKey < @oldRefKey
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr = @oldPrjNr AND BelArt < @oldBelArt
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
                   WHERE PrjNr < @oldPrjNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey 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 PrjNr,BelArt,BelNr,RefKey,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_PosRecherche_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  IF @xLen < 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(RefKey,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  IF @xLen = 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND RefKey = SUBSTRING(@xKey,19,6)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  IF @xLen < 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(RefKey,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  IF @xLen = 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PosRecherche]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND RefKey = SUBSTRING(@xKey,19,6)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,RefKey DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

