|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
trigger
hello,
i have a entity with two key-attributes and one attribute 'number'. if i make an insert and the the two keys exit already, the 'number' should be incremented and the insert-command should not be executed. if the keys don't exist the insert should be executed. is there a possibility to do that with a trigger? IF(OLD.KEY1 == NEW.KEY1) UPDATE ... increment number ELSE ... drop command ????????? thanx |
|
#2
|
||||
|
||||
|
the best way to do that is to check the existence before inserting the row ... but if for any reason you can't do that you can try yhis aproach:
let's suppose you have a table "TABLE1" that has the following data: FIELD1|FIELD2|FIELD3 1 2 NUMBER (this is the number that you want to increment) create a store procedure that takes 2 parameters (the table's fields that need the existence check) execute procedure FIND_DUPLICATES(field1,field2); the procedure code: Code:
if (SELECT COUNT(*) FROM TABLE1 WHERE FIELD1=:field1 and FIELD2=:field2=1) then //IF THERE IS ALREADY 1 ROW WITH THOSE VALUES UPDATE TABLE1 SET NUMBER=NUMBER+1 WHERE FIELD1=field1 and FIELD2=field2; else //IT SHOULD INSERT THE ROW INSERT INTO TABLE1(FIELD1,FIELD2,FIELD3) VALUES(:field1,:field2,1); the above code is written entirely out of my head (i don't have a firebird server available right now) but it should give you an idea on how to code the logic on this one. If you need further help don't hesitate to ask, Good luck.
__________________
If i've been helpful, please add to my reputation. My unfinished site: http://www.dever.ro |
|
#3
|
|||
|
|||
|
ok, thanx a lot, i try to make your suggestion work.
|
![]() |
| Viewing: Dev Shed Forums > Databases > Firebird SQL Development > trigger |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|