USE GrpInsurance

DECLARE @hasil TABLE ( no_proposal VARCHAR(9),
                       id_produk VARCHAR(12),
                       STATUS VARCHAR(1),
                       cek BIT)

DECLARE @no_proposal VARCHAR(9)

DECLARE list_cursor CURSOR FOR  
SELECT No_Proposal
FROM [db_no_replicate_space].[dbo].[temp_prib_list_polis_ajb] 

OPEN list_cursor  
FETCH NEXT FROM list_cursor INTO @no_proposal  

WHILE @@FETCH_STATUS = 0  
BEGIN
  
  IF EXISTS(SELECT * FROM dbo.Produk_Polis WHERE Sub_Kelompok = '0100' AND No_Proposal = @no_proposal)
    BEGIN
      INSERT INTO @hasil
              (no_proposal, id_produk, STATUS, cek)
      SELECT
  	    No_Proposal,
  	    Id_Produk,
  	    Status,
  	    1
      FROM
        dbo.Produk_Polis
      WHERE
        Sub_Kelompok = '0100'
        AND No_Proposal = @no_proposal
    END
  ELSE
    BEGIN
      INSERT INTO @hasil
              (no_proposal, id_produk, STATUS, cek)
      VALUES
              (@no_proposal, -- no_proposal - varchar(9)
               NULL, -- id_produk - varchar(12)
               NULL, -- STATUS - varchar(1)
               0  -- cek - bit
               )
    END
  	
  FETCH NEXT FROM list_cursor INTO @no_proposal  
END  

CLOSE list_cursor  
DEALLOCATE list_cursor 

SELECT * FROM @hasil 
