DatabaseProcApplicationCreatedLinks
sybsystemprocssp_dbremap  31 Aug 14Defects Dependencies

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    create procedure sp_dbremap
15        @dbname varchar(30) /* name of database whose diskmap is to be remapped */
16    as
17    
18        declare @procval int
19    
20    
21        if @@trancount = 0
22        begin
23            set chained off
24        end
25    
26        set transaction isolation level 1
27    
28        /*
29        ** Check for valid identifier.
30        */
31        if valid_name(@dbname) = 0
32        begin
33            /*
34            ** 17284, "'%1!' is not a valid identifier."
35            */
36            raiserror 17284, @dbname
37            return (1)
38        end
39    
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        begin
50            /* 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        end
56        else
57        begin
58            /* 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        end
64    
65        /*
66        **  If we're in a transaction, disallow this since it might make recovery
67        **  impossible.
68        */
69        if @@trancount > 0
70        begin
71            /*
72            ** 17260, "Can't run %1! from within a transaction."
73            */
74            raiserror 17260, "sp_dbremap"
75            return (1)
76        end
77        else
78        begin
79            set chained off
80        end
81    
82        set transaction isolation level 1
83    
84        /*
85        **  Check to see if the database exists.
86        */
87        if not exists (select *
88                from master.dbo.sysdatabases
89                where name = @dbname)
90        begin
91            /*
92            ** 17590, "The specified database does not exist."
93            */
94            raiserror 17590
95            return (1)
96        end
97    
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
DEFECTS
 MGTP 3 Grant to public master..sysdatabases  
 MGTP 3 Grant to public sybsystemprocs..sp_dbremap  
 MUCO 3 Useless Code Useless Brackets 37
 MUCO 3 Useless Code Useless Brackets 48
 MUCO 3 Useless Code Useless Brackets 62
 MUCO 3 Useless Code Useless Brackets 75
 MUCO 3 Useless Code Useless Brackets 95
 MUCO 3 Useless Code Useless Brackets 106
 QISO 3 Set isolation level 26
 QISO 3 Set isolation level 82
 VNRD 3 Variable is not read @procval 61
 MSUB 2 Subquery Marker 87
 MTR1 2 Metrics: Comments Ratio Comments: 54% 14
 MTR2 2 Metrics: Cyclomatic Complexity Cyclo: 3 = 6dec - 5exi + 2 14
 MTR3 2 Metrics: Query Complexity Complexity: 31 14

DEPENDENCIES
PROCS AND TABLES USED
reads table master..sysdatabases (1)