September 26, 2007 at 9:02 am
I was making an example for someone to demonstrate how a transaction rollback does not rollback the identity() of an insert...and i discovered that a table variable is not affected by the transaction. It's not the behavior I expected, so i just thought i'd mention it to all. it's probably because memory items can't be logged, but interesting.
try this: it's the same whether 2000 or 2005:
--unaffected by transaction
set nocount on
declare @sample table (sampleid int identity(1,1), sampletext varchar(30) )
insert into @sample(sampletext) values ('one')
insert into @sample(sampletext) values ('two')
select * from @sample
begin tran
insert into @sample(sampletext) values ('three')
insert into @sample(sampletext) values ('four')
select * from @sample
rollback tran
insert into @sample(sampletext) values ('five, not three')
select * from @sample
--affected as expected by trasnaction
set nocount on
CREATE TABLE #sample (sampleid int identity(1,1), sampletext varchar(30) )
insert into #sample(sampletext) values ('one')
insert into #sample(sampletext) values ('two')
select * from #sample
begin tran
insert into #sample(sampletext) values ('three')
insert into #sample(sampletext) values ('four')
select * from #sample
rollback tran
insert into #sample(sampletext) values ('five, not three')
select * from #sample
drop table #sample
Lowell
September 26, 2007 at 9:05 am
Actually, this is stated in BOL.
September 26, 2007 at 9:07 am
Just to complete your answer : why do you expect a rollback to work when there's nothing logged in the transactions log?
September 26, 2007 at 9:26 am
yeah i agree; it's just my knee jerk reaction was "but it's in a TRANSACTION", like that makes up for everything.
Lowell
September 26, 2007 at 10:11 am
this feature is acctually very usefull in auditing.
for example if you have a need to rollback the data in a trigger or in a sproc but you have to
save the data you changed before a rollback.
look here:
http://weblogs.sqlteam.com/mladenp/archive/2007/04/13/60174.aspx
_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply