Solved

Use SQL Variable in Database name

Posted on 2007-04-02
6
1,770 Views
Last Modified: 2008-01-09
I am trying to run the same query on a series of databases, but T-SQL would not let me use the variable name as the name of the database to query. Consider the following query:

DECLARE db_cursor CURSOR
FOR SELECT name FROM sys.databases WHERE LEFT(name,4) = 'SBO_' AND RIGHT(name,4) <> 'TEST'

-- declare the variables that will hold values retrieved by the cursor
DECLARE @db_id CHAR(10)
OPEN db_cursor

FETCH NEXT FROM db_cursor INTO @db_id

-- loop through the results
WHILE @@FETCH_STATUS = 0
BEGIN
      SELECT CompnyName FROM [@db_id]..[OADM]
      --SELECT @db_id (this works fine, the above does not)
      FETCH NEXT FROM db_cursor INTO @db_id
      
END

-- close and deallocate the cursor
CLOSE db_cursor
DEALLOCATE db_cursor

Anybody have any idea on how to solve this?

Thanks in advance,

Joerg.
0
Comment
Question by:jaldinger
  • 3
  • 3
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18837967
unless you use dynamic sql, there is no way to make the database name a variable
0
 

Author Comment

by:jaldinger
ID: 18837986
Thank you. I have never heard of Dynamic SQL. So what would the script look like in Dynamic SQL that does what I'm looking for?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18838027
before we go into that issue, 2 questions:
* are you SURE that you need a cursor? ie, what is the code about?
* are you SURE that you need to put the dbname as variable? couldn't you connect to the correct database in the first place?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:jaldinger
ID: 18838070
angellll:
What I need to do is execute the same SELECT statement against a varying number of databases on the server. I can do that from code by first getting the database names (SELECT FROM sys.databases) and then running the SELECT query once against each of them. I was looking for a way to get the complete result set with a single query to the server because bandwidth and latency in this application are an issue.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18838392
the following might help:

exec sp_msforeachdb '
 SELECT CompnyName FROM ?..[OADM]
'

0
 

Author Comment

by:jaldinger
ID: 20328795
That hasn't completely solved my problem, but I consider it good enough. Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Oracle SQL with Connect by prior to SQL Server SQL 6 29
VB6 ListBox Question 4 29
Optimizing a query 3 33
SQL Query resolving a string conversion issue 26 36
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now