database trigger instead of auto-increment
database trigger instead of auto-increment
We going to use a trigger instead of auto-increment. How are the MyDAC-componments working with this trigger?
So, I created a trigger on a table
It's working. But there's a disadvantage. It's not returning the new key value and you have to use myquery.refresh to see the new renerated values.
Myquery.refreshrecord doesn't work. The component looks to a refference with the value NULL, but doesn't exists (see log from dbmonitor) and Delphi tells: "refresh failed. Found 0 records"
Is this the only way to accomplish new records into the dataset.
Code: Select all
CREATE
DEFINER = 'john'@'%'
TRIGGER test.trigger1
BEFORE INSERT
ON test.testtable
FOR EACH ROW
BEGIN
DECLARE volgnummer int;
SELECT MAX(ID)
FROM testtable
INTO volgnummer;
SET NEW.ID = volgnummer + 1;
ENDMyquery.refreshrecord doesn't work. The component looks to a refference with the value NULL, but doesn't exists (see log from dbmonitor) and Delphi tells: "refresh failed. Found 0 records"
Is this the only way to accomplish new records into the dataset.
Code: Select all
log DBMonitor
-----------------
C:\Users\john\Documents\RAD Studio\Projects\triggertest\Project5.exe
05-02-2009 16:44:24 n/a MyDAC monitoring is started. Complete
05-02-2009 16:44:24 0:0.141 Connect: john@localhost Complete
05-02-2009 16:44:25 0:0.0 SQL Execute: select * from testtable
Complete
05-02-2009 16:44:44 0:0.31 SQL Execute: INSERT INTO testtable
(name, telephone)
VALUES
(?, ?)
:name(String[13],IN)='refreshrecord'
:telephone(String[7],IN)='3433444' Complete
05-02-2009 16:44:44 0:0.0 SQL Execute: select * from testtable
WHERE
testtable.ID IS NULL
Complete
Oke. Now there's a property in myquery.options.returnparams. This is what help says:
I don't use SQLInsert, but I know when record will be posted, MyQuery creates internally SQL statements. Are these SQL's using SQLInsert? If it is, it should return a new value. As far as I know the trigger will be executed before the actual insert goes to the table.Syntax
property ReturnParams: boolean;
Remarks
Use the ReturnParams property to return the new value of the fields to dataset after insert or update. Actual value of field after insert or update may be different from the value stored in local memory if the table has a trigger. When ReturnParams is True, OUT parameters of SQLInsert and SQLUpdate statements is assigned to corresponding fields. The default value is False.