IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('PrepareAddrCard'))) THEN Drop procedure PrepareAddrCard END IF GO create procedure DBA.PrepareAddrCard(in @Gno integer) begin declare @MonthsGreek varchar(50); declare i integer; declare @BroiPayments integer; declare j integer; declare sqlsttm varchar(200); set @BroiPayments=(select count(*) from Book as B,Articles as A,Invoices as Inv,Currencies as C where B.WhoPays = @Gno and B.KodCurrency = C.KodCurrency and B.RegNum = A.RegNo and A.InvoiceNo = Inv.InvoiceNo and Inv.CancelInvoiceNo = 0); set i=1; set @MonthsGreek=' I II III IV V VI VIIVIII IX X XI XII'; delete from #AddrCardPayments; while i <= 48 loop insert into #AddrCardPayments( Mes,MesReal) values( Trim(SUBSTR(@MonthsGreek,i,4)),i/4+1) ; set i=i+4 end loop; set i=(select min(MONTH(DateRegDt)) from Book where Guest = @Gno); set j=1; for f1 as curs1 scroll cursor for select if TipDDS = 2 then A.Price*(1+bvrDDSPart) else A.Price endif as Price,Inv.InvoiceNo from Book as B,Articles as A,Invoices as Inv,Currencies as C where B.WhoPays = @Gno and B.KodCurrency = C.KodCurrency and B.RegNum = A.RegNo and A.InvoiceNo = Inv.InvoiceNo and Inv.CancelInvoiceNo = 0 order by A.ArticleNo asc do set sqlsttm='update #AddrCardPayments set lvUG' || j || '=' || Round(Price,2) || ',kvUG' || j || '=' || InvoiceNo || ' where MesReal=' || i; execute immediate sqlsttm; set i=i+1; if i > 12 then set i=1; set j=j+1 end if end for end go