DatabaseProcApplicationCreatedLinks
sybsystemprocssp_remap  31 Aug 14Defects Dependencies

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    CREATE PROCEDURE sp_remap
18        @objname varchar(92) /* object to remap */
19    AS
20    
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        BEGIN
34            SET chained off
35        END
36    
37        SET transaction isolation level 1
38    
39        /*
40        ** Initialize some local variables.
41        */
42        select @procid = NULL
43        select @site = NULL
44        select @dbname = NULL
45        select @uname = NULL
46        select @procname = NULL
47        select @other_owner = 0
48    
49        /*
50        ** Crack the name passed in.
51        */
52        EXECUTE sp_namecrack @objname, @site OUTPUT, @dbname OUTPUT,
53            @uname OUTPUT, @procname OUTPUT
54    
55        /*
56        **  Make sure that the user specified an object in this
57        **  database.
58        */
59        IF (@dbname is not NULL AND @dbname != db_name())
60        BEGIN
61            /* 17461, "Object does not exist in this database." */
62            raiserror 17461
63            RETURN (1)
64        END
65    
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 (@uname is NULL)
72        BEGIN
73            /*
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) is NULL)
81            BEGIN
82                /*
83                **  Check if the object really doesn't exist
84                **  before causing an audit record to be sent.
85                */
86                if ((user_id() = 1) OR
87                        (object_id("dbo." + @procname) is NULL))
88                BEGIN
89                    /*
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                END
99                select @other_owner = 1
100           END
101       END
102       ELSE
103       BEGIN
104           IF (user_name() != @uname)
105           BEGIN
106               /*
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) is NULL)
111               BEGIN
112                   /*
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               END
122               SELECT @other_owner = 1
123           END
124       END
125   
126       IF (@other_owner = 1)
127       BEGIN
128           /*
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           BEGIN
134               RETURN 1
135           END
136       END
137   
138       /*
139       **  Get the object id.
140       */
141       SELECT @procid = id
142       FROM sysobjects
143       WHERE name = @procname
144           AND (uid = user_id() OR user_id() = 1)
145           AND type IN ('V ', 'P ', 'R ', 'D ', 'TR', 'IT')
146   
147       /*
148       **  If the object doesn't exist, return.
149       */
150       IF @procid is NULL
151       BEGIN
152           /* 17461, "Object does not exist in this database." */
153           raiserror 17461
154           RETURN (1)
155       END
156   
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
DEFECTS
 MGTP 3 Grant to public sybsystemprocs..sp_remap  
 MGTP 3 Grant to public sybsystemprocs..sysobjects  
 MNER 3 No Error Check should check return value of exec 52
 MUCO 3 Useless Code Useless Brackets 59
 MUCO 3 Useless Code Useless Brackets 63
 MUCO 3 Useless Code Useless Brackets 71
 MUCO 3 Useless Code Useless Brackets 80
 MUCO 3 Useless Code Useless Brackets 86
 MUCO 3 Useless Code Useless Brackets 97
 MUCO 3 Useless Code Useless Brackets 104
 MUCO 3 Useless Code Useless Brackets 110
 MUCO 3 Useless Code Useless Brackets 120
 MUCO 3 Useless Code Useless Brackets 126
 MUCO 3 Useless Code Useless Brackets 132
 MUCO 3 Useless Code Useless Brackets 154
 QISO 3 Set isolation level 37
 QPNC 3 No column in condition 144
 VNRD 3 Variable is not read @site 52
 MTR1 2 Metrics: Comments Ratio Comments: 56% 17
 MTR2 2 Metrics: Cyclomatic Complexity Cyclo: 11 = 15dec - 6exi + 2 17
 MTR3 2 Metrics: Query Complexity Complexity: 52 17

DEPENDENCIES
PROCS AND TABLES USED
calls proc sybsystemprocs..sp_namecrack  
reads table sybsystemprocs..sysobjects