----UPDATE
----  dbo.temp_prib_payor_waiver
----SET
----  Gender = NULL
----WHERE
----  Gender = 'NULL'
  
----UPDATE
----  dbo.temp_prib_payor_waiver
----SET
----  [Premium Payment Term] = NULL
----WHERE
----  [Premium Payment Term] = 'NULL'

----UPDATE
----  dbo.temp_prib_payor_waiver
----SET
----  [Occupation Class] = NULL
----WHERE
----  [Occupation Class] = 'NULL'

----UPDATE
----  dbo.temp_prib_payor_waiver
----SET
----  Risk = NULL
----WHERE
----  Risk = 'NULL'

SELECT
  *
FROM
  dbo.temp_prib_payor_waiver
WHERE [Rate COI  IT] IS NULL
---------------------------------------------------------------------
DECLARE @id_row INT

DECLARE list_cursor CURSOR FOR  
SELECT id_row
FROM dbo.temp_prib_payor_waiver
WHERE [Rate COI  IT] IS NULL


OPEN list_cursor  
FETCH NEXT FROM list_cursor INTO @id_row  

WHILE @@FETCH_STATUS = 0  
BEGIN  
  
  DECLARE
    --@id_plan_dasar VARCHAR(20),
    @jenis_kelamin VARCHAR(2),
    @thn_ke INT,
    @kelas VARCHAR(50)
    
  SELECT
    --@id_plan_dasar = CASE [ID Plan SQL]
    --                    WHEN '1118' THEN '0195'
    --                    WHEN '1122' THEN '0195'
    --                    WHEN '1102' THEN
    --                                    CASE 
    --                                      WHEN [Effective Date From]<'2009-01-01' THEN '0115'
    --                                      ELSE '0159'
    --                                    END
    --                    ELSE '0159'
    --                 END,
    @jenis_kelamin = CASE Gender
                        WHEN 'F' THEN 'P'
                        WHEN 'M' THEN 'L'
                        ELSE 'L'  -- bebas
                     END,
    @thn_ke = CASE [Policy Year From]
                WHEN 0 THEN 1
                ELSE [Policy Year From]
              END,
    @kelas = CASE 
                WHEN [Occupation Class] IS NOT NULL THEN
                  CASE [Occupation Class]
                    WHEN 'CLSS1' THEN '1'
                    WHEN 'CLSS2' THEN '2'
                    WHEN 'CLSS3' THEN '3'
                    WHEN 'CLSS4' THEN '4'
                  END
                ELSE
                  LEFT(CAST([Sum Assured] AS VARCHAR(10)), LEN(CAST([Sum Assured] AS VARCHAR(10)))-3)
             END
  FROM
    dbo.temp_prib_payor_waiver
  WHERE
    id_row = @id_row
  
  BEGIN TRY
  --PRINT @kelas
  --SELECT * FROM dbo.temp_prib_payor_waiver WHERE id_row = @id_row
    BEGIN
      UPDATE
        dbo.temp_prib_payor_waiver
      SET
        --[Rate COI  IT] = dbLife_Tabel.dbo.func_getpremi1103(
        [Rate COI  IT] = dbLife_Tabel.dbo.func_getpremi1110(
            [ID Plan Dasar SQL],
            [ID Plan Rider SQL],
            Currency,
            1,
            @kelas,
            Risk,
            @jenis_kelamin,
            @jenis_kelamin,
            [Entry Age From] + [Policy Year From] - 1,
            [Entry Age From] + [Policy Year From] - 1,
            20,
            [Premium Payment Term],
            [Sum Assured],
            [Sum Assured], 
            20,
            [Premium Payment Term],
            [Sum Assured],
            [Effective Date From],
            [Effective Date From]) * ([Premium Payment Term] - [Policy Year From] + 1)
      FROM
        dbo.temp_prib_payor_waiver
      WHERE
        id_row = @id_row
      
      --SELECT 
      --  [Entry Age From] + [Policy Year From] - 1 AS usia,
      --  [Premium Payment Term] - [Policy Year From] + 1 AS mp,
      --  [Premium Payment Term],
      --  [Entry Age From],
      --  [Policy Year From],
      --  [Rate COI PL],
      --  --[Rate COI  IT],

      --  dbLife_Tabel.dbo.func_getpremi1103(
      --      [ID Plan Dasar SQL],
      --      [ID Plan Rider SQL],
      --      Currency,
      --      1,
      --      @kelas,
      --      Risk,
      --      @jenis_kelamin,
      --      @jenis_kelamin,
      --      [Entry Age From] + [Policy Year From] - 1,
      --      [Entry Age From] + [Policy Year From] - 1,
      --      20,
      --      [Premium Payment Term],
      --      [Sum Assured],
      --      [Sum Assured], 
      --      20,
      --      [Premium Payment Term],
      --      [Sum Assured],
      --      [Effective Date From],
      --      [Effective Date From]) * ([Premium Payment Term] - [Policy Year From] + 1)
      --FROM 
      --  dbo.temp_prib_payor_waiver
      --WHERE
      --  id_row = @id_row
    END
  END TRY
  
  BEGIN CATCH
    PRINT @id_row
    -- continue
  END CATCH

  FETCH NEXT FROM list_cursor INTO @id_row  
END  

CLOSE list_cursor  
DEALLOCATE list_cursor 