-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2026, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_GridColumn')
BEGIN
CREATE TABLE [dbo].[ut_GridColumn]
(
  [RecId]  integer NOT NULL
 ,[FormName] char(8) DEFAULT ''
 ,[GridName] char(40) DEFAULT ''
 ,[Scope] char(1) DEFAULT ''
 ,[ScopeName] char(50) DEFAULT ''
 ,[Position] integer DEFAULT 0
 ,[ColumnId] integer DEFAULT 0
 ,[ColumnType] integer DEFAULT 0
 ,[Activated] tinyint DEFAULT 0
 ,[Width] integer DEFAULT 0
 ,[TextLimit] integer DEFAULT 0
 ,[Decimals] integer DEFAULT 0
 ,[Label] varchar(30) DEFAULT ''
 ,[Comment] varchar(200) DEFAULT ''
 ,[Internal] varchar(20) DEFAULT ''
 ,[Fixed] tinyint DEFAULT 0
 ,[Enabled] tinyint DEFAULT 0
 ,[Combo] tinyint DEFAULT 0
 ,[Save] tinyint DEFAULT 0
 ,[Properties] tinyint DEFAULT 0
 ,[Print] tinyint DEFAULT 0
 ,[Marked] tinyint DEFAULT 0
 ,[Face3D] tinyint DEFAULT 0
 ,[Duplicate] tinyint DEFAULT 0
 ,[Search] tinyint DEFAULT 0
 ,[Remove] tinyint DEFAULT 0
 ,[Append] tinyint DEFAULT 0
 ,[FontIndex] integer DEFAULT 0
 ,[Color] integer DEFAULT 0
  CONSTRAINT [Pk_GridColumn]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_GridColumn_KeyDel')
  DROP TRIGGER [dbo].[tr_GridColumn_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_GridColumn_KeyUpd')
  DROP TRIGGER [dbo].[tr_GridColumn_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_GridColumn_Keys')
  DROP TABLE [dbo].[ut_GridColumn_Keys]
GO

-- add new fields
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'FormName')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [FormName] 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_GridColumn' and sc.[name] = 'GridName')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [GridName] 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_GridColumn' and sc.[name] = 'Scope')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Scope] 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_GridColumn' and sc.[name] = 'ScopeName')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [ScopeName] char(50) 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_GridColumn' and sc.[name] = 'Position')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Position] 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_GridColumn' and sc.[name] = 'ColumnId')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [ColumnId] 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_GridColumn' and sc.[name] = 'ColumnType')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [ColumnType] 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_GridColumn' and sc.[name] = 'Activated')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Activated] 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_GridColumn' and sc.[name] = 'Width')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Width] 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_GridColumn' and sc.[name] = 'TextLimit')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [TextLimit] 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_GridColumn' and sc.[name] = 'Decimals')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Decimals] 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_GridColumn' and sc.[name] = 'Label')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Label] varchar(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_GridColumn' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Comment] varchar(200) 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_GridColumn' and sc.[name] = 'Internal')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Internal] varchar(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_GridColumn' and sc.[name] = 'Fixed')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Fixed] 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_GridColumn' and sc.[name] = 'Enabled')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Enabled] 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_GridColumn' and sc.[name] = 'Combo')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Combo] 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_GridColumn' and sc.[name] = 'Save')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Save] 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_GridColumn' and sc.[name] = 'Properties')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Properties] 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_GridColumn' and sc.[name] = 'Print')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Print] 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_GridColumn' and sc.[name] = 'Marked')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Marked] 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_GridColumn' and sc.[name] = 'Face3D')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Face3D] 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_GridColumn' and sc.[name] = 'Duplicate')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Duplicate] 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_GridColumn' and sc.[name] = 'Search')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Search] 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_GridColumn' and sc.[name] = 'Remove')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Remove] 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_GridColumn' and sc.[name] = 'Append')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Append] 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_GridColumn' and sc.[name] = 'FontIndex')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [FontIndex] 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_GridColumn' and sc.[name] = 'Color')
BEGIN
  ALTER TABLE [dbo].[ut_GridColumn] ADD [Color] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'FormName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'FormName'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [FormName] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'FormName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'FormName'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [FormName] varchar(8)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'GridName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'GridName'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [GridName] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'GridName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'GridName'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [GridName] varchar(40)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Scope'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Scope'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Scope] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Scope'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Scope'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Scope] varchar(1)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'ScopeName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'ScopeName'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [ScopeName] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'ScopeName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'ScopeName'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [ScopeName] varchar(50)
END
GO
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Label'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Label'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Label] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Label'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Label'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Label] varchar(30)
END
GO
IF (200 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Comment'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Comment'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Comment] char(200)
END
GO
IF (200 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Comment'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Comment'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Comment] varchar(200)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Internal'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Internal'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Internal] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Internal'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_GridColumn' and sc.[name] = 'Internal'))
BEGIN
   ALTER TABLE [dbo].[ut_GridColumn] ALTER COLUMN [Internal] varchar(20)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'GridColumn')
  DROP VIEW [dbo].[GridColumn]
GO
CREATE VIEW [dbo].[GridColumn] AS
  SELECT [RecId]
 ,[FormName]
 ,[GridName]
 ,[Scope]
 ,[ScopeName]
 ,[Position]
 ,[ColumnId]
 ,[ColumnType]
 ,[Activated]
 ,[Width]
 ,[TextLimit]
 ,[Decimals]
 ,[Label]
 ,[Comment]
 ,[Internal]
 ,[Fixed]
 ,[Enabled]
 ,[Combo]
 ,[Save]
 ,[Properties]
 ,[Print]
 ,[Marked]
 ,[Face3D]
 ,[Duplicate]
 ,[Search]
 ,[Remove]
 ,[Append]
 ,[FontIndex]
 ,[Color]
  FROM [dbo].[ut_GridColumn]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_GridColumn', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_GridColumn
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_GridColumn() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_GridColumn] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_GridColumn')
  DROP PROCEDURE [dbo].[up_Sel_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Sel_GridColumn]  @mRI integer
AS
  SELECT
    [FormName]
   ,[GridName]
   ,[Scope]
   ,[ScopeName]
   ,[Position]
   ,[ColumnId]
   ,[ColumnType]
   ,[Activated]
   ,[Width]
   ,[TextLimit]
   ,[Decimals]
   ,[Label]
   ,[Comment]
   ,[Internal]
   ,[Fixed]
   ,[Enabled]
   ,[Combo]
   ,[Save]
   ,[Properties]
   ,[Print]
   ,[Marked]
   ,[Face3D]
   ,[Duplicate]
   ,[Search]
   ,[Remove]
   ,[Append]
   ,[FontIndex]
   ,[Color]
  FROM [dbo].[ut_GridColumn] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_GridColumn')
  DROP PROCEDURE [dbo].[up_Sel2_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Sel2_GridColumn]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[GridColumn] WHERE RecId = @mRI
GO

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_GridColumn')
  DROP PROCEDURE [dbo].[up_Append_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Append_GridColumn]
   @Param_FormName char(8)
  ,@Param_GridName char(40)
  ,@Param_Scope char(1)
  ,@Param_ScopeName char(50)
  ,@Param_Position integer
  ,@Param_ColumnId integer
  ,@Param_ColumnType integer
  ,@Param_Activated tinyint
  ,@Param_Width integer
  ,@Param_TextLimit integer
  ,@Param_Decimals integer
  ,@Param_Label varchar(30)
  ,@Param_Comment varchar(200)
  ,@Param_Internal varchar(20)
  ,@Param_Fixed tinyint
  ,@Param_Enabled tinyint
  ,@Param_Combo tinyint
  ,@Param_Save tinyint
  ,@Param_Properties tinyint
  ,@Param_Print tinyint
  ,@Param_Marked tinyint
  ,@Param_Face3D tinyint
  ,@Param_Duplicate tinyint
  ,@Param_Search tinyint
  ,@Param_Remove tinyint
  ,@Param_Append tinyint
  ,@Param_FontIndex integer
  ,@Param_Color integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_GridColumn()
      INSERT INTO [dbo].[ut_GridColumn] (RecId
        ,[FormName]
        ,[GridName]
        ,[Scope]
        ,[ScopeName]
        ,[Position]
        ,[ColumnId]
        ,[ColumnType]
        ,[Activated]
        ,[Width]
        ,[TextLimit]
        ,[Decimals]
        ,[Label]
        ,[Comment]
        ,[Internal]
        ,[Fixed]
        ,[Enabled]
        ,[Combo]
        ,[Save]
        ,[Properties]
        ,[Print]
        ,[Marked]
        ,[Face3D]
        ,[Duplicate]
        ,[Search]
        ,[Remove]
        ,[Append]
        ,[FontIndex]
        ,[Color]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_FormName))
        ,UPPER(RTRIM(@Param_GridName))
        ,UPPER(RTRIM(@Param_Scope))
        ,UPPER(RTRIM(@Param_ScopeName))
        ,@Param_Position
        ,@Param_ColumnId
        ,@Param_ColumnType
        ,@Param_Activated
        ,@Param_Width
        ,@Param_TextLimit
        ,@Param_Decimals
        ,RTRIM(@Param_Label)
        ,RTRIM(@Param_Comment)
        ,RTRIM(@Param_Internal)
        ,@Param_Fixed
        ,@Param_Enabled
        ,@Param_Combo
        ,@Param_Save
        ,@Param_Properties
        ,@Param_Print
        ,@Param_Marked
        ,@Param_Face3D
        ,@Param_Duplicate
        ,@Param_Search
        ,@Param_Remove
        ,@Param_Append
        ,@Param_FontIndex
        ,@Param_Color
          )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END -- WHILE
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_GridColumn')
  DROP PROCEDURE [dbo].[up_Add_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Add_GridColumn]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_GridColumn()
  INSERT INTO [dbo].[ut_GridColumn] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_GridColumn')
  DROP PROCEDURE [dbo].[up_Upd_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Upd_GridColumn] @mRI integer
  ,@Param_FormName char(8)
  ,@Param_GridName char(40)
  ,@Param_Scope char(1)
  ,@Param_ScopeName char(50)
  ,@Param_Position integer
  ,@Param_ColumnId integer
  ,@Param_ColumnType integer
  ,@Param_Activated tinyint
  ,@Param_Width integer
  ,@Param_TextLimit integer
  ,@Param_Decimals integer
  ,@Param_Label varchar(30)
  ,@Param_Comment varchar(200)
  ,@Param_Internal varchar(20)
  ,@Param_Fixed tinyint
  ,@Param_Enabled tinyint
  ,@Param_Combo tinyint
  ,@Param_Save tinyint
  ,@Param_Properties tinyint
  ,@Param_Print tinyint
  ,@Param_Marked tinyint
  ,@Param_Face3D tinyint
  ,@Param_Duplicate tinyint
  ,@Param_Search tinyint
  ,@Param_Remove tinyint
  ,@Param_Append tinyint
  ,@Param_FontIndex integer
  ,@Param_Color integer
AS
  UPDATE [dbo].[ut_GridColumn] SET
    [FormName] = UPPER(RTRIM(@Param_FormName))
   ,[GridName] = UPPER(RTRIM(@Param_GridName))
   ,[Scope] = UPPER(RTRIM(@Param_Scope))
   ,[ScopeName] = UPPER(RTRIM(@Param_ScopeName))
   ,[Position] = @Param_Position
   ,[ColumnId] = @Param_ColumnId
   ,[ColumnType] = @Param_ColumnType
   ,[Activated] = @Param_Activated
   ,[Width] = @Param_Width
   ,[TextLimit] = @Param_TextLimit
   ,[Decimals] = @Param_Decimals
   ,[Label] = RTRIM(@Param_Label)
   ,[Comment] = RTRIM(@Param_Comment)
   ,[Internal] = RTRIM(@Param_Internal)
   ,[Fixed] = @Param_Fixed
   ,[Enabled] = @Param_Enabled
   ,[Combo] = @Param_Combo
   ,[Save] = @Param_Save
   ,[Properties] = @Param_Properties
   ,[Print] = @Param_Print
   ,[Marked] = @Param_Marked
   ,[Face3D] = @Param_Face3D
   ,[Duplicate] = @Param_Duplicate
   ,[Search] = @Param_Search
   ,[Remove] = @Param_Remove
   ,[Append] = @Param_Append
   ,[FontIndex] = @Param_FontIndex
   ,[Color] = @Param_Color
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_GridColumn')
  DROP PROCEDURE [dbo].[up_Upd2_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Upd2_GridColumn] @Param_RecId varchar(20)
  ,@Param_FormName varchar(8)
  ,@Param_GridName varchar(40)
  ,@Param_Scope varchar(1)
  ,@Param_ScopeName varchar(50)
  ,@Param_Position varchar(4)
  ,@Param_ColumnId varchar(4)
  ,@Param_ColumnType varchar(2)
  ,@Param_Activated varchar(1)
  ,@Param_Width varchar(3)
  ,@Param_TextLimit varchar(3)
  ,@Param_Decimals varchar(1)
  ,@Param_Label varchar(30)
  ,@Param_Comment varchar(200)
  ,@Param_Internal varchar(20)
  ,@Param_Fixed varchar(1)
  ,@Param_Enabled varchar(1)
  ,@Param_Combo varchar(1)
  ,@Param_Save varchar(1)
  ,@Param_Properties varchar(1)
  ,@Param_Print varchar(1)
  ,@Param_Marked varchar(1)
  ,@Param_Face3D varchar(1)
  ,@Param_Duplicate varchar(1)
  ,@Param_Search varchar(1)
  ,@Param_Remove varchar(1)
  ,@Param_Append varchar(1)
  ,@Param_FontIndex varchar(2)
  ,@Param_Color varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_GridColumn] SET
    [FormName] = UPPER(RTRIM(@Param_FormName))
   ,[GridName] = UPPER(RTRIM(@Param_GridName))
   ,[Scope] = UPPER(RTRIM(@Param_Scope))
   ,[ScopeName] = UPPER(RTRIM(@Param_ScopeName))
   ,[Position] = dbo.itcfunc_integer(@Param_Position)
   ,[ColumnId] = dbo.itcfunc_integer(@Param_ColumnId)
   ,[ColumnType] = dbo.itcfunc_integer(@Param_ColumnType)
   ,[Activated] = dbo.itcfunc_integer(@Param_Activated)
   ,[Width] = dbo.itcfunc_integer(@Param_Width)
   ,[TextLimit] = dbo.itcfunc_integer(@Param_TextLimit)
   ,[Decimals] = dbo.itcfunc_integer(@Param_Decimals)
   ,[Label] = RTRIM(@Param_Label)
   ,[Comment] = RTRIM(@Param_Comment)
   ,[Internal] = RTRIM(@Param_Internal)
   ,[Fixed] = dbo.itcfunc_integer(@Param_Fixed)
   ,[Enabled] = dbo.itcfunc_integer(@Param_Enabled)
   ,[Combo] = dbo.itcfunc_integer(@Param_Combo)
   ,[Save] = dbo.itcfunc_integer(@Param_Save)
   ,[Properties] = dbo.itcfunc_integer(@Param_Properties)
   ,[Print] = dbo.itcfunc_integer(@Param_Print)
   ,[Marked] = dbo.itcfunc_integer(@Param_Marked)
   ,[Face3D] = dbo.itcfunc_integer(@Param_Face3D)
   ,[Duplicate] = dbo.itcfunc_integer(@Param_Duplicate)
   ,[Search] = dbo.itcfunc_integer(@Param_Search)
   ,[Remove] = dbo.itcfunc_integer(@Param_Remove)
   ,[Append] = dbo.itcfunc_integer(@Param_Append)
   ,[FontIndex] = dbo.itcfunc_integer(@Param_FontIndex)
   ,[Color] = dbo.itcfunc_integer(@Param_Color)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_GridColumn] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_GridColumn')
  DROP PROCEDURE [dbo].[up_Del_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Del_GridColumn] @mRI integer
AS
  DELETE FROM [dbo].[ut_GridColumn] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_GridColumn')
  DROP PROCEDURE [dbo].[up_TrSel_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_TrSel_GridColumn]
AS
  SELECT RecId,
    LTRIM(RTRIM([FormName]))
   +LTRIM(RTRIM([GridName]))
   +LTRIM(RTRIM([Scope]))
   +LTRIM(RTRIM([ScopeName]))
   +LTRIM(RTRIM([Label]))
   +LTRIM(RTRIM([Comment]))
   +LTRIM(RTRIM([Internal]))
  AS Data FROM [dbo].[ut_GridColumn] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_GridColumn')
  DROP PROCEDURE [dbo].[up_Reorg_GridColumn]
GO
CREATE PROCEDURE [dbo].[up_Reorg_GridColumn] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_GridColumn] 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_GridColumn] 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_GridColumn] )
  END
GO

-- logical key  ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_GridColumn' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_GridColumn]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_GridColumn' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_GridColumn] (FormName,GridName,Scope,ScopeName,Position,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_First_GridColumn_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_GridColumn_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_GridColumn_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_GridColumn_Key00] AS EXECUTE [dbo].[up_First_GridColumn_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_Last_GridColumn_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_GridColumn_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_GridColumn_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_GridColumn_Key00] AS EXECUTE [dbo].[up_Last_GridColumn_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_Next_GridColumn_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_GridColumn_Key00] @oldRecId integer AS
  DECLARE @oldFormName char(8)
  DECLARE @oldGridName char(40)
  DECLARE @oldScope char(1)
  DECLARE @oldScopeName char(50)
  DECLARE @oldPosition integer
  SELECT @oldFormName = FormName, @oldGridName = GridName, @oldScope = Scope, @oldScopeName = ScopeName, @oldPosition = Position FROM [dbo].[ut_GridColumn] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName = @oldScopeName) AND (Position > @oldPosition)
    ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName > @oldScopeName)
    ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope > @oldScope)
    ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName > @oldGridName)
    ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName > @oldFormName)
    ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_GridColumn_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_GridColumn_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_GridColumn_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_Prior_GridColumn_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_GridColumn_Key00] @oldRecId integer AS
  DECLARE @oldFormName char(8)
  DECLARE @oldGridName char(40)
  DECLARE @oldScope char(1)
  DECLARE @oldScopeName char(50)
  DECLARE @oldPosition integer
  SELECT @oldFormName = FormName, @oldGridName = GridName, @oldScope = Scope, @oldScopeName = ScopeName, @oldPosition = Position FROM [dbo].[ut_GridColumn] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName = @oldScopeName) AND (Position < @oldPosition)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName < @oldScopeName)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope < @oldScope)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName = @oldFormName) AND (GridName < @oldGridName)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00)
    WHERE (FormName < @oldFormName)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_GridColumn_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_GridColumn_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_GridColumn_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_Next50_GridColumn_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_GridColumn_Key00')
  DROP PROCEDURE [dbo].[qp_Next50_GridColumn_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_GridColumn_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_GridColumn_Key00')
  DROP PROCEDURE [dbo].[qp_Prior50_GridColumn_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_FindF_GridColumn_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_GridColumn_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mFormName char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mGridName char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mScope char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,49,1)
  DECLARE @mScopeName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,50,50)
  DECLARE @mPosition integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,100,4)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 103 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName AND 
      Position = @mPosition
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  IF @xLen = 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  IF @xLen = 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName >= @mFormName AND SUBSTRING(FormName,1,LEN(@mFormName)) = @mFormName
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName >= @mGridName AND SUBSTRING(GridName,1,LEN(@mGridName)) = @mGridName
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  IF @xLen < 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope >= @mScope AND SUBSTRING(Scope,1,LEN(@mScope)) = @mScope
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END

  IF @xLen < 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName >= @mScopeName AND SUBSTRING(ScopeName,1,LEN(@mScopeName)) = @mScopeName
      ORDER BY FormName,GridName,Scope,ScopeName,Position,RecId
    RETURN
  END


  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_GridColumn_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_GridColumn_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_GridColumn_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_GridColumn_Key00')
  DROP PROCEDURE [dbo].[up_FindL_GridColumn_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_GridColumn_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mFormName char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mGridName char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mScope char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,49,1)
  DECLARE @mScopeName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,50,50)
  DECLARE @mPosition integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,100,4)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 103 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName AND 
      Position = @mPosition
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  IF @xLen = 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  IF @xLen = 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(FormName,1,LEN(@mFormName)) = @mFormName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      SUBSTRING(GridName,1,LEN(@mGridName)) = @mGridName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  IF @xLen < 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      SUBSTRING(Scope,1,LEN(@mScope)) = @mScope
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END

  IF @xLen < 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key00) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      SUBSTRING(ScopeName,1,LEN(@mScopeName)) = @mScopeName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,Position desc,RecId desc
    RETURN
  END


  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_GridColumn_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_GridColumn_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_GridColumn_Key00] @xKey, @xLen')
GO


-- logical key  ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_GridColumn' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  DROP INDEX [Ik_Key01] ON [dbo].[ut_GridColumn]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_GridColumn' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  CREATE INDEX [Ik_Key01] ON [dbo].[ut_GridColumn] (FormName,GridName,Scope,ScopeName,ColumnId,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_First_GridColumn_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_GridColumn_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_GridColumn_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_GridColumn_Key01] AS EXECUTE [dbo].[up_First_GridColumn_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_Last_GridColumn_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_GridColumn_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_GridColumn_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_GridColumn_Key01] AS EXECUTE [dbo].[up_Last_GridColumn_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_Next_GridColumn_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_GridColumn_Key01] @oldRecId integer AS
  DECLARE @oldFormName char(8)
  DECLARE @oldGridName char(40)
  DECLARE @oldScope char(1)
  DECLARE @oldScopeName char(50)
  DECLARE @oldColumnId integer
  SELECT @oldFormName = FormName, @oldGridName = GridName, @oldScope = Scope, @oldScopeName = ScopeName, @oldColumnId = ColumnId FROM [dbo].[ut_GridColumn] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName = @oldScopeName) AND (ColumnId > @oldColumnId)
    ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName > @oldScopeName)
    ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope > @oldScope)
    ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName > @oldGridName)
    ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName > @oldFormName)
    ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_GridColumn_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_GridColumn_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_GridColumn_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_Prior_GridColumn_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_GridColumn_Key01] @oldRecId integer AS
  DECLARE @oldFormName char(8)
  DECLARE @oldGridName char(40)
  DECLARE @oldScope char(1)
  DECLARE @oldScopeName char(50)
  DECLARE @oldColumnId integer
  SELECT @oldFormName = FormName, @oldGridName = GridName, @oldScope = Scope, @oldScopeName = ScopeName, @oldColumnId = ColumnId FROM [dbo].[ut_GridColumn] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName = @oldScopeName) AND (ColumnId < @oldColumnId)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (ScopeName < @oldScopeName)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope < @oldScope)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName = @oldFormName) AND (GridName < @oldGridName)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01)
    WHERE (FormName < @oldFormName)
    ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_GridColumn_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_GridColumn_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_GridColumn_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_Next50_GridColumn_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_GridColumn_Key01')
  DROP PROCEDURE [dbo].[qp_Next50_GridColumn_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_GridColumn_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_GridColumn_Key01')
  DROP PROCEDURE [dbo].[qp_Prior50_GridColumn_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_FindF_GridColumn_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_GridColumn_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mFormName char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mGridName char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mScope char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,49,1)
  DECLARE @mScopeName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,50,50)
  DECLARE @mColumnId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,100,4)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 103 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName AND 
      ColumnId = @mColumnId
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  IF @xLen = 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  IF @xLen = 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName >= @mFormName AND SUBSTRING(FormName,1,LEN(@mFormName)) = @mFormName
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName >= @mGridName AND SUBSTRING(GridName,1,LEN(@mGridName)) = @mGridName
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  IF @xLen < 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope >= @mScope AND SUBSTRING(Scope,1,LEN(@mScope)) = @mScope
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END

  IF @xLen < 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName >= @mScopeName AND SUBSTRING(ScopeName,1,LEN(@mScopeName)) = @mScopeName
      ORDER BY FormName,GridName,Scope,ScopeName,ColumnId,RecId
    RETURN
  END


  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_GridColumn_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_GridColumn_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_GridColumn_Key01] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_GridColumn_Key01')
  DROP PROCEDURE [dbo].[up_FindL_GridColumn_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_GridColumn_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mFormName char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mGridName char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mScope char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,49,1)
  DECLARE @mScopeName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,50,50)
  DECLARE @mColumnId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,100,4)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 103 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName AND 
      ColumnId = @mColumnId
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  IF @xLen = 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      ScopeName = @mScopeName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  IF @xLen = 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(FormName,1,LEN(@mFormName)) = @mFormName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      SUBSTRING(GridName,1,LEN(@mGridName)) = @mGridName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  IF @xLen < 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      SUBSTRING(Scope,1,LEN(@mScope)) = @mScope
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END

  IF @xLen < 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key01) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      SUBSTRING(ScopeName,1,LEN(@mScopeName)) = @mScopeName
      ORDER BY FormName desc,GridName desc,Scope desc,ScopeName desc,ColumnId desc,RecId desc
    RETURN
  END


  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_GridColumn_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_GridColumn_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_GridColumn_Key01] @xKey, @xLen')
GO


-- logical key  ORDER BY FormName,GridName,Scope,Label,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_GridColumn' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  DROP INDEX [Ik_Key02] ON [dbo].[ut_GridColumn]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_GridColumn' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  CREATE INDEX [Ik_Key02] ON [dbo].[ut_GridColumn] (FormName,GridName,Scope,Label,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_First_GridColumn_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_GridColumn_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) ORDER BY FormName,GridName,Scope,Label,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_GridColumn_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_GridColumn_Key02] AS EXECUTE [dbo].[up_First_GridColumn_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_Last_GridColumn_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_GridColumn_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_GridColumn_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_GridColumn_Key02] AS EXECUTE [dbo].[up_Last_GridColumn_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_Next_GridColumn_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_GridColumn_Key02] @oldRecId integer AS
  DECLARE @oldFormName char(8)
  DECLARE @oldGridName char(40)
  DECLARE @oldScope char(1)
  DECLARE @oldLabel varchar(30)
  SELECT @oldFormName = FormName, @oldGridName = GridName, @oldScope = Scope, @oldLabel = Label FROM [dbo].[ut_GridColumn] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (Label > @oldLabel)
    ORDER BY FormName,GridName,Scope,Label,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope > @oldScope)
    ORDER BY FormName,GridName,Scope,Label,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName = @oldFormName) AND (GridName > @oldGridName)
    ORDER BY FormName,GridName,Scope,Label,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName > @oldFormName)
    ORDER BY FormName,GridName,Scope,Label,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_GridColumn_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_GridColumn_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_GridColumn_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_Prior_GridColumn_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_GridColumn_Key02] @oldRecId integer AS
  DECLARE @oldFormName char(8)
  DECLARE @oldGridName char(40)
  DECLARE @oldScope char(1)
  DECLARE @oldLabel varchar(30)
  SELECT @oldFormName = FormName, @oldGridName = GridName, @oldScope = Scope, @oldLabel = Label FROM [dbo].[ut_GridColumn] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope = @oldScope) AND (Label < @oldLabel)
    ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName = @oldFormName) AND (GridName = @oldGridName) AND (Scope < @oldScope)
    ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName = @oldFormName) AND (GridName < @oldGridName)
    ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02)
    WHERE (FormName < @oldFormName)
    ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_GridColumn_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_GridColumn_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_GridColumn_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_Next50_GridColumn_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_GridColumn_Key02')
  DROP PROCEDURE [dbo].[qp_Next50_GridColumn_Key02]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_GridColumn_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_GridColumn_Key02')
  DROP PROCEDURE [dbo].[qp_Prior50_GridColumn_Key02]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_FindF_GridColumn_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_GridColumn_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mFormName char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mGridName char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mScope char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,49,1)
  DECLARE @mLabel varchar(30) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,50,30)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 79 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      Label = @mLabel
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  IF @xLen = 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName >= @mFormName AND SUBSTRING(FormName,1,LEN(@mFormName)) = @mFormName
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName >= @mGridName AND SUBSTRING(GridName,1,LEN(@mGridName)) = @mGridName
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  IF @xLen < 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope >= @mScope AND SUBSTRING(Scope,1,LEN(@mScope)) = @mScope
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  IF @xLen < 79 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      Label >= @mLabel AND SUBSTRING(Label,1,LEN(@mLabel)) = @mLabel
      ORDER BY FormName,GridName,Scope,Label,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_GridColumn_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_GridColumn_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_GridColumn_Key02] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_GridColumn_Key02')
  DROP PROCEDURE [dbo].[up_FindL_GridColumn_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_GridColumn_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mFormName char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mGridName char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mScope char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,49,1)
  DECLARE @mLabel varchar(30) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,50,30)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 79 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      Label = @mLabel
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  IF @xLen = 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(FormName,1,LEN(@mFormName)) = @mFormName
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      SUBSTRING(GridName,1,LEN(@mGridName)) = @mGridName
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  IF @xLen < 49 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      SUBSTRING(Scope,1,LEN(@mScope)) = @mScope
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  IF @xLen < 79 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_GridColumn] WITH(INDEX = Ik_Key02) WHERE 
      FormName = @mFormName AND 
      GridName = @mGridName AND 
      Scope = @mScope AND 
      SUBSTRING(Label,1,LEN(@mLabel)) = @mLabel
      ORDER BY FormName desc,GridName desc,Scope desc,Label desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_GridColumn_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_GridColumn_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_GridColumn_Key02] @xKey, @xLen')
GO


