1 2 /* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */3 4 /*
5 ** 17732, "You do not own table '%1!'."
6 ** 17461, "Object does not exist in this database."
7 */8 9 /*
10 ** IMPORTANT NOTE:
11 ** This stored procedure uses the built-in function object_id() in the
12 ** where clause of a select query. If you intend to change this query
13 ** or use the object_id() or db_id() builtin in this procedure, please read the
14 ** READ.ME file in the $DBMS/generic/sproc directory to ensure that the rules
15 ** pertaining to object-id's and db-id's outlined there, are followed.
16 */17 CREATEPROCEDUREsp_remap18 @objname varchar(92)/* object to remap */19 AS20 21 /*
22 ** Local variables here
23 */24 DECLARE@procid int,/* object id of the procedure */25 @site varchar(255),26 @dbname varchar(255),27 @uname varchar(255),28 @procname varchar(255),29 @other_owner int
30 31 32 IF @@trancount = 0
33 BEGIN34 SET chained off35 END36 37 SETtransactionisolationlevel 1
38 39 /*
40 ** Initialize some local variables.
41 */42 select@procid=NULL43 select@site=NULL44 select@dbname=NULL45 select@uname=NULL46 select@procname=NULL47 select@other_owner= 0
48 49 /*
50 ** Crack the name passed in.
51 */52 EXECUTEsp_namecrack@objname,@siteOUTPUT,@dbnameOUTPUT,53 @unameOUTPUT,@procnameOUTPUT54 55 /*
56 ** Make sure that the user specified an object in this
57 ** database.
58 */59 IF(@dbnameisnotNULLAND@dbname!= db_name())60 BEGIN61 /* 17461, "Object does not exist in this database." */62 raiserror 17461
63 RETURN(1)64 END65 66 /*
67 ** Make sure that, if the user specified an object owned
68 ** by somebody other than him/herself, that user has the
69 ** proper permissions to remap that object.
70 */71 IF(@unameisNULL)72 BEGIN73 /*
74 ** By normal SQL Server object resolution standards,
75 ** this means the object is owned either by the user
76 ** *or* by the dbo (if the user doesn't own an object
77 ** with the given name). Let's check for the
78 ** DBO case.
79 */80 IF(object_id(user_name()+ "." +@procname)isNULL)81 BEGIN82 /*
83 ** Check if the object really doesn't exist
84 ** before causing an audit record to be sent.
85 */86 if((user_id()= 1)OR87 (object_id("dbo." +@procname)isNULL))88 BEGIN89 /*
90 ** The object really doesn't exist.
91 */92 /*
93 ** 17461, "Object does not exist
94 ** in this database."
95 */96 raiserror 17461
97 RETURN(1)98 END99 select@other_owner= 1
100 END101 END102 ELSE103 BEGIN104 IF(user_name()!=@uname)105 BEGIN106 /*
107 ** Check if the object really doesn't exist
108 ** before causing an audit record to be sent.
109 */110 IF(object_id(@uname+ "." +@procname)isNULL)111 BEGIN112 /*
113 ** The object really doesn't exist.
114 */115 /*
116 ** 17461, "Object does not exist
117 ** in this database."
118 */119 raiserror 17461
120 RETURN(1)121 END122 SELECT@other_owner= 1
123 END124 END125 126 IF(@other_owner= 1)127 BEGIN128 /*
129 ** The user specified somebody else's object. This
130 ** user must possess SA role in order to proceed.
131 */132 IF(proc_role("sa_role")= 0)133 BEGIN134 RETURN 1
135 END136 END137 138 /*
139 ** Get the object id.
140 */141 SELECT@procid=id142 FROMsysobjects143 WHEREname=@procname144 AND(uid= user_id()OR user_id()= 1)145 ANDtypeIN('V ', 'P ', 'R ', 'D ', 'TR', 'IT')146 147 /*
148 ** If the object doesn't exist, return.
149 */150 IF@procidisNULL151 BEGIN152 /* 17461, "Object does not exist in this database." */153 raiserror 17461
154 RETURN(1)155 END156 157 /*
158 ** Obtain current database information
159 */160 SELECT@dbname= db_name()161 162 /*
163 ** Do the actual remap here now that we have the object id.
164 ** The command is run in the current database.
165 */166 167 DBCC REMAP(@procid,@dbname, 1)168 169
exec sp_procxmode 'sp_remap', 'AnyMode'
go
Grant Execute on sp_remap to public
go