Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DYNAMICALLY SET DATABASE?

Status
Not open for further replies.

sagn

Programmer
Jun 7, 2001
166
US
hI

I was wondering if it were possible to dynamically set the database with the use statement?

I have tried

exec ('use '+@db)

and have tried to use

exec sp_executesql N'use '+@db,N'@db varchar(50)',@db=@dbtobeused

neither seem to work. I am thinking this is because it is out of scope. Is there any way to be able to do this?

thanks!
 
I think your first method would will work if you put a space between use and @db
like this.

EXEC ('USE'+' '+@db)

- Paul
- Database performance looks fine, it must be the Network!
 
Hmm I tried it (it actually results in adding an extra space as I did have one after USE (as 'USE ')

Anyway, still no dice. :(

Any other suggestions?

thanks!
 
I was wrong, it doesn't work.

- Paul
- Database performance looks fine, it must be the Network!
 
This works


DECLARE @db varchar(100)
,@sql varchar(100)

SELECT @db = 'ConfigTest'


SELECT @sql = 'USE'+' '+@db+' '+ 'SELECT * FROM FAQ'

EXEC (@sql)

- Paul
- Database performance looks fine, it must be the Network!
 
Yeah.. I know. I did that too...

I was just hoping to be able to use ONE dynamic statement
in the very beginning but it seems I have to do it for every
command I want to execute. Again, I think its a scope thing.

thanks anyway!
 
Just prefix your object with the db name

DECLARE @db varchar(100),@db2 varchar(100)
,@sql varchar(100)

SELECT @db = 'pubs'
SELECT @db2 = 'master'


SELECT @sql = 'SELECT top 10 * FROM '+ @db + '..authors cross join ' + @db2 + '..sysobjects'

print (@sql)
exec (@sql)

Denis The SQL Menace
SQL blog:
Personal Blog:
 
I agree with Denis. Just make sure you accomodate data bases with a space in the name by wrapping it in square brackets, like so...

SELECT @sql = 'SELECT top 10 * FROM [!][[/!]'+ @db + '[!]][/!]..authors cross join [!][[/!]' + @db2 + '[!]][/!]..sysobjects'


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 

Yeah, I know it can be done that way. Being able to
get the job done was not really the problem. I am just lazy :) and as such wanted to be able to use only one EXEC(@STR) and not have to worry about constructing a string for each operation I want to perform.

Thanks anyway though!
 
or like this

Code:
DECLARE @db varchar(100),@db2 varchar(100)
        ,@sql varchar(100)

SELECT @db = '[pubs]'
SELECT @db2 = '[master]'


SELECT @sql = 'SELECT top 10 * FROM '+  @db + '..authors cross join ' +  @db2 + '..sysobjects'

print (@sql) 
exec (@sql)

or by using the handy dandy QUOTENAME

Code:
DECLARE @db varchar(100),@db2 varchar(100)
        ,@sql varchar(100)

SELECT @db = quotename('pubs')
SELECT @db2 = quotename('master')


SELECT @sql = 'SELECT top 10 * FROM '+  @db + '..authors cross join ' +  @db2 + '..sysobjects'

print (@sql) 
exec (@sql)
Don't you know that ISO 11179-5 standards forbid databases with spaces in them ;-)

Denis The SQL Menace
SQL blog:
Personal Blog:
 
[rofl] True.

I hate db names with spaces. Even though there is an ISO standard, it doesn't mean people follow them. [wink]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
While we are on the topic.. I hate all names with spaces!
drives me crazy.
 
you should try working with a database that starts with a number.

- Paul
- Database performance looks fine, it must be the Network!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top