Subject | Re: newbie on SPs: How to imlement a wlhile not eof loop |
---|---|
Author | Adam |
Post date | 2005-02-15T00:22:26Z |
Hi Carol,
After reading your initial post, I was not quite sure what you were
trying to accomplish. You seemed pretty confident that you were after
a trigger, but I for one am not sure whether that will best suit your
purpse. Perhaps if you can provide an example with a couple of
records and your desired behaviour, we can be a bit more assistance.
the returned records. I don't think you will need suspend though if
you are only using a trigger.
SPs can have undesirable consequences. For example, if you call a SP
from a trigger, and then that SP inserts / updates / deletes
something, another trigger (or even the same one) will fire, and if
you are not careful, you may get into an infinite loop (although from
memory FB gives up after a while).
Forgive me if I have interpreted your pseudo code wrong. As I
understand, you want to insert each tc value into tb alongside the
current ta id?
Your before insert trigger code could look like:
begin
for select distinct campo
from tc
into :campo
do
begin
insert into tb (id, campo) values (new.id, :campo);
end
end
^
You would need to make sure that tc wasn't too big though, as every
insert into ta would need to then read the entire tc, and then insert
that many records into tb
Adam
--- In firebird-support@yahoogroups.com, "Carol Milius" <lista@m...>
wrote:
After reading your initial post, I was not quite sure what you were
trying to accomplish. You seemed pretty confident that you were after
a trigger, but I for one am not sure whether that will best suit your
purpse. Perhaps if you can provide an example with a couple of
records and your desired behaviour, we can be a bit more assistance.
> A question: The SUSPEND stops the routine? (Like an EXIT in code?)Nope, suspend does not exit the routine. It just adds another row to
the returned records. I don't think you will need suspend though if
you are only using a trigger.
> A Question: Is common a SP crash the Server?No, not unless your SP calls a UDF function which crashes, BUT
SPs can have undesirable consequences. For example, if you call a SP
from a trigger, and then that SP inserts / updates / deletes
something, another trigger (or even the same one) will fire, and if
you are not careful, you may get into an infinite loop (although from
memory FB gives up after a while).
Forgive me if I have interpreted your pseudo code wrong. As I
understand, you want to insert each tc value into tb alongside the
current ta id?
Your before insert trigger code could look like:
begin
for select distinct campo
from tc
into :campo
do
begin
insert into tb (id, campo) values (new.id, :campo);
end
end
^
You would need to make sure that tc wasn't too big though, as every
insert into ta would need to then read the entire tc, and then insert
that many records into tb
Adam
--- In firebird-support@yahoogroups.com, "Carol Milius" <lista@m...>
wrote:
> >> The SUSPEND is if your SP has an output variable.Insert.
> >> Every SUSPEND pushes one record out of the SP
> >> with the current value assigned to it.
>
> hmmm... it explains a lot of exemples i've seen.
> Now I am begining to understand 'how' a SP works..
>
>
> >>Now you are doing an "AfterPost" event which I take to mean After
>record
> Yes, you are right.
> :)
>
> >>The trigger you are using to do this only works with the current
> >>only. So your code would be working with the whole table everytime a
> >>record is inserted. I am not sure that is what you intended. Idon't think
> >>you need to loop through all the records every time.You then want
>
> >>I think what you want is the After Insert trigger on Table A.
> >>to insert a record that contains the FIELD value from Table C,and the ID
> >>value from Table A into Table B.inserted)
>
> I need both.
>
> If I need to work with WHOLE table, will this work?
>
> //Table_A.AfterInstert
>
> FOR SELECT A_FIELD FROM TABLE_C1 DO
> BEGIN
> INSERT INTO TABLE_B(ID,FIELD) VALUES(NEW.ID, A_FIELD);
> SUSPEND;
> END
>
> (Reading this the impression I get is that only one record will be
>DAO,
>
>
> >> Hope this helps,
>
> Very much!
>
> Carol.
>
>
>
>
>
> I was much in the same boat, myself. First things first...
>
> >while not TC.EOF do
>
> There is no such thing for SP's. That kind of code is only in ADO,
> etc.statement.
>
> In order to loop through records you need to do an actual SELECT
> If you wanted all records from a table it would be SELECT * FROMTABLE.
>You can put
> To work with it in SP, you would use the following statment:
>
> FOR SELECT * FROM TABLE1 DO
> BEGIN
> <Code>
> SUSPEND;
> END
>
> This would select all records, but you are not limited to that.
> any kind of WHERE, ORDER BY, GROUP CLAUSE you want to use. TheSUSPEND is
> if your SP has an output variable. Every SUSPEND pushes one recordout of
> the SP with the current value assigned to it.Insert.
>
> Now you are doing an "AfterPost" event which I take to mean After
> The trigger you are using to do this only works with the currentrecord
> only. So your code would be working with the whole table everytime a
> record is inserted. I am not sure that is what you intended. Idon't think
> you need to loop through all the records every time.then want
>
> I think what you want is the After Insert trigger on Table A. You
> to insert a record that contains the FIELD value from Table C, andthe ID
> value from Table A into Table B. I think this is what you wantthen.
>TABLE_A.
> DECLARE VARIABLE TEMP_VAL VARCHAR(50);
>
> BEGIN
>
> SELECT FIELD FROM TABLE_C WHERE TABLE_C.ID = NEW.ID INTO :TEMP_VAL;
>
> INSERT INTO TABLE_B(ID,FIELD) VALUES(NEW.ID,:TEMP_VAL);
>
> END
>
>
> The "NEW" allows you access the current values for the record in
> NEW.ID and NEW.FIELD represent what was just inserted into TABLE_A.not
>
>
>
> Hope this helps, and anybody else please comment on this.
>
> -----Original Message-----
> From: Carol Milius [mailto:lista@m...]
> Sent: Monday, February 14, 2005 10:13 AM
> To: firebird-support@yahoogroups.com
> Subject: [firebird-support] newbie on SPs: How to imlement a wlhile
> eof loopa 'little
>
>
>
> Hi!
>
> I am reading some manual, inlcuding material on IBPhoenix but I am
> slow' and the code I wrote is not working.. any tips?
>
> Tables:
> Table A (ID:integer; FIELD:String)
> Table B (ID:integer; FIELD:String)
> Table C (ID:integer; FIELD:String)
>
> ID's are set by Generator (Trigger onBeforeInsert).
>
> The Goal:
> I would like to call a procedure on AfterPost event.
> This procedure would insert records on table B (TB)
> The records to be inserted on TB are from TA and TC
>
> Something like:
>
> Begin
> // AfterPost on TA
> begin
> while not TC.EOF do
> begin
> TB.ID := TA.ID
> TB.Campo := TC.Campo
> TC.next;
> end;
> end;
> end;
>
>
> Best Regards,
>
>
> Carol.