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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

master.dbo.sysdatabases 1

Status
Not open for further replies.

mgp66

Programmer
Jan 19, 2006
7
US
How would I iterate through the master.dbo.sysdatabases names
column using the name in another db query to that (name) particular database.

OR how can I use the database name to query the database tables
 
Code:
DECLARE @db_name VARCHAR(50)

DECLARE daterbass CURSOR FOR
	SELECT [name] AS "dbname" FROM master..sysdatabases
	ORDER BY "dbname"

OPEN daterbass

FETCH NEXT FROM daterbass
INTO @db_name

SET NOCOUNT ON
WHILE @@FETCH_STATUS = 0

BEGIN
  SET @strQuery = 'SELECT colA, colB '
  SET @strQuery = @strQuery + ' FROM ' + @db_name
  SET @strQuery = @strQuery + + ' ..MyTable '
  SET @strQuery = @strQuery + ' WHERE conditions'

  EXECUTE(@strQuery)

  FETCH NEXT FROM daterbass
     INTO @db_name
END

CLOSE daterbass
DEALLOCATE daterbass
SET NOCOUNT OFF
 
DECLARE @strQuery VARCHAR(200)

was the only thing missing.
You rock!!

ThankYou very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top