I have a small SQL script that rotates through all databases on the
server and executes a Stored Procedure in each of them. Here are the
steps:
1: The first step is to get name of databases in to a cursor.
2: Build dynamic SQL to change database to database in variable - this
step fails
3: Execute SP
4: Move to next database.
Is it not possible to change current database using Dynamic SQL? Can
someone help me here please?
Here is the code I am using:
----- EXECUTE SQL Statement on all Databases
DECLARE
@DatabaseName varchar(100)
, @SQL varchar(500)
DECLARE DBNameCursor CURSOR FOR
SELECT
[Name] AS DatabaseName
FROM
master.dbo.sysdatabases
ORDER BY
DatabaseName
OPEN DBNameCursor
FETCH NEXT FROM DBNameCursor
INTO @DatabaseName
WHILE @@FETCH_STATUS = 0
BEGIN
----- Change to Database
SELECT
@SQL = 'USE ' + @DatabaseName
EXEC(@SQL)
----- SQL Statement to be run
EXEC dbo.SPName
FETCH NEXT FROM DBNameCursor
INTO @DatabaseName
END
CLOSE DBNameCursor
DEALLOCATE DBNameCursor
----------------------
Thanks in advance!
Vishal Sinha 5 7182
On Feb 7, 7:02 am, "SQLJunkie" <vsinh...@gmail.comwrote:
I have a small SQL script that rotates through all databases on the
server and executes a Stored Procedure in each of them. Here are the
steps:
1: The first step is to get name of databases in to a cursor.
2: Build dynamic SQL to change database to database in variable - this
step fails
3: Execute SP
4: Move to next database.
Is it not possible to change current database using Dynamic SQL? Can
someone help me here please?
Here is the code I am using:
----- EXECUTE SQL Statement on all Databases
DECLARE
@DatabaseName varchar(100)
, @SQL varchar(500)
DECLARE DBNameCursor CURSOR FOR
SELECT
[Name] AS DatabaseName
FROM
master.dbo.sysdatabases
ORDER BY
DatabaseName
OPEN DBNameCursor
FETCH NEXT FROM DBNameCursor
INTO @DatabaseName
WHILE @@FETCH_STATUS = 0
BEGIN
----- Change to Database
SELECT
@SQL = 'USE ' + @DatabaseName
EXEC(@SQL)
----- SQL Statement to be run
EXEC dbo.SPName
FETCH NEXT FROM DBNameCursor
INTO @DatabaseName
END
CLOSE DBNameCursor
DEALLOCATE DBNameCursor
----------------------
Thanks in advance!
Vishal Sinha
If I recall correctly EXEC starts a new thread that won't know about
the preceeding USE statement.
My favorite dynamic SQL site: http://www.sommarskog.se/dyn-search.html
SQLJunkie (vs******@gmail.com) writes:
I have a small SQL script that rotates through all databases on the
server and executes a Stored Procedure in each of them. Here are the
steps:
1: The first step is to get name of databases in to a cursor.
2: Build dynamic SQL to change database to database in variable - this
step fails
3: Execute SP
4: Move to next database.
Is it not possible to change current database using Dynamic SQL? Can
someone help me here please?
Yes, but the effect of the USE lasts only for the duration of the of the
dynamic SQL.
SELECT
@SQL = 'USE ' + @DatabaseName
EXEC(@SQL)
----- SQL Statement to be run
EXEC dbo.SPName
If all you want to do is to run a stored procedure in each database,
this is the easiest way to do:
SELECT @SPname = @DatabaseName + '.dbo.SPName'
EXEC @SPname
You may also be interested in sp_MSforeachdb:
EXEC sp_MSforeachdb 'EXEC ?.dbo.SPNAme'
This procedure is undocumented and unsupported, but it's nevertheless
fairly popular.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
Thanks everyone for the replies. The following example is fine for
generic SPs:
SELECT @SPname = @DatabaseName + '.dbo.SPName'
EXEC @SPname
But what if I have to run SPs like SP_updatestats etc? then it will
not work.
Thanks!
On Feb 7, 5:35 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
SQLJunkie (vsinh...@gmail.com) writes:
I have a small SQL script that rotates through all databases on the
server and executes a Stored Procedure in each of them. Here are the
steps:
1: The first step is to get name of databases in to a cursor.
2: Build dynamic SQL to change database to database in variable - this
step fails
3: Execute SP
4: Move to next database.
Is it not possible to change current database using Dynamic SQL? Can
someone help me here please?
Yes, but the effect of the USE lasts only for the duration of the of the
dynamic SQL.
SELECT
@SQL = 'USE ' + @DatabaseName
EXEC(@SQL)
----- SQL Statement to be run
EXEC dbo.SPName
If all you want to do is to run a stored procedure in each database,
this is the easiest way to do:
SELECT @SPname = @DatabaseName + '.dbo.SPName'
EXEC @SPname
You may also be interested in sp_MSforeachdb:
EXEC sp_MSforeachdb 'EXEC ?.dbo.SPNAme'
This procedure is undocumented and unsupported, but it's nevertheless
fairly popular.
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
SQLJunkie (vs******@gmail.com) writes:
Thanks everyone for the replies. The following example is fine for
generic SPs:
SELECT @SPname = @DatabaseName + '.dbo.SPName'
EXEC @SPname
But what if I have to run SPs like SP_updatestats etc? then it will
not work.
Au contraire, it will work just fine! If you say:
EXEC mydb..sp_systemprocedure
the system procedure will execute in the context of mydb.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
Thanks - it worked!
Momentary lapse of reason :)
On Feb 8, 5:39 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
SQLJunkie (vsinh...@gmail.com) writes:
Thanks everyone for the replies. The following example is fine for
generic SPs:
SELECT @SPname = @DatabaseName + '.dbo.SPName'
EXEC @SPname
But what if I have to run SPs like SP_updatestats etc? then it will
not work.
Au contraire, it will work just fine! If you say:
EXEC mydb..sp_systemprocedure
the system procedure will execute in the context of mydb.
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Hodapp |
last post by:
I have seen messages posted about this before, and there is a clear
reference to it in the manual, but I have been unable to find a
solution.
I'm on Slackware 9.1, kernel 2.6.0-test11, using...
|
by: Roel Wuyts |
last post by:
CALL FOR CONTRIBUTIONS
International Workshop on Revival of Dynamic Languages
http://pico.vub.ac.be/~wdmeuter/RDL04/index.html
(at OOPSLA2004, Vancouver, British Columbia, Canada, October...
|
by: MikeY |
last post by:
Hi Everyone,
I am working in C#, windows forms.My question is this. All my button dynamic
controls properties are present and accounted for except for the"FlatStyle"
properties. I can't seem to...
|
by: Ender |
last post by:
I have an application that I would like third party developers to be
able to create Plug-ins that will be dynamically loaded into our
application to extend functionality.
I have utilized the...
|
by: Tyler Carver |
last post by:
I am trying to use some dynamic controls that are built and then added
to tables. The problem that I am having is the timing of when I can
populate the controls and have the state remain after a...
|
by: Leo J. Hart IV |
last post by:
OK, here's another question for the experts:
I am building a multi-step (3 steps actually) form using a panel for
each step and hiding/displaying the appropriate panel/panels depending
on which...
|
by: Diffident |
last post by:
Hello All,
I am trying to add dynamic controls onto my page and here is how I am doing
that.
I have a page which has a button called as "AddMoreControls" and in this
button's event handler I...
|
by: Tom |
last post by:
I am developing my pages on my development machine
and then copying to the production server.
I am not pre-compiling, I am using the 'dynamic compile'
feature. This is working fine except that...
|
by: Ghada Al-Mashaqbeh via DotNetMonster.com |
last post by:
Hi all,
I am facing a problem in dynamic code generation at run time, the problem
occurs when the dynmaic code use global data exist within the original
application.
Lets say that my...
|
by: pbd22 |
last post by:
Hi.
This is just a disaster management question.
I am using XMLHTTP for the dynamic loading of content in a very
crucial area of my web site. Same as an IFrame, but using XMLHTTP and
a DIV. I...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |