------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_TableList')
BEGIN
CREATE TABLE [dbo].[ut_TableList]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nr] integer DEFAULT 0
  ,[TabAlias] char(30) DEFAULT ' '
  ,[TabDef] char(30) DEFAULT ' '
  ,[DisableImp] tinyint DEFAULT 0
  ,[Suspend] tinyint DEFAULT 0
  CONSTRAINT [Pk_TableList]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_TableList_KeyDel')
  DROP TRIGGER [dbo].[tr_TableList_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_TableList_KeyUpd')
  DROP TRIGGER [dbo].[tr_TableList_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_TableList_Keys')
  DROP TABLE [dbo].[ut_TableList_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_TableList' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_TableList] ADD [Nr] 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_TableList' and sc.[name] = 'TabAlias')
BEGIN
  ALTER TABLE [dbo].[ut_TableList] ADD [TabAlias] 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_TableList' and sc.[name] = 'TabDef')
BEGIN
  ALTER TABLE [dbo].[ut_TableList] ADD [TabDef] 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_TableList' and sc.[name] = 'DisableImp')
BEGIN
  ALTER TABLE [dbo].[ut_TableList] ADD [DisableImp] 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_TableList' and sc.[name] = 'Suspend')
BEGIN
  ALTER TABLE [dbo].[ut_TableList] ADD [Suspend] tinyint NOT NULL DEFAULT 0
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_TableList' and sc.[name] = 'TabAlias')
BEGIN
  ALTER TABLE [dbo].[ut_TableList] ALTER COLUMN [TabAlias] char(30)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_TableList' and sc.[name] = 'TabDef')
BEGIN
  ALTER TABLE [dbo].[ut_TableList] ALTER COLUMN [TabDef] char(30)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'TableList')
  DROP VIEW [dbo].[TableList]
GO
CREATE VIEW [dbo].[TableList] AS
  SELECT * FROM [dbo].[ut_TableList]
GO
CREATE TABLE [dbo].[ut_TableList_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(13) NOT NULL
  CONSTRAINT [Pk_ut_TableList_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_TableList_KeyDel] ON [dbo].[ut_TableList]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_TableList_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_TableList_KeyUpd] ON [dbo].[ut_TableList]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_TableList_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_TableList_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([Nr],3,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_TableList_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([Nr],3,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_TableList] SET RecId = RecId
GO


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_TableList')
  DROP PROCEDURE [dbo].[up_Add_TableList]
GO
CREATE PROCEDURE [dbo].[up_Add_TableList] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_TableList])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_TableList] (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_TableList')
  DROP PROCEDURE [dbo].[up_Upd_TableList]
GO
CREATE PROCEDURE [dbo].[up_Upd_TableList]
  @RI integer
 ,@Parm_Nr integer
 ,@Parm_TabAlias char(30)
 ,@Parm_TabDef char(30)
 ,@Parm_DisableImp tinyint
 ,@Parm_Suspend tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_TableList] SET
     [Nr] = @Parm_Nr
    ,[TabAlias] = RTRIM(@Parm_TabAlias)
    ,[TabDef] = RTRIM(@Parm_TabDef)
    ,[DisableImp] = @Parm_DisableImp
    ,[Suspend] = @Parm_Suspend
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_TableList')
  DROP PROCEDURE [dbo].[up_Sel_TableList]
GO
CREATE PROCEDURE [dbo].[up_Sel_TableList]
  @RI integer
AS
SELECT
     [Nr]
    ,[TabAlias]
    ,[TabDef]
    ,[DisableImp]
    ,[Suspend]
  FROM [dbo].[ut_TableList]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_TableList')
  DROP PROCEDURE [dbo].[up_Del_TableList]
GO
CREATE PROCEDURE [dbo].[up_Del_TableList]
  @RI integer
AS
  DELETE FROM [dbo].[ut_TableList] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_TableList')
  DROP PROCEDURE [dbo].[up_Reorg_TableList]
GO
CREATE PROCEDURE [dbo].[up_Reorg_TableList]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_TableList] 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_TableList] 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_TableList] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_TableList')
  DROP PROCEDURE [dbo].[up_Append_TableList]
GO
CREATE PROCEDURE [dbo].[up_Append_TableList]
  @Parm_Nr integer
 ,@Parm_TabAlias char(30)
 ,@Parm_TabDef char(30)
 ,@Parm_DisableImp tinyint
 ,@Parm_Suspend tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_TableList])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_TableList] (
    RecId
   ,[Nr]
   ,[TabAlias]
   ,[TabDef]
   ,[DisableImp]
   ,[Suspend]
     ) VALUES (
    @NeueId
   ,@Parm_Nr
   ,RTRIM(@Parm_TabAlias)
   ,RTRIM(@Parm_TabDef)
   ,@Parm_DisableImp
   ,@Parm_Suspend
       )
  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_TableList')
  DROP PROCEDURE [dbo].[up_TrSel_TableList]
GO
CREATE PROCEDURE [dbo].[up_TrSel_TableList]
AS
  SELECT   RecId,
      LTRIM(RTRIM([TabAlias]))
      +LTRIM(RTRIM([TabDef]))
    AS Data
  FROM     [dbo].[ut_TableList]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_TableList_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_TableList_Key00')
  DROP PROCEDURE [dbo].[up_First_TableList_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_TableList_Key00')
  DROP PROCEDURE [dbo].[up_Last_TableList_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_TableList_Key00')
  DROP PROCEDURE [dbo].[up_Prior_TableList_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_TableList_Key00')
  DROP PROCEDURE [dbo].[up_Next_TableList_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_TableList_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_TableList_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_TableList_Key00')
  DROP PROCEDURE [dbo].[up_Next50_TableList_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_TableList_Key00')
  DROP PROCEDURE [dbo].[up_FindL_TableList_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_TableList_Key00')
  DROP PROCEDURE [dbo].[up_FindF_TableList_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_TableList_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_TableList_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_TableList_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_TableList_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_TableList_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_TableList_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_TableList_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_TableList_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_TableList_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_TableList_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_TableList_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_TableList_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_TableList_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_TableList_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_TableList_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_TableList_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_TableList_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_TableList_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_TableList_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_TableList_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

