------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelKalkGruppen')
BEGIN
CREATE TABLE [dbo].[ut_BelKalkGruppen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[Nr] integer DEFAULT 0
  ,[VhMat] float DEFAULT 0
  ,[VhLohn] float DEFAULT 0
  ,[VhFremd] float DEFAULT 0
  ,[VhNe] float DEFAULT 0
  ,[VhSonst] float DEFAULT 0
  ,[Name] char(20) DEFAULT ' '
  ,[LohnAnt1] float DEFAULT 0
  ,[LohnAnt2] float DEFAULT 0
  ,[Faktor1] float DEFAULT 0
  ,[MLohnH] float DEFAULT 0
  CONSTRAINT [Pk_BelKalkGruppen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelKalkGruppen_KeyDel')
  DROP TRIGGER [dbo].[tr_BelKalkGruppen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelKalkGruppen_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelKalkGruppen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelKalkGruppen_Keys')
  DROP TABLE [dbo].[ut_BelKalkGruppen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelKalkGruppen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [PrjNr] char(8) 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_BelKalkGruppen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [BelArt] 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_BelKalkGruppen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [BelNr] char(8) 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_BelKalkGruppen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] 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_BelKalkGruppen' and sc.[name] = 'VhMat')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [VhMat] 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_BelKalkGruppen' and sc.[name] = 'VhLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [VhLohn] 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_BelKalkGruppen' and sc.[name] = 'VhFremd')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [VhFremd] 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_BelKalkGruppen' and sc.[name] = 'VhNe')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [VhNe] 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_BelKalkGruppen' and sc.[name] = 'VhSonst')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [VhSonst] 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_BelKalkGruppen' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [Name] 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_BelKalkGruppen' and sc.[name] = 'LohnAnt1')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [LohnAnt1] 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_BelKalkGruppen' and sc.[name] = 'LohnAnt2')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [LohnAnt2] 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_BelKalkGruppen' and sc.[name] = 'Faktor1')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [Faktor1] 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_BelKalkGruppen' and sc.[name] = 'MLohnH')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ADD [MLohnH] float NOT NULL DEFAULT 0
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelKalkGruppen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelKalkGruppen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelKalkGruppen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ALTER COLUMN [BelNr] char(8)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelKalkGruppen' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_BelKalkGruppen] ALTER COLUMN [Name] char(20)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelKalkGruppen')
  DROP VIEW [dbo].[BelKalkGruppen]
GO
CREATE VIEW [dbo].[BelKalkGruppen] AS
  SELECT * FROM [dbo].[ut_BelKalkGruppen]
GO
CREATE TABLE [dbo].[ut_BelKalkGruppen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(30) NOT NULL
  CONSTRAINT [Pk_ut_BelKalkGruppen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelKalkGruppen_KeyDel] ON [dbo].[ut_BelKalkGruppen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelKalkGruppen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelKalkGruppen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([Nr],2,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelKalkGruppen_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([Nr],2,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelKalkGruppen')
  DROP PROCEDURE [dbo].[up_Add_BelKalkGruppen]
GO
CREATE PROCEDURE [dbo].[up_Add_BelKalkGruppen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelKalkGruppen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelKalkGruppen] (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_BelKalkGruppen')
  DROP PROCEDURE [dbo].[up_Upd_BelKalkGruppen]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelKalkGruppen]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_Nr integer
 ,@Parm_VhMat float
 ,@Parm_VhLohn float
 ,@Parm_VhFremd float
 ,@Parm_VhNe float
 ,@Parm_VhSonst float
 ,@Parm_Name char(20)
 ,@Parm_LohnAnt1 float
 ,@Parm_LohnAnt2 float
 ,@Parm_Faktor1 float
 ,@Parm_MLohnH float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelKalkGruppen] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[Nr] = @Parm_Nr
    ,[VhMat] = @Parm_VhMat
    ,[VhLohn] = @Parm_VhLohn
    ,[VhFremd] = @Parm_VhFremd
    ,[VhNe] = @Parm_VhNe
    ,[VhSonst] = @Parm_VhSonst
    ,[Name] = RTRIM(@Parm_Name)
    ,[LohnAnt1] = @Parm_LohnAnt1
    ,[LohnAnt2] = @Parm_LohnAnt2
    ,[Faktor1] = @Parm_Faktor1
    ,[MLohnH] = @Parm_MLohnH
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelKalkGruppen')
  DROP PROCEDURE [dbo].[up_Sel_BelKalkGruppen]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelKalkGruppen]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[Nr]
    ,[VhMat]
    ,[VhLohn]
    ,[VhFremd]
    ,[VhNe]
    ,[VhSonst]
    ,[Name]
    ,[LohnAnt1]
    ,[LohnAnt2]
    ,[Faktor1]
    ,[MLohnH]
  FROM [dbo].[ut_BelKalkGruppen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelKalkGruppen')
  DROP PROCEDURE [dbo].[up_Del_BelKalkGruppen]
GO
CREATE PROCEDURE [dbo].[up_Del_BelKalkGruppen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelKalkGruppen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelKalkGruppen')
  DROP PROCEDURE [dbo].[up_Reorg_BelKalkGruppen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelKalkGruppen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelKalkGruppen] 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_BelKalkGruppen] 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_BelKalkGruppen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelKalkGruppen')
  DROP PROCEDURE [dbo].[up_Append_BelKalkGruppen]
GO
CREATE PROCEDURE [dbo].[up_Append_BelKalkGruppen]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_Nr integer
 ,@Parm_VhMat float
 ,@Parm_VhLohn float
 ,@Parm_VhFremd float
 ,@Parm_VhNe float
 ,@Parm_VhSonst float
 ,@Parm_Name char(20)
 ,@Parm_LohnAnt1 float
 ,@Parm_LohnAnt2 float
 ,@Parm_Faktor1 float
 ,@Parm_MLohnH float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelKalkGruppen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelKalkGruppen] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[Nr]
   ,[VhMat]
   ,[VhLohn]
   ,[VhFremd]
   ,[VhNe]
   ,[VhSonst]
   ,[Name]
   ,[LohnAnt1]
   ,[LohnAnt2]
   ,[Faktor1]
   ,[MLohnH]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,@Parm_Nr
   ,@Parm_VhMat
   ,@Parm_VhLohn
   ,@Parm_VhFremd
   ,@Parm_VhNe
   ,@Parm_VhSonst
   ,RTRIM(@Parm_Name)
   ,@Parm_LohnAnt1
   ,@Parm_LohnAnt2
   ,@Parm_Faktor1
   ,@Parm_MLohnH
       )
  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_BelKalkGruppen')
  DROP PROCEDURE [dbo].[up_TrSel_BelKalkGruppen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelKalkGruppen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([Name]))
    AS Data
  FROM     [dbo].[ut_BelKalkGruppen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelKalkGruppen_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_First_BelKalkGruppen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelKalkGruppen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelKalkGruppen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelKalkGruppen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelKalkGruppen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelKalkGruppen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelKalkGruppen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelKalkGruppen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelKalkGruppen_Key00]
GO

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

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

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

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

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

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

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

