------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_RessVerbrauch')
BEGIN
CREATE TABLE [dbo].[ut_RessVerbrauch]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nr] char(4) DEFAULT ' '
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[LfdNr] integer DEFAULT 0
  ,[Text1] char(50) DEFAULT ' '
  ,[PostenArt] integer DEFAULT 0
  ,[Menge] float DEFAULT 0
  ,[MSumme] float DEFAULT 0
  ,[Betrag] float DEFAULT 0
  ,[BSumme] float DEFAULT 0
  ,[Konto] char(8) DEFAULT ' '
  ,[Kst] char(8) DEFAULT ' '
  ,[PrjNr] char(8) DEFAULT ' '
  ,[Gebucht] tinyint DEFAULT 0
  CONSTRAINT [Pk_RessVerbrauch]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RessVerbrauch_KeyDel')
  DROP TRIGGER [dbo].[tr_RessVerbrauch_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RessVerbrauch_KeyUpd')
  DROP TRIGGER [dbo].[tr_RessVerbrauch_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_RessVerbrauch_Keys')
  DROP TABLE [dbo].[ut_RessVerbrauch_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RessVerbrauch' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [Nr] char(4) 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_RessVerbrauch' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] 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_RessVerbrauch' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [LfdNr] 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_RessVerbrauch' and sc.[name] = 'Text1')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [Text1] char(50) 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_RessVerbrauch' and sc.[name] = 'PostenArt')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [PostenArt] 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_RessVerbrauch' and sc.[name] = 'Menge')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [Menge] 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_RessVerbrauch' and sc.[name] = 'MSumme')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [MSumme] 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_RessVerbrauch' and sc.[name] = 'Betrag')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [Betrag] 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_RessVerbrauch' and sc.[name] = 'BSumme')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [BSumme] 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_RessVerbrauch' and sc.[name] = 'Konto')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [Konto] 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_RessVerbrauch' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [Kst] 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_RessVerbrauch' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] 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_RessVerbrauch' and sc.[name] = 'Gebucht')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ADD [Gebucht] tinyint NOT NULL DEFAULT 0
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RessVerbrauch' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ALTER COLUMN [Nr] char(4)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RessVerbrauch' and sc.[name] = 'Text1')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ALTER COLUMN [Text1] char(50)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RessVerbrauch' and sc.[name] = 'Konto')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ALTER COLUMN [Konto] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RessVerbrauch' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ALTER COLUMN [Kst] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RessVerbrauch' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_RessVerbrauch] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'RessVerbrauch')
  DROP VIEW [dbo].[RessVerbrauch]
GO
CREATE VIEW [dbo].[RessVerbrauch] AS
  SELECT * FROM [dbo].[ut_RessVerbrauch]
GO
CREATE TABLE [dbo].[ut_RessVerbrauch_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(28) NOT NULL
  CONSTRAINT [Pk_ut_RessVerbrauch_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_RessVerbrauch_KeyDel] ON [dbo].[ut_RessVerbrauch]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_RessVerbrauch_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_RessVerbrauch_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(4),[Nr])+CONVERT(char(10),[Datum],120)+STR([LfdNr],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_RessVerbrauch_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(4),[Nr])+CONVERT(char(10),[Datum],120)+STR([LfdNr],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_RessVerbrauch')
  DROP PROCEDURE [dbo].[up_Add_RessVerbrauch]
GO
CREATE PROCEDURE [dbo].[up_Add_RessVerbrauch] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_RessVerbrauch])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_RessVerbrauch] (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_RessVerbrauch')
  DROP PROCEDURE [dbo].[up_Upd_RessVerbrauch]
GO
CREATE PROCEDURE [dbo].[up_Upd_RessVerbrauch]
  @RI integer
 ,@Parm_Nr char(4)
 ,@Parm_Datum datetime
 ,@Parm_LfdNr integer
 ,@Parm_Text1 char(50)
 ,@Parm_PostenArt integer
 ,@Parm_Menge float
 ,@Parm_MSumme float
 ,@Parm_Betrag float
 ,@Parm_BSumme float
 ,@Parm_Konto char(8)
 ,@Parm_Kst char(8)
 ,@Parm_PrjNr char(8)
 ,@Parm_Gebucht tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_RessVerbrauch] SET
     [Nr] = RTRIM(@Parm_Nr)
    ,[Datum] = @Parm_Datum
    ,[LfdNr] = @Parm_LfdNr
    ,[Text1] = RTRIM(@Parm_Text1)
    ,[PostenArt] = @Parm_PostenArt
    ,[Menge] = @Parm_Menge
    ,[MSumme] = @Parm_MSumme
    ,[Betrag] = @Parm_Betrag
    ,[BSumme] = @Parm_BSumme
    ,[Konto] = RTRIM(@Parm_Konto)
    ,[Kst] = RTRIM(@Parm_Kst)
    ,[PrjNr] = RTRIM(@Parm_PrjNr)
    ,[Gebucht] = @Parm_Gebucht
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_RessVerbrauch')
  DROP PROCEDURE [dbo].[up_Sel_RessVerbrauch]
GO
CREATE PROCEDURE [dbo].[up_Sel_RessVerbrauch]
  @RI integer
AS
SELECT
     [Nr]
    ,[Datum]
    ,[LfdNr]
    ,[Text1]
    ,[PostenArt]
    ,[Menge]
    ,[MSumme]
    ,[Betrag]
    ,[BSumme]
    ,[Konto]
    ,[Kst]
    ,[PrjNr]
    ,[Gebucht]
  FROM [dbo].[ut_RessVerbrauch]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_RessVerbrauch')
  DROP PROCEDURE [dbo].[up_Del_RessVerbrauch]
GO
CREATE PROCEDURE [dbo].[up_Del_RessVerbrauch]
  @RI integer
AS
  DELETE FROM [dbo].[ut_RessVerbrauch] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_RessVerbrauch')
  DROP PROCEDURE [dbo].[up_Reorg_RessVerbrauch]
GO
CREATE PROCEDURE [dbo].[up_Reorg_RessVerbrauch]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_RessVerbrauch] 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_RessVerbrauch] 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_RessVerbrauch] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_RessVerbrauch')
  DROP PROCEDURE [dbo].[up_Append_RessVerbrauch]
GO
CREATE PROCEDURE [dbo].[up_Append_RessVerbrauch]
  @Parm_Nr char(4)
 ,@Parm_Datum datetime
 ,@Parm_LfdNr integer
 ,@Parm_Text1 char(50)
 ,@Parm_PostenArt integer
 ,@Parm_Menge float
 ,@Parm_MSumme float
 ,@Parm_Betrag float
 ,@Parm_BSumme float
 ,@Parm_Konto char(8)
 ,@Parm_Kst char(8)
 ,@Parm_PrjNr char(8)
 ,@Parm_Gebucht tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_RessVerbrauch])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_RessVerbrauch] (
    RecId
   ,[Nr]
   ,[Datum]
   ,[LfdNr]
   ,[Text1]
   ,[PostenArt]
   ,[Menge]
   ,[MSumme]
   ,[Betrag]
   ,[BSumme]
   ,[Konto]
   ,[Kst]
   ,[PrjNr]
   ,[Gebucht]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Nr)
   ,@Parm_Datum
   ,@Parm_LfdNr
   ,RTRIM(@Parm_Text1)
   ,@Parm_PostenArt
   ,@Parm_Menge
   ,@Parm_MSumme
   ,@Parm_Betrag
   ,@Parm_BSumme
   ,RTRIM(@Parm_Konto)
   ,RTRIM(@Parm_Kst)
   ,RTRIM(@Parm_PrjNr)
   ,@Parm_Gebucht
       )
  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_RessVerbrauch')
  DROP PROCEDURE [dbo].[up_TrSel_RessVerbrauch]
GO
CREATE PROCEDURE [dbo].[up_TrSel_RessVerbrauch]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nr]))
      +LTRIM(RTRIM([Text1]))
      +LTRIM(RTRIM([Konto]))
      +LTRIM(RTRIM([Kst]))
      +LTRIM(RTRIM([PrjNr]))
    AS Data
  FROM     [dbo].[ut_RessVerbrauch]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_RessVerbrauch_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_First_RessVerbrauch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_Last_RessVerbrauch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_Prior_RessVerbrauch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_Next_RessVerbrauch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_RessVerbrauch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_Next50_RessVerbrauch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_FindL_RessVerbrauch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_RessVerbrauch_Key00')
  DROP PROCEDURE [dbo].[up_FindF_RessVerbrauch_Key00]
GO

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

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

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

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

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

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

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

