------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelAufmasse')
BEGIN
CREATE TABLE [dbo].[ut_BelAufmasse]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[AmNr] integer DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Abgenommen] tinyint DEFAULT 0
  ,[Bez1] char(60) DEFAULT ' '
  ,[Statistik] tinyint DEFAULT 0
  CONSTRAINT [Pk_BelAufmasse]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelAufmasse_KeyDel')
  DROP TRIGGER [dbo].[tr_BelAufmasse_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelAufmasse_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelAufmasse_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelAufmasse_Keys')
  DROP TABLE [dbo].[ut_BelAufmasse_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelAufmasse' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] 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_BelAufmasse' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] 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_BelAufmasse' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] 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_BelAufmasse' and sc.[name] = 'AmNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] ADD [AmNr] 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_BelAufmasse' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] ADD [Datum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelAufmasse' and sc.[name] = 'Abgenommen')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] ADD [Abgenommen] 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_BelAufmasse' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] ADD [Bez1] char(60) 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_BelAufmasse' and sc.[name] = 'Statistik')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] ADD [Statistik] tinyint 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_BelAufmasse' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] 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_BelAufmasse' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] 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_BelAufmasse' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] ALTER COLUMN [BelNr] char(8)
END
GO
IF 60 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelAufmasse' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelAufmasse] ALTER COLUMN [Bez1] char(60)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelAufmasse')
  DROP VIEW [dbo].[BelAufmasse]
GO
CREATE VIEW [dbo].[BelAufmasse] AS
  SELECT * FROM [dbo].[ut_BelAufmasse]
GO
CREATE TABLE [dbo].[ut_BelAufmasse_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(32) NOT NULL
  CONSTRAINT [Pk_ut_BelAufmasse_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelAufmasse_KeyDel] ON [dbo].[ut_BelAufmasse]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelAufmasse_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelAufmasse')
  DROP PROCEDURE [dbo].[up_Add_BelAufmasse]
GO
CREATE PROCEDURE [dbo].[up_Add_BelAufmasse] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelAufmasse])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelAufmasse] (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_BelAufmasse')
  DROP PROCEDURE [dbo].[up_Upd_BelAufmasse]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelAufmasse]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_AmNr integer
 ,@Parm_Datum datetime
 ,@Parm_Abgenommen tinyint
 ,@Parm_Bez1 char(60)
 ,@Parm_Statistik tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelAufmasse] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[AmNr] = @Parm_AmNr
    ,[Datum] = @Parm_Datum
    ,[Abgenommen] = @Parm_Abgenommen
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[Statistik] = @Parm_Statistik
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelAufmasse')
  DROP PROCEDURE [dbo].[up_Sel_BelAufmasse]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelAufmasse]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[AmNr]
    ,[Datum]
    ,[Abgenommen]
    ,[Bez1]
    ,[Statistik]
  FROM [dbo].[ut_BelAufmasse]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelAufmasse')
  DROP PROCEDURE [dbo].[up_Del_BelAufmasse]
GO
CREATE PROCEDURE [dbo].[up_Del_BelAufmasse]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelAufmasse] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelAufmasse')
  DROP PROCEDURE [dbo].[up_Reorg_BelAufmasse]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelAufmasse]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelAufmasse] 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_BelAufmasse] 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_BelAufmasse] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelAufmasse')
  DROP PROCEDURE [dbo].[up_Append_BelAufmasse]
GO
CREATE PROCEDURE [dbo].[up_Append_BelAufmasse]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_AmNr integer
 ,@Parm_Datum datetime
 ,@Parm_Abgenommen tinyint
 ,@Parm_Bez1 char(60)
 ,@Parm_Statistik tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelAufmasse])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelAufmasse] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[AmNr]
   ,[Datum]
   ,[Abgenommen]
   ,[Bez1]
   ,[Statistik]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,@Parm_AmNr
   ,@Parm_Datum
   ,@Parm_Abgenommen
   ,RTRIM(@Parm_Bez1)
   ,@Parm_Statistik
       )
  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_BelAufmasse')
  DROP PROCEDURE [dbo].[up_TrSel_BelAufmasse]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelAufmasse]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([Bez1]))
    AS Data
  FROM     [dbo].[ut_BelAufmasse]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelAufmasse_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_First_BelAufmasse_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelAufmasse_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelAufmasse_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelAufmasse_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelAufmasse_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelAufmasse_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelAufmasse_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelAufmasse_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelAufmasse_Key00]
GO

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

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

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

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

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

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

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

