Link to home
Start Free TrialLog in
Avatar of BrianMc1958
BrianMc1958

asked on

NEWBIE: How to (fully) copy a database with TSQL?

Dear Experts,

I would like to write a TSQL script that would:

1. Copy an entire database as NewDB, retaining all data, keys and indexes.
2. Assign ownership of NewDB to an existing user (Not the user I'm signed on as).
3. As a bonus, copy it from one machine to another.

Any ideas?

Thanks,
BrianMc1958
Avatar of Aneesh
Aneesh
Flag of Canada image

BrianMc1958,
> 1. Copy an entire database as NewDB, retaining all data, keys and indexes.

 you need to generate the script manually thru EM
BrianMc1958,
> 2. Assign ownership of NewDB to an existing user (Not the user I'm signed on as).

sp_changedbowner [ @loginame = ] 'login'
BrianMc1958,
> . As a bonus, copy it from one machine to another.
it's better to take the backup from the first machine and restore it on the other
for what purpose ?
and how frequently would you be doing it.

have you considered snapshot replication?
Avatar of BrianMc1958
BrianMc1958

ASKER

I'm doing it because as my company's development proceeds, we typically "give" our current database to testers, and then we copy the database to do our new development on.  We do that about once every two weeks.

I will also need to have a customer do it at their site soon, and I would much prefer to just hand them a script to do this, rather than walk them through it.

We keep getting errors in the copy.  MS SQL will sometimes fail to copy the keys and indexes for some tables, or just fail with mysterious messages.  It's costing us enough time that it seems worthwhile to look for a more bulletproof method.

I should one box is MS SQL Server 2000, and the other is 2003.  We sometimes copy within one box, and sometimes between.

I'm a newbie... What's snapshot replication?

Thanks again,
BrianMc1958
Typo:

I should SAY one box is MS SQL Server 2000, and the other is 2003.  We sometimes copy within one box, and sometimes between.
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!  I'll try it...
--BrianMc1958
If you restore to a different machine, you will have to re-sync all native SQL userids with the corresponding native SQL login.

This will generate the commands that need run:

SELECT 'EXEC sp_change_users_login ''UPDATE_ONE'', ''' + su.name + ''', ''' + su.name + ''''
FROM sysusers su
LEFT OUTER JOIN master..syslogins sl ON su.sid = sl.sid
WHERE su.issqluser = 1 AND su.name NOT IN ('guest', 'INFORMATION_SCHEMA')
--AND sl.sid IS NULL