------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_RecordZoom')
BEGIN
CREATE TABLE [dbo].[ut_RecordZoom]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[TableName] char(30) DEFAULT ' '
  ,[LfdNr] char(4) DEFAULT ' '
  ,[FieldNo] integer DEFAULT 0
  ,[DisplayName] char(50) DEFAULT ' '
  CONSTRAINT [Pk_RecordZoom]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RecordZoom_KeyDel')
  DROP TRIGGER [dbo].[tr_RecordZoom_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RecordZoom_KeyUpd')
  DROP TRIGGER [dbo].[tr_RecordZoom_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_RecordZoom_Keys')
  DROP TABLE [dbo].[ut_RecordZoom_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'TableName')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [TableName] 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_RecordZoom' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [LfdNr] 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_RecordZoom' and sc.[name] = 'FieldNo')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [FieldNo] integer 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_RecordZoom' and sc.[name] = 'DisplayName')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [DisplayName] char(50) NOT NULL DEFAULT ' '
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'TableName')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [TableName] char(30)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [LfdNr] char(4)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'DisplayName')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [DisplayName] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'RecordZoom')
  DROP VIEW [dbo].[RecordZoom]
GO
CREATE VIEW [dbo].[RecordZoom] AS
  SELECT * FROM [dbo].[ut_RecordZoom]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_RecordZoom')
  DROP PROCEDURE [dbo].[up_Add_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Add_RecordZoom] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_RecordZoom])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_RecordZoom] (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_RecordZoom')
  DROP PROCEDURE [dbo].[up_Upd_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Upd_RecordZoom]
  @RI integer
 ,@Parm_TableName char(30)
 ,@Parm_LfdNr char(4)
 ,@Parm_FieldNo integer
 ,@Parm_DisplayName char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_RecordZoom] SET
     [TableName] = RTRIM(@Parm_TableName)
    ,[LfdNr] = RTRIM(@Parm_LfdNr)
    ,[FieldNo] = @Parm_FieldNo
    ,[DisplayName] = RTRIM(@Parm_DisplayName)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_RecordZoom')
  DROP PROCEDURE [dbo].[up_Sel_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Sel_RecordZoom]
  @RI integer
AS
SELECT
     [TableName]
    ,[LfdNr]
    ,[FieldNo]
    ,[DisplayName]
  FROM [dbo].[ut_RecordZoom]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_RecordZoom')
  DROP PROCEDURE [dbo].[up_Del_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Del_RecordZoom]
  @RI integer
AS
  DELETE FROM [dbo].[ut_RecordZoom] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_RecordZoom')
  DROP PROCEDURE [dbo].[up_Reorg_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Reorg_RecordZoom]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_RecordZoom] 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_RecordZoom] 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_RecordZoom] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_RecordZoom')
  DROP PROCEDURE [dbo].[up_Append_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Append_RecordZoom]
  @Parm_TableName char(30)
 ,@Parm_LfdNr char(4)
 ,@Parm_FieldNo integer
 ,@Parm_DisplayName char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_RecordZoom])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_RecordZoom] (
    RecId
   ,[TableName]
   ,[LfdNr]
   ,[FieldNo]
   ,[DisplayName]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_TableName)
   ,RTRIM(@Parm_LfdNr)
   ,@Parm_FieldNo
   ,RTRIM(@Parm_DisplayName)
       )
  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_RecordZoom')
  DROP PROCEDURE [dbo].[up_TrSel_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_TrSel_RecordZoom]
AS
  SELECT   RecId,
      LTRIM(RTRIM([TableName]))
      +LTRIM(RTRIM([LfdNr]))
      +LTRIM(RTRIM([DisplayName]))
    AS Data
  FROM     [dbo].[ut_RecordZoom]
  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_RecordZoom'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_RecordZoom]
(TableName,LfdNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_First_RecordZoom_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_Last_RecordZoom_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_Prior_RecordZoom_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_Next_RecordZoom_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_RecordZoom_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_Next50_RecordZoom_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_FindL_RecordZoom_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_FindF_RecordZoom_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus RecordZoom in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_RecordZoom_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] ORDER BY TableName,LfdNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus RecordZoom in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_RecordZoom_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] ORDER BY TableName DESC,LfdNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY TableName,LfdNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_RecordZoom_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTableName char(30)
  DECLARE @oldLfdNr char(4)

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

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT TableName,LfdNr FROM [dbo].[ut_RecordZoom] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldTableName,@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_RecordZoom]
                   WHERE TableName = @oldTableName AND LfdNr = @oldLfdNr AND RecId < @oldRecId
                   ORDER BY TableName 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_RecordZoom]
                   WHERE TableName = @oldTableName AND LfdNr < @oldLfdNr
                   ORDER BY TableName 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_RecordZoom]
                   WHERE TableName < @oldTableName
                   ORDER BY TableName 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 TableName,LfdNr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_RecordZoom_Key00] @xKey char(128),@xLen integer AS

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

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

  IF @xLen < 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom]
      WHERE TableName = SUBSTRING(@xKey,1,30) AND SUBSTRING(LfdNr,1,@xLen-30) = SUBSTRING(@xKey,31,@xLen-30)
      ORDER BY TableName,LfdNr,RecId
    RETURN
  END

  IF @xLen = 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom]
      WHERE TableName = SUBSTRING(@xKey,1,30) AND LfdNr = SUBSTRING(@xKey,31,4)
      ORDER BY TableName,LfdNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  IF @xLen < 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom]
      WHERE TableName = SUBSTRING(@xKey,1,30) AND SUBSTRING(LfdNr,1,@xLen-30) = SUBSTRING(@xKey,31,@xLen-30)
      ORDER BY TableName DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom]
      WHERE TableName = SUBSTRING(@xKey,1,30) AND LfdNr = SUBSTRING(@xKey,31,4)
      ORDER BY TableName DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

