------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelPosMwstSplit')
BEGIN
CREATE TABLE [dbo].[ut_BelPosMwstSplit]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[RefKey] char(6) DEFAULT ' '
  ,[Bereich] char(1) DEFAULT ' '
  ,[KzMwst] char(3) DEFAULT ' '
  ,[BuchGrp] char(10) DEFAULT ' '
  ,[Betrag] float DEFAULT 0
  ,[Lohn] float DEFAULT 0
  ,[Mat] float DEFAULT 0
  ,[Fremd] float DEFAULT 0
  ,[Rabatt] float DEFAULT 0
  ,[Sonst] float DEFAULT 0
  ,[MWSt] float DEFAULT 0
  ,[Brutto] float DEFAULT 0
  CONSTRAINT [Pk_BelPosMwstSplit]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelPosMwstSplit_KeyDel')
  DROP TRIGGER [dbo].[tr_BelPosMwstSplit_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelPosMwstSplit_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelPosMwstSplit_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelPosMwstSplit_Keys')
  DROP TABLE [dbo].[ut_BelPosMwstSplit_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosMwstSplit' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [RefKey] char(6) 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_BelPosMwstSplit' and sc.[name] = 'Bereich')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'KzMwst')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [KzMwst] 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_BelPosMwstSplit' and sc.[name] = 'BuchGrp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [BuchGrp] char(10) 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_BelPosMwstSplit' and sc.[name] = 'Betrag')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'Lohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [Lohn] 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_BelPosMwstSplit' and sc.[name] = 'Mat')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [Mat] 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_BelPosMwstSplit' and sc.[name] = 'Fremd')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [Fremd] 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_BelPosMwstSplit' and sc.[name] = 'Rabatt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [Rabatt] 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_BelPosMwstSplit' and sc.[name] = 'Sonst')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ADD [Sonst] 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_BelPosMwstSplit' and sc.[name] = 'MWSt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'Brutto')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ALTER COLUMN [BelNr] char(8)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosMwstSplit' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ALTER COLUMN [RefKey] char(6)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosMwstSplit' and sc.[name] = 'Bereich')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit' and sc.[name] = 'KzMwst')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ALTER COLUMN [KzMwst] char(3)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosMwstSplit' and sc.[name] = 'BuchGrp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosMwstSplit] ALTER COLUMN [BuchGrp] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelPosMwstSplit')
  DROP VIEW [dbo].[BelPosMwstSplit]
GO
CREATE VIEW [dbo].[BelPosMwstSplit] AS
  SELECT * FROM [dbo].[ut_BelPosMwstSplit]
GO
CREATE TABLE [dbo].[ut_BelPosMwstSplit_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(48) NOT NULL
  CONSTRAINT [Pk_ut_BelPosMwstSplit_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelPosMwstSplit_KeyDel] ON [dbo].[ut_BelPosMwstSplit]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelPosMwstSplit_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelPosMwstSplit_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(6),[RefKey])+convert(char(1),[Bereich])+convert(char(3),[KzMwst])+convert(char(10),[BuchGrp])+STR(RecId,10))

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelPosMwstSplit')
  DROP PROCEDURE [dbo].[up_Add_BelPosMwstSplit]
GO
CREATE PROCEDURE [dbo].[up_Add_BelPosMwstSplit] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelPosMwstSplit])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelPosMwstSplit] (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_BelPosMwstSplit')
  DROP PROCEDURE [dbo].[up_Upd_BelPosMwstSplit]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelPosMwstSplit]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_Bereich char(1)
 ,@Parm_KzMwst char(3)
 ,@Parm_BuchGrp char(10)
 ,@Parm_Betrag float
 ,@Parm_Lohn float
 ,@Parm_Mat float
 ,@Parm_Fremd float
 ,@Parm_Rabatt float
 ,@Parm_Sonst float
 ,@Parm_MWSt float
 ,@Parm_Brutto float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelPosMwstSplit] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[RefKey] = RTRIM(@Parm_RefKey)
    ,[Bereich] = RTRIM(@Parm_Bereich)
    ,[KzMwst] = RTRIM(@Parm_KzMwst)
    ,[BuchGrp] = RTRIM(@Parm_BuchGrp)
    ,[Betrag] = @Parm_Betrag
    ,[Lohn] = @Parm_Lohn
    ,[Mat] = @Parm_Mat
    ,[Fremd] = @Parm_Fremd
    ,[Rabatt] = @Parm_Rabatt
    ,[Sonst] = @Parm_Sonst
    ,[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_BelPosMwstSplit')
  DROP PROCEDURE [dbo].[up_Sel_BelPosMwstSplit]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelPosMwstSplit]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[RefKey]
    ,[Bereich]
    ,[KzMwst]
    ,[BuchGrp]
    ,[Betrag]
    ,[Lohn]
    ,[Mat]
    ,[Fremd]
    ,[Rabatt]
    ,[Sonst]
    ,[MWSt]
    ,[Brutto]
  FROM [dbo].[ut_BelPosMwstSplit]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelPosMwstSplit')
  DROP PROCEDURE [dbo].[up_Del_BelPosMwstSplit]
GO
CREATE PROCEDURE [dbo].[up_Del_BelPosMwstSplit]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelPosMwstSplit] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelPosMwstSplit')
  DROP PROCEDURE [dbo].[up_Reorg_BelPosMwstSplit]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelPosMwstSplit]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelPosMwstSplit] 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_BelPosMwstSplit] 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_BelPosMwstSplit] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelPosMwstSplit')
  DROP PROCEDURE [dbo].[up_Append_BelPosMwstSplit]
GO
CREATE PROCEDURE [dbo].[up_Append_BelPosMwstSplit]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_Bereich char(1)
 ,@Parm_KzMwst char(3)
 ,@Parm_BuchGrp char(10)
 ,@Parm_Betrag float
 ,@Parm_Lohn float
 ,@Parm_Mat float
 ,@Parm_Fremd float
 ,@Parm_Rabatt float
 ,@Parm_Sonst float
 ,@Parm_MWSt float
 ,@Parm_Brutto float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelPosMwstSplit])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelPosMwstSplit] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[RefKey]
   ,[Bereich]
   ,[KzMwst]
   ,[BuchGrp]
   ,[Betrag]
   ,[Lohn]
   ,[Mat]
   ,[Fremd]
   ,[Rabatt]
   ,[Sonst]
   ,[MWSt]
   ,[Brutto]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_RefKey)
   ,RTRIM(@Parm_Bereich)
   ,RTRIM(@Parm_KzMwst)
   ,RTRIM(@Parm_BuchGrp)
   ,@Parm_Betrag
   ,@Parm_Lohn
   ,@Parm_Mat
   ,@Parm_Fremd
   ,@Parm_Rabatt
   ,@Parm_Sonst
   ,@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_BelPosMwstSplit')
  DROP PROCEDURE [dbo].[up_TrSel_BelPosMwstSplit]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelPosMwstSplit]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([RefKey]))
      +LTRIM(RTRIM([Bereich]))
      +LTRIM(RTRIM([KzMwst]))
      +LTRIM(RTRIM([BuchGrp]))
    AS Data
  FROM     [dbo].[ut_BelPosMwstSplit]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelPosMwstSplit_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_First_BelPosMwstSplit_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelPosMwstSplit_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelPosMwstSplit_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelPosMwstSplit_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelPosMwstSplit_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelPosMwstSplit_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelPosMwstSplit_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPosMwstSplit_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelPosMwstSplit_Key00]
GO

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

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

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

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

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

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

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

