plsql - Inserting new id from sequence into table column id and insert that same id to another table without dual table -
i have table:
create table osoba (osoba_id number, ime_osobe varchar2(200), prezime_osobe varchar2(200), kartica_id number) create table kartica (kartica_id number, dozvoljen_ulaz_id number)
i need make procedure that
- inserts data table
osoba
. - checks if there data in table
kartica
columnkartica_id
.- if there is, add id column
kartica_id
in tableosoba
. - if there isn't, add
kartica_id
inkartica
table sequence have created , add newly createdkartica.kartica_id
kartica_id
in tableosoba
.
- if there is, add id column
kartica_id
in kartica
, kartica_id
in osoba
must unique , 1 kartica_id
1 record in osoba
exact record (person added).
if there kartica_id
added osoba.kartica_id
of same value throw error message 'kartica_id exists. no same values allowed.'
, insert next new kartica_id
value kartica
table , pass value kartica_id
in table osoba
.
i'm new pl/sql got far:
create or replace procedure insertosoba ( o_osoba_id in osoba.osoba_id%type default generate_id.nextval, o_ime_osobe in osoba.ime_osobe%type, o_prezime_osobe in osoba.prezime_osobe%type, o_kartica_id in kartica.kartica_id%type default null --must optional ) begin insert osoba (osoba_id,ime_osobe,prezime_osobe,kartica_id) values (o_osoba_id,o_ime_osobe,o_prezime_osobe,o_kartica_id); end insertosoba;
i'm posting solution use kartica_seq
sequence kartica_id
. please replace sequence name or create sequence if not exist.
create sequence kartica_seq start 1 increment 1;
create or replace procedure insertosoba ( o_osoba_id in osoba.osoba_id%type default generate_id.nextval, o_ime_osobe in osoba.ime_osobe%type, o_prezime_osobe in osoba.prezime_osobe%type, o_kartica_id in kartica.kartica_id%type default null --must optional ) cnt number; cnt2 number; begin select count(*) cnt osoba kartica_id = o_kartica_id; select count(*) cnt2 kartica kartica_id = o_kartica_id; if(cnt = 0) --there no person such kartica , user passed existing kartica_id if(o_kartica_id not null , cnt2 > 0) insert osoba (osoba_id,ime_osobe,prezime_osobe,kartica_id) values (o_osoba_id,o_ime_osobe,o_prezime_osobe,o_kartica_id); --there no person such kartica need create 1 entry using sequence else insert kartica (kartica_id) values (kartica_seq.nextval); insert osoba (osoba_id,ime_osobe,prezime_osobe,kartica_id) values (o_osoba_id,o_ime_osobe,o_prezime_osobe,kartica_seq.currval); end if; end if; --there person such kartica if(cnt > 0) dbms_output.put_line('kartica_id exists. no same values allowed.'); --or raise exception here end if; commit; end insertosoba;
Comments
Post a Comment