------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_UstFallSpalte')
BEGIN
CREATE TABLE [dbo].[ut_UstFallSpalte]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[UstFall] char(3) DEFAULT ' '
  ,[Jahr] char(4) DEFAULT ' '
  ,[UstSpalte] char(3) DEFAULT ' '
  CONSTRAINT [Pk_UstFallSpalte]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UstFallSpalte_KeyDel')
  DROP TRIGGER [dbo].[tr_UstFallSpalte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UstFallSpalte_KeyUpd')
  DROP TRIGGER [dbo].[tr_UstFallSpalte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_UstFallSpalte_Keys')
  DROP TABLE [dbo].[ut_UstFallSpalte_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFallSpalte' and sc.[name] = 'UstFall')
BEGIN
  ALTER TABLE [dbo].[ut_UstFallSpalte] ADD [UstFall] char(3) 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_UstFallSpalte' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_UstFallSpalte] ADD [Jahr] 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_UstFallSpalte' and sc.[name] = 'UstSpalte')
BEGIN
  ALTER TABLE [dbo].[ut_UstFallSpalte] ADD [UstSpalte] char(3) NOT NULL DEFAULT ' '
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFallSpalte' and sc.[name] = 'UstFall')
BEGIN
  ALTER TABLE [dbo].[ut_UstFallSpalte] ALTER COLUMN [UstFall] char(3)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFallSpalte' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_UstFallSpalte] ALTER COLUMN [Jahr] char(4)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFallSpalte' and sc.[name] = 'UstSpalte')
BEGIN
  ALTER TABLE [dbo].[ut_UstFallSpalte] ALTER COLUMN [UstSpalte] char(3)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'UstFallSpalte')
  DROP VIEW [dbo].[UstFallSpalte]
GO
CREATE VIEW [dbo].[UstFallSpalte] AS
  SELECT * FROM [dbo].[ut_UstFallSpalte]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_UstFallSpalte')
  DROP PROCEDURE [dbo].[up_Add_UstFallSpalte]
GO
CREATE PROCEDURE [dbo].[up_Add_UstFallSpalte] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_UstFallSpalte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UstFallSpalte] (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_UstFallSpalte')
  DROP PROCEDURE [dbo].[up_Upd_UstFallSpalte]
GO
CREATE PROCEDURE [dbo].[up_Upd_UstFallSpalte]
  @RI integer
 ,@Parm_UstFall char(3)
 ,@Parm_Jahr char(4)
 ,@Parm_UstSpalte char(3)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_UstFallSpalte] SET
     [UstFall] = RTRIM(@Parm_UstFall)
    ,[Jahr] = RTRIM(@Parm_Jahr)
    ,[UstSpalte] = RTRIM(@Parm_UstSpalte)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_UstFallSpalte')
  DROP PROCEDURE [dbo].[up_Sel_UstFallSpalte]
GO
CREATE PROCEDURE [dbo].[up_Sel_UstFallSpalte]
  @RI integer
AS
SELECT
     [UstFall]
    ,[Jahr]
    ,[UstSpalte]
  FROM [dbo].[ut_UstFallSpalte]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_UstFallSpalte')
  DROP PROCEDURE [dbo].[up_Del_UstFallSpalte]
GO
CREATE PROCEDURE [dbo].[up_Del_UstFallSpalte]
  @RI integer
AS
  DELETE FROM [dbo].[ut_UstFallSpalte] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_UstFallSpalte')
  DROP PROCEDURE [dbo].[up_Reorg_UstFallSpalte]
GO
CREATE PROCEDURE [dbo].[up_Reorg_UstFallSpalte]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_UstFallSpalte] 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_UstFallSpalte] 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_UstFallSpalte] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_UstFallSpalte')
  DROP PROCEDURE [dbo].[up_Append_UstFallSpalte]
GO
CREATE PROCEDURE [dbo].[up_Append_UstFallSpalte]
  @Parm_UstFall char(3)
 ,@Parm_Jahr char(4)
 ,@Parm_UstSpalte char(3)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_UstFallSpalte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UstFallSpalte] (
    RecId
   ,[UstFall]
   ,[Jahr]
   ,[UstSpalte]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_UstFall)
   ,RTRIM(@Parm_Jahr)
   ,RTRIM(@Parm_UstSpalte)
       )
  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_UstFallSpalte')
  DROP PROCEDURE [dbo].[up_TrSel_UstFallSpalte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_UstFallSpalte]
AS
  SELECT   RecId,
      LTRIM(RTRIM([UstFall]))
      +LTRIM(RTRIM([Jahr]))
      +LTRIM(RTRIM([UstSpalte]))
    AS Data
  FROM     [dbo].[ut_UstFallSpalte]
  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_UstFallSpalte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_UstFallSpalte]
(UstFall,Jahr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_First_UstFallSpalte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_Last_UstFallSpalte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_Prior_UstFallSpalte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_Next_UstFallSpalte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_UstFallSpalte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_Next50_UstFallSpalte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_FindL_UstFallSpalte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_UstFallSpalte_Key00')
  DROP PROCEDURE [dbo].[up_FindF_UstFallSpalte_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus UstFallSpalte in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_UstFallSpalte_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte] ORDER BY UstFall,Jahr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus UstFallSpalte in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_UstFallSpalte_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte] ORDER BY UstFall DESC,Jahr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY UstFall,Jahr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_UstFallSpalte_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldUstFall char(3)
  DECLARE @oldJahr char(4)

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

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

  IF @xLen < 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE SUBSTRING(UstFall,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY UstFall,Jahr,RecId
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE UstFall = SUBSTRING(@xKey,1,3)
      ORDER BY UstFall,Jahr,RecId
    RETURN
  END

  IF @xLen < 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE UstFall = SUBSTRING(@xKey,1,3) AND SUBSTRING(Jahr,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY UstFall,Jahr,RecId
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE UstFall = SUBSTRING(@xKey,1,3) AND Jahr = SUBSTRING(@xKey,4,4)
      ORDER BY UstFall,Jahr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE SUBSTRING(UstFall,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY UstFall DESC,Jahr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE UstFall = SUBSTRING(@xKey,1,3)
      ORDER BY UstFall DESC,Jahr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE UstFall = SUBSTRING(@xKey,1,3) AND SUBSTRING(Jahr,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY UstFall DESC,Jahr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE UstFall = SUBSTRING(@xKey,1,3) AND Jahr = SUBSTRING(@xKey,4,4)
      ORDER BY UstFall DESC,Jahr 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_UstFallSpalte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_UstFallSpalte]
(Jahr,UstSpalte,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_First_UstFallSpalte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_Last_UstFallSpalte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_Prior_UstFallSpalte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_Next_UstFallSpalte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_UstFallSpalte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_Next50_UstFallSpalte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_FindL_UstFallSpalte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_UstFallSpalte_Key01')
  DROP PROCEDURE [dbo].[up_FindF_UstFallSpalte_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus UstFallSpalte in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_UstFallSpalte_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte] ORDER BY Jahr,UstSpalte,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus UstFallSpalte in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_UstFallSpalte_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte] ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Jahr,UstSpalte,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_UstFallSpalte_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldUstSpalte char(3)

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

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

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

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

  IF @xLen < 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(UstSpalte,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr,UstSpalte,RecId
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND UstSpalte = SUBSTRING(@xKey,5,3)
      ORDER BY Jahr,UstSpalte,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  IF @xLen < 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(UstSpalte,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFallSpalte]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND UstSpalte = SUBSTRING(@xKey,5,3)
      ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

