473,398 Members | 2,113 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.

How can you assign system objects to a variable?#$@!

37
Hi.

I am having a problem trying to find the proper way to assign a system object (or rather in this case a system object name) to a variable to use in my T-SQL code.

I have to run a report across 20 or more databases and need to create a loop to run the same report execution on every database.

This is a snippet of the code I am trying to execute:
-- Create a temp table to store each database on the server in a table with a
-- corresponding incremental integer

create table #databases
(x int identity(1,1),dbase nvarchar(128))
go
insert into #databases (dbase)
select name from sys.databases
where name not in ('master','tempdb','model','msdb')

The problem is here when I try to test and see if I can switch to the first database in the table and perform a query:

select @dbase = dbase from #databases where x = 1
set @dbase = rtrim(@dbase)

use @dbase
go
select distinct customer_id from customers

Error Message -> Incorrect syntax near '@dbase'

Apparently assigning database names to variables is a bit more complicated than I suspected.

Can anyone provide a clue for how I can accomplish this task any other way or is there another way that I can assign the database name to a variable that could work???
Sep 1 '07 #1
2 2044
azimmer
200 Expert 100+
Hi.

I am having a problem trying to find the proper way to assign a system object (or rather in this case a system object name) to a variable to use in my T-SQL code.

I have to run a report across 20 or more databases and need to create a loop to run the same report execution on every database.

This is a snippet of the code I am trying to execute:
-- Create a temp table to store each database on the server in a table with a
-- corresponding incremental integer

create table #databases
(x int identity(1,1),dbase nvarchar(128))
go
insert into #databases (dbase)
select name from sys.databases
where name not in ('master','tempdb','model','msdb')

The problem is here when I try to test and see if I can switch to the first database in the table and perform a query:

select @dbase = dbase from #databases where x = 1
set @dbase = rtrim(@dbase)

use @dbase
go
select distinct customer_id from customers

Error Message -> Incorrect syntax near '@dbase'

Apparently assigning database names to variables is a bit more complicated than I suspected.

Can anyone provide a clue for how I can accomplish this task any other way or is there another way that I can assign the database name to a variable that could work???
Your problem is not with assigning the variable but with using it :). "USE" doesn't take variables as parameters. Try this:
Expand|Select|Wrap|Line Numbers
  1. declare @sqlstatement as varchar(255)
  2.  
  3. select @dbase = dbase from #databases where x = 1
  4. set @dbase = rtrim(@dbase)
  5.  
  6. set @sqlstatement = 'USE ' + @dbase + ';'
  7. set @sqlstatement = @sqlstatement + 'select distinct customer_id from customers;'
  8. exec(@sqlstatement)
  9.  
  10.  
NB: Read EXEC's help carefully
Sep 1 '07 #2
mivey4
37
Your problem is not with assigning the variable but with using it :). "USE" doesn't take variables as parameters. Try this:
Expand|Select|Wrap|Line Numbers
  1. declare @sqlstatement as varchar(255)
  2.  
  3. select @dbase = dbase from #databases where x = 1
  4. set @dbase = rtrim(@dbase)
  5.  
  6. set @sqlstatement = 'USE ' + @dbase + ';'
  7. set @sqlstatement = @sqlstatement + 'select distinct customer_id from customers;'
  8. exec(@sqlstatement)
  9.  
  10.  
NB: Read EXEC's help carefully
Thanks, I will. Your code worked great, the use of the required single quotes seem like it could get pretty messy very quick for more complex queries though. But if there is no other way; there's no other way.....
Sep 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: Rim | last post by:
Hi, I have been thinking about how to overload the assign operation '='. In many cases, I wanted to provide users of my packages a natural interface to the extended built-in types I created for...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
4
by: Andres | last post by:
Hi all, I have the problem to assign a variable of type object to a specific class at runtime. I want to use a string variable that specify the class a want to set this object. Is something...
2
by: Cary | last post by:
In my project, I want to display the version number on the splash screen and in the help section of my application. I have a variable I created called appVersion. When I go to my deployment project...
2
by: Jim McGivney | last post by:
In asp 2.0 I am trying to insert a row using a detailsview control connected to an accessDataSource. I get the error message below. I am having trouble identifing which data field is causing the...
7
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that...
42
by: blisspikle | last post by:
I tried closely copying some code that I found on this group for assigning a type at runtime, but I cannot get it to work. Can someone see what is wrong with my logic? Thanks, Private Sub...
5
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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,...

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.