June 13, 2008 at 1:43 pm
I am running this
declare @database_name varchar(20)
exec sp_msforeachdb 'dbcc checkdb (@database_name)'
and receiving this error.
Server: Msg 137, Level 15, State 2, Line 1
Must declare the variable '@database_name'.
What am I doing wrong?
¤ §unshine ¤
June 13, 2008 at 1:50 pm
change to
EXEC sp_MSforeachdb @command1="DBCC CHECKDB ('?')"
June 16, 2008 at 2:10 am
The dynamic part in sp_msforeachdb and ms_foreachtable is not a variable, but a questionmark. In the actual statement, MSSQL will replace the questionmark in your code by the databasename
Wilfred
The best things in life are the simple things
June 17, 2008 at 7:16 am
Is it possible to set this up with multiple statements?
I'd like to set to single user mode, run a repair and then reset to multi user. I'll be trying a few things on my end in the meantime.
¤ §unshine ¤
June 24, 2008 at 8:20 am
I try this...
DECLARE @cmd1 varchar(100)
SET @cmd1 = 'ALTER ? SET SINGLE_USER'
DECLARE @cmd2 varchar(100)
SET @cmd2 = 'DBCC CHECKDB (''[?]'',REBUILD_REPAIR)'
DECLARE @cmd3 varchar(100)
SET @cmd3 = 'ALTER ? SET MULTI_USER'
EXEC sp_msforeachdb @command1 = @cmd1,
@command2=@cmd2,
@command3=@cmd3
And get these errors...
erver: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'model'.
Server: Msg 2560, Level 16, State 14, Line 1
Parameter 2 is incorrect for this DBCC statement.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'model'.
System db's should not be included but I will figure that one out later.. any help appreciated!
¤ §unshine ¤
June 24, 2008 at 8:45 am
try this (you have some syntax incorrect)
DECLARE @cmd1 varchar(100)
SET @cmd1 = 'ALTER DATABASE [?] SET SINGLE_USER'
DECLARE @cmd2 varchar(100)
SET @cmd2 = 'dbcc checkdb('?',repair_rebuild) '
DECLARE @cmd3 varchar(100)
SET @cmd3 = 'ALTER DATABASE [?] SET MULTI_USER'
//EDIT
forgot to add this link. This is how you exclude the system databases.
Just put your code between the begin and end in the IF block.
http://www.sqlservercentral.com/Forums/Topic438415-266-1.aspx
June 24, 2008 at 8:47 am
Try this cursor option. I know coding with sp_msforeach db is possible. but this is another way to achieve the same results..
DEclare @sqlstm varchar(200)
Declare @dbname sysname
Declare rolldbname cursor for
Select name from master.sys.sysdatabases where dbid>4
open rolldbname
fetch next from rolldbname into @dbname
While @@fetch_status=0
BEGIN
SET @sqlstm='
ALTER Database '+@dbname+' SET SINGLE_USER
WITH ROLLBACK IMMEDIATE
DBCC CHECKDB ('+@dbname+') WITH PHYSICAL_ONLY
ALTER DATABASE '+@dbname+ ' SET MULTI_USER'
EXEC (@sqlstm)
fetch next from rolldbname into @dbname
END
close rolldbname
deallocate rolldbname
Maninder
www.dbanation.com
June 24, 2008 at 9:06 am
I tried the first one and there is still some kind of syntax error. I tried the 2nd one and it seems to work. I would prefer no cursor, but I believe that is what msforeachdb does.
thank you so much for your help!
¤ §unshine ¤
June 24, 2008 at 11:34 am
yes sp_msforeachdb does the cursor thing check this:
use master
sp_helptext sp_msforeachdb
Maninder
www.dbanation.com
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply