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 Administration (2000)
 how I can change database at runtime in cursor

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-09-13 : 06:27:28
hemangi writes "I am writing a cursor in which i want to the address of all .mdb and .ldf files in the all database in sql server current registration. I have all database list in query bye reading them one by one i want to first-
set @sql='use ' + @dbname
exec sp_executesql @sql

(where @dbname is the variable in which I am fetching Databse from the cursor query one bye one)

Above statement no error shows

after this statement again I am Executing the statement

set @sql='exec sp_helpfile '
exec sp_executesql @sql

this statement give me error not find the database file"

nr
SQLTeam MVY

12543 Posts

Posted - 2006-09-13 : 06:51:25
select @sql = 'exec ' + @dbname + '..sp_helpfile '
exec sp_executesql @sql

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-13 : 10:14:52
If you use USE then all must be in same scope

set @sql='use ' + @dbname+' exec sp_helpfile '
exec sp_executesql @sql


Madhivanan

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

- Advertisement -