Matt Velic

Data and Design

Looping Transactions Revised

| 1 Comment

If you’ve been brought here from my post named Looping Transactions, just keep on reading for the script example. If you’ve come to this post first, here’s the reference.

As Noel McKinney (Blog | @NoelMcKinney) pointed out in a comment yesterday and Dave Ballentyne (Blog | @davebally) told me on Twitter, I used soon to be depreciated piece of code in my little looping script. My bad. I guess the Important section in Books Online wasn’t quite large enough. Hey Microsoft, it’s called an H1 tag.* Thankfully or not, we are using 2005 Standard so the script worked as I intended it. Still, we should always aim for forward compatibility.**

I’ve rewritten the little script to account for the ROWCOUNT snafu:

DECLARE @toGo int
SET @toGo = 1

WHILE @toGo > 0
BEGIN
UPDATE TOP (500) dbo.bioInfo
SET dbo.bioInfo.alternate_ID = NULL
WHERE dbo.bioInfo.alternate_ID IS NOT NULL
SET @toGo = @@ROWCOUNT
END

I was able to try this new script on my test box, and it works in the same way. As I’ve mentioned to a few people, my little snafu didn’t turn into a big issue because it was in a one-off piece of code. But if you’re writing this into a stored procedure, or even documenting a procedure in Word or a wiki, you’d be better off staying clear of SET ROWCOUNT.

* Actually, web designers would kill me if I left it there. I wouldn’t use H1, I’d use an H2 because it’s not the most important Title or Heading on the page. Semantics count!

** Another lesson taken from web design.

One Comment

  1. Pingback: Matt Velic » Blog Archive » Knowing versus Understanding

Leave a Reply

Required fields are marked *.

*