Solved

Triggers in Interbase for Delphi

Posted on 2002-07-21
14
245 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

18 Experts available now in Live!

Get 1:1 Help Now