IF (EXISTS (select proc_name from sysprocedure where LCase(proc_name)=LCase('SpaGetSericeQtyForOtchet'))) THEN Drop function SpaGetSericeQtyForOtchet END IF GO CREATE FUNCTION "DBA"."SpaGetSericeQtyForOtchet"(in ServiceNum integer,in FirstInvNo integer,in LastInvNo integer) returns integer begin declare qty integer; select sum(a.Qty) into qty from SpaArticles as a,SpaInvoices as i where(a.ServiceNo=ServiceNum) and(a.Invoiceno>=FirstInvNo and a.InvoiceNo<=LastInvNo) and i.CancelInvoiceNo=0 and a.InvoiceNo=i.InvoiceNo; if qty is null then return(0) else return(qty) end if end