------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Mitarbeitergruppen')
BEGIN
CREATE TABLE [dbo].[ut_Mitarbeitergruppen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[bsgruppe] char(2) DEFAULT ' '
  ,[sollstd] float DEFAULT 0
  ,[VkLohn2] float DEFAULT 0
  CONSTRAINT [Pk_Mitarbeitergruppen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mitarbeitergruppen_KeyDel')
  DROP TRIGGER [dbo].[tr_Mitarbeitergruppen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mitarbeitergruppen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Mitarbeitergruppen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Mitarbeitergruppen_Keys')
  DROP TABLE [dbo].[ut_Mitarbeitergruppen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mitarbeitergruppen' and sc.[name] = 'bsgruppe')
BEGIN
  ALTER TABLE [dbo].[ut_Mitarbeitergruppen] ADD [bsgruppe] char(2) 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_Mitarbeitergruppen' and sc.[name] = 'sollstd')
BEGIN
  ALTER TABLE [dbo].[ut_Mitarbeitergruppen] ADD [sollstd] float 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_Mitarbeitergruppen' and sc.[name] = 'VkLohn2')
BEGIN
  ALTER TABLE [dbo].[ut_Mitarbeitergruppen] ADD [VkLohn2] float NOT NULL DEFAULT 0
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mitarbeitergruppen' and sc.[name] = 'bsgruppe')
BEGIN
  ALTER TABLE [dbo].[ut_Mitarbeitergruppen] ALTER COLUMN [bsgruppe] char(2)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Mitarbeitergruppen')
  DROP VIEW [dbo].[Mitarbeitergruppen]
GO
CREATE VIEW [dbo].[Mitarbeitergruppen] AS
  SELECT * FROM [dbo].[ut_Mitarbeitergruppen]
GO
CREATE TABLE [dbo].[ut_Mitarbeitergruppen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(12) NOT NULL
  CONSTRAINT [Pk_ut_Mitarbeitergruppen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Mitarbeitergruppen_KeyDel] ON [dbo].[ut_Mitarbeitergruppen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Mitarbeitergruppen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Mitarbeitergruppen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key0_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(2),[bsgruppe])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Mitarbeitergruppen_Keys] WITH (SERIALIZABLE) SET
    [Key0_Value] = UPPER(convert(char(2),[bsgruppe])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Mitarbeitergruppen')
  DROP PROCEDURE [dbo].[up_Add_Mitarbeitergruppen]
GO
CREATE PROCEDURE [dbo].[up_Add_Mitarbeitergruppen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Mitarbeitergruppen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mitarbeitergruppen] (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_Mitarbeitergruppen')
  DROP PROCEDURE [dbo].[up_Upd_Mitarbeitergruppen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Mitarbeitergruppen]
  @RI integer
 ,@Parm_bsgruppe char(2)
 ,@Parm_sollstd float
 ,@Parm_VkLohn2 float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Mitarbeitergruppen] SET
     [bsgruppe] = RTRIM(@Parm_bsgruppe)
    ,[sollstd] = @Parm_sollstd
    ,[VkLohn2] = @Parm_VkLohn2
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Mitarbeitergruppen')
  DROP PROCEDURE [dbo].[up_Sel_Mitarbeitergruppen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Mitarbeitergruppen]
  @RI integer
AS
SELECT
     [bsgruppe]
    ,[sollstd]
    ,[VkLohn2]
  FROM [dbo].[ut_Mitarbeitergruppen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Mitarbeitergruppen')
  DROP PROCEDURE [dbo].[up_Del_Mitarbeitergruppen]
GO
CREATE PROCEDURE [dbo].[up_Del_Mitarbeitergruppen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Mitarbeitergruppen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Mitarbeitergruppen')
  DROP PROCEDURE [dbo].[up_Reorg_Mitarbeitergruppen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Mitarbeitergruppen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Mitarbeitergruppen] 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_Mitarbeitergruppen] 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_Mitarbeitergruppen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Mitarbeitergruppen')
  DROP PROCEDURE [dbo].[up_Append_Mitarbeitergruppen]
GO
CREATE PROCEDURE [dbo].[up_Append_Mitarbeitergruppen]
  @Parm_bsgruppe char(2)
 ,@Parm_sollstd float
 ,@Parm_VkLohn2 float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Mitarbeitergruppen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mitarbeitergruppen] (
    RecId
   ,[bsgruppe]
   ,[sollstd]
   ,[VkLohn2]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_bsgruppe)
   ,@Parm_sollstd
   ,@Parm_VkLohn2
       )
  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_Mitarbeitergruppen')
  DROP PROCEDURE [dbo].[up_TrSel_Mitarbeitergruppen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Mitarbeitergruppen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([bsgruppe]))
    AS Data
  FROM     [dbo].[ut_Mitarbeitergruppen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_Mitarbeitergruppen_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_First_Mitarbeitergruppen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_Last_Mitarbeitergruppen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Mitarbeitergruppen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_Next_Mitarbeitergruppen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Mitarbeitergruppen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Mitarbeitergruppen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Mitarbeitergruppen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Mitarbeitergruppen_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Mitarbeitergruppen_Key0]
GO

CREATE PROCEDURE [dbo].[up_First_Mitarbeitergruppen_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Mitarbeitergruppen_Keys] ORDER BY [Key0_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Mitarbeitergruppen_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Mitarbeitergruppen_Keys] ORDER BY [Key0_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Mitarbeitergruppen_Key0] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Mitarbeitergruppen_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_Mitarbeitergruppen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Mitarbeitergruppen_Key0] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Mitarbeitergruppen_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_Mitarbeitergruppen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

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

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

