![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
... or a "deleted" reference in a INSERT trigger. I have two near identical dbs, on the same SQLA 9 server. The trigger belows works fine on db 1 for INSERT, UPDATE and DELETE; however on db 2 the DELETE scenario results in error: "Table 'inserted' not found" There must be a db option involved, but what is it? Joel CREATE TRIGGER HistBudget_ChgMgr AFTER INSERT, UPDATE, DELETE ON DBA.Budget REFERENCING NEW as inserted OLD AS deleted FOR EACH STATEMENT WHEN (EXISTS (select 1 from inserted where inserted.DataFrom = 'Manual') OR EXISTS (select 1 from deleted where deleted.DataFrom = 'Manual')) -- 11/20/07 JSC Adapted from CPLA format BEGIN declare EventIDv integer ; IF INSERTING THEN SET EventCodeIDV = 31 ; --,32,33 ELSEIF UPDATING THEN SET EventCodeIDV = 35 ; --,36,37 ELSEIF DELETING THEN SET EventCodeIDV = 39; -- ,40,41 END IF; INSERT INTO EVENT (EventCodeID,Description,PreviousEventID) select EventCodeIDV,EC.Name,NULL from EventCode EC where EC.EventCodeID = EventCodeIDV; select @@identity into EventIDv ; IF INSERTING OR UPDATING THEN INSERT into HistBudget ( "EventID" , "ChangeID" , "Work_order" , "Contractor" ..... (abbr to keep size down) ) Select EventIDv , "ChangeID" , "Work_order" , "Contractor" ..... from inserted where inserted.DataFrom = 'Manual'; ELSE INSERT into HistBudget ( "EventID" , "ChangeID" , "Work_order" , "Contractor" ..... ) Select EventIDv , "ChangeID" , "Work_order" , "Contractor" ..... from deleted where deleted.DataFrom = 'Manual'; END IF ; END ; |
#3
| |||
| |||
|
|
Hmmm ... a when-clause in a statement trigger ... you working case is probably just ignoring that ... but the docs clearly state WHEN clause The trigger fires only for rows where the search-condition evaluates to true. The WHEN clause can be used only with ***row level triggers***. your's is clearly not a row-level trigger ... and you seem to have fallen into a dark corner ... I would try removing it ... and then consider an alternate implementation. JSCARLSON> wrote ... or a "deleted" reference in a INSERT trigger. I have two near identical dbs, on the same SQLA 9 server. The trigger belows works fine on db 1 for INSERT, UPDATE and DELETE; however on db 2 the DELETE scenario results in error: "Table 'inserted' not found" There must be a db option involved, but what is it? Joel CREATE TRIGGER HistBudget_ChgMgr AFTER INSERT, UPDATE, DELETE ON DBA.Budget -- 11/20/07 JSC Adapted from CPLA format BEGIN declare EventIDv integer ; IF INSERTING THEN SET EventCodeIDV = 31 ; --,32,33 ELSEIF UPDATING THEN SET EventCodeIDV = 35 ; --,36,37 ELSEIF DELETING THEN SET EventCodeIDV = 39; -- ,40,41 END IF; INSERT INTO EVENT (EventCodeID,Description,PreviousEventID) select EventCodeIDV,EC.Name,NULL from EventCode EC where EC.EventCodeID = EventCodeIDV; select @@identity into EventIDv ; IF INSERTING OR UPDATING THEN INSERT into HistBudget ( "EventID" , "ChangeID" , "Work_order" , "Contractor" ..... (abbr to keep size down) ) Select EventIDv , "ChangeID" , "Work_order" , "Contractor" ..... from inserted where inserted.DataFrom = 'Manual'; ELSE INSERT into HistBudget ( "EventID" , "ChangeID" , "Work_order" , "Contractor" ..... ) Select EventIDv , "ChangeID" , "Work_order" , "Contractor" ..... from deleted where deleted.DataFrom = 'Manual'; END IF ; END ; |
#4
| |||
| |||
|
|
It doesn't really answer my question, which was: Why does the trigger (in the original format) perform as expected in one db and not the other db. |
|
Thanks for the reply Nick. I have made the needed changes to get the trigger(s) to work as a statement level trigger. See attached script for the solution I used. It doesn't really answer my question, which was: Why does the trigger (in the original format) perform as expected in one db and not the other db. Seriously, the tables are all but identical, as are the test cases. Joel |
![]() |
| Thread Tools | |
| Display Modes | |
| |