DECLARE 
  @nomor SMALLINT,
  @no_spaj VARCHAR(12)

DECLARE list_cursor CURSOR FOR  
SELECT
  [no],
  no_spaj
FROM
	dbo.temp_spaj_ffbandung
WHERE
  diproses = 1

OPEN list_cursor  
FETCH NEXT FROM list_cursor INTO @nomor, @no_spaj  

WHILE @@FETCH_STATUS = 0  
BEGIN  

  DECLARE 
    @jml_dokumen SMALLINT,
    @server_from VARCHAR(50)
    
  SELECT
  	@jml_dokumen = COUNT(*)
  FROM
  	dbo.web_spaj_dokumen
  WHERE
    no_spaj = @no_spaj
  
  SELECT DISTINCT
  	@server_from = [server]
  FROM
  	dbo.web_spaj_dokumen
  WHERE
    no_spaj = @no_spaj
  
  UPDATE
    dbo.temp_spaj_ffbandung
  SET
    jml_dokumen = @jml_dokumen,
    server_from = @server_from
  WHERE
    [no] = @nomor
    AND no_spaj = @no_spaj

  FETCH NEXT FROM list_cursor INTO @nomor, @no_spaj  
END  

CLOSE list_cursor  
DEALLOCATE list_cursor 
