Cannot change DB context in cursor

Hi There

I am trying to loop through databases to gather information.

However if i loop though datbases in a cursor, by using a select from sysdatabases, once i am in the cursor and SET @Sql = 'use ' + DBName and EXEC(SQL), the database context never changes from the one the cursor is executing in.

Is there a way to loop through database contexts and execute sql

Thanx



Answer this question

Cannot change DB context in cursor

  • V VICTOR

    Hi,

    the cursor is running in its own context. So, if you just want to have a quick view on some data you could use the undocumented feature of the procedure sp_msforeachdb. Use it wisely, because it is undocumented and unsupported and could be deprectaed in further versions.

    sp_msforeachdb 'USE ; Your query here'

    Make sure that you don’t use the "external" variable to execute the command, because it will be not known in the execution context.

    HTH, Jens Suessmeyer.

    ---
    http://www.sqlserver2005.de
    ---


  • XP is self aware

    P.S this is SS2000, so i cannot use sqlcmd.

    Thanx


  • Cannot change DB context in cursor