------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BerichtsVariablen')
BEGIN
CREATE TABLE [dbo].[ut_BerichtsVariablen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nr] char(4) DEFAULT ' '
  ,[LfdNr] char(2) DEFAULT ' '
  ,[Prompt] char(30) DEFAULT ' '
  ,[VarName] char(10) DEFAULT ' '
  ,[Format] char(10) DEFAULT ' '
  ,[DefVal] char(30) DEFAULT ' '
  CONSTRAINT [Pk_BerichtsVariablen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BerichtsVariablen_KeyDel')
  DROP TRIGGER [dbo].[tr_BerichtsVariablen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BerichtsVariablen_KeyUpd')
  DROP TRIGGER [dbo].[tr_BerichtsVariablen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BerichtsVariablen_Keys')
  DROP TABLE [dbo].[ut_BerichtsVariablen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BerichtsVariablen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] 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_BerichtsVariablen' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ADD [LfdNr] 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_BerichtsVariablen' and sc.[name] = 'Prompt')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ADD [Prompt] char(30) 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_BerichtsVariablen' and sc.[name] = 'VarName')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ADD [VarName] 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_BerichtsVariablen' and sc.[name] = 'Format')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ADD [Format] 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_BerichtsVariablen' and sc.[name] = 'DefVal')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ADD [DefVal] char(30) 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_BerichtsVariablen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ALTER COLUMN [Nr] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BerichtsVariablen' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ALTER COLUMN [LfdNr] char(2)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BerichtsVariablen' and sc.[name] = 'Prompt')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ALTER COLUMN [Prompt] char(30)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BerichtsVariablen' and sc.[name] = 'VarName')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ALTER COLUMN [VarName] char(10)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BerichtsVariablen' and sc.[name] = 'Format')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ALTER COLUMN [Format] char(10)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BerichtsVariablen' and sc.[name] = 'DefVal')
BEGIN
  ALTER TABLE [dbo].[ut_BerichtsVariablen] ALTER COLUMN [DefVal] char(30)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BerichtsVariablen')
  DROP VIEW [dbo].[BerichtsVariablen]
GO
CREATE VIEW [dbo].[BerichtsVariablen] AS
  SELECT RecId
    ,[Nr]
    ,[LfdNr]
    ,[Prompt]
    ,[VarName]
    ,[Format]
    ,[DefVal]
  FROM [dbo].[ut_BerichtsVariablen]
GO

------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_BerichtsVariablen', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_BerichtsVariablen
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_BerichtsVariablen() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_BerichtsVariablen] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Sel_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Sel_BerichtsVariablen]
  @RI integer
AS
SELECT
     [Nr]
    ,[LfdNr]
    ,[Prompt]
    ,[VarName]
    ,[Format]
    ,[DefVal]
  FROM [dbo].[ut_BerichtsVariablen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Sel2_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Sel2_BerichtsVariablen]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[BerichtsVariablen]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Append_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Append_BerichtsVariablen]
  @Parm_Nr char(4)
 ,@Parm_LfdNr char(2)
 ,@Parm_Prompt char(30)
 ,@Parm_VarName char(10)
 ,@Parm_Format char(10)
 ,@Parm_DefVal char(30)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_BerichtsVariablen()
      INSERT INTO [dbo].[ut_BerichtsVariablen] (RecId
       ,[Nr]
       ,[LfdNr]
       ,[Prompt]
       ,[VarName]
       ,[Format]
       ,[DefVal]
         ) VALUES (@NeueId
       ,RTRIM(@Parm_Nr)
       ,RTRIM(@Parm_LfdNr)
       ,RTRIM(@Parm_Prompt)
       ,RTRIM(@Parm_VarName)
       ,RTRIM(@Parm_Format)
       ,RTRIM(@Parm_DefVal)
           )
      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_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Upd_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Upd_BerichtsVariablen]
  @RI integer
 ,@Parm_Nr char(4)
 ,@Parm_LfdNr char(2)
 ,@Parm_Prompt char(30)
 ,@Parm_VarName char(10)
 ,@Parm_Format char(10)
 ,@Parm_DefVal char(30)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BerichtsVariablen] SET
     [Nr] = RTRIM(@Parm_Nr)
    ,[LfdNr] = RTRIM(@Parm_LfdNr)
    ,[Prompt] = RTRIM(@Parm_Prompt)
    ,[VarName] = RTRIM(@Parm_VarName)
    ,[Format] = RTRIM(@Parm_Format)
    ,[DefVal] = RTRIM(@Parm_DefVal)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Upd2_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_BerichtsVariablen]
  @Param_RecId varchar(20)
 ,@Param_Nr varchar(4)
 ,@Param_LfdNr varchar(2)
 ,@Param_Prompt varchar(30)
 ,@Param_VarName varchar(10)
 ,@Param_Format varchar(10)
 ,@Param_DefVal varchar(30)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_BerichtsVariablen] SET
     [Nr] = UPPER(RTRIM(@Param_Nr))
    ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
    ,[Prompt] = RTRIM(@Param_Prompt)
    ,[VarName] = RTRIM(@Param_VarName)
    ,[Format] = RTRIM(@Param_Format)
    ,[DefVal] = RTRIM(@Param_DefVal)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_BerichtsVariablen] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_TrSel_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BerichtsVariablen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nr]))
      +LTRIM(RTRIM([LfdNr]))
      +LTRIM(RTRIM([Prompt]))
      +LTRIM(RTRIM([VarName]))
      +LTRIM(RTRIM([Format]))
      +LTRIM(RTRIM([DefVal]))
    AS Data
  FROM     [dbo].[ut_BerichtsVariablen]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Add_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Add_BerichtsVariablen] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_BerichtsVariablen()
  INSERT INTO [dbo].[ut_BerichtsVariablen] (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_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Del_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Del_BerichtsVariablen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BerichtsVariablen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BerichtsVariablen')
  DROP PROCEDURE [dbo].[up_Reorg_BerichtsVariablen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BerichtsVariablen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BerichtsVariablen] 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_BerichtsVariablen] 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_BerichtsVariablen] )
  END
GO


IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_BerichtsVariablen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_KEY00')
CREATE INDEX [Ik_KEY00] ON [dbo].[ut_BerichtsVariablen]
(Nr,LfdNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_First_BerichtsVariablen_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_Last_BerichtsVariablen_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_Prior_BerichtsVariablen_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_Next_BerichtsVariablen_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_Prior50_BerichtsVariablen_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_Next50_BerichtsVariablen_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_FindL_BerichtsVariablen_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BerichtsVariablen_KEY00')
  DROP PROCEDURE [dbo].[up_FindF_BerichtsVariablen_KEY00]
GO

------------------------------------------------------------
/* ersten Datensatz aus BerichtsVariablen in Folge KEY00 ermittln */
CREATE PROCEDURE [dbo].[up_First_BerichtsVariablen_KEY00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BerichtsVariablen] ORDER BY Nr,LfdNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BerichtsVariablen in Folge KEY00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BerichtsVariablen_KEY00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BerichtsVariablen] ORDER BY Nr DESC,LfdNr DESC,RecId DESC
GO

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

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

