DECLARE @list_data TABLE ( 
                          [id_row] [smallint] IDENTITY(1,1) NOT NULL,
                          [Date] DATETIME, 
                          No_Invoice VARCHAR(50), 
                          [Description] VARCHAR(50), 
                          Details_Groups VARCHAR(50), 
                          Amount MONEY,
                          No_Peserta VARCHAR(12),
                          Nama VARCHAR(50),
                          Nik VARCHAR(30),
                          Tgl_Lahir DATETIME,
                          Kelamin VARCHAR(1),
                          Grade SMALLINT,
                          Pln SMALLINT,
                          Nama_Paket VARCHAR(50),
                          Nama_Sub VARCHAR(30),
                          Curr_Premi FLOAT,
                          Curr_Santunan FLOAT
                         )

DECLARE 
  @id_row SMALLINT,
  @no_invoice VARCHAR(30)

DECLARE list_cursor CURSOR FOR  
SELECT  id_row, No_Invoice
FROM [db_no_replicate_space].[dbo].[temp_prib_rincian_dn]
WHERE COALESCE(No_Invoice,'') <>''

OPEN list_cursor  
FETCH NEXT FROM list_cursor INTO @id_row, @no_invoice

WHILE @@FETCH_STATUS = 0  
BEGIN  
  INSERT INTO @list_data
          (Date,
           No_Invoice,
           Description,
           Details_Groups,
           Amount)
  SELECT
    [Date],
    [No_Invoice],
    [Description],
    [Details_Groups],
    [Amount]
  FROM
    [db_no_replicate_space].[dbo].[temp_prib_rincian_dn]
  WHERE
    [id_row] = @id_row
           
  INSERT INTO @list_data
          (No_Peserta,
           Nama,
           Nik,
           Tgl_Lahir,
           Kelamin,
           Grade,
           Pln,
           Nama_Paket,
           Nama_Sub,
           Curr_Premi,
           Curr_Santunan)         
  SELECT
    pst.No_Peserta,
    pst.Nama,
    pst.Nik,
    pst.Tgl_Lahir,
    pst.Kelamin,
    pst.Grade,
    pst.Pln,
    pp.Nama_Paket,
    sk.Nama_Sub,
    ppd.Curr_Premi,
    ppd.Curr_Santunan--,
  	--ppd.*
  FROM
  	dbo.Note_Terbit_Master AS ntm INNER JOIN
  	dbo.Peserta_Produk_Detail AS ppd ON ntm.No_Ref = ppd.No_Ref INNER JOIN
  	dbo.Peserta AS pst ON ppd.No_Peserta = pst.No_Peserta AND ppd.No_Proposal = pst.No_Proposal INNER JOIN
  	dbo.Produk_Polis AS pp ON ppd.Id_Produk = pp.Id_Produk INNER JOIN
  	dbo.Sub_Kelompok AS sk ON pp.Sub_Kelompok = sk.Sub_Kelompok
  WHERE
    ntm.No_Note = @no_invoice
  
  INSERT INTO @list_data ( [Date] ) VALUES (NULL)

  FETCH NEXT FROM list_cursor INTO @id_row, @no_invoice
END  

CLOSE list_cursor  
DEALLOCATE list_cursor

SELECT
	*
FROM
	@list_data
ORDER BY
  id_row