------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Kostenstellen')
BEGIN
CREATE TABLE [dbo].[ut_Kostenstellen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[BTRNUMMER] char(6) DEFAULT ' '
  ,[BTRKENN] char(4) DEFAULT ' '
  ,[KSTNUMMER] char(20) DEFAULT ' '
  ,[NAME] char(25) DEFAULT ' '
  ,[VKZ] char(1) DEFAULT ' '
  CONSTRAINT [Pk_Kostenstellen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Kostenstellen_KeyDel')
  DROP TRIGGER [dbo].[tr_Kostenstellen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Kostenstellen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Kostenstellen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Kostenstellen_Keys')
  DROP TABLE [dbo].[ut_Kostenstellen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kostenstellen' and sc.[name] = 'BTRNUMMER')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ADD [BTRNUMMER] 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_Kostenstellen' and sc.[name] = 'BTRKENN')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ADD [BTRKENN] 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_Kostenstellen' and sc.[name] = 'KSTNUMMER')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ADD [KSTNUMMER] char(20) 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_Kostenstellen' and sc.[name] = 'NAME')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ADD [NAME] char(25) 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_Kostenstellen' and sc.[name] = 'VKZ')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ADD [VKZ] char(1) NOT NULL DEFAULT ' '
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kostenstellen' and sc.[name] = 'BTRNUMMER')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ALTER COLUMN [BTRNUMMER] char(6)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kostenstellen' and sc.[name] = 'BTRKENN')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ALTER COLUMN [BTRKENN] char(4)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kostenstellen' and sc.[name] = 'KSTNUMMER')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ALTER COLUMN [KSTNUMMER] char(20)
END
GO
IF 25 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kostenstellen' and sc.[name] = 'NAME')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ALTER COLUMN [NAME] char(25)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kostenstellen' and sc.[name] = 'VKZ')
BEGIN
  ALTER TABLE [dbo].[ut_Kostenstellen] ALTER COLUMN [VKZ] char(1)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Kostenstellen')
  DROP VIEW [dbo].[Kostenstellen]
GO
CREATE VIEW [dbo].[Kostenstellen] AS
  SELECT * FROM [dbo].[ut_Kostenstellen]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Kostenstellen')
  DROP PROCEDURE [dbo].[up_Add_Kostenstellen]
GO
CREATE PROCEDURE [dbo].[up_Add_Kostenstellen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Kostenstellen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Kostenstellen] (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_Kostenstellen')
  DROP PROCEDURE [dbo].[up_Upd_Kostenstellen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Kostenstellen]
  @RI integer
 ,@Parm_BTRNUMMER char(6)
 ,@Parm_BTRKENN char(4)
 ,@Parm_KSTNUMMER char(20)
 ,@Parm_NAME char(25)
 ,@Parm_VKZ char(1)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Kostenstellen] SET
     [BTRNUMMER] = RTRIM(@Parm_BTRNUMMER)
    ,[BTRKENN] = RTRIM(@Parm_BTRKENN)
    ,[KSTNUMMER] = RTRIM(@Parm_KSTNUMMER)
    ,[NAME] = RTRIM(@Parm_NAME)
    ,[VKZ] = RTRIM(@Parm_VKZ)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Kostenstellen')
  DROP PROCEDURE [dbo].[up_Sel_Kostenstellen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Kostenstellen]
  @RI integer
AS
SELECT
     [BTRNUMMER]
    ,[BTRKENN]
    ,[KSTNUMMER]
    ,[NAME]
    ,[VKZ]
  FROM [dbo].[ut_Kostenstellen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Kostenstellen')
  DROP PROCEDURE [dbo].[up_Del_Kostenstellen]
GO
CREATE PROCEDURE [dbo].[up_Del_Kostenstellen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Kostenstellen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Kostenstellen')
  DROP PROCEDURE [dbo].[up_Reorg_Kostenstellen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Kostenstellen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Kostenstellen] 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_Kostenstellen] 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_Kostenstellen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Kostenstellen')
  DROP PROCEDURE [dbo].[up_Append_Kostenstellen]
GO
CREATE PROCEDURE [dbo].[up_Append_Kostenstellen]
  @Parm_BTRNUMMER char(6)
 ,@Parm_BTRKENN char(4)
 ,@Parm_KSTNUMMER char(20)
 ,@Parm_NAME char(25)
 ,@Parm_VKZ char(1)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Kostenstellen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Kostenstellen] (
    RecId
   ,[BTRNUMMER]
   ,[BTRKENN]
   ,[KSTNUMMER]
   ,[NAME]
   ,[VKZ]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_BTRNUMMER)
   ,RTRIM(@Parm_BTRKENN)
   ,RTRIM(@Parm_KSTNUMMER)
   ,RTRIM(@Parm_NAME)
   ,RTRIM(@Parm_VKZ)
       )
  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_Kostenstellen')
  DROP PROCEDURE [dbo].[up_TrSel_Kostenstellen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Kostenstellen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([BTRNUMMER]))
      +LTRIM(RTRIM([BTRKENN]))
      +LTRIM(RTRIM([KSTNUMMER]))
      +LTRIM(RTRIM([NAME]))
      +LTRIM(RTRIM([VKZ]))
    AS Data
  FROM     [dbo].[ut_Kostenstellen]
  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_Kostenstellen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key0')
CREATE INDEX [Ik_Key0] ON [dbo].[ut_Kostenstellen]
(KSTNUMMER,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_First_Kostenstellen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_Last_Kostenstellen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Kostenstellen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_Next_Kostenstellen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Kostenstellen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Kostenstellen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Kostenstellen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Kostenstellen_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Kostenstellen_Key0]
GO

------------------------------------------------------------
/* ersten Datensatz aus Kostenstellen in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_First_Kostenstellen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kostenstellen] ORDER BY KSTNUMMER,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Kostenstellen in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Kostenstellen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kostenstellen] ORDER BY KSTNUMMER DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY KSTNUMMER,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Kostenstellen_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKSTNUMMER char(20)

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kostenstellen]
      WHERE KSTNUMMER = SUBSTRING(@xKey,1,20)
      ORDER BY KSTNUMMER 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_Kostenstellen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key1')
CREATE INDEX [Ik_Key1] ON [dbo].[ut_Kostenstellen]
(NAME,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_First_Kostenstellen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_Last_Kostenstellen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Kostenstellen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_Next_Kostenstellen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_Kostenstellen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_Next50_Kostenstellen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Kostenstellen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Kostenstellen_Key1')
  DROP PROCEDURE [dbo].[up_FindF_Kostenstellen_Key1]
GO

------------------------------------------------------------
/* ersten Datensatz aus Kostenstellen in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_First_Kostenstellen_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kostenstellen] ORDER BY NAME,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Kostenstellen in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Kostenstellen_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kostenstellen] ORDER BY NAME DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY NAME,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Kostenstellen_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNAME char(25)

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

