| Author |
Topic |
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-21 : 17:42:52
|
| Why the 2nd row never get inserted?create table t (n int primary key)gocreate trigger tr on tfor insertascommit tranreturngoinsert into t select 5insert into t select 6goselect * from tdrop trigger trdrop table tgo"Don't ask a question if you won't understand the answer" |
|
|
Granick
Starting Member
46 Posts |
Posted - 2003-11-21 : 18:37:36
|
| I am not sure of the exact reasoning on this, but the commit must be finalizing the implied transaction, and since there is no "go" between the 2 INSERT statements, and it is being finalized before the second one ever fires, it is not being done. I put a SELECT * FROM inserted into the Trigger, and it is only being fired once. If you put the go between the inserts, and it fires twice.Shannon |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-11-21 : 18:40:03
|
| Unmatched transaction.A trigger is run inside a transaction, your commit gives a trancount of 0 which gets decremented on completion of the trigger to -1 which is invalid.The question should be why doesn't it give an error message before aborting.What happens if you run it from something like VB.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-21 : 18:52:45
|
| Very interesting.. I will have to digest this.. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-11-21 : 18:59:35
|
| lolIn SQL Server 2000, if a COMMIT TRANSACTION or COMMIT WORK statement is executed in a trigger, and there is no corresponding explicit or implicit BEGIN TRANSACTION statement at the start of the trigger, users may see different behavior than on SQL Server version 7.0. Placing COMMIT TRANSACTION or COMMIT WORK statements in a trigger is not recommended.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-21 : 19:13:30
|
Dim cn As ADODB.ConnectionSet cn = CurrentProject.Connectioncn.Execute "insert into t select 5; insert into t select 6"MsgBox cn.Errors.Count0 errors!! |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-21 : 19:44:21
|
| nr,(I overlooked your last post + I'm testing it on sql 7.0),really lol... btw this inserts two rows (with the same trigger):begin traninsert into t select 5insert into t select 6commit tran |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-11-21 : 20:06:08
|
| Doesn't on mine. (sp3)needs 2 begin transbegin tranbegin traninsert into t select 5insert into t select 6commit trancommit tran==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-11-21 : 20:24:28
|
| When it exits the trigger with a 0 trancount it executes no more statements. The commit in the trigger does decrement the trancount and will commit if it reaches 0. When it reaches 0 you also lose the inserted table entry.create trigger tr on tfor insertasselect beforecommitintrigger = @@trancount, (select * from inserted)commit transelect aftercommitintrigger = @@trancount, (select * from inserted)gobegin tranbegin transelect beforeinsert1 = @@trancountinsert into t select 5select afterinsert1 = @@trancountcommit traninsert into t select 6select afterinsert2 = @@trancountselect afterinsert2 = @@trancountgo==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-22 : 06:47:34
|
| Thanks, Nigel, for explanations.. BTW, they provide a workaround forthe following question (I met it recently on an other forum):how to prevent terminating of a batch if in the 'middle' of it in somefired trigger was met condition for ROLLING BACK?Workaround in bold:create table t (n int primary key)gocreate trigger tr on tfor insertasif (select * from inserted)=15 begin rollback tran begin tran endgoinsert into t select 5insert into t select 6insert into t select 15 -- << bad valueinsert into t select 888insert into t select 9999goselect * from tdrop trigger trdrop table tgo |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-11-22 : 07:20:58
|
| That comes under "things not to do".Think about what will happen if someone decides to put a transaction round the inserts.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-22 : 16:07:32
|
| Yes.. it is dangerous workaround..But interesting to play around with.. |
 |
|
|
|