1 2 /* Sccsid = "@(#) generic/sproc/src/%M% %I% %G%" */3 4 /*
5 ** Messages for "sp_dbremap" 17550
6 **
7 ** 17260, "Can't run %1! from within a transaction."
8 ** 17284, "'%1!' is not a valid identifier."
9 ** 17590, "The specified database does not exist."
10 ** 18336, "Permission denied. Your curwrite label must be
11 ** set at the hurdle of the affected database."
12 */13 14 createproceduresp_dbremap15 @dbname varchar(30)/* name of database whose diskmap is to be remapped */16 as17 18 declare@procval int
19 20 21 if @@trancount = 0
22 begin23 set chained off24 end25 26 settransactionisolationlevel 1
27 28 /*
29 ** Check for valid identifier.
30 */31 if valid_name(@dbname)= 0
32 begin33 /*
34 ** 17284, "'%1!' is not a valid identifier."
35 */36 raiserror 17284,@dbname37 return(1)38 end39 40 41 /*
42 ** Only the Database Owner (DBO) or
43 ** Accounts with SA role can execute it.
44 ** if user had SA role he would be the dbo hence check only
45 ** whether user is DBO.
46 ** Call proc_role() with the required SA role.
47 */48 if(user_id()= 1)49 begin50 /* If user has sa role audit this as a successful sa
51 ** command execution.
52 */53 if charindex("sa_role", show_role())> 0
54 select@procval= proc_role("sa_role")55 end56 else57 begin58 /* user_id() is not DBO hence user does not have SA role
59 ** audit this as a failed sa command execution.
60 */61 select@procval= proc_role("sa_role")62 return(1)63 end64 65 /*
66 ** If we're in a transaction, disallow this since it might make recovery
67 ** impossible.
68 */69 if @@trancount > 0
70 begin71 /*
72 ** 17260, "Can't run %1! from within a transaction."
73 */74 raiserror 17260, "sp_dbremap"
75 return(1)76 end77 else78 begin79 set chained off80 end81 82 settransactionisolationlevel 1
83 84 /*
85 ** Check to see if the database exists.
86 */87 ifnotexists(select*88 frommaster.dbo.sysdatabases89 wherename=@dbname)90 begin91 /*
92 ** 17590, "The specified database does not exist."
93 */94 raiserror 17590
95 return(1)96 end97 98 99 100 /*
101 ** Now go ahead and update the in-core image of the database usage map table.
102 */103 dbcc dbrepair(@dbname, remap)104 105 106 return(0)107
exec sp_procxmode 'sp_dbremap', 'AnyMode'
go
Grant Execute on sp_dbremap to public
go