Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: And another one about PL/SQL
"Howard J. Rogers" <dba_at_hjrdba.com> wrote in message news:<aepmec$tbf$1_at_lust.ihug.co.nz>...
> Sorry about this...
>
> If I am inserting a record into a child table, is it possible to have a
> before trigger consult the parent table to retrieve a value to insert into
> the child record?
>
> For example, a table SITES has a CATEGORY. That is supposed to be used as
> the default CATEGORY for each of the features that are eventually housed on
> a site.
>
> For example. My site has a code BRITPK, category XXX.
>
> I am inserting a child record that says site:BRITPK feature:BR1 plot:1,
> category unspecified.
>
> I want the before insert trigger to select CATEGORY from SITES where
> SITECODE=the sitecode of the child record I am inserting, and thus to
> complete the child record.
>
> Is that even remotely possible? Sounds a bit recursive to me. I was trying
> something like this:
>
> create or replace trigger FEATURE_DETAILS
> before insert on SITEFEATURES
> for each row
> begin
> cursor get_data is
> select sitecode, category from sites where sitecode=:NEW.sitecode;
> select get_data.category into :NEW.category;
> end;
> /
>
> But it's producing a slew of compilation errors. Which says something about
> my development prowess, I guess.
Since triggers are known to be very unstable, I would highly recommend
using PACKAGES.
Slap a PACKAGE against your child table which will have an INSERT
procedure to handle all INSERTS against your child table. Assuming you
have these tables with some data,
SITECODE as
site_code VARCHAR2(20), category VARCHAR2(20) SITEFEATURES as site_code VARCHAR2(20), feature VARCHAR2(20), plot VARCHAR2(10), category VARCHAR2(20)
Create your package...
PROCEDURE Insert_Site_Features (
site_ IN VARCHAR2,
feature_ IN VARCHAR2,
plot_ IN VARCHAR2 );
END Site_Features_Pkg;
/
SHOW ERROR CREATE OR REPLACE PACKAGE BODY Site_Features_Pkg AS
FUNCTION Get_Category (
site_code_ IN VARCHAR2 ) RETURN VARCHAR2 IS
category_ VARCHAR2(20) := NULL;
CURSOR cur_ IS
SELECT category FROM sitecode WHERE site_code = site_code_;
PROCEDURE Insert_Site_Features (
site_ IN VARCHAR2,
feature_ IN VARCHAR2,
plot_ IN VARCHAR2 )
IS
category_ VARCHAR2(20);
BEGIN
category_ := Get_Category(site_);
END Site_Features_Pkg;
/
SHOW ERROR
HTH
//Rauf Sarwar
Received on Wed Jun 19 2002 - 17:05:55 CDT