Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Firebird cursor loop in triggers

Posted on 2007-10-12
8
Medium Priority
?
3,861 Views
Last Modified: 2013-12-09
Hi,


Firebird 2.x

Can anyone tell me if its possible to create a cursor loop in a firebird trigger?  If not can it be done using a SP and calling from the trigger?

The scenario is that I have 3 tables, A, B and C.  A and B are master detail and C is a lookup for B.  After an insert of a record into table A I want to selected all records in table C and insert them into table B, key being A (PK) + C (PK).  Therefore I want an after insert trigger on A to insert a row in B for each row in C.

Hope this makes sense!!!

Many thanks
0
Comment
Question by:Donoss
  • 4
  • 4
8 Comments
 
LVL 19

Accepted Solution

by:
NickUpson earned 2000 total points
ID: 20066155
possible yes, but not necessary

in the trigger or SP, your choice

for select 1 from tableC do begin
  insert into tableB (whatever fields), values (whatever values)
end

or even more tidy, 1 statement

insert into tableB( whatever fields) select (whatever fields or constant values) from tableC;
0
 

Author Comment

by:Donoss
ID: 20079380
Hi,

I need to set the primary key of table B to that of both table A and C so your second statement will not work, I have however tied the first statement but get a compile error at the 'Do'.  Code is as follows:

BEGIN
  for select
    REF_ID,
    QUESTION,
    COMMENTS
  from DSE_ASSESSMENT_QUESTION_LIST
  do begin
    insert into DSE_ASSESSMENT_QUESTION(
      DSE_ASSESSMENT_NBR,
      REF_ID,
      QUESTION,
      COMMENTS)
    values(
      DSE_ASSESSMENT_QUESTION.DSE_ASSESSMENT_NBR,
      DSE_ASSESSMENT_QUESTION_LIST.REF_ID,
      DSE_ASSESSMENT_QUESTION_LIST.QUESTION,
      DSE_ASSESSMENT_QUESTION_LIST.COMMENTS);
  END
END

Any help will be much appreciated.

Many thanks
0
 

Author Comment

by:Donoss
ID: 20079412
The whole trigger is:

CREATE TRIGGER DSE_ASSESSMENT_AI FOR DSE_ASSESSMENT
ACTIVE AFTER INSERT
POSITION 11
AS
BEGIN
  for select
    REF_ID,
    QUESTION,
    COMMENTS
  from DSE_ASSESSMENT_QUESTION_LIST
  do begin
    insert into DSE_ASSESSMENT_QUESTION(
      DSE_ASSESSMENT_NBR,
      REF_ID,
      QUESTION,
      COMMENTS)
    values(
      DSE_ASSESSMENT_QUESTION.DSE_ASSESSMENT_NBR,
      DSE_ASSESSMENT_QUESTION_LIST.REF_ID,
      DSE_ASSESSMENT_QUESTION_LIST.QUESTION,
      DSE_ASSESSMENT_QUESTION_LIST.COMMENTS);
  END
END;

and I get the following compile error:

Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 11, column 3.
Do.


0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 19

Expert Comment

by:NickUpson
ID: 20080287
CREATE TRIGGER DSE_ASSESSMENT_AI FOR DSE_ASSESSMENT
ACTIVE AFTER INSERT
POSITION 11
AS
BEGIN
  for select
    REF_ID,
    QUESTION,
    COMMENTS
  from DSE_ASSESSMENT_QUESTION_LIST
into :a,:b,:c                                                                /* need to define these as well */
  do begin
    insert into DSE_ASSESSMENT_QUESTION(
      DSE_ASSESSMENT_NBR,
      REF_ID,
      QUESTION,
      COMMENTS)
    values(
      DSE_ASSESSMENT_QUESTION.DSE_ASSESSMENT_NBR,
      DSE_ASSESSMENT_QUESTION_LIST.REF_ID,
      DSE_ASSESSMENT_QUESTION_LIST.QUESTION,
      DSE_ASSESSMENT_QUESTION_LIST.COMMENTS);
  END
END;
0
 

Author Comment

by:Donoss
ID: 20081092
Nick,

are a, b, and c local variables?  which aare then used in the insert?
0
 
LVL 19

Expert Comment

by:NickUpson
ID: 20081436
yes, that's correct
0
 

Author Comment

by:Donoss
ID: 20081508
Nick,

I managed to get it sorted using:

insert into DSE_ASSESSMENT_QUESTION (
    DSE_ASSESSMENT_NBR,
    REF_ID,
    QUESTION,
    COMMENTS)
  select
    new.DSE_ASSESSMENT_NBR,
    REF_ID,
    QUESTION,
    COMMENTS
  from DSE_ASSESSMENT_QUESTION_LIST;

I didn't try the other solution but you where right in your original post so I'll aware you all the points, many thanks for your help and immediate response.

0
 
LVL 19

Expert Comment

by:NickUpson
ID: 20081572
yep, that is the most efficient as well
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question