SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Run SQL code on each database


Run SQL code on each database

Author
Message
AshMc
AshMc
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 159
Comments posted to this topic are about the item Run SQL code on each database
RichardDouglas
RichardDouglas
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1341 Visits: 707
First off, thanks for taking the time to compose and share this script.

There are a few things that you might want to take into consideration:
1) The state of the database. You're going to run into problems if a DB is in a restore state for example.
2) Secondly by using the windows functions you're precluding it's use on some earlier versions.

As I said, thanks for sharing. If you could amend to cater for these points then you have a great little script on your hands.

Hope this helps,
Rich



Stewart M.
Stewart M.
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 42
I recently discovered the original stored procedure that you referenced. The one issue that I have with it is the limitation that it runs on ALL databases! There is no way to limit the list of databases it runs on. I would like to see an additional parameter (such as @LimitDB) which would contain a like clause that would be used when querying for the list of databases (if not specified then all databases would be selected).

--Stewart McGuire
crashdan
crashdan
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1028 Visits: 757
smcguire (4/10/2013)
I recently discovered the original stored procedure that you referenced. The one issue that I have with it is the limitation that it runs on ALL databases! There is no way to limit the list of databases it runs on. I would like to see an additional parameter (such as @LimitDB) which would contain a like clause that would be used when querying for the list of databases (if not specified then all databases would be selected).

--Stewart McGuire


You can take what was posted and add like below (as an example)

INSERT INTO @DBs
select name as DBName, ROW_NUMBER() OVER(PARTITION BY 1 ORDER BY name) AS DBNumber
from sys.databases
AND name like @LimitDB
AshMc
AshMc
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 159
crashdan (4/10/2013)
smcguire (4/10/2013)
I recently discovered the original stored procedure that you referenced. The one issue that I have with it is the limitation that it runs on ALL databases! There is no way to limit the list of databases it runs on. I would like to see an additional parameter (such as @LimitDB) which would contain a like clause that would be used when querying for the list of databases (if not specified then all databases would be selected).

--Stewart McGuire


You can take what was posted and add like below (as an example)

INSERT INTO @DBs
select name as DBName, ROW_NUMBER() OVER(PARTITION BY 1 ORDER BY name) AS DBNumber
from sys.databases
AND name like @LimitDB


Firstly thanks for the feedback guys

In regards to limiting the databases I actually do similar to what you are suggesting crashdan, but I must admit I hard code it in and have a flag to ignore the hard coded databases or not. In my circumstance though we usually only want to run it on a handful of constant databases but I leave open the option to run it on all.

The other option, as per the example, is at run time by looking at the current database it has reached (by using the ? variable) and using an if to decide to run your script or not. Much like your suggestion you can feed in the databases you want to run the script/s on.

@Command1 = ' if ''?'' not like ''%master%'' begin select DB_NAME() end' 


Thanks
Ash
Charles Kincaid
Charles Kincaid
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3307 Visits: 2384
You might consider the need to surround the database name in the USE statement with brackets. I just ran
CREATE DATABASE [Stop that]

and now have a database with a space in the database name.

ATBCharles Kincaid
aleksey donskoy
aleksey donskoy
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 519
To the Point #1:

Add/comment the initial SELECT where clause:

where name <> 'TEMPDB'
and (status & 512) = 0 /*online dbs only*/
and (status & 1024) = 0 /*read-write dbs only*/


As the TEMPDB is not the database you want to run your scripts in

Alex Donskoy
Markus Pöhler
Markus Pöhler
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 90
Thanks for sharing. one comment, it is a little less overhead to increment negatively to zweo instead of having another variable @i and incrementing it by +1:

Declare @DBCnt int, @iCur int;
DECLARE @DBs as Table (DBName varchar(100), DBNumber int) --Temp table for DBs to run against
INSERT INTO @DBs select [name] as DBName, ROW_NUMBER() OVER(PARTITION BY 1 ORDER BY [name]) AS DBNumber from sys.databases;
set @DBCnt = @@rowcount
WHILE @DBCnt > 0 --Start loop over all DB's
BEGIN
select * FROM @DBs where DBNumber = @DBCnt;
-- do something
set @DBCnt = @DBCnt-1;
END

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search