Solved

Triggers in Interbase for Delphi

Posted on 2002-07-21
14
254 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 157
Connection between libmysql.dll and MySQL Versions 7 113
Adoquery sql  left join does not work 25 99
Firemonkey allowing RTL on android 6 47
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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