------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_ProcessingTicket')
BEGIN
CREATE TABLE [dbo].[ut_ProcessingTicket]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[Uid] char(30) DEFAULT ' '
  ,[StartDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[StartTime] char(5) DEFAULT ' '
  ,[EndDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[EndTime] char(5) DEFAULT ' '
  ,[TicketGuid] char(40) DEFAULT ' '
  ,[Success] tinyint DEFAULT 0
  ,[Comment] char(50) DEFAULT ' '
  CONSTRAINT [Pk_ProcessingTicket]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ProcessingTicket_KeyDel')
  DROP TRIGGER [dbo].[tr_ProcessingTicket_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ProcessingTicket_KeyUpd')
  DROP TRIGGER [dbo].[tr_ProcessingTicket_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_ProcessingTicket_Keys')
  DROP TABLE [dbo].[ut_ProcessingTicket_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ProcessingTicket' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [Knr] 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_ProcessingTicket' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [Uid] char(30) 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_ProcessingTicket' and sc.[name] = 'StartDate')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [StartDate] 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_ProcessingTicket' and sc.[name] = 'StartTime')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [StartTime] 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_ProcessingTicket' and sc.[name] = 'EndDate')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [EndDate] 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_ProcessingTicket' and sc.[name] = 'EndTime')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [EndTime] 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_ProcessingTicket' and sc.[name] = 'TicketGuid')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [TicketGuid] 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_ProcessingTicket' and sc.[name] = 'Success')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [Success] 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_ProcessingTicket' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ADD [Comment] char(50) NOT NULL DEFAULT ' '
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ProcessingTicket' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ALTER COLUMN [Knr] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ProcessingTicket' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ALTER COLUMN [Uid] char(30)
END
GO
IF 5 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ProcessingTicket' and sc.[name] = 'StartTime')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ALTER COLUMN [StartTime] char(5)
END
GO
IF 5 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ProcessingTicket' and sc.[name] = 'EndTime')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ALTER COLUMN [EndTime] char(5)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ProcessingTicket' and sc.[name] = 'TicketGuid')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ALTER COLUMN [TicketGuid] char(40)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ProcessingTicket' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_ProcessingTicket] ALTER COLUMN [Comment] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'ProcessingTicket')
  DROP VIEW [dbo].[ProcessingTicket]
GO
CREATE VIEW [dbo].[ProcessingTicket] AS
  SELECT * FROM [dbo].[ut_ProcessingTicket]
GO
CREATE TABLE [dbo].[ut_ProcessingTicket_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(50) NOT NULL
  ,[Key01_Value] CHAR(115) NOT NULL
  CONSTRAINT [Pk_ut_ProcessingTicket_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_ProcessingTicket_KeyDel] ON [dbo].[ut_ProcessingTicket]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_ProcessingTicket_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_ProcessingTicket_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(40),[TicketGuid])+STR(RecId,10))
     ,UPPER(convert(char(20),[Knr])+convert(char(30),[Uid])+CONVERT(char(10),[StartDate],120)+convert(char(5),[StartTime])+convert(char(40),[TicketGuid])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_ProcessingTicket_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(40),[TicketGuid])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(20),[Knr])+convert(char(30),[Uid])+CONVERT(char(10),[StartDate],120)+convert(char(5),[StartTime])+convert(char(40),[TicketGuid])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_ProcessingTicket')
  DROP PROCEDURE [dbo].[up_Add_ProcessingTicket]
GO
CREATE PROCEDURE [dbo].[up_Add_ProcessingTicket] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_ProcessingTicket])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ProcessingTicket] (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_ProcessingTicket')
  DROP PROCEDURE [dbo].[up_Upd_ProcessingTicket]
GO
CREATE PROCEDURE [dbo].[up_Upd_ProcessingTicket]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_StartDate datetime
 ,@Parm_StartTime char(5)
 ,@Parm_EndDate datetime
 ,@Parm_EndTime char(5)
 ,@Parm_TicketGuid char(40)
 ,@Parm_Success tinyint
 ,@Parm_Comment char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_ProcessingTicket] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[Uid] = RTRIM(@Parm_Uid)
    ,[StartDate] = @Parm_StartDate
    ,[StartTime] = RTRIM(@Parm_StartTime)
    ,[EndDate] = @Parm_EndDate
    ,[EndTime] = RTRIM(@Parm_EndTime)
    ,[TicketGuid] = RTRIM(@Parm_TicketGuid)
    ,[Success] = @Parm_Success
    ,[Comment] = RTRIM(@Parm_Comment)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_ProcessingTicket')
  DROP PROCEDURE [dbo].[up_Sel_ProcessingTicket]
GO
CREATE PROCEDURE [dbo].[up_Sel_ProcessingTicket]
  @RI integer
AS
SELECT
     [Knr]
    ,[Uid]
    ,[StartDate]
    ,[StartTime]
    ,[EndDate]
    ,[EndTime]
    ,[TicketGuid]
    ,[Success]
    ,[Comment]
  FROM [dbo].[ut_ProcessingTicket]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_ProcessingTicket')
  DROP PROCEDURE [dbo].[up_Del_ProcessingTicket]
GO
CREATE PROCEDURE [dbo].[up_Del_ProcessingTicket]
  @RI integer
AS
  DELETE FROM [dbo].[ut_ProcessingTicket] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_ProcessingTicket')
  DROP PROCEDURE [dbo].[up_Reorg_ProcessingTicket]
GO
CREATE PROCEDURE [dbo].[up_Reorg_ProcessingTicket]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_ProcessingTicket] 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_ProcessingTicket] 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_ProcessingTicket] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_ProcessingTicket')
  DROP PROCEDURE [dbo].[up_Append_ProcessingTicket]
GO
CREATE PROCEDURE [dbo].[up_Append_ProcessingTicket]
  @Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_StartDate datetime
 ,@Parm_StartTime char(5)
 ,@Parm_EndDate datetime
 ,@Parm_EndTime char(5)
 ,@Parm_TicketGuid char(40)
 ,@Parm_Success tinyint
 ,@Parm_Comment char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_ProcessingTicket])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ProcessingTicket] (
    RecId
   ,[Knr]
   ,[Uid]
   ,[StartDate]
   ,[StartTime]
   ,[EndDate]
   ,[EndTime]
   ,[TicketGuid]
   ,[Success]
   ,[Comment]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Knr)
   ,RTRIM(@Parm_Uid)
   ,@Parm_StartDate
   ,RTRIM(@Parm_StartTime)
   ,@Parm_EndDate
   ,RTRIM(@Parm_EndTime)
   ,RTRIM(@Parm_TicketGuid)
   ,@Parm_Success
   ,RTRIM(@Parm_Comment)
       )
  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_ProcessingTicket')
  DROP PROCEDURE [dbo].[up_TrSel_ProcessingTicket]
GO
CREATE PROCEDURE [dbo].[up_TrSel_ProcessingTicket]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Uid]))
      +LTRIM(RTRIM([StartTime]))
      +LTRIM(RTRIM([EndTime]))
      +LTRIM(RTRIM([TicketGuid]))
      +LTRIM(RTRIM([Comment]))
    AS Data
  FROM     [dbo].[ut_ProcessingTicket]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_ProcessingTicket_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_First_ProcessingTicket_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_Last_ProcessingTicket_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_Prior_ProcessingTicket_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_Next_ProcessingTicket_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_ProcessingTicket_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_Next50_ProcessingTicket_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_FindL_ProcessingTicket_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ProcessingTicket_Key00')
  DROP PROCEDURE [dbo].[up_FindF_ProcessingTicket_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_ProcessingTicket_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_First_ProcessingTicket_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_Last_ProcessingTicket_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_Prior_ProcessingTicket_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_Next_ProcessingTicket_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_ProcessingTicket_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_Next50_ProcessingTicket_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_FindL_ProcessingTicket_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ProcessingTicket_Key01')
  DROP PROCEDURE [dbo].[up_FindF_ProcessingTicket_Key01]
GO

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

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

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

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

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

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

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

