posted by Brad Prendergast at 9:04:00 PM
(0 comments)
Links to this post
Permalink
SQL: DBCC CHECKTABLE on multiple tables

-- set options
SET NOCOUNT ON;
SET ANSI_PADDING ON;
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;

-- declare table to store table names
-- in SQL 2005 you can use table variables
-- if you do not need or want to store the data you can store it in a table
-- variable instead of a temporary table
DECLARE @tablenames TABLE (
TableName VARCHAR(255)
);


-- populate table with tablenames
INSERT INTO @tablenames
SELECT
name
FROM
sys.objects
-- additional filters can be set to narrow table selection
WHERE
([type] = 'U');


-- Declare a cursor to loop through table
DECLARE tablenames INSENSITIVE SCROLL CURSOR
FOR
SELECT
TableName
FROM
@tablenames;



-- Open the cursor.
OPEN tablenames


-- declare variables
DECLARE @curtablename AS VARCHAR(255);
DECLARE @cmd AS VARCHAR(1024);


-- Loop through all the tables
WHILE (1=1)
BEGIN
FETCH NEXT FROM tablenames INTO @curtablename;
IF @@fetch_status <> 0 BREAK;
-- run the command
set @cmd = 'DBCC CHECKTABLE ([' + rtrim(@curtablename) + '])';
exec (@cmd)
print @cmd + CHAR(10) + CHAR(13);
end


-- Close cursor
CLOSE tablenames;
DEALLOCATE tablenames;
SQL: Index Fragmentation Maintenance
A fragmented index affects performance (table scans and index seeks) when the DBMS needs to search for records.
I prefer to have a little more control and use the following script as part of my maintenance plan. This script allows for a little more control in the selecting of tables, indexes and databases to maintain. I tend to rebuild indexes that are more than 30% fragmented and reorganize those that are greater than 5%.
/**//** **/
-- Create table to hold index information
SET NOCOUNT ON;
create table _IndexInformation (
database_id smallint,
ObjectName varchar(255),
[object_id] int,
IndexName varchar(255),
[index_id] int,
index_level int,
index_type_desc varchar(60),
avg_fragmentation_in_percent float,
fragment_count float,
page_count bigint,
record_count bigint,
index_depth int
);
/**//** **/
/**//** **/
-- clear table contents
truncate table _IndexInformation;
-- populate table
insert into _IndexInformation
select
database_id,
object_name(i.object_id) AS [ObjectName],
i.[object_id],
i.name AS [IndexName],
dm.index_id,
dm.index_level,
dm.index_type_desc,
dm.avg_fragmentation_in_percent,
dm.fragment_count,
dm.page_count,
dm.record_count,
dm.index_depth
from
-- database, table and index information can be specified
/**//*
sys.dm_db_index_physical_stats (
{ database_id NULL 0 DEFAULT }
, { object_id NULL 0 DEFAULT }
, { index_id NULL 0 -1 DEFAULT }
, { partition_number NULL 0 DEFAULT }
, { mode NULL DEFAULT }
)
This example uses database 1; you should replace this with the ID of the database you wish to process
*/
sys.dm_db_index_physical_stats(1,null,null,null,'detailed') dm
INNER JOIN sys.indexes i ON
(i.object_id = dm.object_id) AND (i.index_id = dm.index_id);
/**//** **/

/**//** **/
-- read index table and maintain indexes
SET NOCOUNT ON;
SET ANSI_PADDING ON;
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;

-- Declare variables.
declare @tablename as varchar(255)
declare @indexname as varchar(255)
declare @frag as float
declare @cmd as varchar(1024)
-- Declare a cursor.
declare indexes insensitive scroll cursor
for
select
ObjectName,
IndexName,
avg_fragmentation_in_percent
from
_IndexInformation
where
(avg_fragmentation_in_percent >= 5);
-- Open the cursor.
open indexes

-- Loop through all the indexes.
while (1=1)
begin
fetch next from indexes into @tablename, @indexname, @frag;
if @@fetch_status <> 0 break;
-- if fragmentation is above 30% rebuild index
-- if fragmentation is above 5% reorganize index
if (@frag >= 30)
begin
set @cmd = 'ALTER INDEX [' + rtrim(@indexname) + '] ON [' + @tablename +'] REBUILD;'
exec (@cmd)
print @cmd;
end
else
if (@frag >= 5)
begin
set @cmd = 'ALTER INDEX [' + rtrim(@indexname) + '] ON [' + @tablename +'] REORGANIZE;'
exec (@cmd)
print @cmd;
end;
end


-- Close cursor
close indexes;
deallocate indexes;
/**//** **/

/**//** **/
-- delete index table
drop table _IndexInformation;
/**//** **/

SQL: Where are the database files?
SELECT
database_id,
DB_Name(database_id) as database_name,
[file_id],
type_desc,
[name],
physical_name,
state_desc,
[size],
max_size
FROM
sys.master_files
WHERE
-- you can use the DB_ID function to return the
-- database id of a database by name
(database_id = DB_ID(N'master'))
-- if you know the id you can filter by it directly
-- (database_id = 1)
ORDER BY
type_desc DESC,
physical_name;
SELECT
database_id,
[name],
compatibility_level,
collation_name,
state_desc
FROM
sys.databases;
SQL: Remove / Delete Orphan Users
SQL Delete/Drop a User from each Database
The stored procedure is called like;
SQL Optimization: Am I missing any indexes? - Part I