dbTalk Databases Forums  

Database Attach Scripts

microsoft.public.sqlserver.clustering microsoft.public.sqlserver.clustering


Discuss Database Attach Scripts in the microsoft.public.sqlserver.clustering forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Charlie
 
Posts: n/a

Default Database Attach Scripts - 11-24-2007 , 05:52 PM






I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #2  
Old   
Linchi Shea
 
Posts: n/a

Default RE: Database Attach Scripts - 11-24-2007 , 11:46 PM






Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

Quote:
I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #3  
Old   
Uri Dimant
 
Posts: n/a

Default Re: Database Attach Scripts - 11-25-2007 , 12:48 AM



Charlie
http://dimantdatabasesolutions.blogs...-database.html




"Charlie" <Charlie (AT) discussions (DOT) microsoft.com> wrote

Quote:
I have a few existing 2000 instances that we are migrating to 2005 SP2.
The
instances have a lot of databases on them. Is there an easy way to
generate
the attach scripts for migrating?



Reply With Quote
  #4  
Old   
Charlie
 
Posts: n/a

Default RE: Database Attach Scripts - 11-25-2007 , 05:23 PM



What does this script do exactly?

"Linchi Shea" wrote:

Quote:
Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #5  
Old   
Linchi Shea
 
Posts: n/a

Default RE: Database Attach Scripts - 11-25-2007 , 09:10 PM



Quote:
What does this script do exactly?
Generate a T-SQL script to sp_attach_db every user database.

Linchi

"Charlie" wrote:

Quote:
What does this script do exactly?

"Linchi Shea" wrote:

Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #6  
Old   
Charlie
 
Posts: n/a

Default RE: Database Attach Scripts - 11-26-2007 , 07:39 AM



Where does it get a list? We are moving the user db's to a new environment.
I need to attach the raw files, sepcifying the .mdf and .ldf file locations
and db name....

Make sense?

Maybe I am not understanding what you are telling me....

"Linchi Shea" wrote:

Quote:
What does this script do exactly?
Generate a T-SQL script to sp_attach_db every user database.

Linchi

"Charlie" wrote:

What does this script do exactly?

"Linchi Shea" wrote:

Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #7  
Old   
Linchi Shea
 
Posts: n/a

Default RE: Database Attach Scripts - 11-26-2007 , 01:18 PM



The script reads the info from the sysaltfiles table on the current server.
If you are moving the files to a new server, you may need to edit the script
to point to the files in different locations.

Unless you have some overriding reasons to use a different directory
structure or a different drive, it's often simpler to keep the file locations
identical.

Linchi

"Charlie" wrote:

Quote:
Where does it get a list? We are moving the user db's to a new environment.
I need to attach the raw files, sepcifying the .mdf and .ldf file locations
and db name....

Make sense?

Maybe I am not understanding what you are telling me....

"Linchi Shea" wrote:

What does this script do exactly?
Generate a T-SQL script to sp_attach_db every user database.

Linchi

"Charlie" wrote:

What does this script do exactly?

"Linchi Shea" wrote:

Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #8  
Old   
Charlie
 
Posts: n/a

Default RE: Database Attach Scripts - 11-26-2007 , 02:01 PM



That is really what I need!

This script does not print anything. A assume you want me to run it in
query analyzer?

"Linchi Shea" wrote:

Quote:
The script reads the info from the sysaltfiles table on the current server.
If you are moving the files to a new server, you may need to edit the script
to point to the files in different locations.

Unless you have some overriding reasons to use a different directory
structure or a different drive, it's often simpler to keep the file locations
identical.

Linchi

"Charlie" wrote:

Where does it get a list? We are moving the user db's to a new environment.
I need to attach the raw files, sepcifying the .mdf and .ldf file locations
and db name....

Make sense?

Maybe I am not understanding what you are telling me....

"Linchi Shea" wrote:

What does this script do exactly?
Generate a T-SQL script to sp_attach_db every user database.

Linchi

"Charlie" wrote:

What does this script do exactly?

"Linchi Shea" wrote:

Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #9  
Old   
Charlie
 
Posts: n/a

Default RE: Database Attach Scripts - 11-26-2007 , 02:17 PM



Nevermind.

Sorry!

Thanks for the help!

"Charlie" wrote:

Quote:
That is really what I need!

This script does not print anything. A assume you want me to run it in
query analyzer?

"Linchi Shea" wrote:

The script reads the info from the sysaltfiles table on the current server.
If you are moving the files to a new server, you may need to edit the script
to point to the files in different locations.

Unless you have some overriding reasons to use a different directory
structure or a different drive, it's often simpler to keep the file locations
identical.

Linchi

"Charlie" wrote:

Where does it get a list? We are moving the user db's to a new environment.
I need to attach the raw files, sepcifying the .mdf and .ldf file locations
and db name....

Make sense?

Maybe I am not understanding what you are telling me....

"Linchi Shea" wrote:

What does this script do exactly?
Generate a T-SQL script to sp_attach_db every user database.

Linchi

"Charlie" wrote:

What does this script do exactly?

"Linchi Shea" wrote:

Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
  #10  
Old   
Charlie
 
Posts: n/a

Default RE: Database Attach Scripts - 11-26-2007 , 02:46 PM



It truncates after 32 db's. Is there any way to avoid this?

"Linchi Shea" wrote:

Quote:
The script reads the info from the sysaltfiles table on the current server.
If you are moving the files to a new server, you may need to edit the script
to point to the files in different locations.

Unless you have some overriding reasons to use a different directory
structure or a different drive, it's often simpler to keep the file locations
identical.

Linchi

"Charlie" wrote:

Where does it get a list? We are moving the user db's to a new environment.
I need to attach the raw files, sepcifying the .mdf and .ldf file locations
and db name....

Make sense?

Maybe I am not understanding what you are telling me....

"Linchi Shea" wrote:

What does this script do exactly?
Generate a T-SQL script to sp_attach_db every user database.

Linchi

"Charlie" wrote:

What does this script do exactly?

"Linchi Shea" wrote:

Something like this:

declare @dbid int,
@sql varchar(4000),
@name sysname,
@old_name sysname,
@filename nchar(260);

declare db_cr cursor
for select dbid, filename
from master..sysaltfiles
where dbid > 4
order by dbid;

open db_cr;

set @sql = '';
set @old_name = '';

fetch next from db_cr into @dbid, @filename;
while @@fetch_status = 0
begin
If @old_name <> db_name(@dbid)
select @sql = @sql + char(13) + char(10) +
'EXEC sp_attach_db N''' + rtrim(db_name(@dbid)) + ''',
N''' + rtrim(@filename) + '''';
else
select @sql = @sql + ', N''' + rtrim(@filename) + '''';

select @old_name = db_name(@dbid);
fetch next from db_cr into @dbid, @filename;
end
close db_cr;
deallocate db_cr;
print @sql;

Linchi

"Charlie" wrote:

I have a few existing 2000 instances that we are migrating to 2005 SP2. The
instances have a lot of databases on them. Is there an easy way to generate
the attach scripts for migrating?

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.