------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Erloesgruppen')
BEGIN
CREATE TABLE [dbo].[ut_Erloesgruppen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nr] char(3) DEFAULT ' '
  ,[Zeile] char(2) DEFAULT ' '
  ,[EgText] char(40) DEFAULT ' '
  ,[KtoHaben] char(8) DEFAULT ' '
  ,[KtoSoll] char(8) DEFAULT ' '
  ,[BetrUmkehr] tinyint DEFAULT 0
  ,[KtoUmkehr] tinyint DEFAULT 0
  ,[Mit_Kst] tinyint DEFAULT 0
  ,[Mit_Ktr] tinyint DEFAULT 0
  ,[UstSchl] integer DEFAULT 0
  ,[BetrSchl] integer DEFAULT 0
  ,[Betrag] integer DEFAULT 0
  CONSTRAINT [Pk_Erloesgruppen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Erloesgruppen_KeyDel')
  DROP TRIGGER [dbo].[tr_Erloesgruppen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Erloesgruppen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Erloesgruppen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Erloesgruppen_Keys')
  DROP TABLE [dbo].[ut_Erloesgruppen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Erloesgruppen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [Nr] 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_Erloesgruppen' and sc.[name] = 'Zeile')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [Zeile] 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_Erloesgruppen' and sc.[name] = 'EgText')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [EgText] char(40) 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_Erloesgruppen' and sc.[name] = 'KtoHaben')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [KtoHaben] 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_Erloesgruppen' and sc.[name] = 'KtoSoll')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [KtoSoll] 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_Erloesgruppen' and sc.[name] = 'BetrUmkehr')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [BetrUmkehr] 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_Erloesgruppen' and sc.[name] = 'KtoUmkehr')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [KtoUmkehr] 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_Erloesgruppen' and sc.[name] = 'Mit_Kst')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [Mit_Kst] 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_Erloesgruppen' and sc.[name] = 'Mit_Ktr')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [Mit_Ktr] 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_Erloesgruppen' and sc.[name] = 'UstSchl')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [UstSchl] 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_Erloesgruppen' and sc.[name] = 'BetrSchl')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [BetrSchl] 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_Erloesgruppen' and sc.[name] = 'Betrag')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ADD [Betrag] integer NOT NULL DEFAULT 0
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Erloesgruppen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ALTER COLUMN [Nr] char(3)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Erloesgruppen' and sc.[name] = 'Zeile')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ALTER COLUMN [Zeile] char(2)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Erloesgruppen' and sc.[name] = 'EgText')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ALTER COLUMN [EgText] char(40)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Erloesgruppen' and sc.[name] = 'KtoHaben')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ALTER COLUMN [KtoHaben] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Erloesgruppen' and sc.[name] = 'KtoSoll')
BEGIN
  ALTER TABLE [dbo].[ut_Erloesgruppen] ALTER COLUMN [KtoSoll] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Erloesgruppen')
  DROP VIEW [dbo].[Erloesgruppen]
GO
CREATE VIEW [dbo].[Erloesgruppen] AS
  SELECT * FROM [dbo].[ut_Erloesgruppen]
GO
CREATE TABLE [dbo].[ut_Erloesgruppen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Primary_Value] CHAR(15) NOT NULL
  ,[Suchen_Value] CHAR(15) NOT NULL
  CONSTRAINT [Pk_ut_Erloesgruppen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Erloesgruppen_KeyDel] ON [dbo].[ut_Erloesgruppen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Erloesgruppen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Erloesgruppen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Primary_Value],[Suchen_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(3),[Nr])+convert(char(2),[Zeile])+STR(RecId,10))
     ,UPPER(convert(char(2),[Zeile])+convert(char(3),[Nr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Erloesgruppen_Keys] WITH (SERIALIZABLE) SET
    [Primary_Value] = UPPER(convert(char(3),[Nr])+convert(char(2),[Zeile])+STR([RecId],10))
   ,[Suchen_Value] = UPPER(convert(char(2),[Zeile])+convert(char(3),[Nr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Erloesgruppen')
  DROP PROCEDURE [dbo].[up_Add_Erloesgruppen]
GO
CREATE PROCEDURE [dbo].[up_Add_Erloesgruppen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Erloesgruppen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Erloesgruppen] (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_Erloesgruppen')
  DROP PROCEDURE [dbo].[up_Upd_Erloesgruppen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Erloesgruppen]
  @RI integer
 ,@Parm_Nr char(3)
 ,@Parm_Zeile char(2)
 ,@Parm_EgText char(40)
 ,@Parm_KtoHaben char(8)
 ,@Parm_KtoSoll char(8)
 ,@Parm_BetrUmkehr tinyint
 ,@Parm_KtoUmkehr tinyint
 ,@Parm_Mit_Kst tinyint
 ,@Parm_Mit_Ktr tinyint
 ,@Parm_UstSchl integer
 ,@Parm_BetrSchl integer
 ,@Parm_Betrag integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Erloesgruppen] SET
     [Nr] = RTRIM(@Parm_Nr)
    ,[Zeile] = RTRIM(@Parm_Zeile)
    ,[EgText] = RTRIM(@Parm_EgText)
    ,[KtoHaben] = RTRIM(@Parm_KtoHaben)
    ,[KtoSoll] = RTRIM(@Parm_KtoSoll)
    ,[BetrUmkehr] = @Parm_BetrUmkehr
    ,[KtoUmkehr] = @Parm_KtoUmkehr
    ,[Mit_Kst] = @Parm_Mit_Kst
    ,[Mit_Ktr] = @Parm_Mit_Ktr
    ,[UstSchl] = @Parm_UstSchl
    ,[BetrSchl] = @Parm_BetrSchl
    ,[Betrag] = @Parm_Betrag
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Erloesgruppen')
  DROP PROCEDURE [dbo].[up_Sel_Erloesgruppen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Erloesgruppen]
  @RI integer
AS
SELECT
     [Nr]
    ,[Zeile]
    ,[EgText]
    ,[KtoHaben]
    ,[KtoSoll]
    ,[BetrUmkehr]
    ,[KtoUmkehr]
    ,[Mit_Kst]
    ,[Mit_Ktr]
    ,[UstSchl]
    ,[BetrSchl]
    ,[Betrag]
  FROM [dbo].[ut_Erloesgruppen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Erloesgruppen')
  DROP PROCEDURE [dbo].[up_Del_Erloesgruppen]
GO
CREATE PROCEDURE [dbo].[up_Del_Erloesgruppen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Erloesgruppen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Erloesgruppen')
  DROP PROCEDURE [dbo].[up_Reorg_Erloesgruppen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Erloesgruppen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Erloesgruppen] 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_Erloesgruppen] 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_Erloesgruppen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Erloesgruppen')
  DROP PROCEDURE [dbo].[up_Append_Erloesgruppen]
GO
CREATE PROCEDURE [dbo].[up_Append_Erloesgruppen]
  @Parm_Nr char(3)
 ,@Parm_Zeile char(2)
 ,@Parm_EgText char(40)
 ,@Parm_KtoHaben char(8)
 ,@Parm_KtoSoll char(8)
 ,@Parm_BetrUmkehr tinyint
 ,@Parm_KtoUmkehr tinyint
 ,@Parm_Mit_Kst tinyint
 ,@Parm_Mit_Ktr tinyint
 ,@Parm_UstSchl integer
 ,@Parm_BetrSchl integer
 ,@Parm_Betrag integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Erloesgruppen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Erloesgruppen] (
    RecId
   ,[Nr]
   ,[Zeile]
   ,[EgText]
   ,[KtoHaben]
   ,[KtoSoll]
   ,[BetrUmkehr]
   ,[KtoUmkehr]
   ,[Mit_Kst]
   ,[Mit_Ktr]
   ,[UstSchl]
   ,[BetrSchl]
   ,[Betrag]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Nr)
   ,RTRIM(@Parm_Zeile)
   ,RTRIM(@Parm_EgText)
   ,RTRIM(@Parm_KtoHaben)
   ,RTRIM(@Parm_KtoSoll)
   ,@Parm_BetrUmkehr
   ,@Parm_KtoUmkehr
   ,@Parm_Mit_Kst
   ,@Parm_Mit_Ktr
   ,@Parm_UstSchl
   ,@Parm_BetrSchl
   ,@Parm_Betrag
       )
  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_Erloesgruppen')
  DROP PROCEDURE [dbo].[up_TrSel_Erloesgruppen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Erloesgruppen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nr]))
      +LTRIM(RTRIM([Zeile]))
      +LTRIM(RTRIM([EgText]))
      +LTRIM(RTRIM([KtoHaben]))
      +LTRIM(RTRIM([KtoSoll]))
    AS Data
  FROM     [dbo].[ut_Erloesgruppen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Primary] ON [dbo].[ut_Erloesgruppen_Keys]
( [Primary_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_First_Erloesgruppen_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Last_Erloesgruppen_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Prior_Erloesgruppen_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Next_Erloesgruppen_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Prior50_Erloesgruppen_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Next50_Erloesgruppen_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_FindL_Erloesgruppen_Primary]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Erloesgruppen_Primary')
  DROP PROCEDURE [dbo].[up_FindF_Erloesgruppen_Primary]
GO

CREATE PROCEDURE [dbo].[up_First_Erloesgruppen_Primary]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys] ORDER BY [Primary_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Erloesgruppen_Primary]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys] ORDER BY [Primary_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Erloesgruppen_Primary] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys]
  WHERE [Primary_Value] > (SELECT [Primary_Value] FROM [dbo].[ut_Erloesgruppen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Primary_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Erloesgruppen_Primary] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys]
  WHERE [Primary_Value] > (SELECT [Primary_Value] FROM [dbo].[ut_Erloesgruppen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Primary_Value]
GO

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

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

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

CREATE INDEX [Ik_Suchen] ON [dbo].[ut_Erloesgruppen_Keys]
( [Suchen_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_First_Erloesgruppen_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_Last_Erloesgruppen_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_Prior_Erloesgruppen_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_Next_Erloesgruppen_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_Prior50_Erloesgruppen_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_Next50_Erloesgruppen_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_FindL_Erloesgruppen_Suchen]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Erloesgruppen_Suchen')
  DROP PROCEDURE [dbo].[up_FindF_Erloesgruppen_Suchen]
GO

CREATE PROCEDURE [dbo].[up_First_Erloesgruppen_Suchen]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys] ORDER BY [Suchen_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Erloesgruppen_Suchen]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys] ORDER BY [Suchen_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Erloesgruppen_Suchen] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys]
  WHERE [Suchen_Value] > (SELECT [Suchen_Value] FROM [dbo].[ut_Erloesgruppen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Suchen_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Erloesgruppen_Suchen] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Erloesgruppen_Keys]
  WHERE [Suchen_Value] > (SELECT [Suchen_Value] FROM [dbo].[ut_Erloesgruppen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Suchen_Value]
GO

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

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

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

