------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelRaum')
BEGIN
CREATE TABLE [dbo].[ut_BelRaum]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[RaumNr] char(6) DEFAULT ' '
  ,[Bauteil] char(6) DEFAULT ' '
  ,[Bez1] char(80) DEFAULT ' '
  CONSTRAINT [Pk_BelRaum]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelRaum_KeyDel')
  DROP TRIGGER [dbo].[tr_BelRaum_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelRaum_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelRaum_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelRaum_Keys')
  DROP TABLE [dbo].[ut_BelRaum_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelRaum' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] 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_BelRaum' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] 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_BelRaum' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] 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_BelRaum' and sc.[name] = 'RaumNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] ADD [RaumNr] 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_BelRaum' and sc.[name] = 'Bauteil')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] ADD [Bauteil] 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_BelRaum' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] ADD [Bez1] char(80) 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_BelRaum' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] 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_BelRaum' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] 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_BelRaum' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] 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_BelRaum' and sc.[name] = 'RaumNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] ALTER COLUMN [RaumNr] char(6)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelRaum' and sc.[name] = 'Bauteil')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] ALTER COLUMN [Bauteil] char(6)
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelRaum' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BelRaum] ALTER COLUMN [Bez1] char(80)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelRaum')
  DROP VIEW [dbo].[BelRaum]
GO
CREATE VIEW [dbo].[BelRaum] AS
  SELECT * FROM [dbo].[ut_BelRaum]
GO
CREATE TABLE [dbo].[ut_BelRaum_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(40) NOT NULL
  ,[Key01_Value] CHAR(40) NOT NULL
  CONSTRAINT [Pk_ut_BelRaum_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelRaum_KeyDel] ON [dbo].[ut_BelRaum]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelRaum_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelRaum_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RaumNr])+convert(char(6),[Bauteil])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[Bauteil])+convert(char(6),[RaumNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelRaum_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RaumNr])+convert(char(6),[Bauteil])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[Bauteil])+convert(char(6),[RaumNr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelRaum')
  DROP PROCEDURE [dbo].[up_Add_BelRaum]
GO
CREATE PROCEDURE [dbo].[up_Add_BelRaum] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelRaum])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelRaum] (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_BelRaum')
  DROP PROCEDURE [dbo].[up_Upd_BelRaum]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelRaum]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RaumNr char(6)
 ,@Parm_Bauteil char(6)
 ,@Parm_Bez1 char(80)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelRaum] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[RaumNr] = RTRIM(@Parm_RaumNr)
    ,[Bauteil] = RTRIM(@Parm_Bauteil)
    ,[Bez1] = RTRIM(@Parm_Bez1)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelRaum')
  DROP PROCEDURE [dbo].[up_Sel_BelRaum]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelRaum]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[RaumNr]
    ,[Bauteil]
    ,[Bez1]
  FROM [dbo].[ut_BelRaum]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelRaum')
  DROP PROCEDURE [dbo].[up_Del_BelRaum]
GO
CREATE PROCEDURE [dbo].[up_Del_BelRaum]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelRaum] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelRaum')
  DROP PROCEDURE [dbo].[up_Reorg_BelRaum]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelRaum]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelRaum] 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_BelRaum] 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_BelRaum] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelRaum')
  DROP PROCEDURE [dbo].[up_Append_BelRaum]
GO
CREATE PROCEDURE [dbo].[up_Append_BelRaum]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RaumNr char(6)
 ,@Parm_Bauteil char(6)
 ,@Parm_Bez1 char(80)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelRaum])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelRaum] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[RaumNr]
   ,[Bauteil]
   ,[Bez1]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_RaumNr)
   ,RTRIM(@Parm_Bauteil)
   ,RTRIM(@Parm_Bez1)
       )
  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_BelRaum')
  DROP PROCEDURE [dbo].[up_TrSel_BelRaum]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelRaum]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([RaumNr]))
      +LTRIM(RTRIM([Bauteil]))
      +LTRIM(RTRIM([Bez1]))
    AS Data
  FROM     [dbo].[ut_BelRaum]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelRaum_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_First_BelRaum_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelRaum_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelRaum_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelRaum_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelRaum_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelRaum_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelRaum_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelRaum_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelRaum_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_BelRaum_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_First_BelRaum_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelRaum_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelRaum_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_Next_BelRaum_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_BelRaum_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_Next50_BelRaum_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelRaum_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelRaum_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelRaum_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_BelRaum_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelRaum_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelRaum_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelRaum_Keys] ORDER BY [Key01_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_BelRaum_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelRaum_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_BelRaum_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

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

