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
 Transact-SQL (2000)
 Selecting database

Author  Topic 

bkpgamble
Starting Member

30 Posts

Posted - 2006-10-09 : 07:21:48
I need to be able to switch DB in a cursor loop

but how do I use a variable to change the DB ?

I tried this but dosent work

USE @DBNameWithSimpleLog

Annyone have an idea ?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-09 : 07:27:47
set @mysqlcmd = 'USE ' + @DBNameWithSimpleLog
EXEC (@mysqlcmd)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-09 : 07:33:32
if you need to access a table from another database use the full naming

select *
from db2.dbo.table



KH

Go to Top of Page

bkpgamble
Starting Member

30 Posts

Posted - 2006-10-09 : 07:55:26
Peso solved it :)

dident even consider using EXEC

thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-09 : 10:22:37

http://www.sommarskog.se/dynamic_sql.html


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-10-09 : 10:25:23
I guess we are not talking about sprocs then



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2006-10-09 : 10:45:24
Remember that if you do use this approach

set @mysqlcmd = 'USE ' + @DBNameWithSimpleLog
EXEC (@mysqlcmd)

Then the context is only changed for the duration of the EXEC, not for anything after that.

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

- Advertisement -