DatabaseProcApplicationCreatedLinks
sybsystemprocssp_addmessage  31 Aug 14Defects Dependencies

1     
2     /* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */
3     /* generic/sproc/addmessage	14.2	4/25/91 */
4     
5     /* 
6     ** Messages from sysmessages
7     **
8     ** 17210, "Message number must be at least 20000."
9     ** 17211, "Cannot add message until Sysusermessages system table is created properly by Upgrade."
10    ** 17201, "'%1!' is not an official language name from Syslanguages."
11    ** 17212, "A message with number %1! in the specified language already exists."
12    ** 17213, "Drop the old message first if you still wish to add this one."
13    ** 17214, "The message has been inserted."
14    ** 17215, "The message has not been inserted."
15    ** 17216, "Only the System Administrator (SA) or the Database Owner (dbo) may add messages which set the WITH_LOG option to `'true'`."
16    ** 17217, "The only valid @with_log values are TRUE or FALSE."
17    ** 17240, "'%1!' is not a valid name."
18    ** 17756, "The execution of the stored procedure '%1!' in database
19    **         '%2!' was aborted because there was an error in writing the
20    **         replication log record."
21    */
22    create procedure sp_addmessage
23        @message_num int,
24        @message_text varchar(1024),
25        @language varchar(255) = NULL,
26        @with_log varchar(5) = 'FALSE',
27        @replace varchar(7) = NULL
28    as
29    
30        declare @msg varchar(1024)
31        declare @returncode int
32        declare @dlevel smallint
33        declare @suid int
34        declare @eventnum int /* event number for add message auditing */
35        declare @mod_ok int /* successful add message auditing  */
36        declare @mod_fail int /* failure add message auditing  */
37    
38    
39        select @eventnum = 15 /* event number for add message */
40        select @mod_ok = 1
41        select @mod_fail = 2
42        declare @dbname varchar(255)
43    
44    
45        /* Use the default language by default */
46        declare @lang_id smallint
47        select @lang_id = @@langid
48    
49    
50        if @@trancount = 0
51        begin
52            set chained off
53        end
54        set transaction isolation level 1
55    
56        if @message_num < 20000
57        BEGIN
58            /* 17210, "Message number must be at least 20000." */
59            raiserror 17210
60            return (1)
61        END
62    
63        /*
64        ** Check to be sure Sysusermessages exists
65        */
66        if not exists (select * from sysobjects
67                where name = "sysusermessages"
68                    and type = 'S')
69        BEGIN
70            /* 
71            ** 17211, "Cannot add message until Sysusermessages system table is
72            **		created properly by Upgrade."
73            */
74            raiserror 17211
75            return (1)
76        END
77    
78        /*
79        ** Check that language is valid.
80        */
81        if @language is not NULL
82        BEGIN
83            execute @returncode = sp_validlang @language
84            if @returncode != 0
85            begin
86                /* Us_english is always valid */
87                if @language != "us_english"
88                BEGIN
89                    /* 
90                    ** 17201, "'%1!' is not an official language
91                    **	name from Syslanguages."
92                    */
93                    raiserror 17201, @language
94                    return @returncode
95                END
96    
97                /* set to us_english */
98                select @lang_id = NULL
99            end
100   
101           else
102               select @lang_id = langid from master.dbo.syslanguages
103               where @language = name
104       END
105   
106       /* The langid is assigned 0 since it gets its value from @@langid. */
107       /* For us_english, we have to insert it as NULL and not 0, this is */
108       /* to maintain compatibility with the current conventions          */
109   
110       if @lang_id = 0
111       begin
112           select @lang_id = NULL
113       end
114   
115       /*
116       ** Only the DBO or SA (has "sa_role") may add messages which set the 
117       ** WITH_LOG option to `true'
118       */
119       if ((user_id() != 1) and (charindex("sa_role", show_role()) = 0))
120           and (rtrim(upper(@with_log)) = 'TRUE')
121       begin
122           /*
123           ** 17216, "Only the System Administrator (SA) or the Database
124           ** 	owner (dbo) may add messages which set the WITH_LOG 
125           **	option to `'true''."
126           */
127           raiserror 17216
128           return (1)
129       end
130   
131       /*
132       ** @with_log must be 'TRUE' or 'FALSE'
133       */
134       if (upper(@with_log) not in ('TRUE', 'FALSE'))
135       begin
136           /*
137           ** 17217, "The only valid @with_log values are TRUE or FALSE."
138           */
139           raiserror 17217
140           return (1)
141       end
142   
143       /*
144       ** Set the dlevel bit accordingly
145       */
146       if (rtrim(upper(@with_log)) = 'TRUE')
147           select @dlevel = hextoint("0x80")
148       else
149           select @dlevel = 0x0
150   
151       /*
152       **  Does this message already exist, and if so are we REPLACEing it?
153       */
154       select @suid = uid from sysusermessages
155       where error = @message_num and langid = @lang_id
156   
157       if (@@rowcount > 0)
158       BEGIN
159           if lower(@replace) = 'replace'
160               if (@suid = suser_id())
161               begin
162                   print 'Replacing message.'
163                   delete from sysusermessages where error = @message_num
164                       and langid = @lang_id
165               end
166               else
167               begin
168                   /* 17218, "Message number %1! with language ID %2! was
169                   ** created by user ID %3!. You cannot replace that
170                   ** message; you must first delete the existing message
171                   ** using sp_dropmessage, then add the changed message."
172                   */
173                   raiserror 17218, @message_num, @lang_id, @suid
174                   return (1)
175               end
176           else
177           begin
178               /* 17212, "A message with number %1! in the specified 
179               ** language already exists." 
180               */
181               raiserror 17212, @message_num
182   
183               /* 17213, "Drop the old message first if you still wish to 
184               ** add this one." 
185               */
186               raiserror 17213
187               return (1)
188           end
189       END
190   
191       /*
192       ** Check that the message is a valid one.
193       */
194       print @message_text, "<arg1>", "<arg2>", "<arg3>", "<arg4>",
195           "<arg5>", "<arg6>", "<arg7>", "<arg8>",
196           "<arg9>", "<arg10>", "<arg11>", "<arg12>",
197           "<arg13>", "<arg14>", "<arg15>", "<arg16>",
198           "<arg17>", "<arg18>", "<arg19>", "<arg20>"
199   
200       /*
201       ** If there was an error, @@error will be non-zero
202       */
203       if @@error != 0
204           return (1)
205   
206   
207   
208       /*
209       ** Generate successful audit record.
210       */
211       select @returncode = ad_hoc_audit(@eventnum, @mod_ok, str(@message_num),
212               db_name(), "sysusermessages", suser_name(), 0, object_id("sysusermessages")
213   
214           )
215   
216       /* 
217       ** This transaction also writes a log record for replicating the
218       ** invocation of this procedure. If logexec() fails, the transaction
219       ** is aborted.
220       **
221       ** IMPORTANT: The name rs_logexec is significant and is used by
222       ** Replication Server.
223       */
224       begin transaction rs_logexec
225   
226       /* Now, insert the message */
227       insert into sysusermessages
228       values (@message_num, suser_id(), @message_text, @lang_id, @dlevel)
229       if @@error != 0
230       begin
231           /* 17215, "The message has not been inserted." */
232           raiserror 17215
233   
234           rollback transaction rs_logexec
235           return (1)
236       end
237   
238       /*
239       ** Write the log record to replicate this invocation 
240       ** of the stored procedure.
241       */
242       if (logexec() != 1)
243       begin
244           /*
245           ** 17756, "The execution of the stored procedure '%1!' in
246           **         database '%2!' was aborted because there was an
247           **         error in writing the replication log record."
248           */
249           select @dbname = db_name()
250           raiserror 17756, "sp_addmessage", @dbname
251   
252           rollback transaction rs_logexec
253           return (1)
254       end
255   
256       commit transaction
257   
258       /* 17214, "The message has been inserted." */
259       exec sp_getmessage 17214, @msg output
260       print @msg
261   
262       return (0)
263   


exec sp_procxmode 'sp_addmessage', 'AnyMode'
go

Grant Execute on sp_addmessage to public
go
DEFECTS
 MINU 4 Unique Index with nullable columns master..syslanguages master..syslanguages
 MINU 4 Unique Index with nullable columns sybsystemprocs..sysusermessages sybsystemprocs..sysusermessages
 MTYP 4 Assignment type mismatch @lang_id: smallint = int 47
 MBLI 3 Integer Value of Binary Literal is Platform Dependant 149
 MGTP 3 Grant to public master..syslanguages  
 MGTP 3 Grant to public sybsystemprocs..sp_addmessage  
 MGTP 3 Grant to public sybsystemprocs..sysobjects  
 MGTP 3 Grant to public sybsystemprocs..sysusermessages  
 MNER 3 No Error Check should check @@error after delete 163
 MNER 3 No Error Check should check return value of exec 259
 MUCO 3 Useless Code Useless Brackets 60
 MUCO 3 Useless Code Useless Brackets 75
 MUCO 3 Useless Code Useless Brackets 128
 MUCO 3 Useless Code Useless Brackets 134
 MUCO 3 Useless Code Useless Brackets 140
 MUCO 3 Useless Code Useless Brackets 146
 MUCO 3 Useless Code Useless Brackets 157
 MUCO 3 Useless Code Useless Brackets 160
 MUCO 3 Useless Code Useless Brackets 174
 MUCO 3 Useless Code Useless Brackets 187
 MUCO 3 Useless Code Useless Brackets 204
 MUCO 3 Useless Code Useless Brackets 235
 MUCO 3 Useless Code Useless Brackets 242
 MUCO 3 Useless Code Useless Brackets 253
 MUCO 3 Useless Code Useless Brackets 262
 QISO 3 Set isolation level 54
 QPRI 3 Join or Sarg with Rooted Partial Index Use SARG Candidate index: sysobjects.ncsysobjects unique
(name, uid)
Intersection: {name}
67
 VNRD 3 Variable is not read @mod_fail 41
 VNRD 3 Variable is not read @returncode 211
 MSUB 2 Subquery Marker 66
 MTR1 2 Metrics: Comments Ratio Comments: 49% 22
 MTR2 2 Metrics: Cyclomatic Complexity Cyclo: 12 = 20dec - 10exi + 2 22
 MTR3 2 Metrics: Query Complexity Complexity: 102 22

DEPENDENCIES
PROCS AND TABLES USED
reads table master..syslanguages (1)  
calls proc sybsystemprocs..sp_validlang  
   reads table master..syslanguages (1)  
calls proc sybsystemprocs..sp_getmessage  
   reads table sybsystemprocs..sysusermessages  
   reads table master..sysmessages (1)  
   reads table master..syslanguages (1)  
   calls proc sybsystemprocs..sp_validlang  
reads table sybsystemprocs..sysobjects  
read_writes table sybsystemprocs..sysusermessages