------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_HelpContent')
BEGIN
CREATE TABLE [dbo].[ut_HelpContent]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[FormNo] integer DEFAULT 0
  ,[HelpId] integer DEFAULT 0
  ,[Comment] char(80) DEFAULT ' '
  ,[HelpLink] char(80) DEFAULT ' '
  CONSTRAINT [Pk_HelpContent]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_HelpContent_KeyDel')
  DROP TRIGGER [dbo].[tr_HelpContent_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_HelpContent_KeyUpd')
  DROP TRIGGER [dbo].[tr_HelpContent_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_HelpContent_Keys')
  DROP TABLE [dbo].[ut_HelpContent_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_HelpContent' and sc.[name] = 'FormNo')
BEGIN
  ALTER TABLE [dbo].[ut_HelpContent] ADD [FormNo] 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_HelpContent' and sc.[name] = 'HelpId')
BEGIN
  ALTER TABLE [dbo].[ut_HelpContent] ADD [HelpId] 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_HelpContent' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_HelpContent] ADD [Comment] char(80) 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_HelpContent' and sc.[name] = 'HelpLink')
BEGIN
  ALTER TABLE [dbo].[ut_HelpContent] ADD [HelpLink] char(80) NOT NULL DEFAULT ' '
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_HelpContent' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_HelpContent] ALTER COLUMN [Comment] char(80)
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_HelpContent' and sc.[name] = 'HelpLink')
BEGIN
  ALTER TABLE [dbo].[ut_HelpContent] ALTER COLUMN [HelpLink] char(80)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'HelpContent')
  DROP VIEW [dbo].[HelpContent]
GO
CREATE VIEW [dbo].[HelpContent] AS
  SELECT * FROM [dbo].[ut_HelpContent]
GO
CREATE TABLE [dbo].[ut_HelpContent_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(20) NOT NULL
  CONSTRAINT [Pk_ut_HelpContent_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_HelpContent_KeyDel] ON [dbo].[ut_HelpContent]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_HelpContent_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_HelpContent_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([FormNo],6,0)+STR([HelpId],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_HelpContent_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([FormNo],6,0)+STR([HelpId],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_HelpContent')
  DROP PROCEDURE [dbo].[up_Add_HelpContent]
GO
CREATE PROCEDURE [dbo].[up_Add_HelpContent] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_HelpContent])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_HelpContent] (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_HelpContent')
  DROP PROCEDURE [dbo].[up_Upd_HelpContent]
GO
CREATE PROCEDURE [dbo].[up_Upd_HelpContent]
  @RI integer
 ,@Parm_FormNo integer
 ,@Parm_HelpId integer
 ,@Parm_Comment char(80)
 ,@Parm_HelpLink char(80)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_HelpContent] SET
     [FormNo] = @Parm_FormNo
    ,[HelpId] = @Parm_HelpId
    ,[Comment] = RTRIM(@Parm_Comment)
    ,[HelpLink] = RTRIM(@Parm_HelpLink)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_HelpContent')
  DROP PROCEDURE [dbo].[up_Sel_HelpContent]
GO
CREATE PROCEDURE [dbo].[up_Sel_HelpContent]
  @RI integer
AS
SELECT
     [FormNo]
    ,[HelpId]
    ,[Comment]
    ,[HelpLink]
  FROM [dbo].[ut_HelpContent]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_HelpContent')
  DROP PROCEDURE [dbo].[up_Del_HelpContent]
GO
CREATE PROCEDURE [dbo].[up_Del_HelpContent]
  @RI integer
AS
  DELETE FROM [dbo].[ut_HelpContent] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_HelpContent')
  DROP PROCEDURE [dbo].[up_Reorg_HelpContent]
GO
CREATE PROCEDURE [dbo].[up_Reorg_HelpContent]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_HelpContent] 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_HelpContent] 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_HelpContent] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_HelpContent')
  DROP PROCEDURE [dbo].[up_Append_HelpContent]
GO
CREATE PROCEDURE [dbo].[up_Append_HelpContent]
  @Parm_FormNo integer
 ,@Parm_HelpId integer
 ,@Parm_Comment char(80)
 ,@Parm_HelpLink char(80)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_HelpContent])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_HelpContent] (
    RecId
   ,[FormNo]
   ,[HelpId]
   ,[Comment]
   ,[HelpLink]
     ) VALUES (
    @NeueId
   ,@Parm_FormNo
   ,@Parm_HelpId
   ,RTRIM(@Parm_Comment)
   ,RTRIM(@Parm_HelpLink)
       )
  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_HelpContent')
  DROP PROCEDURE [dbo].[up_TrSel_HelpContent]
GO
CREATE PROCEDURE [dbo].[up_TrSel_HelpContent]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Comment]))
      +LTRIM(RTRIM([HelpLink]))
    AS Data
  FROM     [dbo].[ut_HelpContent]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_HelpContent_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_First_HelpContent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_Last_HelpContent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_Prior_HelpContent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_Next_HelpContent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_HelpContent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_Next50_HelpContent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_FindL_HelpContent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_HelpContent_Key00')
  DROP PROCEDURE [dbo].[up_FindF_HelpContent_Key00]
GO

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

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

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

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

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

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

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

