BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 Subscribe!

Tuesday, November 25, 2008
SQL: DBCC CHECKTABLE on multiple tables

Part of decent SQL database maintenence is periodically checking the integrity, both logical and physical, of database objects. SQL Server has the DBCC CHECKDB command that will check the integrity of the objects and report back the results. DBCC CHECKDB runs CHECKALLOC, CHECKTABLE and CHECKCATALOG on the specified database.



DBCC CHECKTABLE allows you to check the integrity of a specified table or indexed view. If there are a large number of tables to check, the task of running the command for each tablename could be time consuming. Creating a script that loops through a set of database tables and runs DBCC CHECKTABLE for each, is a bit easier.

The sys.objects contains each of the user-defined schema objects in a database. Rows that are of [Type] = 'U' are tables. This makes it easy to get all the tables within a database (of course other filters can be applied). The following script will loop through and run the DBCC CHECKTABLE command for each table in the database (the command can be easily replaced to perform another action on each returned table):
1
2-- set options
3SET NOCOUNT ON;
4SET ANSI_PADDING ON;
5SET ANSI_NULLS ON;
6SET QUOTED_IDENTIFIER ON;
7
8
9-- declare table to store table names
10-- in SQL 2005 you can use table variables
11-- if you do not need or want to store the data you can store it in a table
12-- variable instead of a temporary table
13DECLARE @tablenames TABLE (
14 TableName VARCHAR(255)
15);
16
17
18
19-- populate table with tablenames
20INSERT INTO @tablenames
21 SELECT
22 name
23 FROM
24 sys.objects
25 -- additional filters can be set to narrow table selection
26 WHERE
27 ([type] = 'U');
28
29
30
31-- Declare a cursor to loop through table
32DECLARE tablenames INSENSITIVE SCROLL CURSOR
33 FOR
34 SELECT
35 TableName
36 FROM
37 @tablenames;
38
39
40
41
42-- Open the cursor.
43OPEN tablenames
44
45
46
47-- declare variables
48DECLARE @curtablename AS VARCHAR(255);
49DECLARE @cmd AS VARCHAR(1024);
50
51
52
53-- Loop through all the tables
54WHILE (1=1)
55 BEGIN
56 FETCH NEXT FROM tablenames INTO @curtablename;
57 IF @@fetch_status <> 0 BREAK;
58 -- run the command
59 set @cmd = 'DBCC CHECKTABLE ([' + rtrim(@curtablename) + '])';
60 exec (@cmd)
61 print @cmd + CHAR(10) + CHAR(13);
62 end
63
64
65
66-- Close cursor
67CLOSE tablenames;
68DEALLOCATE tablenames;
69

Labels: ,

posted by Brad Prendergast at 9:04:00 PM
Comments:
Links to this post:

Create a Link

Recent Posts
 SQL: Index Fragmentation Maintenance
 Off-topic: Uhm, Rickroll?
 SQL: Where are the database files?
 Show Desktop in my QuickLaunch Toolbar?
 Command Line: Visual Source Safe
 SQL: Remove / Delete Orphan Users
 SQL Delete/Drop a User from each Database
 Is there an 'I' in phone?
 SQL Optimization: Am I missing any indexes? - Part...
 Meaningful Signature File Quotes

 Subscribe!


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger