Solved

Triggers in Interbase for Delphi

Posted on 2002-07-21
14
250 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
ID: 7167928
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
ID: 7167931
sorry, this header
procedure Tfr_ppo.tblPPOBeforePost(DataSet: TDataSet);

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

;-)
0
 
LVL 3

Expert Comment

by:cubud
ID: 7168101
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 7179520
hello?
0
 

Author Comment

by:Chalo
ID: 7187125
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
ID: 7187149
>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
ID: 7187171
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7187192
>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
ID: 7187200
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
ID: 7187201
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7187204
>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
ID: 7187206
Ok, i will appreciate that...
Please, have in mind the subject "Delphi-Interbase Triggers"
0
 

Expert Comment

by:CleanupPing
ID: 9343022
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
ID: 9461363
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2 60
How to create virtual printer while installing my application 5 52
Not able to call Delphi XE10 dll function from Delphi 6.0 4 28
enhance the following code 3 32
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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