1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*----------------------------------------*/
[email protected]: target bak name
[email protected]: target restore directory end by '/'
/*----------------------------------------*/

declare @path nvarchar(200)
declare @filename nvarchar(400)
SET @path='D:\backup\'
SET @[email protected]path+'\MobileSaleStatMain.bak'

RESTORE DATABASE MobileSaleStatMain FROM DISK = @filename
with replace,
move 'MobileSaleStatMain' to 'D:\MSSQL\Data\MobileSaleStatMain\MobileSaleStatMain.mdf',
move 'MobileSaleStatMain_log' to 'D:\MSSQL\Data\MobileSaleStatMain\MobileSaleStatMain_log.ldf',
recovery
GO

USE [MobileSaleStatMain]
GO
IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'mobilesalestatmain')
DROP USER [mobilesalestatmain]
GO

USE [MobileSaleStatMain]
SELECT s.name
FROM sys.schemas s
WHERE s.principal_id = USER_ID('mobilesalestatmain');

--modidy user
ALTER AUTHORIZATION ON SCHEMA::db_owner TO dbo;
GO