Solved

How to switch database dynamicaly in T-sql

Posted on 2007-11-20
8
3,501 Views
Last Modified: 2008-02-01
I have a cursor loop on sysdatabases and i need to query the sysfiles table for each database in system to build an attach script for all databases.
How can i switch the current database dynamicaly inside the cursor. Use keyword doesn't work
0
Comment
Question by:mordi
  • 3
  • 3
  • 2
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
what about using sp_MSForeachDB?
here a small sample:

exec sp_MSForEachDb ' select * from ?..sysfiles '
0
 
LVL 31

Expert Comment

by:James Murrell
Comment Utility
agree 100% with angelIII- he got me on these... take a look at http://www.databasejournal.com/features/mssql/article.php/3441031
0
 
LVL 3

Author Comment

by:mordi
Comment Utility
Fine.
But there are are several files for each database and i need to build the attach command with all the file names in one line.
0
 
LVL 31

Expert Comment

by:James Murrell
Comment Utility
you could cal commands, what is the script you want t use
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

by:mordi
Comment Utility
the script is as follows:
sp_attach_db @dbname = N'dbname' , @filename1 = N'db  file path' @filename2 = N'db  file path', @filename3 = ........
0
 
LVL 3

Author Comment

by:mordi
Comment Utility
the script is as follows:
sp_attach_db @dbname = N'dbname' , @filename1 = N'db  file path' @filename2 = N'db  file path', @filename3 = ........
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
Comment Utility
please check out this script:
exec sp_msforeachdb '

declare @res varchar(8000) 

set nocount on

set @res = ''exec sp_attach_db N''''?'''' ''

select @res = @res +  '','''''' + rtrim(cast(filename as varchar(100))) + ''''''''

from ?..sysfiles 

order by fileid asc

select @res 

'

Open in new window

0
 
LVL 31

Expert Comment

by:James Murrell
Comment Utility
Thanks angelIII - my email stopped and did not get notified
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now