------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelBauabschnitte')
BEGIN
CREATE TABLE [dbo].[ut_BelBauabschnitte]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[BaRef] char(20) DEFAULT ' '
  ,[Fertig] tinyint DEFAULT 0
  ,[Bez1] char(60) DEFAULT ' '
  ,[Bez2] char(60) DEFAULT ' '
  CONSTRAINT [Pk_BelBauabschnitte]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelBauabschnitte_KeyDel')
  DROP TRIGGER [dbo].[tr_BelBauabschnitte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelBauabschnitte_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelBauabschnitte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelBauabschnitte_Keys')
  DROP TABLE [dbo].[ut_BelBauabschnitte_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelBauabschnitte' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte' and sc.[name] = 'BaRef')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] ADD [BaRef] 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_BelBauabschnitte' and sc.[name] = 'Fertig')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] ADD [Fertig] 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_BelBauabschnitte' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] ADD [Bez2] char(60) NOT NULL DEFAULT ' '
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelBauabschnitte' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte' and sc.[name] = 'BaRef')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] ALTER COLUMN [BaRef] char(20)
END
GO
IF 60 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelBauabschnitte' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] ALTER COLUMN [Bez1] char(60)
END
GO
IF 60 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelBauabschnitte' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_BelBauabschnitte] ALTER COLUMN [Bez2] char(60)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelBauabschnitte')
  DROP VIEW [dbo].[BelBauabschnitte]
GO
CREATE VIEW [dbo].[BelBauabschnitte] AS
  SELECT * FROM [dbo].[ut_BelBauabschnitte]
GO
CREATE TABLE [dbo].[ut_BelBauabschnitte_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(48) NOT NULL
  CONSTRAINT [Pk_ut_BelBauabschnitte_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelBauabschnitte_KeyDel] ON [dbo].[ut_BelBauabschnitte]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelBauabschnitte_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelBauabschnitte')
  DROP PROCEDURE [dbo].[up_Add_BelBauabschnitte]
GO
CREATE PROCEDURE [dbo].[up_Add_BelBauabschnitte] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelBauabschnitte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelBauabschnitte] (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_BelBauabschnitte')
  DROP PROCEDURE [dbo].[up_Upd_BelBauabschnitte]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelBauabschnitte]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_BaRef char(20)
 ,@Parm_Fertig tinyint
 ,@Parm_Bez1 char(60)
 ,@Parm_Bez2 char(60)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelBauabschnitte] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[BaRef] = RTRIM(@Parm_BaRef)
    ,[Fertig] = @Parm_Fertig
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[Bez2] = RTRIM(@Parm_Bez2)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelBauabschnitte')
  DROP PROCEDURE [dbo].[up_Sel_BelBauabschnitte]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelBauabschnitte]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[BaRef]
    ,[Fertig]
    ,[Bez1]
    ,[Bez2]
  FROM [dbo].[ut_BelBauabschnitte]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelBauabschnitte')
  DROP PROCEDURE [dbo].[up_Del_BelBauabschnitte]
GO
CREATE PROCEDURE [dbo].[up_Del_BelBauabschnitte]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelBauabschnitte] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelBauabschnitte')
  DROP PROCEDURE [dbo].[up_Reorg_BelBauabschnitte]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelBauabschnitte]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelBauabschnitte] 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_BelBauabschnitte] 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_BelBauabschnitte] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelBauabschnitte')
  DROP PROCEDURE [dbo].[up_Append_BelBauabschnitte]
GO
CREATE PROCEDURE [dbo].[up_Append_BelBauabschnitte]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_BaRef char(20)
 ,@Parm_Fertig tinyint
 ,@Parm_Bez1 char(60)
 ,@Parm_Bez2 char(60)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelBauabschnitte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelBauabschnitte] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[BaRef]
   ,[Fertig]
   ,[Bez1]
   ,[Bez2]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_BaRef)
   ,@Parm_Fertig
   ,RTRIM(@Parm_Bez1)
   ,RTRIM(@Parm_Bez2)
       )
  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_BelBauabschnitte')
  DROP PROCEDURE [dbo].[up_TrSel_BelBauabschnitte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelBauabschnitte]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([BaRef]))
      +LTRIM(RTRIM([Bez1]))
      +LTRIM(RTRIM([Bez2]))
    AS Data
  FROM     [dbo].[ut_BelBauabschnitte]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelBauabschnitte_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_First_BelBauabschnitte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelBauabschnitte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelBauabschnitte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelBauabschnitte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelBauabschnitte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelBauabschnitte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelBauabschnitte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelBauabschnitte_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelBauabschnitte_Key00]
GO

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

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

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

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

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

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

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

