IDENTITY(int, 1, 1) AS FragIndexId,
[object_id] AS ObjectId,
index_id AS IndexId,
avg_fragmentation_in_percent AS FragPercent,
record_count AS RecordCount,
partition_number AS PartitionNumber,
index_type_desc AS IndexType,
alloc_unit_type_desc AS AllocUnitType
FROM sys.dm_db_index_physical_stats (DB_ID(@dbName), NULL, NULL, NULL, @statsMode)
avg_fragmentation_in_percent > @minFragPercent AND
avg_fragmentation_in_percent < @maxFragPercent AND
index_id > 0
ORDER BY ObjectId
This query which is found on Tara Kizer's blog, for defragmenting and updating indexes in a database, I get the error "Incorrect syntax near '(' and it points me to the line in marked in color above...
Why all this? Well, my database is running quite slow after a Complete Restore of it, so I want to update all stats and rebuild all indexes on it.
PS: Using sql server 2008 R2.
Edit: Quite strange, I craeted a new Empty database and right clicked the database "new query", and pasted the whole procedure inside it, then created the procedure successfully. Why???
So I changed the beginning of the whole procedure from "Use db<companyname>;" to "Use Test;" at the very top, and it created it...
Guess it is solved then, but strange that I couldnt create the stored procedure within the database I wanted, ...oh well..