declare @datum_od datetime
declare @datum_do datetime
declare @sifrausluga varchar(10)
declare @novacena decimal
DECLARE @sifraEFOsiguranik int
DECLARE @cena decimal

set @datum_od = '01.01.2009 0:00:00'
set @datum_do = '19.01.2009 0:00:00'
set @sifrausluga = '1000017'
set @novacena = 231.00

update efstavka
set efstavka.cena = @novacena,
efstavka.ukupnacena = efstavka.kolicina * @novacena
where efstavka.sifraUsluga = @sifrausluga
and efstavka.datum>=@datum_od and efstavka.datum<@datum_do

DECLARE c CURSOR
FOR 
select sifra 
from efosiguranik 
where datumod>= @datum_od and datumdo<@datum_do and datumod is not null

OPEN c
FETCH c INTO @sifraEFOsiguranik

WHILE @@fetch_status=0
BEGIN

	select @cena = sum(efstavka.cena * efstavka.kolicina) + 
	(Select isnull(sum(efsanmat.cena * efsanmat.kolicina),0) from efsanmat, efstavka where efsanmat.sifraEFStavka=efstavka.sifra and efstavka.sifraefosiguranik = @sifraEFOsiguranik)
	from efstavka
	where efstavka.sifraefosiguranik = @sifraEFOsiguranik

	update efosiguranik
	set iznfak = @cena
	where sifra = @sifraEFOsiguranik

	FETCH c INTO @sifraEFOsiguranik
END

CLOSE c
DEALLOCATE c 

DECLARE @sifraELFak int
DECLARE @iznosfakture decimal

DECLARE ef CURSOR
FOR
select sifra from elfak where obrisana = 0

OPEN ef
FETCH ef INTO @sifraELFak

WHILE @@fetch_status=0
BEGIN

	select @iznosfakture = sum(IznFak)
	from EFOsiguranik
	where SifraELFak = @sifraELFak

	update ELFak
	set iznos = @iznosfakture
	where Sifra = @sifraELFak

	FETCH ef INTO @sifraELFak
END

CLOSE ef
DEALLOCATE ef 