------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebLohnart')
BEGIN
CREATE TABLE [dbo].[ut_WebLohnart]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[StatusNum] integer DEFAULT 0
  ,[Lohnart] char(20) DEFAULT ' '
  ,[Comment] char(50) DEFAULT ' '
  CONSTRAINT [Pk_WebLohnart]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebLohnart_KeyDel')
  DROP TRIGGER [dbo].[tr_WebLohnart_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebLohnart_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebLohnart_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebLohnart_Keys')
  DROP TABLE [dbo].[ut_WebLohnart_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLohnart' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebLohnart] ADD [Knr] 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_WebLohnart' and sc.[name] = 'StatusNum')
BEGIN
  ALTER TABLE [dbo].[ut_WebLohnart] ADD [StatusNum] 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_WebLohnart' and sc.[name] = 'Lohnart')
BEGIN
  ALTER TABLE [dbo].[ut_WebLohnart] ADD [Lohnart] 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_WebLohnart' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_WebLohnart] ADD [Comment] char(50) 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_WebLohnart' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebLohnart] ALTER COLUMN [Knr] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLohnart' and sc.[name] = 'Lohnart')
BEGIN
  ALTER TABLE [dbo].[ut_WebLohnart] ALTER COLUMN [Lohnart] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLohnart' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_WebLohnart] ALTER COLUMN [Comment] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebLohnart')
  DROP VIEW [dbo].[WebLohnart]
GO
CREATE VIEW [dbo].[WebLohnart] AS
  SELECT * FROM [dbo].[ut_WebLohnart]
GO
CREATE TABLE [dbo].[ut_WebLohnart_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(32) NOT NULL
  CONSTRAINT [Pk_ut_WebLohnart_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WebLohnart_KeyDel] ON [dbo].[ut_WebLohnart]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WebLohnart_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_WebLohnart_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(20),[Knr])+STR([StatusNum],2,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_WebLohnart_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(20),[Knr])+STR([StatusNum],2,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebLohnart')
  DROP PROCEDURE [dbo].[up_Add_WebLohnart]
GO
CREATE PROCEDURE [dbo].[up_Add_WebLohnart] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WebLohnart])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebLohnart] (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_WebLohnart')
  DROP PROCEDURE [dbo].[up_Upd_WebLohnart]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebLohnart]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_StatusNum integer
 ,@Parm_Lohnart char(20)
 ,@Parm_Comment char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebLohnart] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[StatusNum] = @Parm_StatusNum
    ,[Lohnart] = RTRIM(@Parm_Lohnart)
    ,[Comment] = RTRIM(@Parm_Comment)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebLohnart')
  DROP PROCEDURE [dbo].[up_Sel_WebLohnart]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebLohnart]
  @RI integer
AS
SELECT
     [Knr]
    ,[StatusNum]
    ,[Lohnart]
    ,[Comment]
  FROM [dbo].[ut_WebLohnart]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WebLohnart')
  DROP PROCEDURE [dbo].[up_Del_WebLohnart]
GO
CREATE PROCEDURE [dbo].[up_Del_WebLohnart]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebLohnart] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebLohnart')
  DROP PROCEDURE [dbo].[up_Reorg_WebLohnart]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebLohnart]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebLohnart] 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_WebLohnart] 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_WebLohnart] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebLohnart')
  DROP PROCEDURE [dbo].[up_Append_WebLohnart]
GO
CREATE PROCEDURE [dbo].[up_Append_WebLohnart]
  @Parm_Knr char(20)
 ,@Parm_StatusNum integer
 ,@Parm_Lohnart char(20)
 ,@Parm_Comment char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebLohnart])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebLohnart] (
    RecId
   ,[Knr]
   ,[StatusNum]
   ,[Lohnart]
   ,[Comment]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Knr)
   ,@Parm_StatusNum
   ,RTRIM(@Parm_Lohnart)
   ,RTRIM(@Parm_Comment)
       )
  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_WebLohnart')
  DROP PROCEDURE [dbo].[up_TrSel_WebLohnart]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebLohnart]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Lohnart]))
      +LTRIM(RTRIM([Comment]))
    AS Data
  FROM     [dbo].[ut_WebLohnart]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebLohnart_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_First_WebLohnart_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebLohnart_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebLohnart_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebLohnart_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebLohnart_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebLohnart_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebLohnart_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebLohnart_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebLohnart_Key00]
GO

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

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

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

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

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

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

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

