------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelegMwstSummen')
BEGIN
CREATE TABLE [dbo].[ut_BelegMwstSummen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[Bereich] char(1) DEFAULT ' '
  ,[MWStKateg] char(3) DEFAULT ' '
  ,[Bez1] char(50) DEFAULT ' '
  ,[Bez2] char(50) DEFAULT ' '
  ,[Satz] float DEFAULT 0
  ,[DrkNull] tinyint DEFAULT 0
  ,[Netto] float DEFAULT 0
  ,[MWSt] float DEFAULT 0
  ,[Brutto] float DEFAULT 0
  CONSTRAINT [Pk_BelegMwstSummen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegMwstSummen_KeyDel')
  DROP TRIGGER [dbo].[tr_BelegMwstSummen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegMwstSummen_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelegMwstSummen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelegMwstSummen_Keys')
  DROP TABLE [dbo].[ut_BelegMwstSummen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstSummen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] 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_BelegMwstSummen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] 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_BelegMwstSummen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] 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_BelegMwstSummen' and sc.[name] = 'Bereich')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [Bereich] char(1) 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_BelegMwstSummen' and sc.[name] = 'MWStKateg')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [MWStKateg] char(3) 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_BelegMwstSummen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [Bez1] 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_BelegMwstSummen' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [Bez2] 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_BelegMwstSummen' and sc.[name] = 'Satz')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [Satz] 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_BelegMwstSummen' and sc.[name] = 'DrkNull')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [DrkNull] 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_BelegMwstSummen' and sc.[name] = 'Netto')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [Netto] 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_BelegMwstSummen' and sc.[name] = 'MWSt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [MWSt] 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_BelegMwstSummen' and sc.[name] = 'Brutto')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ADD [Brutto] 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_BelegMwstSummen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] 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_BelegMwstSummen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] 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_BelegMwstSummen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ALTER COLUMN [BelNr] char(8)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstSummen' and sc.[name] = 'Bereich')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ALTER COLUMN [Bereich] char(1)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstSummen' and sc.[name] = 'MWStKateg')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ALTER COLUMN [MWStKateg] char(3)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstSummen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ALTER COLUMN [Bez1] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstSummen' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstSummen] ALTER COLUMN [Bez2] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelegMwstSummen')
  DROP VIEW [dbo].[BelegMwstSummen]
GO
CREATE VIEW [dbo].[BelegMwstSummen] AS
  SELECT * FROM [dbo].[ut_BelegMwstSummen]
GO
CREATE TABLE [dbo].[ut_BelegMwstSummen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(32) NOT NULL
  CONSTRAINT [Pk_ut_BelegMwstSummen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelegMwstSummen_KeyDel] ON [dbo].[ut_BelegMwstSummen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelegMwstSummen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelegMwstSummen_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(1),[Bereich])+convert(char(3),[MWStKateg])+STR(RecId,10))

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelegMwstSummen')
  DROP PROCEDURE [dbo].[up_Add_BelegMwstSummen]
GO
CREATE PROCEDURE [dbo].[up_Add_BelegMwstSummen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelegMwstSummen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelegMwstSummen] (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_BelegMwstSummen')
  DROP PROCEDURE [dbo].[up_Upd_BelegMwstSummen]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelegMwstSummen]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_Bereich char(1)
 ,@Parm_MWStKateg char(3)
 ,@Parm_Bez1 char(50)
 ,@Parm_Bez2 char(50)
 ,@Parm_Satz float
 ,@Parm_DrkNull tinyint
 ,@Parm_Netto float
 ,@Parm_MWSt float
 ,@Parm_Brutto float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelegMwstSummen] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[Bereich] = RTRIM(@Parm_Bereich)
    ,[MWStKateg] = RTRIM(@Parm_MWStKateg)
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[Bez2] = RTRIM(@Parm_Bez2)
    ,[Satz] = @Parm_Satz
    ,[DrkNull] = @Parm_DrkNull
    ,[Netto] = @Parm_Netto
    ,[MWSt] = @Parm_MWSt
    ,[Brutto] = @Parm_Brutto
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelegMwstSummen')
  DROP PROCEDURE [dbo].[up_Sel_BelegMwstSummen]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelegMwstSummen]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[Bereich]
    ,[MWStKateg]
    ,[Bez1]
    ,[Bez2]
    ,[Satz]
    ,[DrkNull]
    ,[Netto]
    ,[MWSt]
    ,[Brutto]
  FROM [dbo].[ut_BelegMwstSummen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelegMwstSummen')
  DROP PROCEDURE [dbo].[up_Del_BelegMwstSummen]
GO
CREATE PROCEDURE [dbo].[up_Del_BelegMwstSummen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelegMwstSummen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelegMwstSummen')
  DROP PROCEDURE [dbo].[up_Reorg_BelegMwstSummen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelegMwstSummen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelegMwstSummen] 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_BelegMwstSummen] 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_BelegMwstSummen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelegMwstSummen')
  DROP PROCEDURE [dbo].[up_Append_BelegMwstSummen]
GO
CREATE PROCEDURE [dbo].[up_Append_BelegMwstSummen]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_Bereich char(1)
 ,@Parm_MWStKateg char(3)
 ,@Parm_Bez1 char(50)
 ,@Parm_Bez2 char(50)
 ,@Parm_Satz float
 ,@Parm_DrkNull tinyint
 ,@Parm_Netto float
 ,@Parm_MWSt float
 ,@Parm_Brutto float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelegMwstSummen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelegMwstSummen] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[Bereich]
   ,[MWStKateg]
   ,[Bez1]
   ,[Bez2]
   ,[Satz]
   ,[DrkNull]
   ,[Netto]
   ,[MWSt]
   ,[Brutto]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_Bereich)
   ,RTRIM(@Parm_MWStKateg)
   ,RTRIM(@Parm_Bez1)
   ,RTRIM(@Parm_Bez2)
   ,@Parm_Satz
   ,@Parm_DrkNull
   ,@Parm_Netto
   ,@Parm_MWSt
   ,@Parm_Brutto
       )
  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_BelegMwstSummen')
  DROP PROCEDURE [dbo].[up_TrSel_BelegMwstSummen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelegMwstSummen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([Bereich]))
      +LTRIM(RTRIM([MWStKateg]))
      +LTRIM(RTRIM([Bez1]))
      +LTRIM(RTRIM([Bez2]))
    AS Data
  FROM     [dbo].[ut_BelegMwstSummen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelegMwstSummen_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_First_BelegMwstSummen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelegMwstSummen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelegMwstSummen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelegMwstSummen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelegMwstSummen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelegMwstSummen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelegMwstSummen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegMwstSummen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelegMwstSummen_Key00]
GO

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

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

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

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

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

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

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

