Defragging SQL indexes

Occasionally it will be necessary to defrag table indeces if you run SQL server. A useful little script.

USE dbname –Enter the name of the database you want to reindex

DECLARE @TblName varchar(255)
Declare @Indexname varchar(50)

DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = ‘base table’

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TblName
WHILE @@FETCH_STATUS = 0
BEGIN

DECLARE INdexCursor CURSOR FOR
SELECT     i.name AS IndexName
FROM         sysobjects o, sysindexes i
WHERE   (o.id = i.id and o.name = @tblName) AND (i.status = 18450 OR   i.status = 2097152)

OPEN IndexCursor

FETCH NEXT FROM INdexCursor INTO @IndexName
WHILE @@FETCH_STATUS = 0
BEGIN

DBCC INDEXDEFRAG (dbname, @tblname, @Indexname)

FETCH NEXT FROM INDexcursor INTO @Indexname
END

CLOSE IndexCursor

DEALLOCATE IndexCursor

FETCH NEXT FROM TableCursor INTO @TblName
END

CLOSE TableCursor

DEALLOCATE TableCursor

Have you any grapes?

A duck walks into a bar and says, “Do you have any grapes?” The bartender says, “No, this is a bar, get lost.” So the next day the duck comes back and asks, “Do you have any grapes?” The bartender said, “No, no grapes. It’s not like we were just OUT of grapes when you asked yesterday, it’s that we NEVER have grapes. Now get out of here.”

The duck comes back again and asks, “Do you have any grapes?” The bartender says, “Look, I’m getting sick and tired of this! The next time you come in hear asking for grapes, I’m gonna nail your frickin’ bill to the counter, got it?!?” So the duck comes back the next day and asks, “Do you have any grapes?”

The bartender slams the counter and screams, “That does it! I’m gonna nail your frickin’ bill to the counter!” So he reaches down to pick up his hammer and his nail but when he gets back up he sees that he’s staring straight down the barrel of a semi-automatic rifle that the duck is holding. He doesn’t even have time to drop his jaw before the bullets start RIPPING through his body, shaking it like a marionette on heroin and spurting blood everywhere.

It just so happens that there’s an off-duty policeman in the bar, and he draws his piece, thinking he’s gonna take the duck out, right? But the duck SEES him in the reflection of the mirror, okay? And he leaps off the barstool doing a spinning 180 and drops the cop with a single short blast. Then the duck jumps over the counter and walks past the bartender’s bleeding body on the floor and opens the mini-fridge under the bar, and all the grapes start spilling out.

And now the duck is pissed! He grabs a bunch of grapes and stuffs them in the bartender’s mouth, then he swaps his rifle for a shotgun, and starts jamming the grapes in the bartender’s mouth with the end of the gun, yelling, “No grapes?! No grapes, huh?!” [shotgun blast, stuff more grapes into mouth, another shotgun blast] And at this point this mother is crying while her baby is wailing at the top of his lungs, and the duck jumps on the counter and yells, “STOP SCREAMING! STOP SCREAMING!”