------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelegArchiv')
BEGIN
CREATE TABLE [dbo].[ut_BelegArchiv]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[Type] integer DEFAULT 0
  ,[BlobId] integer DEFAULT 0
  ,[Manuell] tinyint DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Uhrzeit] char(5) DEFAULT ' '
  ,[BelDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Netto] float DEFAULT 0
  ,[BshLogin] char(20) DEFAULT ' '
  ,[WinLogin] char(128) DEFAULT ' '
  ,[Station] char(128) DEFAULT ' '
  ,[FileName] char(250) DEFAULT ' '
  ,[Bemerkung] char(80) DEFAULT ' '
  ,[FileStamp] char(32) DEFAULT ' '
  CONSTRAINT [Pk_BelegArchiv]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegArchiv_KeyDel')
  DROP TRIGGER [dbo].[tr_BelegArchiv_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegArchiv_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelegArchiv_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelegArchiv_Keys')
  DROP TABLE [dbo].[ut_BelegArchiv_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'Type')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [Type] 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_BelegArchiv' and sc.[name] = 'BlobId')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [BlobId] 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_BelegArchiv' and sc.[name] = 'Manuell')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [Manuell] 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_BelegArchiv' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [Uhrzeit] char(5) 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_BelegArchiv' and sc.[name] = 'BelDatum')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [BelDatum] 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_BelegArchiv' and sc.[name] = 'Netto')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'BshLogin')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [BshLogin] 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_BelegArchiv' and sc.[name] = 'WinLogin')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [WinLogin] char(128) 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_BelegArchiv' and sc.[name] = 'Station')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [Station] char(128) 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_BelegArchiv' and sc.[name] = 'FileName')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [FileName] char(250) 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_BelegArchiv' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [Bemerkung] char(80) 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_BelegArchiv' and sc.[name] = 'FileStamp')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ADD [FileStamp] char(32) 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_BelegArchiv' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] 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_BelegArchiv' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [BelNr] char(8)
END
GO
IF 5 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [Uhrzeit] char(5)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'BshLogin')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [BshLogin] char(20)
END
GO
IF 128 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'WinLogin')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [WinLogin] char(128)
END
GO
IF 128 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'Station')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [Station] char(128)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'FileName')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [FileName] char(250)
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [Bemerkung] char(80)
END
GO
IF 32 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegArchiv' and sc.[name] = 'FileStamp')
BEGIN
  ALTER TABLE [dbo].[ut_BelegArchiv] ALTER COLUMN [FileStamp] char(32)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelegArchiv')
  DROP VIEW [dbo].[BelegArchiv]
GO
CREATE VIEW [dbo].[BelegArchiv] AS
  SELECT * FROM [dbo].[ut_BelegArchiv]
GO
CREATE TABLE [dbo].[ut_BelegArchiv_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(32) NOT NULL
  ,[Key01_Filter] TINYINT NOT NULL
  ,[Key01_Value] CHAR(32) NOT NULL
  ,[Key02_Value] CHAR(43) NOT NULL
  CONSTRAINT [Pk_ut_BelegArchiv_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelegArchiv_KeyDel] ON [dbo].[ut_BelegArchiv]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelegArchiv_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelegArchiv_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Filter],[Key01_Value],[Key02_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([LfdNr],4,0)+STR(RecId,10))
     ,CASE WHEN 1 =  Manuell THEN 1 ELSE 0 END
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([LfdNr],4,0)+STR(RecId,10))
     ,UPPER(CONVERT(char(10),[Datum],120)+convert(char(5),[Uhrzeit])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelegArchiv_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([LfdNr],4,0)+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([LfdNr],4,0)+STR([RecId],10))
   ,[Key01_Filter] = CASE WHEN 1 =  Manuell THEN 1 ELSE 0 END
   ,[Key02_Value] = UPPER(CONVERT(char(10),[Datum],120)+convert(char(5),[Uhrzeit])+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelegArchiv')
  DROP PROCEDURE [dbo].[up_Add_BelegArchiv]
GO
CREATE PROCEDURE [dbo].[up_Add_BelegArchiv] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelegArchiv])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelegArchiv] (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_BelegArchiv')
  DROP PROCEDURE [dbo].[up_Upd_BelegArchiv]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelegArchiv]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_LfdNr integer
 ,@Parm_Type integer
 ,@Parm_BlobId integer
 ,@Parm_Manuell tinyint
 ,@Parm_Datum datetime
 ,@Parm_Uhrzeit char(5)
 ,@Parm_BelDatum datetime
 ,@Parm_Netto float
 ,@Parm_BshLogin char(20)
 ,@Parm_WinLogin char(128)
 ,@Parm_Station char(128)
 ,@Parm_FileName char(250)
 ,@Parm_Bemerkung char(80)
 ,@Parm_FileStamp char(32)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelegArchiv] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[LfdNr] = @Parm_LfdNr
    ,[Type] = @Parm_Type
    ,[BlobId] = @Parm_BlobId
    ,[Manuell] = @Parm_Manuell
    ,[Datum] = @Parm_Datum
    ,[Uhrzeit] = RTRIM(@Parm_Uhrzeit)
    ,[BelDatum] = @Parm_BelDatum
    ,[Netto] = @Parm_Netto
    ,[BshLogin] = RTRIM(@Parm_BshLogin)
    ,[WinLogin] = RTRIM(@Parm_WinLogin)
    ,[Station] = RTRIM(@Parm_Station)
    ,[FileName] = RTRIM(@Parm_FileName)
    ,[Bemerkung] = RTRIM(@Parm_Bemerkung)
    ,[FileStamp] = RTRIM(@Parm_FileStamp)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelegArchiv')
  DROP PROCEDURE [dbo].[up_Sel_BelegArchiv]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelegArchiv]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[LfdNr]
    ,[Type]
    ,[BlobId]
    ,[Manuell]
    ,[Datum]
    ,[Uhrzeit]
    ,[BelDatum]
    ,[Netto]
    ,[BshLogin]
    ,[WinLogin]
    ,[Station]
    ,[FileName]
    ,[Bemerkung]
    ,[FileStamp]
  FROM [dbo].[ut_BelegArchiv]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelegArchiv')
  DROP PROCEDURE [dbo].[up_Del_BelegArchiv]
GO
CREATE PROCEDURE [dbo].[up_Del_BelegArchiv]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelegArchiv] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelegArchiv')
  DROP PROCEDURE [dbo].[up_Reorg_BelegArchiv]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelegArchiv]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelegArchiv] 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_BelegArchiv] 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_BelegArchiv] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelegArchiv')
  DROP PROCEDURE [dbo].[up_Append_BelegArchiv]
GO
CREATE PROCEDURE [dbo].[up_Append_BelegArchiv]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_LfdNr integer
 ,@Parm_Type integer
 ,@Parm_BlobId integer
 ,@Parm_Manuell tinyint
 ,@Parm_Datum datetime
 ,@Parm_Uhrzeit char(5)
 ,@Parm_BelDatum datetime
 ,@Parm_Netto float
 ,@Parm_BshLogin char(20)
 ,@Parm_WinLogin char(128)
 ,@Parm_Station char(128)
 ,@Parm_FileName char(250)
 ,@Parm_Bemerkung char(80)
 ,@Parm_FileStamp char(32)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelegArchiv])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelegArchiv] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[LfdNr]
   ,[Type]
   ,[BlobId]
   ,[Manuell]
   ,[Datum]
   ,[Uhrzeit]
   ,[BelDatum]
   ,[Netto]
   ,[BshLogin]
   ,[WinLogin]
   ,[Station]
   ,[FileName]
   ,[Bemerkung]
   ,[FileStamp]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,@Parm_LfdNr
   ,@Parm_Type
   ,@Parm_BlobId
   ,@Parm_Manuell
   ,@Parm_Datum
   ,RTRIM(@Parm_Uhrzeit)
   ,@Parm_BelDatum
   ,@Parm_Netto
   ,RTRIM(@Parm_BshLogin)
   ,RTRIM(@Parm_WinLogin)
   ,RTRIM(@Parm_Station)
   ,RTRIM(@Parm_FileName)
   ,RTRIM(@Parm_Bemerkung)
   ,RTRIM(@Parm_FileStamp)
       )
  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_BelegArchiv')
  DROP PROCEDURE [dbo].[up_TrSel_BelegArchiv]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelegArchiv]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([Uhrzeit]))
      +LTRIM(RTRIM([BshLogin]))
      +LTRIM(RTRIM([WinLogin]))
      +LTRIM(RTRIM([Station]))
      +LTRIM(RTRIM([FileName]))
      +LTRIM(RTRIM([Bemerkung]))
      +LTRIM(RTRIM([FileStamp]))
    AS Data
  FROM     [dbo].[ut_BelegArchiv]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelegArchiv_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_First_BelegArchiv_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelegArchiv_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelegArchiv_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelegArchiv_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelegArchiv_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelegArchiv_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelegArchiv_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegArchiv_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelegArchiv_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_BelegArchiv_Keys]
( [Key01_Filter],[Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_First_BelegArchiv_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelegArchiv_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelegArchiv_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_Next_BelegArchiv_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_BelegArchiv_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_Next50_BelegArchiv_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelegArchiv_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegArchiv_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelegArchiv_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_BelegArchiv_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelegArchiv_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelegArchiv_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_BelegArchiv_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_BelegArchiv_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_BelegArchiv_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_BelegArchiv_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_BelegArchiv_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_BelegArchiv_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_BelegArchiv_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_BelegArchiv_Key01] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key01_Value] AS Key_Value FROM [dbo].[ut_BelegArchiv_Keys]
      WHERE [Key01_Filter] = 1 AND [Key01_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key01_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_BelegArchiv_Key01] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key01_Value] DESC
GO

CREATE INDEX [Ik_Key02] ON [dbo].[ut_BelegArchiv_Keys]
( [Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_First_BelegArchiv_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_Last_BelegArchiv_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BelegArchiv_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_Next_BelegArchiv_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_BelegArchiv_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_Next50_BelegArchiv_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_FindL_BelegArchiv_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegArchiv_Key02')
  DROP PROCEDURE [dbo].[up_FindF_BelegArchiv_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_BelegArchiv_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys] ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelegArchiv_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys] ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelegArchiv_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_BelegArchiv_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelegArchiv_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_BelegArchiv_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

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

CREATE PROCEDURE [dbo].[up_FindF_BelegArchiv_Key02] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key02_Value] AS Key_Value FROM [dbo].[ut_BelegArchiv_Keys]
      WHERE [Key02_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key02_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_BelegArchiv_Key02] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelegArchiv_Keys]
  WHERE [Key02_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key02_Value] DESC
GO

