Tuesday, March 27, 2012

Change how command runs

This should be an easy one. I want to change the command to use the
DBID number instead of the database name to run the process. The
current loop does not keep the order if you add or remove databases
beecuse i am using the min() function.
DECLARE @.sql varchar(4000)
DECLARE @.db varchar(64)
SET @.db=''
SELECT @.db=min(name)
FROM master.dbo.sysdatabases
WHERE dbid > 4 and name > @.db
WHILE @.db is not null
BEGIN
SET @.sql='use ' +@.db + ' exec dbo.sp_UD_RebuildIndexes 30, 100'
EXEC (@.sql)
SELECT @.db=min(name)
FROM master.dbo.sysdatabases
WHERE dbid > 4 and name > @.db
END
Thanks
-Matt-not sure what are you exactly looking for.
but if you want to loop through the databases on the basis of DBID rather
than database name , then you can try following procedural code.
DECLARE @.sql varchar(4000)
DECLARE @.db int
SELECT @.db=min(dbid)
FROM master.dbo.sysdatabases
WHERE dbid > 4
WHILE @.db is not null
BEGIN
select @.sql='use ' + name + ' exec dbo.sp_UD_RebuildIndexes 30, 100'
from master..sysdatabases where dbid =@.db
EXEC (@.sql)
SELECT @.db=min(dbid)
FROM master.dbo.sysdatabases
WHERE dbid > @.db
END
"MKruer@.gmail.com" wrote:

> This should be an easy one. I want to change the command to use the
> DBID number instead of the database name to run the process. The
> current loop does not keep the order if you add or remove databases
> beecuse i am using the min() function.
> DECLARE @.sql varchar(4000)
> DECLARE @.db varchar(64)
> SET @.db=''
> SELECT @.db=min(name)
> FROM master.dbo.sysdatabases
> WHERE dbid > 4 and name > @.db
> WHILE @.db is not null
> BEGIN
> SET @.sql='use ' +@.db + ' exec dbo.sp_UD_RebuildIndexes 30, 100'
> EXEC (@.sql)
> SELECT @.db=min(name)
> FROM master.dbo.sysdatabases
> WHERE dbid > 4 and name > @.db
> END
> Thanks
> -Matt-
>|||That was it. The Query is to run a stored procedure that auto reindex
the entire Database. The only problem with the stored procedure is that
it only works on the database that it was executed on. This is more or
a temporary work around. Ideally I want to be able to specify
individual indexes directly using a table.
Creating a Stored Procedure to collect DB information and then run
another Stored Procedure using that information
http://groups.google.com/group/micr...bdb43d49fed239e
Query for Database, Table, Index name all on one list.
http://groups.google.com/group/micr...17098a35404820c|||That was it. The Query is to run a stored procedure that auto reindex
the entire Database. The only problem with the stored procedure is that
it only works on the database that it was executed on. This is more or
a temporary work around. Ideally I want to be able to specify
individual indexes directly using a table.
Creating a Stored Procedure to collect DB information and then run
another Stored Procedure using that information
http://groups.google.com/group/micr...bdb43d49fed239e
Query for Database, Table, Index name all on one list.
http://groups.google.com/group/micr...17098a35404820csql

No comments:

Post a Comment