IF (EXISTS (select proc_name from sysprocedure where proc_name='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 GO if (select count(*) from sys.syscolumns where tname='SpaCounters' and cname='OtchetCounter')=0 then alter table SpaCounters Add OtchetCounter integer default 0 end if