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)
 USE statement

Author  Topic 

meboz
Starting Member

1 Post

Posted - 2006-07-13 : 19:47:05
Hi all,

im new to this forum.

I need to execute something along these lines but it doesnt wanna work

declare @sql varchar(50)
declare @db varchar(20)

set @db = 'some_database'
set @sql = 'USE ' + @db

execute (@sql)

it runs but doesnt switch to some_database.

thanks in advance

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2006-07-13 : 21:35:10
it actually works but it doesn't set you to the database you specified because it is run in the context of the dynamic sql

i'm not very good with words, better add another line in the dsql and see if it shows you the result you expect...


set @sql='use ' + @db + ';select * from tablename'

what are you trying to achieve? there must be some simpler way like cross database querying perhaps?

--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-14 : 03:24:54
As you are using Dynamic SQL, scope is over once it is executed. You need to use them in same scope as Jen suggested

Madhivanan

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

- Advertisement -