|
-
March 15th, 2007, 10:16 AM
#1
Dynamic DB Selection
I'm using SQL Server 2000 and am looking for a way to dynamically select which database to select from in a stored procedure.
I have 4 possible DBs to choose from and don't want to write 5 SPs: 1 to choose which DB and 1 for each DB, simply changing the [db].dbo.tableName, know what I mean.
I'm aware of the exec('SELECT * FROM [db].dbo.tableName') option, but I'm looking for a (hopefully) easier solution.
Something like SELECT * FROM @myDB.dbo.tableName (which doesn't work).
Anyone have any suggestions?
-
April 5th, 2007, 12:36 PM
#2
Re: Dynamic DB Selection
I would use dynamic sql string. Just build the sql as a string and execute all within your stored procedure.
-
April 8th, 2007, 06:44 AM
#3
Re: Dynamic DB Selection
As long as you have the permissions to connect to the different databases - you can switch the database context using the "USE" statement. http://msdn2.microsoft.com/en-us/library/ms188366.aspx Can't you?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|