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)
 Changing Db

Author  Topic 

tanhs
Starting Member

2 Posts

Posted - 2005-07-22 : 03:17:36
Hi,

I am using mssql7.0 I would like to dynamicly change Db in the
stored procedure

eg.

....
execute('use master') -- this doesn't work
execute('select * from sysobjects')
print db_name()
....
How should I go about to do this ?

Tq.

tanhs
Starting Member

2 Posts

Posted - 2005-07-22 : 03:24:42
sorry here are some changes
....
execute('use master') -- this doesn't work
declare cur cursor for
select name from sysobejcts where type='U'
print db_name()
....
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-22 : 04:49:12
You can use

execute('select * from master..sysobjects')


Madhivanan

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

Mala
Starting Member

26 Posts

Posted - 2005-07-26 : 04:07:02
declare @db varchar(10),
@sql nvarchar(500)

set @db='master'
set @sql='select * from '+@db+'..sysobjects'
exec (@sql)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-26 : 04:44:08
Mala, I think Dynamic SQL is not neccessary in this case

Madhivanan

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

- Advertisement -