Solved

Triggers in Interbase for Delphi

Posted on 2002-07-21
14
242 Views
Last Modified: 2010-04-04
So as we all know, in Interbase we can´t increment for saying IdProduct with ONE. Therefore we need a trigger who calls a generatorID. For example:

**********
CREATE TRIGGER IdProduct FOR TblProducts
BEFORE INSERT
AS
BEGIN
NEW.IdProduct=GEN_ID(ProductGen,1);
END
**********

... knowing that the syntexis for the generator is :
**********
Create Generator ProductGen
**********

My question is: How can i create a trigger or Procedure who can transfer at the same time the NEW.IdProduct to another table for example TblProductDescription and at the same time this table need to autoincrement the field IdProductDescription with 1.....

TblProduct with fields:
IdProduct <----------------------------
Code                                          |
Name                                          |
Price                                         |
                                              |
TblProductDescription with fields:            |
IdProductDescription                          |
IdProduct <----------------------------
Description
Stock
Rate

0
Comment
Question by:Chalo
14 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
instead of using a trigger use a stored-proecdure for this like

--Interbase

CREATE PROCEDURE TblProducts_NEXT_ID  RETURNS (
  ID INTEGER
) AS
BEGIN
  ID = GEN_ID(ProductGen,1);
END

now you could use a storedproc-object and supply the id in the beforepost-event, if in insert-state like

--Delphi

Function Tform1.Get_Next_Id(SPName : String) : Integer;
begin
  Try
    StoredProc1.StoredProcName := SPName;
    StoredProc1.ExecProc;
    Result :=
      StoredProc1.ParamByName('ID').AsInteger;
  Except
    Raise;
  end;
end;

procedure Tfr_ppo.tblPPOBeforePost(DataSet: TDataSet);
begin
  if TblProducts.State = dsInsert then
  begin
    TblProducts.FieldByName('ID').AsInteger :=  get_next_id(TblProducts_NEXT_ID');
    //now you could use the value for assigning to the details
    fnewPPOID := TblProducts.FieldByName('ID').AsInteger;  //store in a var for ex.
  end;
end;

just as suggestion

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 90 total points
Comment Utility
sorry, this header
procedure Tfr_ppo.tblPPOBeforePost(DataSet: TDataSet);

should be
procedure Tform1.TblProductsBeforePost(DataSet: TDataSet);

;-)
0
 
LVL 3

Expert Comment

by:cubud
Comment Utility
I do this completely differently.

PurchaseOrder
=============
ID INTEGER NOT NULL,
OrderNumber INTEGER,
OtherColumns

PurchaseOrderLine
=================
ID INTEGER NOT NULL,
PURCHASEORDERID INTEGER NOT NULL,
OtherColumns

The PurchaseOrder table has an ID column which means nothing more than a unique ID for the records in the child table. I do
"SELECT GEN_ID(NEXTPOID, 1) FROM RDB$DATABASE AS NEXTID"
after I insert a PurchaseOrder so that the Purchase order has an ID for the children to point to.

Once the purchase order + lines have been posted without problems, and just before I commit my transaction, I then do
"SELECT GEN_ID(NEXTPONUMBER, 1) FROM RDB$DATABASE AS NEXTID"
and assign it to the OrderNumber column.

This means that the ID of the main table is meaningless, so I can therefore assign it at the start and not care if it is not comitted and thus causing the numbers to be out of sequence.

Pete
====
Record, Play, Read, Write, Compress audio files. http://www.DroopyEyes.com
Read / write articles about just about anything http://www.HowToDoThings.com
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hello?
0
 

Author Comment

by:Chalo
Comment Utility
Sorry, i was on vacation.......
I've tried the examples of both kretzschmar and cubud, but i think that i'm doing something wrong....

Is it possible to send me some examples? My adress is Chalo34@Hotmail.Com  
Please write as subject: "Delphi-Interbase Triggers"
This as security. If not, all emails i 'don't know, will be deleted without reading.

PS: How can i declare a variable in interbase to store any graphic or picture (extension *.jpg)?
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
>How can i declare a variable in interbase to store any
>graphic or picture (extension *.jpg)?

the type must be blob
0
 

Author Comment

by:Chalo
Comment Utility
The Blob type is only for text, isn't it?
Example:

"GRAPHIC" Blob Sub_Type BLR SEGMENT SIZE 80

With this declaration an error occures....
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
>The Blob type is only for text, isn't it?
Binary Large OBject can store anything

well, have the subtypes not in mind yet
0
 

Author Comment

by:Chalo
Comment Utility
Ok, let me try....
What about the triggers??
Are you using MSN Messenger??? Perhaps we can have a little conversation?
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
>Are you using MSN Messenger???
no, sorry, but i can send you a sample this evening
(in ~11 hours, i have no delphi nor interbase on hand yet)

meikl ;-)
0
 

Author Comment

by:Chalo
Comment Utility
Ok, i will appreciate that...
Please, have in mind the subject "Delphi-Interbase Triggers"
0
 

Expert Comment

by:CleanupPing
Comment Utility
Chalo:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
Comment Utility
Chalo,
No comment has been added lately (18 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to kretzschmar http:#7167931

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now