by MAK [Muthusamy Anantha Kumar]
Step 3: Automate the restore process
Create a job for restoring the database backup in the test
server with the following job steps.

Kill Users
Add this SQL statement as the first job step, which kills
all the users on the database.
EXEC USP_KILLPROCESS " MyDatabaseName"
Restore database
Add this SQL statement as the second job step to restore
the database from the fileserver. Remember to change the database name, data
and log location.
Restore database MyDatabaseName from disk =
\\Fileserver\Sharedfolder\Product.Bak with replace,
Move "Product_Data" to "d:\mssql\data\Product_data.mdf",
Move "Product_Data" to "d:\mssql\log\Product_log.ldf"
Encrypt Sensitive data
Add these SQL statements as the third step, which encrypts
sensitive data such as SSN, Credit card numbers etc.
Use Mydatabasename
Go
Update accounts set revenue = master.dbo.encryp(revenue, char(rand()*100))
Go
Update personal set crcardnum = master.dbo.encryp(crcardnum, char(rand()*100))
Go
Update accounts set SSN = master.dbo.encryp(SSN, char(rand()*100))
Go
Synchronize sysusers
In practice, production and test servers will have the
same logins and users but the passwords will be different. When the production
database backup has been restored in the test server, the sid column in the sysusers
table will not be in sync with master table login. In order to bring it back
into sync, add the script below as the fourth step.
use master
go
sp_configure "allow updates",1
go
reconfigure with override
go
use MyDatabaseName
go
update sysusers set sid =
(select sid from master.dbo.syslogins where name = 'UserId1') where name = 'UserId1'
go
update sysusers set sid =
(select sid from master.dbo.syslogins where name = 'UserId2') where name = 'UserId2'
go
sp_configure "allow updates",0
go
reconfigure with override
go
Step 4: Schedule the job
This job can either be scheduled daily at night or can be
run on demand.

Conclusion:
By providing an automated production-test data sync
process, the QA group does not have to depend on the DBA for restoring the
production data; they could run this job whenever they needed. You can use the
same process for all the databases that you would like to synchronize with
production data.