DECLARE 
    @no_polis VARCHAR(11),
    @email VARCHAR(80),
    @no_peserta VARCHAR(12)

DECLARE list_cursor CURSOR FAST_FORWARD FOR  

SELECT
	No_Polis,
	No_Peserta,
	email
FROM
	dbo.Peserta_handphone
WHERE
	No_Polis = '2016.0140.1'
	AND COALESCE(email,'') <> ''

OPEN list_cursor  
FETCH NEXT FROM list_cursor INTO @no_polis, @no_peserta, @email

WHILE @@FETCH_STATUS = 0  
BEGIN  
		
		
		IF NOT EXISTS(SELECT
									 *
									FROM
									 custport_user_login_client
									WHERE
									 username = @email)
			BEGIN
				DECLARE @id_user INT
				INSERT INTO [dbo].[custport_user_login_client]
					    ([username]
					    ,[password]
					    ,[no_polis]
					    ,[no_peserta]
					    ,[status]
					    ,[create_by]
					    ,[create_date]
					    ,[change_by]
					    ,[change_date])
					VALUES
					    (@email
					    ,NULL
					    ,@no_polis
					    ,@no_peserta
					    ,'G'
					    ,'SYS'
					    ,GETDATE()
					    ,'SYS'
					    ,GETDATE())
				SELECT @id_user = SCOPE_IDENTITY()
				INSERT INTO [dbo].[custport_user_login_client_polis]
					   ([create_by]
					   ,[create_date]
					   ,[change_by]
					   ,[change_date]
					   ,[is_deleted]
					   ,[id_user]
					   ,[no_polis]
					   ,[no_peserta]
					   ,[status])
				  VALUES
					   ('SYS'
					   ,GETDATE()
					   ,'SYS'
					   ,GETDATE()
					   ,0
					   ,@id_user
					   ,@no_polis
					   ,@no_peserta
					   ,'I')
			END

    FETCH NEXT FROM list_cursor INTO @no_polis, @no_peserta, @email
END  

CLOSE list_cursor  
DEALLOCATE list_cursor 