473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

[TRANSACT]Can't switch from master to another database

Hello,

I encounter a problem with a small portion of sqlcode. I try to go on
database using "use dbname" but i always stay in master. I execute
script with the sa user.

declare @dbname sysname
declare @ret_code int

DECLARE db_cursor CURSOR FOR
select
name
from
master..sysdatabases
where
name not in ('master', 'model', 'tempdb', 'pubs', 'Northwind')

-- Open cursor
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN
execute ('use ' + @dbname)
execute ('select db_name()')

Thank's for help,
Pierrot.

Jul 23 '05 #1
2 1856
USE is scoped only within the EXEC statement so you'll have to combine
the two:

EXEC ('USE ' + @dbname+' SELECT DB_NAME()')

If this is for non-production code you might consider using the
following undocumented proc instead of the cursor:

EXEC sp_msforeachdb 'USE ? SELECT DB_NAME()'

This still implements a cursor behind the scenes but keeps some of the
complexity out of your own code.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
execute('use ' + @dbname + ';select db_name()')
HTH

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hennie de Nooijer | last post by:
Hi, Ik created an application with visuals basic.NET. This has a connection string to one database, let's say 'A'. In this database a stored procedure is called which should execute a string...
3
by: Szaki | last post by:
I must import some exemplary file to database (MS Srrver 2000) ofcourse using procedure Transact SQL. This file must: 1.Read the xml file 2. Create table 3. Import this date from xml file to my...
1
by: Phil | last post by:
Hi, I have my create statments for tables, procedures, views, etc in individual Transact-SQL script files (.sql). I wnat to write another script file that executes these scripts in the...
0
by: Hennie de Nooijer | last post by:
Hi, Ik created an application with visuals basic.NET. This has a connection string to one database, let's say 'A'. In this database a stored procedure is called which should execute a string...
0
by: Igor2004 | last post by:
Ladies and Gentlemen, I would like to offer you the following string functions Transact-SQL GETWORDCOUNT() Counts the words in a string GETWORDNUM() Returns a specified word from a string...
0
by: Igor Nikiforov | last post by:
Ladies and Gentlemen, I would like to offer you the following string functions Transact-SQL GETWORDCOUNT() Counts the words in a string GETWORDNUM() Returns a specified word from a string...
12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
29
by: A.P. Hofstede | last post by:
Could someone tell me where MS-Access (current and 97?) fit(s) on the RDBMS - ORDBMS - ODBMS spectrum? I gather it's relational, but how does it size up against/follow SQL2/3/4 definitions and how...
2
by: Zach | last post by:
Re SQL Server 2005 Express When I attempt to copy the master file for backing up, it is being used by another application, even after shutting down the PC completely and powering it up again....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.