Attach, Detach, Grant Access, Restore Sql Databases

Attach, Detach, Grant Access, Restore Sql Databases

Reference:

You must alter some of the substrings to fit your correct situation. In addition, you may have to grant permissions to everyone to run the batch files. Also, you can just grab the sql statements listed below and run them directly in your sql server environment to achieve the desired results.
See the following sql and batch statments below for correct code:

========
FileName: db_attach.bat

Content:

sqlcmd -S Matt-laptop\SQLExpress -E /i db_attach.sql

 

========

FileName: db_attach.sql

Content:

create database Halloween
on primary (filename = 'C:\LocalSites\Murach\Original database\Halloween.mdf')
log on (filename =     'C:\LocalSites\Murach\Original database\Halloween_log.ldf')
for attach
go

========

FileName:  db_detach.bat

Content:

sqlcmd -S localhost\SQLExpress -E /i db_detach.sql

========

FileName: db_detach.sql

Content:

sp_detach_db 'Halloween'

========

FileName: db_grant_access.bat

Content:

sqlcmd -S Matt-Laptop\SQLExpress -E /i db_grant_access.sql

========

FileName:  db_grant_access.sql

Content:

sp_grantlogin 'Matt-Laptop\ASPNET'
go

use Halloween
go

sp_grantlogin 'Matt-Laptop\ASPNET'
go

sp_grantdbaccess 'Matt-Laptop\ASPNET'
go

sp_addrolemember 'db_owner', 'Matt-Laptop\ASPNET'
go

exit

========

FileName: db_restore.bat

Content:

sqlcmd -S localhost\SQLExpress -E /i db_detach.sql
copy "C:\Murach\ASP2VB\Original database\Halloween.mdf"     "C:\Murach\ASP2VB\Database"
copy "C:\Murach\ASP2VB\Original database\Halloween_log.ldf" "C:\Murach\ASP2VB\Database"
sqlcmd -S localhost\SQLExpress -E /i db_attach.sql


 

 
Maint