------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Waehrungen')
BEGIN
CREATE TABLE [dbo].[ut_Waehrungen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nummer] char(4) DEFAULT ' '
  ,[Name] char(10) DEFAULT ' '
  ,[Kuerzel] char(10) DEFAULT ' '
  ,[Eurokurs] float DEFAULT 0
  ,[Bemerkung] char(42) DEFAULT ' '
  ,[Zeichen] char(1) DEFAULT ' '
  CONSTRAINT [Pk_Waehrungen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Waehrungen_KeyDel')
  DROP TRIGGER [dbo].[tr_Waehrungen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Waehrungen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Waehrungen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Waehrungen_Keys')
  DROP TABLE [dbo].[ut_Waehrungen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Waehrungen' and sc.[name] = 'Nummer')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ADD [Nummer] 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_Waehrungen' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ADD [Name] 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_Waehrungen' and sc.[name] = 'Kuerzel')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ADD [Kuerzel] 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_Waehrungen' and sc.[name] = 'Eurokurs')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ADD [Eurokurs] 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_Waehrungen' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ADD [Bemerkung] char(42) 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_Waehrungen' and sc.[name] = 'Zeichen')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ADD [Zeichen] char(1) NOT NULL DEFAULT ' '
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Waehrungen' and sc.[name] = 'Nummer')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ALTER COLUMN [Nummer] char(4)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Waehrungen' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ALTER COLUMN [Name] char(10)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Waehrungen' and sc.[name] = 'Kuerzel')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ALTER COLUMN [Kuerzel] char(10)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Waehrungen' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ALTER COLUMN [Bemerkung] char(42)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Waehrungen' and sc.[name] = 'Zeichen')
BEGIN
  ALTER TABLE [dbo].[ut_Waehrungen] ALTER COLUMN [Zeichen] char(1)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Waehrungen')
  DROP VIEW [dbo].[Waehrungen]
GO
CREATE VIEW [dbo].[Waehrungen] AS
  SELECT * FROM [dbo].[ut_Waehrungen]
GO
CREATE TABLE [dbo].[ut_Waehrungen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(20) NOT NULL
  ,[Key1_Value] CHAR(20) NOT NULL
  ,[Key2_Value] CHAR(14) NOT NULL
  CONSTRAINT [Pk_ut_Waehrungen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Waehrungen_KeyDel] ON [dbo].[ut_Waehrungen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Waehrungen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Waehrungen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key0_Value],[Key1_Value],[Key2_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(10),[Name])+STR(RecId,10))
     ,UPPER(convert(char(10),[Kuerzel])+STR(RecId,10))
     ,UPPER(convert(char(4),[Nummer])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Waehrungen_Keys] WITH (SERIALIZABLE) SET
    [Key0_Value] = UPPER(convert(char(10),[Name])+STR([RecId],10))
   ,[Key1_Value] = UPPER(convert(char(10),[Kuerzel])+STR([RecId],10))
   ,[Key2_Value] = UPPER(convert(char(4),[Nummer])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Waehrungen')
  DROP PROCEDURE [dbo].[up_Add_Waehrungen]
GO
CREATE PROCEDURE [dbo].[up_Add_Waehrungen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Waehrungen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Waehrungen] (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_Waehrungen')
  DROP PROCEDURE [dbo].[up_Upd_Waehrungen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Waehrungen]
  @RI integer
 ,@Parm_Nummer char(4)
 ,@Parm_Name char(10)
 ,@Parm_Kuerzel char(10)
 ,@Parm_Eurokurs float
 ,@Parm_Bemerkung char(42)
 ,@Parm_Zeichen char(1)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Waehrungen] SET
     [Nummer] = RTRIM(@Parm_Nummer)
    ,[Name] = RTRIM(@Parm_Name)
    ,[Kuerzel] = RTRIM(@Parm_Kuerzel)
    ,[Eurokurs] = @Parm_Eurokurs
    ,[Bemerkung] = RTRIM(@Parm_Bemerkung)
    ,[Zeichen] = RTRIM(@Parm_Zeichen)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Waehrungen')
  DROP PROCEDURE [dbo].[up_Sel_Waehrungen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Waehrungen]
  @RI integer
AS
SELECT
     [Nummer]
    ,[Name]
    ,[Kuerzel]
    ,[Eurokurs]
    ,[Bemerkung]
    ,[Zeichen]
  FROM [dbo].[ut_Waehrungen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Waehrungen')
  DROP PROCEDURE [dbo].[up_Del_Waehrungen]
GO
CREATE PROCEDURE [dbo].[up_Del_Waehrungen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Waehrungen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Waehrungen')
  DROP PROCEDURE [dbo].[up_Reorg_Waehrungen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Waehrungen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Waehrungen] 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_Waehrungen] 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_Waehrungen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Waehrungen')
  DROP PROCEDURE [dbo].[up_Append_Waehrungen]
GO
CREATE PROCEDURE [dbo].[up_Append_Waehrungen]
  @Parm_Nummer char(4)
 ,@Parm_Name char(10)
 ,@Parm_Kuerzel char(10)
 ,@Parm_Eurokurs float
 ,@Parm_Bemerkung char(42)
 ,@Parm_Zeichen char(1)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Waehrungen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Waehrungen] (
    RecId
   ,[Nummer]
   ,[Name]
   ,[Kuerzel]
   ,[Eurokurs]
   ,[Bemerkung]
   ,[Zeichen]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Nummer)
   ,RTRIM(@Parm_Name)
   ,RTRIM(@Parm_Kuerzel)
   ,@Parm_Eurokurs
   ,RTRIM(@Parm_Bemerkung)
   ,RTRIM(@Parm_Zeichen)
       )
  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_Waehrungen')
  DROP PROCEDURE [dbo].[up_TrSel_Waehrungen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Waehrungen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nummer]))
      +LTRIM(RTRIM([Name]))
      +LTRIM(RTRIM([Kuerzel]))
      +LTRIM(RTRIM([Bemerkung]))
      +LTRIM(RTRIM([Zeichen]))
    AS Data
  FROM     [dbo].[ut_Waehrungen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_Waehrungen_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_First_Waehrungen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_Last_Waehrungen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Waehrungen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_Next_Waehrungen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Waehrungen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Waehrungen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Waehrungen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Waehrungen_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Waehrungen_Key0]
GO

CREATE PROCEDURE [dbo].[up_First_Waehrungen_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys] ORDER BY [Key0_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Waehrungen_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys] ORDER BY [Key0_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Waehrungen_Key0] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_Waehrungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Waehrungen_Key0] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_Waehrungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

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

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

CREATE INDEX [Ik_Key1] ON [dbo].[ut_Waehrungen_Keys]
( [Key1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_First_Waehrungen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_Last_Waehrungen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Waehrungen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_Next_Waehrungen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_Waehrungen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_Next50_Waehrungen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Waehrungen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Waehrungen_Key1')
  DROP PROCEDURE [dbo].[up_FindF_Waehrungen_Key1]
GO

CREATE PROCEDURE [dbo].[up_First_Waehrungen_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys] ORDER BY [Key1_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Waehrungen_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys] ORDER BY [Key1_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Waehrungen_Key1] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_Waehrungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Waehrungen_Key1] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_Waehrungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

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

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

CREATE INDEX [Ik_Key2] ON [dbo].[ut_Waehrungen_Keys]
( [Key2_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_First_Waehrungen_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_Last_Waehrungen_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_Prior_Waehrungen_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_Next_Waehrungen_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_Waehrungen_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_Next50_Waehrungen_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_FindL_Waehrungen_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Waehrungen_Key2')
  DROP PROCEDURE [dbo].[up_FindF_Waehrungen_Key2]
GO

CREATE PROCEDURE [dbo].[up_First_Waehrungen_Key2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys] ORDER BY [Key2_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Waehrungen_Key2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys] ORDER BY [Key2_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Waehrungen_Key2] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys]
  WHERE [Key2_Value] > (SELECT [Key2_Value] FROM [dbo].[ut_Waehrungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key2_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Waehrungen_Key2] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Waehrungen_Keys]
  WHERE [Key2_Value] > (SELECT [Key2_Value] FROM [dbo].[ut_Waehrungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key2_Value]
GO

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

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

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

