SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Does a "Begin... End" in a While loop act as a transaction?


Does a "Begin... End" in a While loop act as a transaction?

Author
Message
jasona.work
jasona.work
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10306 Visits: 12595
Reading Lynns comments in this topic: http://www.sqlservercentral.com/Forums/Topic1447456-391-1.aspx, I found myself wondering if a script I've got to re-org or re-build indexes was going to use transactions, or be one big transaction.

The code in question reads the tables that need to be re-orged or re-built from a temp table, and loops through until every row in the temp table has been read.

So it's similar to:
Set @loopcnt = 0
While @loopcnt != (select COUNT(DISTINCT TableName) from #IndicesToReorganize)
Begin
Set @loopcnt = @loopcnt + 1
set @tblName = (Select tabletowork from (Select TableName as tabletowork, ROW_NUMBER() OVER(ORDER BY TableName) as rn
from #IndicesToReorganize) tmp
Where rn = @loopcnt)
Set @Alter = N'alter index ALL on ' + @dbname + '.dbo.' + @tblName + ' REORGANIZE'
exec(@Alter)
End



So, do those Begin / Ends inside the While run each reorg as a transaction, or do I need to wrap the "exec(@Alter)" in a "Begin Transaction / End Transaction"?
Taking a look in BOL, it looks like, NO they do not function as a begin / end transaction, and I will need to put such in the loop.
Correct?

Thank you,
Jason
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35261 Visits: 16683
Jason

You're only running one ALTER INDEX statement at a time, so it doesn't make any difference whether you use an explicit transaction or not. I think what Lynn was asking in the other thread was whether the whole WHILE loop was enclosed in an explicit transaction.

John
jasona.work
jasona.work
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10306 Visits: 12595
Ah, OK.

Thanks,
Jason
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search