------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BuchblattNamen')
BEGIN
CREATE TABLE [dbo].[ut_BuchblattNamen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[BBKtoTyp] char(1) DEFAULT ' '
  ,[BBKtoNr] char(8) DEFAULT ' '
  ,[BBUser] char(20) DEFAULT ' '
  ,[BelegNr] char(8) DEFAULT ' '
  ,[IncBelegNr] tinyint DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Vortrag] tinyint DEFAULT 0
  ,[ASSaldo] float DEFAULT 0
  ,[ASSumme] float DEFAULT 0
  ,[Bezeich] char(50) DEFAULT ' '
  ,[FilterView] integer DEFAULT 0
  CONSTRAINT [Pk_BuchblattNamen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BuchblattNamen_KeyDel')
  DROP TRIGGER [dbo].[tr_BuchblattNamen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BuchblattNamen_KeyUpd')
  DROP TRIGGER [dbo].[tr_BuchblattNamen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BuchblattNamen_Keys')
  DROP TABLE [dbo].[ut_BuchblattNamen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'BBKtoTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [BBKtoTyp] char(1) 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_BuchblattNamen' and sc.[name] = 'BBKtoNr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [BBKtoNr] 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_BuchblattNamen' and sc.[name] = 'BBUser')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [BBUser] 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_BuchblattNamen' and sc.[name] = 'BelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [BelegNr] 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_BuchblattNamen' and sc.[name] = 'IncBelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [IncBelegNr] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [Datum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'Vortrag')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [Vortrag] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'ASSaldo')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [ASSaldo] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'ASSumme')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [ASSumme] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'Bezeich')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [Bezeich] char(50) 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_BuchblattNamen' and sc.[name] = 'FilterView')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ADD [FilterView] integer NOT NULL DEFAULT 0
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'BBKtoTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ALTER COLUMN [BBKtoTyp] char(1)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'BBKtoNr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ALTER COLUMN [BBKtoNr] char(8)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'BBUser')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ALTER COLUMN [BBUser] char(20)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'BelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ALTER COLUMN [BelegNr] char(8)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattNamen' and sc.[name] = 'Bezeich')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattNamen] ALTER COLUMN [Bezeich] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BuchblattNamen')
  DROP VIEW [dbo].[BuchblattNamen]
GO
CREATE VIEW [dbo].[BuchblattNamen] AS
  SELECT * FROM [dbo].[ut_BuchblattNamen]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BuchblattNamen')
  DROP PROCEDURE [dbo].[up_Add_BuchblattNamen]
GO
CREATE PROCEDURE [dbo].[up_Add_BuchblattNamen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BuchblattNamen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BuchblattNamen] (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_BuchblattNamen')
  DROP PROCEDURE [dbo].[up_Upd_BuchblattNamen]
GO
CREATE PROCEDURE [dbo].[up_Upd_BuchblattNamen]
  @RI integer
 ,@Parm_BBKtoTyp char(1)
 ,@Parm_BBKtoNr char(8)
 ,@Parm_BBUser char(20)
 ,@Parm_BelegNr char(8)
 ,@Parm_IncBelegNr tinyint
 ,@Parm_Datum datetime
 ,@Parm_Vortrag tinyint
 ,@Parm_ASSaldo float
 ,@Parm_ASSumme float
 ,@Parm_Bezeich char(50)
 ,@Parm_FilterView integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BuchblattNamen] SET
     [BBKtoTyp] = RTRIM(@Parm_BBKtoTyp)
    ,[BBKtoNr] = RTRIM(@Parm_BBKtoNr)
    ,[BBUser] = RTRIM(@Parm_BBUser)
    ,[BelegNr] = RTRIM(@Parm_BelegNr)
    ,[IncBelegNr] = @Parm_IncBelegNr
    ,[Datum] = @Parm_Datum
    ,[Vortrag] = @Parm_Vortrag
    ,[ASSaldo] = @Parm_ASSaldo
    ,[ASSumme] = @Parm_ASSumme
    ,[Bezeich] = RTRIM(@Parm_Bezeich)
    ,[FilterView] = @Parm_FilterView
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BuchblattNamen')
  DROP PROCEDURE [dbo].[up_Sel_BuchblattNamen]
GO
CREATE PROCEDURE [dbo].[up_Sel_BuchblattNamen]
  @RI integer
AS
SELECT
     [BBKtoTyp]
    ,[BBKtoNr]
    ,[BBUser]
    ,[BelegNr]
    ,[IncBelegNr]
    ,[Datum]
    ,[Vortrag]
    ,[ASSaldo]
    ,[ASSumme]
    ,[Bezeich]
    ,[FilterView]
  FROM [dbo].[ut_BuchblattNamen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BuchblattNamen')
  DROP PROCEDURE [dbo].[up_Del_BuchblattNamen]
GO
CREATE PROCEDURE [dbo].[up_Del_BuchblattNamen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BuchblattNamen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BuchblattNamen')
  DROP PROCEDURE [dbo].[up_Reorg_BuchblattNamen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BuchblattNamen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BuchblattNamen] 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_BuchblattNamen] 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_BuchblattNamen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BuchblattNamen')
  DROP PROCEDURE [dbo].[up_Append_BuchblattNamen]
GO
CREATE PROCEDURE [dbo].[up_Append_BuchblattNamen]
  @Parm_BBKtoTyp char(1)
 ,@Parm_BBKtoNr char(8)
 ,@Parm_BBUser char(20)
 ,@Parm_BelegNr char(8)
 ,@Parm_IncBelegNr tinyint
 ,@Parm_Datum datetime
 ,@Parm_Vortrag tinyint
 ,@Parm_ASSaldo float
 ,@Parm_ASSumme float
 ,@Parm_Bezeich char(50)
 ,@Parm_FilterView integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BuchblattNamen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BuchblattNamen] (
    RecId
   ,[BBKtoTyp]
   ,[BBKtoNr]
   ,[BBUser]
   ,[BelegNr]
   ,[IncBelegNr]
   ,[Datum]
   ,[Vortrag]
   ,[ASSaldo]
   ,[ASSumme]
   ,[Bezeich]
   ,[FilterView]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_BBKtoTyp)
   ,RTRIM(@Parm_BBKtoNr)
   ,RTRIM(@Parm_BBUser)
   ,RTRIM(@Parm_BelegNr)
   ,@Parm_IncBelegNr
   ,@Parm_Datum
   ,@Parm_Vortrag
   ,@Parm_ASSaldo
   ,@Parm_ASSumme
   ,RTRIM(@Parm_Bezeich)
   ,@Parm_FilterView
       )
  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_BuchblattNamen')
  DROP PROCEDURE [dbo].[up_TrSel_BuchblattNamen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BuchblattNamen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([BBKtoTyp]))
      +LTRIM(RTRIM([BBKtoNr]))
      +LTRIM(RTRIM([BBUser]))
      +LTRIM(RTRIM([BelegNr]))
      +LTRIM(RTRIM([Bezeich]))
    AS Data
  FROM     [dbo].[ut_BuchblattNamen]
  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_BuchblattNamen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_BuchblattNamen]
(BBKtoTyp,BBKtoNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_First_BuchblattNamen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_Last_BuchblattNamen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BuchblattNamen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_Next_BuchblattNamen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BuchblattNamen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BuchblattNamen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BuchblattNamen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BuchblattNamen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BuchblattNamen_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus BuchblattNamen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_BuchblattNamen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen] ORDER BY BBKtoTyp,BBKtoNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BuchblattNamen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BuchblattNamen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen] ORDER BY BBKtoTyp DESC,BBKtoNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY BBKtoTyp,BBKtoNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BuchblattNamen_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBBKtoTyp char(1)
  DECLARE @oldBBKtoNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BBKtoTyp,BBKtoNr FROM [dbo].[ut_BuchblattNamen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBBKtoTyp,@oldBBKtoNr
  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_BuchblattNamen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND RecId > @oldRecId
                   ORDER BY BBKtoTyp,BBKtoNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr > @oldBBKtoNr
                   ORDER BY BBKtoTyp,BBKtoNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen]
                   WHERE BBKtoTyp > @oldBBKtoTyp
                   ORDER BY BBKtoTyp,BBKtoNr,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 BBKtoTyp DESC,BBKtoNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BuchblattNamen_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBBKtoTyp char(1)
  DECLARE @oldBBKtoNr char(8)

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

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

  IF @xLen < 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen]
      WHERE BBKtoTyp = SUBSTRING(@xKey,1,1) AND SUBSTRING(BBKtoNr,1,@xLen-1) = SUBSTRING(@xKey,2,@xLen-1)
      ORDER BY BBKtoTyp,BBKtoNr,RecId
    RETURN
  END

  IF @xLen = 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen]
      WHERE BBKtoTyp = SUBSTRING(@xKey,1,1) AND BBKtoNr = SUBSTRING(@xKey,2,8)
      ORDER BY BBKtoTyp,BBKtoNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

  IF @xLen < 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen]
      WHERE BBKtoTyp = SUBSTRING(@xKey,1,1) AND SUBSTRING(BBKtoNr,1,@xLen-1) = SUBSTRING(@xKey,2,@xLen-1)
      ORDER BY BBKtoTyp DESC,BBKtoNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattNamen]
      WHERE BBKtoTyp = SUBSTRING(@xKey,1,1) AND BBKtoNr = SUBSTRING(@xKey,2,8)
      ORDER BY BBKtoTyp DESC,BBKtoNr DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

