[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

dbcc on one or several databases

Posted on 2012-12-27
5
Medium Priority
?
272 Views
Last Modified: 2013-01-22
Hello,

How can I modify this script for executing dbcc on one or several databases?


Open in new window

function main()
{
      if ($verbose) {$VerbosePreference = "Continue"}
      if ($debug) {$DebugPreference = "Continue"}
      dbcc_checkdb $I $D
}

function dbcc_checkdb ($I, $D)
{
      $cn = new-object system.data.SqlClient.SqlConnection( `
            "Data Source=$I;Integrated Security=SSPI;Initial Catalog=$D");
      $ds = new-object System.Data.DataSet "dsCheckDB"
      $query = "DBCC CHECKDB($D) WITH TABLERESULTS"
      $da = new-object "System.Data.SqlClient.SqlDataAdapter" ($query, $cn)
      $da.Fill($ds)

      $dtCheckDB = new-object "System.Data.DataTable" "dsCheckDB"
      $dtCheckDB = $ds.Tables[0]
      $dtCheckDB | Format-Table -autosize `
      -property Error, Level, State, MessageText, `
                                    RepairLevel, Status, DbId, ObjectId, `
                                    IndexId, PartitionId, AllocUnitId, File, `
                                    Page, Slot, RefFile, RefPage, RefSlot, Allocation
}

Thanks

bibi
0
Comment
Question by:bibi92
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:sachitjain
ID: 38726112
If you run following logic on master database, it would run dbcc checkdb for all the databases

declare @table table (SNO int identity(1,1), [name] sysname)
declare @i int, @cnt int, @dbname sysname, @query nvarchar(100)

insert into @table ([name])
select [name] from sys.sysdatabases

set @i = 1
while @i <= @cnt
begin
      select @dbname = [name] from @table where SNO = @i
      set @query = ';
                              DBCC CHECKDB;'
      set @query = 'use ' + CAST(@dbname as varchar(30)) + ' ' + @query
      print @query
      exec sp_executesql @query
      set @i = @i + 1
end
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 38727424
Well, the script appears to be OK with a single database - right ?

So, the question is how to make it loop around for multiple $D given the instance of $I

Ideally, create a procedure on your SQL Server and simply pass the $D as a parameter to the stored procedure and the parameter can have multiple database names seperated by a delimiter (like a comma).

Or, create an arrary of database names and then use that to loop around and build up your query. Or, if you need to process each database seperately (because of results) then you will also need to include keeping the results / processing before moving onto next.

Or, you could build up a query to go through each one and collect the results (and I have cheated a little by simply referring to column names as col1 col2 etc). Note that the #checkdb temp table MUST match the correct number of columns, so if doing a different checkdb then that table will be different....

if object_id('tempdb..#checkdb','U') is not null drop table #checkdb
create table #checkdb (id int identity,[error] varchar(100), [level] varchar(100), [state] varchar(100), [MessageText] varchar(555), [repairlevel] varchar(100), [status] varchar(100), col1 varchar(100), col2 varchar(100), col3 varchar(100), col4 varchar(100), col5 varchar(100), col6 varchar(100), col7 varchar(100), col8 varchar(100), col9 varchar(100), col10 varchar(100),col11 varchar(100), col12 varchar(100), col13 varchar(100), col14 varchar(100), col15 varchar(100), col16 varchar(100), col17 varchar(100))

insert #checkdb (MessageText) values ('dbcc checkdb(my_db) with tableresults')
insert #checkdb
exec ('dbcc checkdb(my_db) with tableresults')

insert #checkdb (MessageText) values ('dbcc checkdb(adventureworks2012) with tableresults')
insert #checkdb
exec ('dbcc checkdb(adventureworks2012) with tableresults')

select * from #checkdb

Open in new window


And the above could be made into a SP on the server and pass a delimited list of DB names
0
 

Author Comment

by:bibi92
ID: 38745298
Thanks but I have to use powershell.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 2000 total points
ID: 38745959
OK...

Then have a look at : http://cornasdf.blogspot.com.au/2012/06/powershell-to-run-dbcc-checkdb-loop-for.html

Thats doing a checkdb, with no info messages, and you want with tableresults, so, the output will be slightly different.

But the important part is it shows how to loop around (I wouldnt kill the connection each time either).
0
 

Author Closing Comment

by:bibi92
ID: 38804936
Thanks bibi
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question