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
 General SQL Server Forums
 New to SQL Server Programming
 Programatically change DBs

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-09-19 : 07:41:36
Carlos writes "I am trying to create a sp that would jump from one DB to another using the USE command. I built a string and then I tried to run EXECUTE(@SQLString) with no lock. This is the sample code I am using:

declare @DBName varchar(200)
declare @SQlString varchar(300)
set @DBName='model'
set @SQlString = 'use '+@DBName
execute(@SQlString)

Is there any way I can accomplish that?

Thanks

Carlos"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-19 : 07:51:03
Changing DBName dynamically is not recommended
Try this


declare @DBName varchar(20)
declare @SQlString varchar(300)
set @DBName='model'
set @SQlString = 'Select columns from '+@DBName+'..TableName'
execute(@SQlString)



Madhivanan

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

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2005-09-19 : 22:02:07
why do you need to "jump" from one db to another?

you can cross-query with the appropriate permissions

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

- Advertisement -