------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_DlgPagesNames')
BEGIN
CREATE TABLE [dbo].[ut_DlgPagesNames]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[DlgNr] integer DEFAULT 0
  ,[SubType] integer DEFAULT 0
  ,[View] integer DEFAULT 0
  ,[PageNo] integer DEFAULT 0
  ,[Hidden] tinyint DEFAULT 0
  ,[PageName] char(20) DEFAULT ' '
  ,[PageTitle] char(30) DEFAULT ' '
  ,[TestRights] char(100) DEFAULT ' '
  ,[TestTables] char(100) DEFAULT ' '
  CONSTRAINT [Pk_DlgPagesNames]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_DlgPagesNames_KeyDel')
  DROP TRIGGER [dbo].[tr_DlgPagesNames_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_DlgPagesNames_KeyUpd')
  DROP TRIGGER [dbo].[tr_DlgPagesNames_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_DlgPagesNames_Keys')
  DROP TABLE [dbo].[ut_DlgPagesNames_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DlgPagesNames' and sc.[name] = 'DlgNr')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [DlgNr] 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_DlgPagesNames' and sc.[name] = 'SubType')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [SubType] 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_DlgPagesNames' and sc.[name] = 'View')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [View] 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_DlgPagesNames' and sc.[name] = 'PageNo')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [PageNo] 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_DlgPagesNames' and sc.[name] = 'Hidden')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [Hidden] 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_DlgPagesNames' and sc.[name] = 'PageName')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [PageName] 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_DlgPagesNames' and sc.[name] = 'PageTitle')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [PageTitle] 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_DlgPagesNames' and sc.[name] = 'TestRights')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [TestRights] char(100) 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_DlgPagesNames' and sc.[name] = 'TestTables')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ADD [TestTables] char(100) NOT NULL DEFAULT ' '
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DlgPagesNames' and sc.[name] = 'PageName')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ALTER COLUMN [PageName] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DlgPagesNames' and sc.[name] = 'PageTitle')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ALTER COLUMN [PageTitle] char(30)
END
GO
IF 100 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DlgPagesNames' and sc.[name] = 'TestRights')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ALTER COLUMN [TestRights] char(100)
END
GO
IF 100 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DlgPagesNames' and sc.[name] = 'TestTables')
BEGIN
  ALTER TABLE [dbo].[ut_DlgPagesNames] ALTER COLUMN [TestTables] char(100)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'DlgPagesNames')
  DROP VIEW [dbo].[DlgPagesNames]
GO
CREATE VIEW [dbo].[DlgPagesNames] AS
  SELECT * FROM [dbo].[ut_DlgPagesNames]
GO
CREATE TABLE [dbo].[ut_DlgPagesNames_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(28) NOT NULL
  CONSTRAINT [Pk_ut_DlgPagesNames_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_DlgPagesNames_KeyDel] ON [dbo].[ut_DlgPagesNames]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_DlgPagesNames_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_DlgPagesNames_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([DlgNr],6,0)+STR([SubType],4,0)+STR([View],4,0)+STR([PageNo],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_DlgPagesNames_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([DlgNr],6,0)+STR([SubType],4,0)+STR([View],4,0)+STR([PageNo],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_DlgPagesNames')
  DROP PROCEDURE [dbo].[up_Add_DlgPagesNames]
GO
CREATE PROCEDURE [dbo].[up_Add_DlgPagesNames] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_DlgPagesNames])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_DlgPagesNames] (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_DlgPagesNames')
  DROP PROCEDURE [dbo].[up_Upd_DlgPagesNames]
GO
CREATE PROCEDURE [dbo].[up_Upd_DlgPagesNames]
  @RI integer
 ,@Parm_DlgNr integer
 ,@Parm_SubType integer
 ,@Parm_View integer
 ,@Parm_PageNo integer
 ,@Parm_Hidden tinyint
 ,@Parm_PageName char(20)
 ,@Parm_PageTitle char(30)
 ,@Parm_TestRights char(100)
 ,@Parm_TestTables char(100)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_DlgPagesNames] SET
     [DlgNr] = @Parm_DlgNr
    ,[SubType] = @Parm_SubType
    ,[View] = @Parm_View
    ,[PageNo] = @Parm_PageNo
    ,[Hidden] = @Parm_Hidden
    ,[PageName] = RTRIM(@Parm_PageName)
    ,[PageTitle] = RTRIM(@Parm_PageTitle)
    ,[TestRights] = RTRIM(@Parm_TestRights)
    ,[TestTables] = RTRIM(@Parm_TestTables)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_DlgPagesNames')
  DROP PROCEDURE [dbo].[up_Sel_DlgPagesNames]
GO
CREATE PROCEDURE [dbo].[up_Sel_DlgPagesNames]
  @RI integer
AS
SELECT
     [DlgNr]
    ,[SubType]
    ,[View]
    ,[PageNo]
    ,[Hidden]
    ,[PageName]
    ,[PageTitle]
    ,[TestRights]
    ,[TestTables]
  FROM [dbo].[ut_DlgPagesNames]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_DlgPagesNames')
  DROP PROCEDURE [dbo].[up_Del_DlgPagesNames]
GO
CREATE PROCEDURE [dbo].[up_Del_DlgPagesNames]
  @RI integer
AS
  DELETE FROM [dbo].[ut_DlgPagesNames] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_DlgPagesNames')
  DROP PROCEDURE [dbo].[up_Reorg_DlgPagesNames]
GO
CREATE PROCEDURE [dbo].[up_Reorg_DlgPagesNames]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_DlgPagesNames] 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_DlgPagesNames] 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_DlgPagesNames] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_DlgPagesNames')
  DROP PROCEDURE [dbo].[up_Append_DlgPagesNames]
GO
CREATE PROCEDURE [dbo].[up_Append_DlgPagesNames]
  @Parm_DlgNr integer
 ,@Parm_SubType integer
 ,@Parm_View integer
 ,@Parm_PageNo integer
 ,@Parm_Hidden tinyint
 ,@Parm_PageName char(20)
 ,@Parm_PageTitle char(30)
 ,@Parm_TestRights char(100)
 ,@Parm_TestTables char(100)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_DlgPagesNames])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_DlgPagesNames] (
    RecId
   ,[DlgNr]
   ,[SubType]
   ,[View]
   ,[PageNo]
   ,[Hidden]
   ,[PageName]
   ,[PageTitle]
   ,[TestRights]
   ,[TestTables]
     ) VALUES (
    @NeueId
   ,@Parm_DlgNr
   ,@Parm_SubType
   ,@Parm_View
   ,@Parm_PageNo
   ,@Parm_Hidden
   ,RTRIM(@Parm_PageName)
   ,RTRIM(@Parm_PageTitle)
   ,RTRIM(@Parm_TestRights)
   ,RTRIM(@Parm_TestTables)
       )
  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_DlgPagesNames')
  DROP PROCEDURE [dbo].[up_TrSel_DlgPagesNames]
GO
CREATE PROCEDURE [dbo].[up_TrSel_DlgPagesNames]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PageName]))
      +LTRIM(RTRIM([PageTitle]))
      +LTRIM(RTRIM([TestRights]))
      +LTRIM(RTRIM([TestTables]))
    AS Data
  FROM     [dbo].[ut_DlgPagesNames]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_DlgPagesNames_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_First_DlgPagesNames_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_Last_DlgPagesNames_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_Prior_DlgPagesNames_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_Next_DlgPagesNames_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_DlgPagesNames_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_Next50_DlgPagesNames_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_FindL_DlgPagesNames_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_DlgPagesNames_Key00')
  DROP PROCEDURE [dbo].[up_FindF_DlgPagesNames_Key00]
GO

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

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_DlgPagesNames_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_DlgPagesNames_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_DlgPagesNames_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_DlgPagesNames_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

