DECLARE defaultFeladatKategoriaCursor CURSOR LOCAL FOR
  SELECT
   rb.ID
  FROM T_RENDSZERBEALLITAS rb
  WHERE rb.C_BEALLITASTIPUS = 7565
    AND rb.TOROLT = 'F'

DECLARE
   @rbId INT
  ,@alapJson NVARCHAR(MAX)
  ,@alapOptions NVARCHAR(MAX)
  ,@newAlapJson NVARCHAR(MAX)
  ,@ertekJson NVARCHAR(MAX)
  ,@ertekOptions NVARCHAR(MAX)
  ,@newErtekJson NVARCHAR(MAX)

OPEN defaultFeladatKategoriaCursor
FETCH NEXT FROM defaultFeladatKategoriaCursor INTO @rbId
WHILE @@FETCH_STATUS = 0
BEGIN
  SELECT
     @alapJson = JSON_QUERY(rb.C_ALAPERTEK)
    ,@ertekJson = JSON_QUERY(rb.C_ERTEK)
  FROM T_RENDSZERBEALLITAS rb
  WHERE rb.ID = @rbId

  SET @alapOptions = (
    SELECT
       ISNULL(j.[Selected], 0) AS [Selected]
      ,j.[Text]
      ,j.[Value]
      ,ISNULL(j.[Disabled], 0) AS [Disabled]
      ,j.[Group]
    FROM OPENJSON(@alapJson, '$.Options') WITH (
       [Selected] BIT '$.Selected'
      ,[Text] NVARCHAR(200) '$.Text'
      ,[Value] NVARCHAR(4) '$.Value'
      ,[Disabled] BIT '$.Disabled'
      ,[Group] NVARCHAR(200) '$.Group'
      ) j
    FOR JSON PATH, INCLUDE_NULL_VALUES, ROOT ('Options')
  )

  SET @alapOptions = RIGHT(@alapOptions, LEN(@alapOptions) - 1)
  SET @alapOptions = LEFT(@alapOptions, LEN(@alapOptions) - 1)
  
  SET @newAlapJson = '{"Id":"Default_Feladat_Kategoria",' + @alapOptions + '}'

    SET @ertekOptions = (
    SELECT
       ISNULL(j.[Selected], 0) AS [Selected]
      ,j.[Text]
      ,j.[Value]
      ,ISNULL(j.[Disabled], 0) AS [Disabled]
      ,j.[Group]
    FROM OPENJSON(@ertekJson, '$.Options') WITH (
       [Selected] BIT '$.Selected'
      ,[Text] NVARCHAR(200) '$.Text'
      ,[Value] NVARCHAR(4) '$.Value'
      ,[Disabled] BIT '$.Disabled'
      ,[Group] NVARCHAR(200) '$.Group'
      ) j
    FOR JSON PATH, INCLUDE_NULL_VALUES, ROOT ('Options')
  )

  SET @ertekOptions = RIGHT(@ertekOptions, LEN(@ertekOptions) - 1)
  SET @ertekOptions = LEFT(@ertekOptions, LEN(@ertekOptions) - 1)
  
  SET @newErtekJson = '{"Id":"Default_Feladat_Kategoria",' + @ertekOptions + '}'

  UPDATE T_RENDSZERBEALLITAS
  SET
     C_ALAPERTEK = @newAlapJson
    ,C_ERTEK = @newErtekJson
    ,SERIAL = SERIAL + 1
    ,LASTCHANGED = GETDATE()
  WHERE ID = @rbId

  FETCH NEXT FROM defaultFeladatKategoriaCursor INTO @rbId
END

CLOSE defaultFeladatKategoriaCursor
DEALLOCATE defaultFeladatKategoriaCursor
