Create Trigger - Insert, Update - not updating record on Insert

Create Trigger - Insert, Update - not updating record on Insert

Post by James TerHar » Fri, 18 Jan 2002 06:00:37



When entering a value into the field for the first time on
an Insert, the trigger does not fire.

If you modify data in the field with the trigger, the
trigger will fire.

Cannot determine why the trigger will not fire.  No errors
are generated.

Win 2000 Professional, SQL 2000 SP2

  trSLA_Hours.txt
1K Download
 
 
 

Create Trigger - Insert, Update - not updating record on Insert

Post by oj » Fri, 18 Jan 2002 06:20:21


james,

there are a few flaws with this:
1. you're testing on update(completed) which is not true when, say, you
execute this
    insert tasks(opendate) select getdate()   --completed column is not
updated

and new completed value is '01/01/01'

--
-oj
http://rac4sql.home.attbi.com


Quote:> When entering a value into the field for the first time on
> an Insert, the trigger does not fire.

> If you modify data in the field with the trigger, the
> trigger will fire.

> Cannot determine why the trigger will not fire.  No errors
> are generated.

> Win 2000 Professional, SQL 2000 SP2


 
 
 

Create Trigger - Insert, Update - not updating record on Insert

Post by Scott Morri » Fri, 18 Jan 2002 06:31:13


Just to expand on this a bit ...

During an insert, the deleted table is empty.  Your logic is dependent on the
presence of rows in the deleted table.  Under this situation, your last IF

state as opposed to TRUE).  You should also change your logic to support
multi-row inserts/updates.  Seems like all of your logic could be accomodated in
a single update statement.  Your logic is also flawed the other way if the
completed column allows NULLs and if something that was completed can be
"uncompleted" (as in a correction). Under this circumstance, the select
statement in the last IF returns NULL resulting in the unknown state again.
However, your system may not allow that but it would be best if you coded as if
it did.

And for completeness, you might want to add a comment that documents the formula
for this calculation just in case someone else has to support this later on.


> james,

> there are a few flaws with this:
> 1. you're testing on update(completed) which is not true when, say, you
> execute this
>     insert tasks(opendate) select getdate()   --completed column is not
> updated

> and new completed value is '01/01/01'

> --
> -oj
> http://rac4sql.home.attbi.com



> > When entering a value into the field for the first time on
> > an Insert, the trigger does not fire.

> > If you modify data in the field with the trigger, the
> > trigger will fire.

> > Cannot determine why the trigger will not fire.  No errors
> > are generated.

> > Win 2000 Professional, SQL 2000 SP2

 
 
 

Create Trigger - Insert, Update - not updating record on Insert

Post by James TerHar » Sat, 19 Jan 2002 01:16:45


Ok, I understand what you are saying, but I'm still not

would be Null, thus causing the last if statement to not
properly execute.  HOw can I get around this?

Quote:>-----Original Message-----
>Just to expand on this a bit ...

>During an insert, the deleted table is empty.  Your logic
is dependent on the
>presence of rows in the deleted table.  Under this

situation, your last IF
(resulting in the unknown
Quote:>state as opposed to TRUE).  You should also change your
logic to support
>multi-row inserts/updates.  Seems like all of your logic

could be accomodated in
Quote:>a single update statement.  Your logic is also flawed the
other way if the
>completed column allows NULLs and if something that was
completed can be
>"uncompleted" (as in a correction). Under this

circumstance, the select
Quote:>statement in the last IF returns NULL resulting in the

unknown state again.
Quote:>However, your system may not allow that but it would be

best if you coded as if
Quote:>it did.

>And for completeness, you might want to add a comment

that documents the formula
Quote:>for this calculation just in case someone else has to

support this later on.


>> james,

>> there are a few flaws with this:
>> 1. you're testing on update(completed) which is not
true when, say, you
>> execute this
>>     insert tasks(opendate) select getdate()   --

completed column is not
>> updated



>> and new completed value is '01/01/01'

>> --
>> -oj
>> http://rac4sql.home.attbi.com



>> > When entering a value into the field for the first
time on
>> > an Insert, the trigger does not fire.

>> > If you modify data in the field with the trigger, the
>> > trigger will fire.

>> > Cannot determine why the trigger will not fire.  No
errors
>> > are generated.

>> > Win 2000 Professional, SQL 2000 SP2

>.

 
 
 

Create Trigger - Insert, Update - not updating record on Insert

Post by Scott Morri » Sat, 19 Jan 2002 02:17:30


Here is my guess.  It is completely untested, might have typos or
unbalanced parentheses, etc.

UPDATE TASKS
   SET Hours = CASE WHEN Completed is NULL or OpenDate is NULL
                    THEN DEFAULT
                    ELSE CAST(datediff(mi, OpenDate, Completed) as
float) +
                         COALESCE ((SELECT sum(hours) FROM tasksact
WHERE
                                  tasksact.wo_num = TASKS.wo_num), 0)
                    END
WHERE exists (select * from inserted WHERE TASKS.wo_num =
inserted.wo_num)

Things to keep in mind.  The calculation is my best guess for your
original posted trigger - read my last comment in my first post.  I
assume that wo_num is the PK for TASKS.  You should type variable names
using the same case as when they were created; it is best (imho) to be
consistent regarding case in identifiers (some are mixed case, some are
all lower, table name is all upper).  I don't know what the flow of data
is for a given row over time.  For example, can a completed task be
corrected such that it is no longer completed?  If so, review the logic
to make sure that the appropriate columns are correctly updated (in this
case, Hours is set to its default value).  Can rows be inserted or
updated in the tasksact table for a completed task? Is so, should there
be a trigger on that table which performs this type of update?

Some considerations for the future.  You could avoid all of this trigger
writing if you relied on a view.  The view would do the calculation by
using the same join (or an outer join) as above.  Note that this assumes
that the calculation can be done as written above.


> Ok, I understand what you are saying, but I'm still not

> would be Null, thus causing the last if statement to not
> properly execute.  HOw can I get around this?

> >-----Original Message-----
> >Just to expand on this a bit ...

> >During an insert, the deleted table is empty.  Your logic
> is dependent on the
> >presence of rows in the deleted table.  Under this
> situation, your last IF

> (resulting in the unknown
> >state as opposed to TRUE).  You should also change your
> logic to support
> >multi-row inserts/updates.  Seems like all of your logic
> could be accomodated in
> >a single update statement.  Your logic is also flawed the
> other way if the
> >completed column allows NULLs and if something that was
> completed can be
> >"uncompleted" (as in a correction). Under this
> circumstance, the select
> >statement in the last IF returns NULL resulting in the
> unknown state again.
> >However, your system may not allow that but it would be
> best if you coded as if
> >it did.

> >And for completeness, you might want to add a comment
> that documents the formula
> >for this calculation just in case someone else has to
> support this later on.


> >> james,

> >> there are a few flaws with this:
> >> 1. you're testing on update(completed) which is not
> true when, say, you
> >> execute this
> >>     insert tasks(opendate) select getdate()   --
> completed column is not
> >> updated


> >> and new completed value is '01/01/01'

> >> --
> >> -oj
> >> http://rac4sql.home.attbi.com



> >> > When entering a value into the field for the first
> time on
> >> > an Insert, the trigger does not fire.

> >> > If you modify data in the field with the trigger, the
> >> > trigger will fire.

> >> > Cannot determine why the trigger will not fire.  No
> errors
> >> > are generated.

> >> > Win 2000 Professional, SQL 2000 SP2

> >.

 
 
 

1. Updating Inserted record in Insert Trigger?

I have a number of primary tables that require a unique numeric
identifier. The unique numeric identifier is acquired by an insert into
a single column table where the column is an identity column.
Does anyone have a method for acquiring the next identity number from
the id table and appending it to the inserted record in the Insert
Trigger of the primary table.
Thank you in advance.
S Walker

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Newbie question

3. trigger - update a record on insert/update

4. Help with dBaseIV Record Deleting

5. puzzling issue with inserted, updated and IF UPDATE(column) in an update trigger

6. Changing column focus in DBGrid

7. Which record triggers an INSERT, UPDATE Trigger

8. Forget sa password

9. Insert/Update Trigger - multiple inserts.

10. Waht is better: Insert and update or Select Update else insert

11. INSERT trigger updating the inserted row

12. ODBC - insert/updating two databases with one insert/update

13. newbie : on insert triggers can i update the row i just inserted