473,835 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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..sysdata bases
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 1879
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
1465
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 (which is passed by the) VB tool. This string is a CREATE VIEW statement en this should be executed in another database let's say 'B'. I tried this in Transact - SQL
3
5458
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 database Ps. I create procedure who File xml imports to base, but unfortunately she only schedule when earlier create a table or table is created.So I need (I think) create such mini parser in language transact SQL.
1
2797
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 correct order to create the database. What is the syntax for executing script files from Transact-SQL?
0
1193
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 (which is passed by the) VB tool. This string is a CREATE VIEW statement en this should be executed in another database let's say 'B'. I tried this in Transact - SQL EXEC('USE B;' + Query)
0
1334
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 AT() Returns the beginning numeric position of the first occurrence of a character expression within another character expression, counting from the leftmost character RAT() Returns the numeric position of the last (rightmost) occurrence of a
0
1313
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 AT() Returns the beginning numeric position of the first occurrence of a character expression within another character expression, counting from the leftmost character RAT() Returns the numeric position of the last (rightmost) occurrence of a
12
22952
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 Server database that we have. Question follows: The following SQL statement, used in VBScript,
29
5916
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 does it compare to other database vendors? Any articles that might be of interest? It's for an essay on database models... Thanks in advance, Alex
2
1619
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. What can I do to make back-ups? Zach
0
9802
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10807
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10517
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10559
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10230
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9343
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5802
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3086
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.