text: CREATE PROCEDURE WSO2_TOKEN_CLEANUP_DATA_RESTORE_SP
  AS
  
  BEGIN
  
  DECLARE @rowCount INT;
  DECLARE @enableLog BIT;
  DECLARE @logLevel VARCHAR(10);
  
  -- ------------------------------------------
  -- CONFIGURABLE ATTRIBUTES
  -- ------------------------------------------
  SET @enableLog = 'TRUE'; -- ENABLE LOGGING [DEFAULT : TRUE]
  SET @logLevel = 'TRACE'; -- SET LOG LEVELS : TRACE
  
  
  
  IF (@enableLog = 1) BEGIN
  SELECT  'CLEANUP DATA RESTORATION STARTED .... !';
  END
  
  -- ---------------------
  
  SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME IN ('IDN_OAUTH2_ACCESS_TOKEN');
  IF (@rowCount = 1)
  BEGIN
  IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
  SELECT  'CLEANUP DATA RESTORATION STARTED ON IDN_OAUTH2_ACCESS_TOKEN TABLE !';
  END
  INSERT INTO dbo.IDN_OAUTH2_ACCESS_TOKEN SELECT A.* FROM dbo.BAK_IDN_OAUTH2_ACCESS_TOKEN AS A LEFT JOIN dbo.IDN_OAUTH2_ACCESS_TOKEN AS B ON A.TOKEN_ID = B.TOKEN_ID WHERE B.TOKEN_ID IS NULL;
  SELECT @rowCount =  @@rowcount;
  IF (@enableLog = 1 ) BEGIN
  SELECT  'CLEANUP DATA RESTORATION COMPLETED ON IDN_OAUTH2_ACCESS_TOKEN WITH '+CAST(@rowCount as varchar)
  END
  END
  
  ---- ---------------------
  
  SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME IN ('IDN_OAUTH2_AUTHORIZATION_CODE');
  IF (@rowCount = 1)
  BEGIN
  IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
  SELECT  'CLEANUP DATA RESTORATION STARTED ON IDN_OAUTH2_AUTHORIZATION_CODE TABLE !';
  END
  INSERT INTO dbo.IDN_OAUTH2_AUTHORIZATION_CODE SELECT A.* FROM dbo.BAK_IDN_OAUTH2_AUTHORIZATION_CODE AS A LEFT JOIN dbo.IDN_OAUTH2_AUTHORIZATION_CODE AS B ON A.CODE_ID = B.CODE_ID WHERE B.CODE_ID IS NULL;
  SELECT @rowCount =  @@rowcount;
  IF (@enableLog = 1 ) BEGIN
  SELECT  'CLEANUP DATA RESTORATION COMPLETED ON IDN_OAUTH2_AUTHORIZATION_CODE WITH '+CAST(@rowCount as varchar)
  END
  END
  
  ---- ---------------------
  
  SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE  TABLE_NAME IN ('IDN_OAUTH2_ACCESS_TOKEN_SCOPE');
  IF (@rowCount = 1)
  BEGIN
  IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
  SELECT  'CLEANUP DATA RESTORATION STARTED ON IDN_OAUTH2_ACCESS_TOKEN_SCOPE TABLE !';
  END
  INSERT INTO dbo.IDN_OAUTH2_ACCESS_TOKEN_SCOPE SELECT A.* FROM dbo.BAK_IDN_OAUTH2_ACCESS_TOKEN_SCOPE AS A LEFT JOIN dbo.IDN_OAUTH2_ACCESS_TOKEN_SCOPE AS B ON A.TOKEN_ID = B.TOKEN_ID AND A.TOKEN_SCOPE = B.TOKEN_SCOPE WHERE B.TOKEN_ID IS NULL;
  SELECT @rowCount =  @@rowcount;
  IF (@enableLog = 1 ) BEGIN
  SELECT  'CLEANUP DATA RESTORATION COMPLETED ON IDN_OAUTH2_ACCESS_TOKEN_SCOPE WITH '+CAST(@rowCount as varchar)
  END
  END
  
  -- ---------------------
  
  SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME IN ('IDN_OIDC_REQ_OBJECT_REFERENCE');
  IF (@rowCount = 1)
  BEGIN
  IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
  SELECT  'CLEANUP DATA RESTORATION STARTED ON IDN_OIDC_REQ_OBJECT_REFERENCE TABLE !';
  END
  SET IDENTITY_INSERT IDN_OIDC_REQ_OBJECT_REFERENCE ON
  INSERT INTO dbo.IDN_OIDC_REQ_OBJECT_REFERENCE (ID, CONSUMER_KEY_ID,CODE_ID, TOKEN_ID, SESSION_DATA_KEY)  SELECT A.ID, A.CONSUMER_KEY_ID, A.CODE_ID, A.TOKEN_ID, A.SESSION_DATA_KEY FROM dbo.BAK_IDN_OIDC_REQ_OBJECT_REFERENCE AS A LEFT JOIN dbo.IDN_OIDC_REQ_OBJECT_REFERENCE AS B ON A.ID = B.ID WHERE B.ID IS NULL;
  SET IDENTITY_INSERT IDN_OIDC_REQ_OBJECT_REFERENCE OFF
  SELECT @rowCount =  @@rowcount;
  IF (@enableLog = 1 ) BEGIN
  SELECT  'CLEANUP DATA RESTORATION COMPLETED ON IDN_OIDC_REQ_OBJECT_REFERENCE WITH '+CAST(@rowCount as varchar)
  END
  END
  
  -- --------------------
  
  SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE  TABLE_NAME IN ('IDN_OIDC_REQ_OBJECT_CLAIMS');
  IF (@rowCount = 1)
  BEGIN
  IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
  SELECT  'CLEANUP DATA RESTORATION STARTED ON IDN_OIDC_REQ_OBJECT_CLAIMS TABLE !';
  END
  SET IDENTITY_INSERT IDN_OIDC_REQ_OBJECT_CLAIMS ON
  INSERT INTO IDN_OIDC_REQ_OBJECT_CLAIMS  (ID, REQ_OBJECT_ID,CLAIM_ATTRIBUTE, ESSENTIAL, VALUE,IS_USERINFO)  SELECT A.ID, A.REQ_OBJECT_ID,A.CLAIM_ATTRIBUTE, A.ESSENTIAL, A.VALUE,A.IS_USERINFO FROM BAK_IDN_OIDC_REQ_OBJECT_CLAIMS AS A LEFT JOIN IDN_OIDC_REQ_OBJECT_CLAIMS AS B ON A.ID = B.ID WHERE B.ID IS NULL;
  SET IDENTITY_INSERT IDN_OIDC_REQ_OBJECT_CLAIMS OFF
  SELECT @rowCount =  @@rowcount;
  IF (@enableLog = 1 ) BEGIN
  SELECT  'CLEANUP DATA RESTORATION COMPLETED ON IDN_OIDC_REQ_OBJECT_CLAIMS WITH '+CAST(@rowCount as varchar)
  END
  END
  
  ---- -------------------
  
  SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE  TABLE_NAME IN ('IDN_OIDC_REQ_OBJ_CLAIM_VALUES');
  IF (@rowCount = 1)
  BEGIN
  IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
  SELECT  'CLEANUP DATA RESTORATION STARTED ON IDN_OIDC_REQ_OBJ_CLAIM_VALUES TABLE !';
  END
  SET IDENTITY_INSERT IDN_OIDC_REQ_OBJ_CLAIM_VALUES ON
  INSERT INTO IDN_OIDC_REQ_OBJ_CLAIM_VALUES (ID,REQ_OBJECT_CLAIMS_ID,CLAIM_VALUES) SELECT A.ID,A.REQ_OBJECT_CLAIMS_ID,A.CLAIM_VALUES FROM BAK_IDN_OIDC_REQ_OBJ_CLAIM_VALUES AS A LEFT JOIN IDN_OIDC_REQ_OBJ_CLAIM_VALUES AS B ON A.ID = B.ID WHERE B.ID IS NULL;
  SET IDENTITY_INSERT IDN_OIDC_REQ_OBJ_CLAIM_VALUES OFF
  SELECT @rowCount =  @@rowcount;
  IF (@enableLog = 1 ) BEGIN
  SELECT  'CLEANUP DATA RESTORATION COMPLETED ON IDN_OIDC_REQ_OBJ_CLAIM_VALUES WITH '+CAST(@rowCount as varchar)
  END
  END
  
  IF (@enableLog = 1) BEGIN
  SELECT  'CLEANUP DATA RESTORATION COMPLETED .... !';
  END
  
  END
  