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
 SQL Server Development (2000)
 Help with: exec ('USE [' + @DBName + ']')

Author  Topic 

anam27
Starting Member

1 Post

Posted - 2008-02-22 : 08:59:11

Hy, I want to do something like this:

declare @DBName varchar(20)
set @DBName = 'Northwind'

exec ('USE [' + @DBName + ']')

Idea is to change Database in Query Analyzer depends on DB I choose.

But, when I started this script, all I got is 'The command(s) completed successfully.'
I use this script because I need to create a database dinamically (variable @DBName) with their roles and tables. How can I do?

Thanks.

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-22 : 09:11:09
You can't.

Why you want to generate databases and tables dynamically?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-02-22 : 10:05:21
you can use the 3 part name to referece any object in the SQL Server as long as you have the access to it

select * from [Northwind].[dbo].[yourtablename]



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -