Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 use @DatabaseName variable

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-08-26 : 08:09:46
Brent writes "I have an accounting package with multiple company databases, I currently have seperate queries in a StoredProc which go and gather like data from each of 18 company databases, and insert it into an Enterprise reporting table.

I am searching for a way to use only one query in my Stored Procedure, that has a variable, that represents the Database to use.

example

Current Query

insert into EnterpriseReportTable

select * from Database1.dbo.table
--
insert into EnterpriseReportTable

select * from Database2.dbo.table
--
insert into EnterpriseReportTable

select * from Database3.dbo.table



I want to turn this into one query where the database, can be declared as a variable."

mr_mist
Grunnio

1870 Posts

Posted - 2003-08-26 : 08:36:31
You would need to use dynamic SQL to do that, and even then it would be difficult because of the scope of dynamic SQL when dealing with swapping databases.

You would find it easier if it was seperate tables instead of databases, or better yet a single table with a relational field indicating the company.

-------
Moo. :)
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2003-08-26 : 08:49:00
also have a look at .....http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=28657
Go to Top of Page
   

- Advertisement -