nnnnnattach all the datababses on a SQL Server:
If you are trying to attach all the files to a simillar location as the current server, you can use the script below. It will print out the script with sp_attach_db statements for all the databases. It doesn’t look for secondary files and you can update the script accordingly.
–Script to attach all User databases to original location.
declare curname cursor for select dbid from sysdatabases where dbid > 4
open curname
declare @dbid int
fetch next from curname into @dbid
while @@fetch_status = 0
begin
declare @sql varchar(500)
declare @datafile varchar(100)
declare @logfile varchar(100)
select @datafile=filename from sysaltfiles where dbid = @dbid and groupid = 1
select @logfile=filename from sysaltfiles where dbid = @dbid and groupid = 0
–print @logfile
set @sql = ‘sp_attach_db ‘ db_name(@dbid) ‘,”’ ltrim(rtrim(@datafile)) ”’,”’ ltrim(rtrim(@logfile)) ”’ ;’
print @sql
fetch next from curname into @dbid
end
close curname
deallocate curname
Leave a Reply