Хранимые процедуры
1. Создание хранимой процедуры на удаление. 1.1. Для таблицы GRAFIK_VIPLAT. Команда GRANT позволяют системным администраторам создавать пользователей MySQL, а также предоставлять права пользователям или лишать их прав. EXECUTE - разрешает пользователю запускать хранимые процедуры. DELETE - разрешает использование DELETE. SELECT- разрешает использование SELECT. CREATE PROCEDURE DELETE_GR ( id_plat_rasch_a integer) as begin delete from grafik_viplat WHERE ID_PLAT_RASCH=:id_plat_rasch_a; suspend; end^ SET TERM; ^ GRANT SELECT,DELETE ON GRAFIK_VIPLAT TO PROCEDURE DELETE_GR; GRANT EXECUTE ON PROCEDURE DELETE_GR TO SYSDBA; 1.2. Для таблицы KLIENT. CREATE PROCEDURE DELETE_KL ( id_klienta_a integer, id_vidachi_a integer) as begin delete from klient where ID_KLIENTA=:id_klienta_a; delete from vidacha where (vidacha.id_klienta=:id_klienta_a) AND (ID_VIDACHI=:id_vidachi_a); delete from grafik_viplat where grafik_viplat.id_vidachi=:id_vidachi_a; delete from plategi where plategi.id_vidachi=:id_vidachi_a; suspend; end^ SET TERM; ^ GRANT SELECT,DELETE ON KLIENT TO PROCEDURE DELETE_KL; GRANT SELECT,DELETE ON VIDACHA TO PROCEDURE DELETE_KL; GRANT SELECT,DELETE ON GRAFIK_VIPLAT TO PROCEDURE DELETE_KL; GRANT SELECT,DELETE ON PLATEGI TO PROCEDURE DELETE_KL; GRANT EXECUTE ON PROCEDURE DELETE_KL TO SYSDBA; 1.3. Для таблицы PLATEGI. CREATE PROCEDURE DELETE_PL ( id_plat_a integer) as begin delete from plategi where ID_PLAT=:id_plat_a; suspend; end^ SET TERM; ^ GRANT SELECT,DELETE ON PLATEGI TO PROCEDURE DELETE_PL; GRANT EXECUTE ON PROCEDURE DELETE_PL TO SYSDBA; 1.4. Для таблицы SHTRAFI. CREATE PROCEDURE DELETE_SH ( id_shtrafa_a integer) as begin delete from shtraf WHERE ID_SHTRAFA=:id_shtrafa_a; suspend; end^ SET TERM; ^ GRANT SELECT,DELETE ON SHTRAF TO PROCEDURE DELETE_SH; GRANT EXECUTE ON PROCEDURE DELETE_SH TO SYSDBA; 1.5. Для таблицы TIP_KREDITA. CREATE PROCEDURE DELETE_TIP ( id_kredita_a integer) as begin delete from tip_kredita where ID_KREDITA=:id_kredita_a; suspend; end^ SET TERM; ^ GRANT SELECT,DELETE ON TIP_KREDITA TO PROCEDURE DELETE_TIP; GRANT EXECUTE ON PROCEDURE DELETE_TIP TO SYSDBA; 1.6. Для таблицы VIDACHA. CREATE PROCEDURE DELETE_VID ( id_klienta_a integer) as begin delete from vidacha where ID_KLIENTA=:id_klienta_a; suspend; end^ SET TERM; ^ GRANT SELECT,DELETE ON VIDACHA TO PROCEDURE DELETE_VID; GRANT EXECUTE ON PROCEDURE DELETE_VID TO SYSDBA; 2. Создание хранимых процедур на добавление. 2.1. В таблицу GRAFIK_VIPLAT. CREATE PROCEDURE INSERT_GR ( id_a integer, id_plat_rasch_a integer, data_plat_rasch_a date, ost_zadolgn_a integer, symma_plat_rasch_a integer, id_vidachi_a integer) as begin INSERT INTO grafik_viplat(id_plat_rasch, data_plat_rasch, ost_zadolgn,symma_plat_rasch,id_vidachi,id) values (:id_plat_rasch_a,:data_plat_rasch_a,:ost_zadolgn_a,:symma_plat_rasch_a,:id_vidachi_a,:id_a); suspend; end^ SET TERM; ^ GRANT INSERT ON GRAFIK_VIPLAT TO PROCEDURE INSERT_GR; GRANT EXECUTE ON PROCEDURE INSERT_GR TO SYSDBA; 2.2. В таблицу KLIENT. CREATE PROCEDURE INSERT_KL ( id_klienta_a integer, fio_a varchar(30), nomer_pasporta_a varchar(10), cobstv_a varchar(15), adres_a varchar(50), telefon_a varchar(11)) as begin INSERT INTO KLIENT (id_klienta,fio,nomer_pasporta,cobstv,adres,telefon) values (gen_id(gen_klient_id,1),:fio_a,:nomer_pasporta_a,:cobstv_a,:adres_a,:telefon_a); suspend; end^ SET TERM; ^ GRANT INSERT ON KLIENT TO PROCEDURE INSERT_KL; GRANT EXECUTE ON PROCEDURE INSERT_KL TO SYSDBA; 2.3. В таблицу PLATEGI. CREATE PROCEDURE INSERT_PL ( id_a integer, id_plat_a integer, data_plat_a date, sym_plat_a float, ostatok_a float, id_vidachi_a integer, itog_a float, shtrafi_a integer) as begin insert into plategi (id_plat,data_plat,sym_plat,ostatok,id_vidachi,ID,shtrafi,ITOG) values (gen_id(gen_plategi_id,7),:data_plat_a,:sym_plat_a,:ostatok_a,:id_vidachi_a,:ID_A,:shtrafi_a,:itog_a); suspend; end^ SET TERM; ^ GRANT INSERT ON PLATEGI TO PROCEDURE INSERT_PL; GRANT EXECUTE ON PROCEDURE INSERT_PL TO SYSDBA; 2.4. В таблицу SHTRAF. CREATE PROCEDURE INSERT_SH ( id_shtrafa_a integer, symma_shtrafa_a integer) as begin insert into shtraf (id_shtrafa,symma_shtrafa) values (:id_shtrafa_a,:symma_shtrafa_a); suspend; end^ SET TERM; ^ GRANT INSERT ON SHTRAF TO PROCEDURE INSERT_SH; GRANT EXECUTE ON PROCEDURE INSERT_SH TO SYSDBA;
2.5. В таблицу TIP_KREDITA. CREATE PROCEDURE INSERT_TIP ( id_kredita_a integer, procent_a integer, nazvanie_kredita_a varchar(20), srok_a integer, opisanie_a varchar(30)) as begin insert into tip_kredita(id_kredita,procent,nazvanie_kredita,srok,opisanie) values(:id_kredita_a,:procent_a,:nazvanie_kredita_a,:srok_a,:opisanie_a); suspend; end^ SET TERM; ^ GRANT INSERT ON TIP_KREDITA TO PROCEDURE INSERT_TIP; GRANT EXECUTE ON PROCEDURE INSERT_TIP TO SYSDBA; 2.6.В таблицу VIDACHA CREATE PROCEDURE INSERT_VID ( id_vidachi_a integer, symma_kredita_a integer, data_vidachi_a date, id_klienta_a integer, symma_kredita_poln_a integer, id_kredita_a integer) as begin insert INTO vidacha(id_vidachi,symma_kredita,data_vidachi,id_klienta, symma_kredita_poln,id_kredita) values (:id_vidachi_a,:symma_kredita_a,:data_vidachi_a,:id_klienta_a, :symma_kredita_poln_a,:id_kredita_a); suspend; end^ SET TERM; ^ GRANT INSERT ON VIDACHA TO PROCEDURE INSERT_VID; GRANT EXECUTE ON PROCEDURE INSERT_VID TO SYSDBA; 3. Создание хранимых процедур на обновление. 3.1. В таблице GRAFIK_VIPLAT CREATE PROCEDURE UPDATE_GR ( id_a integer, id_plat_rasch_a integer, data_plat_rasch_a date, ost_zadolgn_a integer, symma_plat_rasch_a integer, id_vidachi_a integer) as begin update grafik_viplat SET id_plat_rasch=:id_plat_rasch_a, data_plat_rasch=:data_plat_rasch_a,ost_zadolgn=:ost_zadolgn_a, symma_plat_rasch=:symma_plat_rasch_a,id_vidachi=:id_vidachi_a,ID=:id_a where id_plat_rasch=:id_plat_rasch_a; suspend; end^ SET TERM; ^ GRANT SELECT,UPDATE ON GRAFIK_VIPLAT TO PROCEDURE UPDATE_GR; GRANT EXECUTE ON PROCEDURE UPDATE_GR TO SYSDBA; 3.2. В таблице KLIENT. CREATE PROCEDURE UPDATE_KL ( id_klienta_a integer, fio_a varchar(30), nomer_pasporta_a varchar(10), cobstv_a varchar(15), adres_a varchar(50), telefon_a varchar(11)) as begin UPDATE klient SET id_klienta=:id_klienta_a,FIO=:fio_a,nomer_pasporta=:nomer_pasporta_a, cobstv=:cobstv_a,adres=:adres_a,telefon=:telefon_a where id_klienta=:id_klienta_a; suspend; end^ SET TERM; ^ GRANT SELECT,UPDATE ON KLIENT TO PROCEDURE UPDATE_KL; GRANT EXECUTE ON PROCEDURE UPDATE_KL TO SYSDBA; 3.3. В таблице PLATEGI. CREATE PROCEDURE UPDATE_PL ( id_a integer, id_plat_a integer, data_plat_a date, sym_plat_a numeric(4,1), ostatok_a numeric(4,1), id_vidachi_a integer, shtrafi_a integer, itog_a numeric(4,1)) as begin UPDATE plategi SET id_plat=:id_plat_a,data_plat=:data_plat_a,sym_plat=:sym_plat_a, ostatok=:ostatok_a,id_vidachi=:id_vidachi_a,ID=:id_a,SHTRAFI=:shtrafi_a,ITOG=:itog_a WHERE id_plat=:id_plat_a; suspend; end^ SET TERM; ^ GRANT SELECT,UPDATE ON PLATEGI TO PROCEDURE UPDATE_PL; GRANT EXECUTE ON PROCEDURE UPDATE_PL TO SYSDBA; 3.4. В таблице SHTRAF. CREATE PROCEDURE UPDATE_SH ( id_shtrafa_a integer, symma_shtrafa_a integer) as begin UPDATE shtraf SET ID_SHTRAFA=:id_shtrafa_a,symma_shtrafa=:symma_shtrafa_a WHERE id_shtrafa=:id_shtrafa_a; suspend; end^ SET TERM; ^ GRANT SELECT,UPDATE ON SHTRAF TO PROCEDURE UPDATE_SH; GRANT EXECUTE ON PROCEDURE UPDATE_SH TO SYSDBA; 3.5. В таблице TIP_KREDITA. CREATE PROCEDURE UPDATE_TIP ( id_kredita_a integer, procent_a integer, nazvanie_kredita_a varchar(20), srok_a integer, opisanie_a varchar(30)) as begin update tip_kredita SET id_kredita=:id_kredita_a,procent=:procent_a,nazvanie_kredita=:nazvanie_kredita_a, srok=:srok_a,opisanie=:opisanie_a WHERE id_kredita=:id_kredita_a; suspend; end^ SET TERM; ^ GRANT SELECT,UPDATE ON TIP_KREDITA TO PROCEDURE UPDATE_TIP; GRANT EXECUTE ON PROCEDURE UPDATE_TIP TO SYSDBA; 3.6.В таблице VIDACHA. CREATE PROCEDURE UPDATE_VID ( id_vidachi_a integer, symma_kredita_a integer, data_vidachi_a date, id_klienta_a integer, symma_kredita_poln_a float, id_kredita_a integer) as begin UPDATE VIDACHA SET id_vidachi=:id_vidachi_a,symma_kredita=:symma_kredita_a, data_vidachi=:data_vidachi_a,id_klienta=:id_klienta_a,symma_kredita_poln=:symma_kredita_poln_a, id_kredita=:id_kredita_a WHERE id_vidachi=:id_vidachi_a; suspend; end^ SET TERM; ^ GRANT SELECT,UPDATE ON VIDACHA TO PROCEDURE UPDATE_VID; GRANT EXECUTE ON PROCEDURE UPDATE_VID TO SYSDBA; 4. Создание хранимой процедуры на вычисление суммы кредита с учетом процентной ставки. CREATE PROCEDURE SYMMA ( id_vidachi_a integer, id_klienta_a integer, id_kredita_a integer) as declare variable x integer; declare variable y numeric(4,1); declare variable z numeric(4,1); begin SELECT VIDACHA.symma_kredita FROM VIDACHA WHERE VIDACHA.id_vidachi=:id_vidachi_a into:x; SELECT tip_kredita.procent FROM tip_kredita WHERE tip_kredita.id_kredita=:id_kredita_a INTO:Y; SELECT tip_kredita.srok FROM tip_kredita WHERE tip_kredita.id_kredita=:id_kredita_a INTO:Z; if (z=12) then update vidacha SET SYMMA_KREDITA_POLN=:x+:x*:y*0.01 where VIDACHA.id_vidachi=:id_vidachi_a; if (z<12) then update vidacha SET SYMMA_KREDITA_POLN=:x+:x*:z*:y/12*0.01 where VIDACHA.id_vidachi=:id_vidachi_a; if (z>12) then update vidacha SET SYMMA_KREDITA_POLN=:x+:x*:z/12*:y*0.01 where VIDACHA.id_vidachi=:id_vidachi_a; suspend; end^ SET TERM; ^ GRANT SELECT,UPDATE ON VIDACHA TO PROCEDURE SYMMA; GRANT SELECT ON TIP_KREDITA TO PROCEDURE SYMMA; GRANT EXECUTE ON PROCEDURE SYMMA TO SYSDBA; 5. Создание хранимой процедуры на построение графика выплат для заданного номера выдачи. CREATE PROCEDURE GRAFIK ( id_kredita_a integer, id_vidachi_a integer) returns ( s float, i integer, d date, x float, y float, p float) as begin i=0; while (i<1) do begin select vidacha.symma_kredita_poln FROM VIDACHA where VIDACHA.id_vidachi=:id_vidachi_a INTO:x; s=x; SELECT tip_kredita.srok from tip_kredita where tip_kredita.id_kredita=:id_kredita_a INTO:y; p=:X/:y; i=i+1; end i=0; select Vidacha.data_vidachi from vidacha where Vidacha.id_vidachi=:id_vidachi_a into:d; while(s>1) do begin i=i+1; d=d+31; s=s-p; INSERT into grafik_viplat (id_plat_rasch,DATA_PLAT_RASCH,OST_ZADOLGN,SYMMA_PLAT_RASCH,ID_VIDACHI,id) values (gen_id(gen_grafik_viplat,1),:D,:s,:p,:id_vidachi_a,:i); end s=0; suspend; end^ SET TERM; ^ GRANT SELECT ON VIDACHA TO PROCEDURE GRAFIK; GRANT SELECT ON TIP_KREDITA TO PROCEDURE GRAFIK; GRANT INSERT ON GRAFIK_VIPLAT TO PROCEDURE GRAFIK; GRANT EXECUTE ON PROCEDURE GRAFIK TO SYSDBA; 6. Создание хранимой процедуры на вычисление остатка от суммы кредита после выплаты платежа. CREATE PROCEDURE OST ( id_vidachi_a integer, id_a integer) as declare variable x float; declare variable y float; declare variable s float; begin if (id_a=1) then begin SELECT VIDACHA.symma_kredita_poln FROM VIDACHA WHERE VIDACHA.id_vidachi=:id_vidachi_a into:x; s=x; SELECT plategi.sym_plat FROM plategi WHERE (plategi.id=:id_a)and(plategi.id_vidachi=:id_vidachi_a) into:Y; s=s-y; update plategi SET OSTATOK=:s where (PLATEGI.id=:id_a)and(plategi.id_vidachi=:id_vidachi_a);end ELSE begin SELECT plategi.ostatok FROM PLATEGI WHERE (plategi.id=:id_a-1)and(plategi.id_vidachi=:id_vidachi_a) into:x; s=x; SELECT plategi.sym_plat FROM plategi WHERE (plategi.id=:id_a)and(plategi.id_vidachi=:id_vidachi_a) into:Y; s=s-y; update plategi SET OSTATOK=:s where (PLATEGI.id=:id_a)and(plategi.id_vidachi=:id_vidachi_a);end suspend; end^ SET TERM; ^ GRANT SELECT ON VIDACHA TO PROCEDURE OST; GRANT SELECT,UPDATE ON PLATEGI TO PROCEDURE OST; GRANT EXECUTE ON PROCEDURE OST TO SYSDBA; 7. Создание хранимой процедуры на отслеживание своевременных выплат платежей и начисление штрафов. CREATE PROCEDURE PROSROCH ( id_plat_a integer, id_vidachi_a integer) as declare variable d date; declare variable k date; declare variable s integer; begin select grafik_viplat.data_plat_rasch FROM grafik_viplat WHERE (grafik_viplat.id=:id_plat_a)and(grafik_viplat.id_vidachi=:id_vidachi_a) into:d; SELECT plategi.data_plat FROM plategi WHERE (PLATEGI.id=:id_plat_a)and(plategi.id_vidachi=:id_vidachi_a) INTO:k; SELECT SHTRAF.symma_shtrafa FROM shtraf WHERE SHTRAF.id_shtrafa=1 INTO:s; if ((D-K)<0) then begin update plategi set shtrafi=:s WHERE (PLATEGI.id=:id_plat_a)and(plategi.id_vidachi=:id_vidachi_a); end suspend; end^ SET TERM; ^ GRANT SELECT ON GRAFIK_VIPLAT TO PROCEDURE PROSROCH; GRANT SELECT,UPDATE ON PLATEGI TO PROCEDURE PROSROCH; GRANT SELECT ON SHTRAF TO PROCEDURE PROSROCH; GRANT EXECUTE ON PROCEDURE PROSROCH TO SYSDBA; 8. Создание хранимой процедуры на вывод информации о заданном клиенте. CREATE PROCEDURE KL ( id_klienta_a integer) returns ( fio varchar(30), cobstv varchar(15), nomer_pasporta varchar(10), adres varchar(50), telefon varchar(11), data_vidachi date, symma_kredita_poln float, id_klienta integer) as begin for select KLIENT.ID_KLIENTA,FIO,COBSTV,NOMER_PASPORTA,ADRES,TELEFON,DATA_VIDACHI,SYMMA_KREDITA_POLN FROM klient inner join VIDACHA ON (KLIENT.id_klienta=:id_klienta_a)AND(VIDACHA.ID_KLIENTA=KLIENT.id_klienta) into:id_klienta,:fio,:cobstv,:nomer_pasporta,:adres,:telefon,:data_vidachi,:symma_kredita_poln do suspend; end^ SET TERM; ^ GRANT SELECT ON KLIENT TO PROCEDURE KL; GRANT SELECT ON VIDACHA TO PROCEDURE KL; GRANT EXECUTE ON PROCEDURE KL TO SYSDBA; 9. Создание хранимой процедуры на вывод информации о платежах по заданному клиенту. CREATE PROCEDURE PLAT ( id_vidachi_a integer) returns ( id_vidachi integer, sym_plat float, id_plat integer, data_plat date, id integer, itog float, ostatok float, shtrafi integer) as begin for select ID_PLAT,id, id_vidachi,sym_plat,ostatok,itog, data_plat,shtrafi FROM PLATEGI where id_vidachi =:id_vidachi_a into:id_plat,:id,:id_vidachi,:sym_plat,:ostatok,:itog,:data_plat,:shtrafi do suspend; end^ SET TERM; ^ GRANT SELECT ON PLATEGI TO PROCEDURE PLAT; GRANT EXECUTE ON PROCEDURE PLAT TO SYSDBA; 10. Создание хранимой процедуры на вывод графика платежей по заданному клиенту. CREATE PROCEDURE GR ( id_vidachi_a integer) returns ( id_vidachi integer, symma_plat_rasch float, id_plat_rasch integer, data_plat_rasch date, id integer, ost_zadolgn float) as begin for select ID_PLAT_RASCH,id, id_vidachi,symma_plat_rasch,ost_zadolgn, data_plat_rasch FROM grafik_viplat where id_vidachi =:id_vidachi_a into:id_plat_rasch,:id,:id_vidachi,:symma_plat_rasch,:ost_zadolgn,:data_plat_rasch do suspend; end^ SET TERM; ^ GRANT SELECT ON GRAFIK_VIPLAT TO PROCEDURE GR; GRANT EXECUTE ON PROCEDURE GR TO SYSDBA;
|