Fix test
[dbsrgits/SQL-Translator.git] / t / data / sybase / create.sql
1 /* Script generated by dbschema.pl(2.4.2) on Mon Aug 18 14:15:25 2003.  */
2 /* Script extracted on a solaris system. */
3
4 use master
5 go
6
7 /* Groups... */
8
9 /* No groups found. */
10
11
12 /* Users... */
13
14 exec sp_adduser 'guest', 'guest'
15 exec sp_adduser 'probe', 'probe'
16 go
17
18 /* Aliases... */
19
20 /* No aliases found. */
21
22 /* Add user-defined data types: */
23
24 /* No user defined types found. */
25
26
27 /* Rules... */
28
29 /* No rules found. */
30
31 /* Defaults... */
32
33 /* No defaults found. */
34 /* Bind rules & defaults to user data types... */
35
36 /* No defaults to bind. */
37
38 /* No rules to bind. */
39
40
41 /* Start of description of table dbo.jdbc_function_escapes */
42
43 setuser 'dbo'
44 go
45
46 CREATE TABLE dbo.jdbc_function_escapes (
47         escape_name varchar(40) NOT NULL,
48         map_string  varchar(40) NOT NULL
49 )
50 go
51
52
53 /* Add permissions for table... */
54 IF OBJECT_ID('dbo.jdbc_function_escapes') IS NOT NULL
55 BEGIN
56     GRANT SELECT ON dbo.jdbc_function_escapes TO public
57 END
58 go
59
60 /* Bind rules & defaults to columns... */
61 /* End of description of table dbo.jdbc_function_escapes */
62
63
64 /* Start of description of table dbo.spt_jdbc_conversion */
65
66 setuser 'dbo'
67 go
68
69 CREATE TABLE dbo.spt_jdbc_conversion (
70         datatype   int      NOT NULL,
71         conversion char(20) NOT NULL
72 )
73 go
74
75
76 /* Add permissions for table... */
77 IF OBJECT_ID('dbo.spt_jdbc_conversion') IS NOT NULL
78 BEGIN
79     GRANT SELECT ON dbo.spt_jdbc_conversion TO public
80 END
81 go
82
83 /* Bind rules & defaults to columns... */
84 /* End of description of table dbo.spt_jdbc_conversion */
85
86
87 /* Start of description of table dbo.spt_jdbc_table_types */
88
89 setuser 'dbo'
90 go
91
92 CREATE TABLE dbo.spt_jdbc_table_types (
93         TABLE_TYPE char(15) NOT NULL
94 )
95 go
96
97
98 /* Add permissions for table... */
99 IF OBJECT_ID('dbo.spt_jdbc_table_types') IS NOT NULL
100 BEGIN
101     GRANT SELECT ON dbo.spt_jdbc_table_types TO public
102 END
103 go
104
105 /* Bind rules & defaults to columns... */
106 /* End of description of table dbo.spt_jdbc_table_types */
107
108
109 /* Start of description of table dbo.spt_jtext */
110
111 setuser 'dbo'
112 go
113
114 CREATE TABLE dbo.spt_jtext (
115         mdinfo varchar(30) NOT NULL,
116         value  text        NOT NULL,
117     UNIQUE (mdinfo)
118 )
119 go
120
121
122 /* Add permissions for table... */
123 IF OBJECT_ID('dbo.spt_jtext') IS NOT NULL
124 BEGIN
125     GRANT SELECT ON dbo.spt_jtext TO public
126 END
127 go
128
129 /* Bind rules & defaults to columns... */
130 /* End of description of table dbo.spt_jtext */
131
132
133 /* Start of description of table dbo.spt_limit_types */
134
135 setuser 'dbo'
136 go
137
138 CREATE TABLE dbo.spt_limit_types (
139         name        char(30) NOT NULL,
140         id          smallint NOT NULL,
141         enforced    tinyint  NOT NULL,
142         object_type smallint NOT NULL,
143         scope       smallint NOT NULL,
144         units       char(60) NOT NULL
145 )
146 go
147
148
149 /* Add permissions for table... */
150 IF OBJECT_ID('dbo.spt_limit_types') IS NOT NULL
151 BEGIN
152     GRANT SELECT ON dbo.spt_limit_types TO public
153 END
154 go
155
156 /* Bind rules & defaults to columns... */
157 /* End of description of table dbo.spt_limit_types */
158
159
160 /* Start of description of table dbo.spt_mda */
161
162 setuser 'dbo'
163 go
164
165 CREATE TABLE dbo.spt_mda (
166         mdinfo       varchar(30)  NOT NULL,
167         querytype    tinyint      NOT NULL,
168         query        varchar(255)     NULL,
169         mdaver_start tinyint      NOT NULL,
170         mdaver_end   tinyint      NOT NULL,
171         srvver_start int          NOT NULL,
172         srvver_end   int          NOT NULL
173 )
174 go
175
176
177 IF OBJECT_ID('dbo.spt_mda') IS NOT NULL
178 BEGIN
179     CREATE UNIQUE NONCLUSTERED INDEX spt_mda_ind
180     ON spt_mda (mdinfo, mdaver_end, srvver_end)
181 END
182 go
183
184
185 /* Add permissions for table... */
186 IF OBJECT_ID('dbo.spt_mda') IS NOT NULL
187 BEGIN
188     GRANT SELECT ON dbo.spt_mda TO public
189 END
190 go
191
192 /* Bind rules & defaults to columns... */
193 /* End of description of table dbo.spt_mda */
194
195
196 /* Start of description of table dbo.spt_monitor */
197
198 setuser 'dbo'
199 go
200
201 CREATE TABLE dbo.spt_monitor (
202         lastrun       datetime NOT NULL,
203         cpu_busy      int      NOT NULL,
204         io_busy       int      NOT NULL,
205         idle          int      NOT NULL,
206         pack_received int      NOT NULL,
207         pack_sent     int      NOT NULL,
208         connections   int      NOT NULL,
209         pack_errors   int      NOT NULL,
210         total_read    int      NOT NULL,
211         total_write   int      NOT NULL,
212         total_errors  int      NOT NULL
213 )
214 ON system
215 go
216
217
218 /* Add permissions for table... */
219 IF OBJECT_ID('dbo.spt_monitor') IS NOT NULL
220 BEGIN
221     GRANT SELECT ON dbo.spt_monitor TO public
222 END
223 go
224
225 /* Bind rules & defaults to columns... */
226 /* End of description of table dbo.spt_monitor */
227
228
229 /* Start of description of table dbo.spt_values */
230
231 setuser 'dbo'
232 go
233
234 CREATE TABLE dbo.spt_values (
235         name   varchar(28)     NULL,
236         number int         NOT NULL,
237         type   char(2)     NOT NULL,
238         low    int             NULL,
239         high   int             NULL,
240         msgnum int             NULL
241 )
242 ON system
243 go
244
245
246 IF OBJECT_ID('dbo.spt_values') IS NOT NULL
247 BEGIN
248     CREATE CLUSTERED INDEX spt_valuesclust
249     ON spt_values (number, type)
250 END
251 go
252
253
254 /* Add permissions for table... */
255 IF OBJECT_ID('dbo.spt_values') IS NOT NULL
256 BEGIN
257     GRANT SELECT ON dbo.spt_values TO public
258 END
259 go
260
261 /* Bind rules & defaults to columns... */
262 /* End of description of table dbo.spt_values */
263
264
265 /* Start of description of table dbo.syblicenseslog */
266
267 setuser 'dbo'
268 go
269
270 CREATE TABLE dbo.syblicenseslog (
271         status      smallint NOT NULL,
272         logdate     datetime NOT NULL,
273         maxlicenses int      NOT NULL
274 )
275 go
276
277
278 /* Add permissions for table... */
279 /* Bind rules & defaults to columns... */
280 /* End of description of table dbo.syblicenseslog */
281
282
283 /* Now create the key definitions ...*/
284
285
286 setuser 'dbo'
287 go
288
289 /* Views... */
290
291 /* No views found. */
292
293 /* Procedures... */
294
295 /* Procedure sp_configure, owner dbo */
296
297 setuser 'dbo'
298 go
299
300 /* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */
301 /*      4.8     1.1     06/14/90        sproc/src/configure */
302
303 /*
304 ** Messages for "sp_configure"          17410
305 **      Must use "langid" when referencing spt_values
306 **
307 ** 17260, "Can't run %1! from within a transaction." 
308 ** 17410, "Configuration option doesn't exist."
309 ** 17411, "Configuration option is not unique."
310 ** 17413, "The value of the 'number of devices' must be greater than the highest VDEVNO, '%1!', defined in sysdevices."
311 ** 17414, "You can't set the default language to a language ID that is not defined in Syslanguages."
312 ** 17415, "Configuration option value is not legal."
313 ** 17418, "'%1!' is an invalid file command. The valid commands are 'verify', 'read', 'write', and 'restore'."
314 ** 17419, "Configuration option changed. The SQL Server need not be rebooted since the option is dynamic.
315 ** 18123, "Configuration option changed. The SQL Server must be rebooted before the change in effect since the option is static."
316 ** 18124, "No matching configuration options.  Here is a listing of groups:"
317 ** 18125, "Must provide the parameter 'filename'."
318 ** 18133, "The character set, '%1!', is invalid since it is not defined in Syscharsets."
319 ** 18134, "The sortorder, '%1!', is invalid since it is not defined in Syscharsets."
320 ** 18549, "Invalid third argument supplied: '%1!'. Valid choices are
321 **         'with truncate' or 'default'."
322 */
323 create procedure sp_configure
324 @configname varchar(80) = NULL,         /* configure option name */
325 @configvalue int = NULL,                /* configure value */
326 @configvalue2 varchar(255) = NULL,      /* config file command/charset info */
327 @configvalue3 varchar(255) = NULL       /* physical name of file */
328 as
329
330 declare @confignum int                  /* number of option to be configured */
331 declare @configcount int                /* number of options like @configname */
332 declare @whichone       int             /* using english or default lang ? */
333 declare @cmd            smallint        /* configuration file command */
334 declare @status int                     /* return status for misc calls */
335 declare @children int                   /* number of children in a group */
336 declare @parent int                     /* config number of parent group */
337 declare @msg varchar(255)               /* temp buffer for messages */
338 declare @sysconfig smallint             /* contents of sysconfigures.config */
339 declare @sysname varchar(255)           /* contents of sysconfigures.comment */
340 declare @sysparent smallint             /* contents of sysconfigures.parent */
341 declare @sysstatus smallint             /* contents of sysconfigures.status */
342 declare @value int                      /* default charset/sort order id */
343 declare @user_displaylevel int          /* user display level */
344 declare @maxvdevno int                  /* highest number of vdevno */
345 declare @sortorder_id int               /* current sortorder id */
346 declare @charset_id int                 /* current charset id */
347 declare @use_wildcard tinyint           /* use wildcard to search option name or not */
348 declare @match_count int                /* number of option found by name match */
349 declare @cache_part_temp      int         /* cache partition number        */
350 declare @partition_number     int         /* cache partition number        */
351 declare @cmpstate       int             /* Local NODE state in companionship */
352 declare @nocase         tinyint         /* case-sensitive sort order flag */
353
354 select @whichone = 0
355 select @status = 0
356 select @cmd = 1
357 select @value = NULL
358 select @user_displaylevel = NULL
359 select @sortorder_id =
360         value from master.dbo.syscurconfigs where config = 123
361 select @charset_id =
362         value from master.dbo.syscurconfigs where config = 131 
363  
364 select @use_wildcard = 1
365
366 /*
367 ** Check if the default sort order is case-insensitive.
368 */
369 if ("A" = "a")
370         select @nocase = 1
371 else
372         select @nocase = 0
373
374 /*
375 **  Disallow running sp_configure within a transaction since it might make
376 **  recovery impossible.
377 */
378 if @@trancount > 0
379 begin
380         /*
381         ** 17260, "Can't run %1! from within a transaction."
382         */
383                 raiserror 17260, "sp_configure"
384         return (1)
385 end
386 else
387 begin
388         set chained off
389 end
390
391 set transaction isolation level 1
392 set nocount on
393
394 /*
395 **      If the "default sortorder" is case insensitive dictionary sort order,
396 ** the procedure will just print out all the options and their values
397 ** without grouping if no option name is given.
398 */
399 if (@nocase = 1 and @configname is NULL)
400         begin
401                select "Parameter Name" = convert(char(30), name),
402                         "Default" = convert(char(11), space(11-char_length(
403                                     convert(varchar(11), defvalue)))+
404                                     convert(varchar(11), defvalue)),
405                         "Memory Used" = convert(char(11), space(11-char_length(
406                                         convert(varchar(11), b.comment)))+
407                                         convert(varchar(11), b.comment)),
408                         "Config Value" =convert(char(11), space(11-char_length(
409                           isnull(a.value2, convert(char(32), a.value)))) +
410                           isnull(a.value2, convert(char(32), a.value))),
411                         "Run Value" = convert(char(11), space(11-char_length(
412                           isnull(b.value2, convert(char(32), b.value)))) +
413                           isnull(b.value2, convert(char(32), b.value)))
414                 from master.dbo.sysconfigures a,
415                         master.dbo.syscurconfigs b
416                 where
417                         a.config *= b.config
418                         and parent != 19
419                         and a.config != 19
420                 order by name
421  
422                 return (0)
423 end
424
425 /* Validate the configname if it not NULL */
426 if @configname is not NULL
427 begin
428         select @configcount = count(*)
429                 from master.dbo.sysconfigures
430                         where name like "%" + @configname + "%"
431                         and parent != 19
432
433         /*
434         **      If configure option is not unique and case-insensitive
435         ** dictionary sort order is used, check if unique option found
436         ** by exact name match, if so, then disable wildcard match
437         ** for searching option name.
438         */
439         if (@configcount > 1 and @nocase = 1)
440         begin
441                 /* check if unique option found by exact name match */
442                 select @match_count = count(*)
443                         from master.dbo.sysconfigures
444                                 where name = @configname
445                                         and parent != 19
446                 if @match_count =1
447                 begin
448                         select @use_wildcard = 0  /* don't use wildcard */
449                         select @configcount = @match_count
450                 end
451         end
452
453         /*
454         ** If more than one option like @configname,
455         ** show the duplicates and return.
456         */
457         if @configcount > 1
458         begin
459                 /*
460                 ** 17411, "Configuration option is not unique."
461                 */
462                 raiserror 17411
463                 print ""
464
465                 select "Parameter Name" = convert(char(30), name),
466                         "Default" = convert(char(11), space(11-char_length(
467                                     convert(varchar(11), defvalue)))+
468                                     convert(varchar(11), defvalue)),
469                         "Memory Used" = convert(char(11), space(11-char_length(
470                                         convert(varchar(11), b.comment)))+
471                                         convert(varchar(11), b.comment)),
472                         "Config Value" =convert(char(11), space(11-char_length(
473                           isnull(a.value2, convert(char(32), a.value)))) +
474                           isnull(a.value2, convert(char(32), a.value))),
475                         "Run Value" = convert(char(11), space(11-char_length(
476                           isnull(b.value2, convert(char(32), b.value)))) +
477                           isnull(b.value2, convert(char(32), b.value)))
478                 from master.dbo.sysconfigures a,
479                         master.dbo.syscurconfigs b
480                 where 
481                         a.config *= b.config
482                         and name like "%" + @configname + "%"
483                         and parent != 19
484                         and a.config != 19
485                 order by name
486
487                 return (1)
488         end
489         
490         /*
491         ** if it is a valid option and the @configvalue is not NULL,
492         ** set the option
493         */
494         if (@configcount != 0) and (@configvalue is not NULL)
495         begin
496                 /* set @confignum */
497                 select  @confignum = config,
498                         @sysstatus = status
499                         from master.dbo.sysconfigures
500                                 where name like "%" + @configname + "%"
501                                 and parent != 19
502                                 and config != 19
503
504                 /*
505                 ** If @configvalue2 is "default",
506                 ** setting the value to default
507                 */
508                 if (@configvalue2 = "default")
509                         select @value = 1
510                 else
511                         select @value = 0
512
513                 /*
514                 **  If the option name is "configuration file"
515                 */
516                 if @confignum = 114
517                 begin
518                         /*
519                         ** if the file command is one of the valid
520                         ** commands.
521                         */
522                         if ((@configvalue2 = "read") or
523                                 (@configvalue2 = "write") or
524                                 (@configvalue2 = "restore") or
525                                 (@configvalue2 = "verify"))
526                         begin
527                                 /*
528                                 ** if filename is NULL
529                                 */
530                                 if (@configvalue3 is NULL)
531                                 begin
532                                         /* 18125, "Must provide the parameter 'filename'." */
533                                         raiserror 18125
534                                 return(1)
535                                 end
536                                 /*
537                                 ** Must have sa_role to run these
538                                 ** commands 
539                                 */
540                                 if (proc_role("sa_role") < 1)
541                                 begin
542                                         return(1)
543                                 end
544                                 else
545                                 begin
546                                         if (@configvalue2 = "verify")
547                                                 select @cmd = 2
548                                         else
549                                         if (@configvalue2 = "read")
550                                                 select @cmd = 3
551                                         else
552                                         if (@configvalue2 = "write")
553                                                 select @cmd = 4
554                                         else
555                                         if (@configvalue2 = "restore")
556                                                 select @cmd = 5
557                                 end
558                         end
559                         else
560                         begin
561                                 /*
562                                 ** print the message to show the valid
563                                 ** file command
564                                 */
565                                 raiserror 17418, @configvalue2
566                                 return(1)
567                         end
568
569                         select @status = config_admin(@cmd,0,0,0,NULL,
570                                                 @configvalue3)
571
572                         if (@status = 1)
573                         begin
574                                 return(0)
575                         end
576                         else
577                         begin
578                                 return (1)
579                         end
580                 end
581
582                 if @confignum = 123
583                 begin
584                         /* get current default charset id */
585                         select @value = value from
586                                 master.dbo.sysconfigures
587                                 where config = 131
588
589                         if @configvalue2 is not NULL
590                         begin
591                                 /* validate the charset id */
592                                 if not exists (select *
593                                         from master..syscharsets
594                                         where name = @configvalue2 
595                                         and type between 1000 and 1999)
596
597                                 begin
598                                         /* 18133, "The character set, '%1!', is invalid since it 
599                                         ** is not defined in Syscharsets."
600                                         */
601                                         raiserror 18133, @configvalue2
602                                         return (1)
603                                 end
604
605                                 /* get default charset id from name */
606                                 select @value = id
607                                         from master..syscharsets
608                                         where name = @configvalue2 
609                                         and type between 1000 and 1999
610                         end
611                 end
612
613                 if @confignum = 131
614                 begin
615                         /* get current default sortord id */
616                         select @value = value from
617                                 master.dbo.sysconfigures
618                                 where config = 123
619
620                         if @configvalue2 is not NULL
621                         begin
622                                 /* validate the sortord id */
623                                 if not exists (select *
624                                         from master..syscharsets
625                                         where name = @configvalue2
626                                         and type between 2000 and 2999)
627                                 begin
628                                         /* 18134, "The sortorder, '%1!', is invalid since it 
629                                         ** is not defined in Syscharsets."
630                                         */
631                                         raiserror 18134, @configvalue2
632                                         return (1)
633                                 end
634
635                                 /* get default sortorder id from name */
636                                 select @value = id
637                                         from master..syscharsets
638                                         where name = @configvalue2
639                                         and type between 2000 and 2999
640                         end
641                 end
642
643                 /*
644                 ** If an attempt to enable a disk mirroring is made, and
645                 ** if this happens to be a server with HA services turned
646                 ** on, we disallow. Currently we do not support ASE HA
647                 ** services along with sybase mirroring.
648                 */
649                 if (@confignum = 140 and @configvalue = 0)
650                 begin
651                         select @cmpstate = @@cmpstate
652                         if @cmpstate >= 0
653                         begin
654                                 /* 18816 Mirroring not allowed in ASE HA */
655                                 raiserror 18816
656                                 return(1)
657                         end
658                 end
659                 
660                 /*
661                 ** If an attempt to disable disk mirroring is being made,
662                 ** ensure that there are no devices that are currently
663                 ** being mirrored.
664                 */
665         
666                 if (@confignum = 140 and @configvalue = 1)
667                 begin
668                         if (select count(*) from master.dbo.sysdevices
669                                 where status & 512 = 512) > 0
670                         begin
671
672                           /* 18750, Unable to disable disk mirroring
673                           ** because some devices are currently
674                           ** mirrored. Use 'disk unmirror' to
675                           ** unmirror these devices and then
676                           ** re-run this sp_configure command.
677                           */
678
679                           raiserror 18570
680                           return (1)
681                         end
682                 end
683
684                 /*
685                 **  If this is the number of devices configuration
686                 **  parameter, we want to make sure that it's not being
687                 **  set to lower than the
688                 **  number of devices in sysdevices.
689                 */
690                 if @confignum = 116
691                 begin
692                         /*
693                         ** Get the default value if trying to set the
694                         ** value to the default value
695                         */
696                         if (@value = 1)
697                         begin
698                                 select @configvalue = convert(int, defvalue)
699                                         from master.dbo.syscurconfigs
700                                         where config = 116
701                         end
702
703                         /*
704                         **  Get the max vdevno.
705                         */
706                         select @maxvdevno = max(
707                                 convert(tinyint,
708                                         substring(convert(binary(4), d.low),
709                                         v.low, 1)))
710                                 from master.dbo.sysdevices d,
711                                 master.dbo.spt_values v
712
713                         if (@configvalue <= @maxvdevno)
714                         begin
715                                 /* 17413, "The value of the 'number of devices' must be 
716                                 ** greater than the highest VDEVNO, '%1!', defined 
717                                 ** in sysdevices."
718                                 */
719                                 raiserror 17413, @maxvdevno
720                                 return (1)
721                         end
722                 end
723
724
725                 /* 
726                 **  If this is the number of default language, we want
727                 **  to make sure that the new value is a valid language
728                 **  id in Syslanguages.
729                 */ 
730                 if @confignum = 124
731                 begin
732                         if not exists (select *
733                                 from master.dbo.syslanguages
734                                         where langid = @configvalue)   
735                         begin
736                                 /* 0 is default language, us_english */
737                                 if @configvalue != 0
738                                 begin
739                                         /* 17414, "You can't set the default language to a 
740                                         ** language ID that is not defined in Syslanguages."
741                                         */
742                                         raiserror 17414
743                                         return (1)
744                                 end
745                         end
746                 end
747
748                 /* 
749                 **  If this is the number of current audit table  we want
750                 **  to make sure that if "with truncate" opiton is not 
751                 **  provided new table is empty other wise fail.
752                 */ 
753                 if @confignum = 260
754                 begin
755                         if @configvalue2 is not NULL
756                         begin
757                                 if (@configvalue2 not in ("with truncate", 
758                                                                 "default"))
759                                 begin
760                                         /*
761                                         ** 18549, "Invalid third argument
762                                         **         supplied: '%1!'. Valid
763                                         **         choices are 'with truncate'
764                                         **         or 'default'."
765                                         */
766                                         raiserror 18549, @configvalue2
767                                         return(1)
768                                 end
769                         end
770                         else
771                         begin
772                                 select @value = 2
773                         end
774                 end
775
776                 if @confignum = 337
777                 begin
778                         if @configvalue is not NULL
779                         begin
780                                 if (@configvalue2 = "default")
781                                 begin
782                                         select @partition_number = 1
783                                 end
784                                 else
785                                 begin
786                                         select @partition_number = 
787                                                 @configvalue
788                                 end
789
790                                 if (@partition_number <= 0) OR
791                                         (@partition_number > 64)
792                                 begin
793                                         raiserror 18611
794                                         return(1)
795                                 end
796
797                                 select @cache_part_temp = 2
798                                 while @cache_part_temp < @partition_number
799                                         select @cache_part_temp =
800                                                 @cache_part_temp * 2
801                                 if @partition_number != 1 AND
802                                         @cache_part_temp != @partition_number
803                                 begin
804                                         raiserror 18611
805                                         return(1)
806                                 end
807                         end
808                 end
809                                         
810                 /* call config_admin() to set the new value */
811                 select @status = config_admin(@cmd, @confignum,
812                                 @configvalue, @value, NULL, @configvalue2)
813
814                 /* if successful */
815                 if (@status = 1)
816                 begin
817                         /* Display the new value */
818                         select "Parameter Name" = convert(char(30), name),
819                           "Default" = convert(char(11), space(11-char_length(
820                                 convert(varchar(11), defvalue))) +
821                                 convert(varchar(11), defvalue)),
822                           "Memory Used" = convert(char(11),space(11-char_length(
823                                 convert(varchar(11), c.comment))) +
824                                 convert(varchar(11), c.comment)),
825                           "Config Value" = convert(char(11),
826                                 space(11-char_length(
827                                 isnull(b.value2, convert(char(32), b.value)))) +
828                                 isnull(b.value2, convert(char(32), b.value))),
829                           "Run Value"  = convert(char(11), space(11-char_length(
830                                 isnull(c.value2, convert(char(32), c.value)))) +
831                                 isnull(c.value2, convert(char(32), c.value)))
832                         from master.dbo.sysconfigures b,
833                                 master.dbo.syscurconfigs c
834                         where 
835                                 b.config = @confignum and
836                                 b.config *= c.config
837
838                         /*
839                         ** print reboot message if this option is not
840                         ** dynamic.
841                         */
842                         select @sysstatus = @sysstatus & 8
843                         if @sysstatus = 8
844                         begin
845                                 exec sp_getmessage 17419, @msg output
846                                 print @msg
847                         end
848                         else
849                         begin
850                                 exec sp_getmessage 18123, @msg output
851                                 print @msg
852                         end
853
854                         return(0)
855                 end
856                 else
857                         return(1)
858         end
859 end
860
861 /*
862 ** @configcount=0 implies @configname is not valid
863 ** @configname=NULL implies displaying all the parameters except for
864 ** the parameters with the config number equal to 19 or the parent equal
865 ** to 19 since those parameters are displayed by sp_cacheconfig.
866 */
867 if (@configcount =0)
868 begin
869         /* 18124, "No matching configuration options.  
870         ** Here is a listing of groups:" 
871         */
872         raiserror 18124
873         select convert(char(50), name)
874                 from master.dbo.sysconfigures
875                 where config < 100
876                 and parent != 19
877                 and config != 19
878                 order by name
879         return(1)
880 end
881 else if (@configname is NULL)
882         select @configname = "Config"
883
884 /*
885 ** retrieve the display level from sysattributes
886 */
887 select @user_displaylevel = int_value from master.dbo.sysattributes where
888         class = 4 AND
889         attribute = 0 AND
890         object_type = 'L' AND
891         object = suser_id()
892
893 /*
894 ** set the default display level to 10 if it is not defined in sysattributes
895 */
896 if (@user_displaylevel = NULL)
897         select @user_displaylevel = 10
898
899 /*
900 **      If @use_wildcard = 0 and the default sortorder is case-insensitive
901 ** dictionary sort order, use exact match: name = @configname to get row,
902 ** otherwise use wildcard match: name like "%" + @configname + "%".
903 */
904  
905 if (@use_wildcard = 0 and @nocase = 1)
906 begin
907         select  @confignum = config,
908                 @parent = config,
909                 @sysname = name,
910                 @sysstatus = status
911         from master.dbo.sysconfigures
912                 where name = @configname
913                 and config != 19
914 end
915 else
916 begin
917         select  @confignum = config, 
918                 @parent = config,
919                 @sysname = name,
920                 @sysstatus = status
921         from master.dbo.sysconfigures
922                 where name like "%" + @configname + "%"
923                 and config != 19
924 end
925
926 select  @children = count(*)
927         from master.dbo.sysconfigures
928         where parent =  @confignum
929
930 if @children = 0
931 begin
932         /* @@nestlevel is problem area if a sproc calls sp_configure */
933         /* could pass in another param when recursing */
934         if @@nestlevel > 1
935         begin
936                 /* reached a leaf, notify parent */
937                 return(1)
938         end
939         else
940         begin
941                 /* display the information of the config parameter */
942                 select "Parameter Name" = convert(char(30), name),
943                         "Default" = convert(char(11), space(11-char_length(
944                           convert(varchar(11), defvalue))) +
945                           convert(varchar(11), defvalue)),
946                         "Memory Used" = convert(char(11), space(11-char_length(
947                           convert(varchar(11), c.comment))) +
948                           convert(varchar(11), c.comment)),
949                         "Config Value" = convert(char(11), space(11-char_length(
950                           isnull(b.value2, convert(char(32), b.value)))) +
951                           isnull(b.value2, convert(char(32), b.value))),
952                         "Run Value"  = convert(char(11), space(11-char_length(
953                           isnull(c.value2, convert(char(32), c.value)))) +
954                           isnull(c.value2, convert(char(32), c.value)))
955                 from master.dbo.sysconfigures b,
956                         master.dbo.syscurconfigs c
957                 where 
958                         b.config *= c.config
959                         and name like "%" + @configname + "%"
960                         and b.config != 19
961                         and parent != 19
962                         
963         end
964 end
965 else
966 begin
967         select @msg = "Group: " + @sysname
968         print ""
969         print @msg
970         print ""
971
972         /* this poor guy has kids, so recurse to leaves */
973         declare config_curs cursor for 
974                 select config, name, parent 
975                 from master.dbo.sysconfigures 
976                 where parent = @parent
977                 order by name
978
979         open config_curs
980
981         fetch config_curs into @sysconfig, @sysname, @sysparent
982
983         while (@@sqlstatus = 0)
984         begin
985
986                 execute @status = sp_configure @sysname
987
988
989                 if (@status = 1)
990                 begin
991                         /*
992                         ** this guy has leaves as kids,
993                         ** so print out the leaves with
994                         ** display level <= @user_displaylevel
995                         ** Note: If a config parameter has more than one
996                         ** parent, the extra parents are stored in
997                         ** 'sysattribures'.
998                         */
999                         create table #configure_temp (config int)
1000
1001                         insert into #configure_temp
1002                                 select a.config
1003                                         from master.dbo.sysconfigures a,
1004                                         master.dbo.syscurconfigs b
1005                                         where
1006                                            display_level <= @user_displaylevel
1007                                            and parent = @parent
1008                                            and a.config != 19
1009                                            and a.config = b.config
1010                                     union
1011                                     select config
1012                                         from master.dbo.syscurconfigs,
1013                                         master.dbo.sysattributes
1014                                         where
1015                                            display_level <= @user_displaylevel
1016                                            and class = 4
1017                                            and attribute = 1
1018                                            and object_type = 'CP'
1019                                            and int_value = @parent
1020                                            and object = config
1021                                            and config != 19
1022
1023                         if exists (select * from #configure_temp)
1024                         begin
1025                          select "Parameter Name" = convert(char(30), name),
1026                          "Default" = convert(char(11), space(11-char_length(
1027                                 convert(varchar(11), defvalue))) +
1028                                 convert(varchar(11), defvalue)),
1029                          "Memory Used" = convert(char(11), space(11-char_length(
1030                                 convert(varchar(11), c.comment))) +
1031                                 convert(varchar(11), c.comment)),
1032                          "Config Value" = convert(char(11),space(11-char_length(
1033                                 isnull(b.value2, convert(char(32), b.value)))) +
1034                                 isnull(b.value2, convert(char(32), b.value))),
1035                          "Run Value"  = convert(char(11), space(11-char_length(
1036                                 isnull(c.value2, convert(char(32), c.value)))) +
1037                                 isnull(c.value2, convert(char(32), c.value)))
1038                          from master.dbo.sysconfigures b,
1039                                 master.dbo.syscurconfigs c
1040                          where b.config in
1041                                 (select config from #configure_temp)
1042                                 and b.config = c.config
1043                          order by name
1044                         end
1045
1046                         drop table #configure_temp
1047
1048                         close config_curs
1049                         deallocate cursor config_curs
1050
1051                         return(0)
1052                 end
1053                 else
1054                 begin
1055                         /*
1056                         ** this lucky guy has grandkids, so, continue
1057                         */
1058                         fetch config_curs into 
1059                                 @sysconfig, @sysname, @sysparent
1060
1061                 end
1062         end
1063
1064         close config_curs
1065         deallocate cursor config_curs
1066
1067         return(0)
1068 end
1069 go
1070
1071 IF OBJECT_ID('dbo.sp_configure') IS NOT NULL
1072 BEGIN
1073     GRANT EXECUTE ON dbo.sp_configure TO public
1074 END
1075 go
1076 /* Procedure sp_dboption, owner dbo */
1077
1078 setuser 'dbo'
1079 go
1080
1081 /* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */
1082 /*      4.8     1.1     06/14/90        sproc/src/a_values */
1083
1084 /*
1085 ** Messages for "sp_dboption"           17420
1086 **      Use "langid" when looking at spt_values ???
1087 **
1088 ** 17260, "Can't run %1! from within a transaction." 
1089 ** 17420, "Settable database options."
1090 ** 17421, "No such database -- run sp_helpdb to list databases."
1091 ** 17422, "The 'master' database's options can not be changed."
1092 ** 17423, "Usage: sp_dboption [dbname, optname, {true | false}]"
1093 ** 17424, "Database option doesn't exist or can't be set by user."
1094 ** 17425, "Run sp_dboption with no parameters to see options."
1095 ** 17426, "Database option is not unique."
1096 ** 17428, "You must be in the 'master' database in order to change
1097 **      database options."                                
1098 ** 17429, "The database is currently in use -- 'read only' option
1099 **      disallowed."                                       
1100 ** 17430, "Run the CHECKPOINT command in the database that was changed."
1101 ** 17431, "true"
1102 ** 17432, "false"
1103 ** 17433, "Database option '%1!' turned ON for database '%2!'."
1104 ** 17434, "Database option '%1!' turned OFF for database '%2!'."
1105 ** 17289, "Set your curwrite to the hurdle of current database."
1106 ** 17436, "The 'single user' option is not valid for the 'tempdb'
1107 **      database."
1108 ** 17439, "You cannot turn on ''%1!' for '%2!' because it is an HA server 
1109 **      that has been configured with the proxy_db option."
1110 */
1111
1112 create procedure sp_dboption
1113 @dbname varchar(30) = NULL,             /* database name to change */
1114 @optname varchar(20) = NULL,            /* option name to turn on/off */
1115 @optvalue varchar(10) = NULL            /* true or false */
1116 as
1117
1118 declare @dbid int                       /* dbid of the database */
1119 declare @dbuid int                      /* id of the owner of the database */
1120 declare @statvalue smallint,            /* number of option */
1121         @stattype char(2),              /* status field flag */
1122         @statopt smallint,              /* option mask, part 1 */
1123         @stat2opt smallint              /* option mask, part 2 */
1124 declare @optcount int                   /* number of options like @optname */
1125 declare @success_msg varchar(255)       /* success status message */
1126 declare @msg varchar(250)
1127 declare @sptlang        int
1128 declare @true           varchar(10)
1129 declare @false          varchar(10)
1130 declare @whichone       int             /* which language? */
1131 declare @name           varchar(30)
1132 declare @optmsgnum      int             /* identify one msgnum to compare */
1133 declare @msgcnt         int             /* count distinct dups */
1134
1135
1136
1137 if @@trancount = 0
1138 begin
1139         set chained off
1140 end
1141
1142 set transaction isolation level 1
1143
1144 select @sptlang = @@langid,  @whichone = 0
1145
1146 if @@langid != 0
1147 begin
1148         if not exists (
1149                 select * from master.dbo.sysmessages where error
1150                 between 17050 and 17069
1151                 and langid = @@langid)
1152         select @sptlang = 0
1153 end
1154
1155
1156 /*
1157 **  If no @dbname given, just list the possible dboptions.
1158 **  Only certain status bits may be set or cleared.  
1159 **         settable                         not settable
1160 **      ------------------------------  --------------------------
1161 **      allow select into/bulkcopy (4)  don't recover (32)
1162 **      read only (1024)                not recovered (256)
1163 **      dbo use only (2048)             dbname has changed (16384)
1164 **      single user (4096)
1165 **      truncate log on checkpoint (8)
1166 **      no checkpoint on recovery (16)
1167 **      allow null (8192)
1168 **      ddl in tran (512)
1169 **      ALL SETTABLE OPTIONS (15900)
1170 **      abort xact on log full (1, type='D2')
1171 **      no space accounting (2, type='D2')
1172 **      auto identity(4, type='D2')
1173 **      identity in nonunique index(8, type='D2')
1174 **      auto identity unique index(64, type='D2')
1175 */
1176
1177 /*
1178 ** Look for the "settable options" mask in spt_values
1179 */
1180 select @statopt = number
1181 from master.dbo.spt_values
1182 where type = "D"
1183   and name = "ALL SETTABLE OPTIONS"
1184
1185 select @stat2opt = number
1186 from master.dbo.spt_values
1187 where type = "D2"
1188   and name = "ALL SETTABLE OPTIONS"
1189
1190 /*
1191 ** If we can't find the option masks, guess at them
1192 */
1193 if @statopt is null
1194         select @statopt  = 4 | 8 | 16 | 512 | 1024 | 2048 | 4096 | 8192
1195 if @stat2opt is null
1196         select @stat2opt = 1 | 2 | 4 | 8 | 64
1197
1198 if @dbname is null
1199 begin
1200         /*
1201         ** 17420, "Settable database options."
1202         */
1203         exec sp_getmessage 17420, @msg output
1204         print @msg
1205
1206         if @sptlang = 0
1207             select database_options = name
1208                 from master.dbo.spt_values
1209                 where ((type = "D"
1210                         and number & @statopt = number
1211                         and number & @statopt != @statopt)
1212                     or (type = "D2"
1213                         and number & @stat2opt = number
1214                         and number & @stat2opt != @stat2opt))
1215                 order by name
1216         else
1217             select database_options = name, convert(char(22), description)
1218                 from master.dbo.spt_values, master.dbo.sysmessages
1219                 where ((type = "D"
1220                         and number & @statopt = number
1221                         and number & @statopt != @statopt)
1222                     or (type = "D2"
1223                         and number & @stat2opt = number
1224                         and number & @stat2opt != @stat2opt))
1225                   and msgnum = error
1226                   and langid = @sptlang
1227                 order by name
1228         return (0)
1229 end
1230
1231 /*
1232 **  Verify the database name and get the @dbid and @dbuid
1233 */
1234 select @dbid = dbid, @dbuid = suid
1235         from master.dbo.sysdatabases
1236                 where name = @dbname
1237
1238 /*
1239 **  If @dbname not found, say so and list the databases.
1240 */
1241 if @dbid is NULL
1242 begin
1243         /*
1244         ** 17421, "No such database -- run sp_helpdb to list databases."
1245         */
1246         raiserror 17421
1247         return (1)
1248 end
1249
1250 /*
1251 **  Only the Database Owner (DBO) or
1252 **  Accounts with SA role can execute it.
1253 **  Call proc_role() with the required SA role.
1254 */
1255 if ((suser_id() != @dbuid) and (proc_role("sa_role") < 1))
1256         return(1)
1257
1258 /*
1259 **  You can not change any of the options in master.  If the user tries to
1260 **  do so tell them they can't.
1261 */
1262 if @dbid = 1
1263 begin
1264         /*
1265         ** 17422, "The 'master' database's options can not be changed."
1266         */
1267         raiserror 17422
1268         return (1)
1269 end
1270
1271 /*
1272 **  Check remaining parameters.
1273 */
1274 /* 17431, "true" */
1275 exec sp_getmessage 17431, @true out
1276 /* 17432, "false" */
1277 exec sp_getmessage 17432, @false out
1278 if @optname is NULL or lower(@optvalue) not in 
1279         ("true", "false", @true, @false) or @optvalue is null
1280 begin
1281         /*
1282         ** 17423, "Usage: sp_dboption [dbname, optname, {true | false}]"
1283         */
1284         raiserror 17423
1285         return (1)
1286 end
1287
1288 /*
1289 **  Use @optname and try to find the right option.
1290 **  If there isn't just one, print appropriate diagnostics and return.
1291 */
1292 select @optcount = count(*)
1293            from master.dbo.spt_values
1294                 where name like "%" + @optname + "%"
1295                   and ((type = "D"
1296                         and number & @statopt = number)
1297                     or (type = "D2"
1298                         and number & @stat2opt = number))
1299 /*
1300 **  If more than one option like @optname, make sure they are not the same
1301 **  option ("trunc" and "trunc.", for example)
1302 */
1303 if @optcount > 1
1304 begin
1305         select  @optmsgnum = msgnum
1306            from master.dbo.spt_values
1307                 where name like "%" + @optname + "%"
1308                   and ((type = "D"
1309                         and number & @statopt = number)
1310                     or (type = "D2"
1311                         and number & @stat2opt = number))
1312
1313         select @msgcnt = count(msgnum)
1314            from master.dbo.spt_values
1315                 where name like "%" + @optname + "%"
1316                   and ((type = "D"
1317                         and number & @statopt = number)
1318                     or (type = "D2"
1319                         and number & @stat2opt = number))
1320                   and msgnum != @optmsgnum
1321
1322         /* 
1323         ** msgcnt of 0 indicates we really have just 1 unique dboption, 
1324         ** probably due to alternate spelling.
1325         */
1326         if (@msgcnt = 0) 
1327                 select @optcount = 1
1328 end
1329 /*
1330 ** If no option, and alternate language is set, use other language
1331 */
1332 if @optcount = 0 and @sptlang != 0
1333 begin
1334     select @optcount = count(*)
1335            from master.dbo.spt_values, master.dbo.sysmessages
1336                 where description like "%" + @optname + "%"
1337                   and ((type = "D"
1338                         and number & @statopt = number)
1339                     or (type = "D2"
1340                         and number & @stat2opt = number))
1341                   and msgnum = error
1342                   and langid = @sptlang
1343     select @whichone = 1
1344     /*
1345     **  If more than one option like @optname, make sure they are not the same
1346     **  option ("trunc" and "trunc.", for example)
1347     */
1348     if @optcount > 1
1349     begin
1350         select  @optmsgnum = msgnum
1351            from master.dbo.spt_values, master.dbo.sysmessages
1352                 where description like "%" + @optname + "%"
1353                   and ((type = "D"
1354                         and number & @statopt = number)
1355                     or (type = "D2"
1356                         and number & @stat2opt = number))
1357                   and msgnum = error
1358                   and langid = @sptlang
1359
1360         select @msgcnt = count(msgnum)
1361            from master.dbo.spt_values, master.dbo.sysmessages
1362                 where description like "%" + @optname + "%"
1363                   and ((type = "D"
1364                         and number & @statopt = number)
1365                     or (type = "D2"
1366                         and number & @stat2opt = number))
1367                   and msgnum = error
1368                   and langid = @sptlang
1369                   and msgnum != @optmsgnum
1370
1371         /* 
1372         ** msgcnt of 0 indicates we really have just 1 unique dboption, 
1373         ** probably due to alternate spelling.
1374         */
1375         if (@msgcnt = 0) 
1376                 select @optcount = 1
1377     end
1378 end
1379
1380 /*
1381 **  If no option, show the user what the options are.
1382 */
1383 if @optcount = 0
1384 begin
1385         /*
1386         ** 17424, "Database option doesn't exist or can't be set by user."
1387         */
1388         raiserror 17424
1389
1390         /*
1391         ** 17425, "Run sp_dboption with no parameters to see options."
1392         */
1393         exec sp_getmessage 17425, @msg output
1394         print @msg
1395         return (1)
1396 end
1397
1398 /*
1399 **  If more than one option like @optname, show the duplicates and return.
1400 */
1401 if @optcount > 1
1402 begin
1403         /*
1404         ** 17426, "Database option is not unique."
1405         */
1406         raiserror 17426
1407
1408         if @sptlang = 0
1409             select duplicate_options = name
1410                 from master.dbo.spt_values
1411                         where name like "%" + @optname + "%"
1412                           and ((type = "D"
1413                                 and number & @statopt = number)
1414                             or (type = "D2"
1415                                 and number & @stat2opt = number))
1416         else
1417             select duplicate_options = name, convert(char(22), description)
1418                 from master.dbo.spt_values, master.dbo.sysmessages
1419                         where (name like "%" + @optname + "%"
1420                                 or description like "%" + @optname + "%")
1421                           and ((type = "D"
1422                                 and number & @statopt = number)
1423                             or (type = "D2"
1424                                 and number & @stat2opt = number))
1425                           and msgnum = error
1426                           and langid = @sptlang
1427
1428         return (1)
1429 end
1430
1431
1432 if db_name() != "master"
1433 begin
1434         /*
1435         ** 17428, "You must be in the 'master' database in order to change database options."                                
1436         */
1437         raiserror 17428
1438         return (1)
1439 end
1440
1441 /*
1442 ** User cannot set "tempdb" database in single user mode.
1443 */
1444 select @statvalue = number
1445    from master.dbo.spt_values
1446         where name like "%" + @optname + "%"
1447           and ((type = "D"
1448                 and number & @statopt = number)
1449             or (type = "D2"
1450                 and number & @stat2opt = number))
1451  
1452 if (@dbid = 2) and (@statvalue = 4096)
1453 begin
1454         /*
1455         ** 17436, "The 'single user' option is not valid for the 'tempdb'
1456         **         database."
1457         */
1458         raiserror 17436
1459         return (1)
1460 end
1461
1462 /*
1463 **  If we're in a transaction, disallow this since it might make recovery
1464 **  impossible.
1465 */
1466 if @@trancount > 0
1467 begin
1468         /*
1469         ** 17260, "Can't run %1! from within a transaction." 
1470         */
1471         raiserror 17260, "sp_dboption"
1472         return (1)
1473 end
1474 else
1475 begin
1476         set chained off
1477 end
1478
1479 set transaction isolation level 1
1480
1481 /*
1482 **  Get the number which is the bit value to set
1483 */
1484 if @whichone = 0
1485     select @statvalue = number, @stattype = type, @success_msg = name
1486            from master.dbo.spt_values
1487                 where name like "%" + @optname + "%"
1488                   and ((type = "D"
1489                         and number & @statopt = number)
1490                     or (type = "D2"
1491                         and number & @stat2opt = number))
1492 else
1493     select @statvalue = number, @stattype = type, @success_msg = name
1494            from master.dbo.spt_values, master.dbo.sysmessages
1495                 where description like "%" + @optname + "%"
1496                         and ((type = "D"
1497                               and number & @statopt = number)
1498                           or (type = "D2"
1499                               and number & @stat2opt = number))
1500                         and msgnum = error
1501                         and langid = @sptlang
1502
1503 /*
1504 ** We do not allow 'sybsecurity' to be set to 'single user' since,
1505 ** if auditing is enabled and we try to set 'sybsecurity' database to
1506 ** 'single user' then, the audit process is killed because audit process
1507 ** tries to do 'usedb' and it fails (look at utils/auditing.c).
1508 */
1509 if (@dbname = "sybsecurity") and (@statvalue = 4096)
1510 begin
1511         /*
1512         ** 17435, "The 'single user' option is not valid for the
1513         ** 'sybsecurity' database.
1514         */
1515         raiserror 17435
1516         return (1)
1517 end
1518  
1519
1520 /*
1521 **  Now update sysdatabases.
1522
1523 */
1524 if lower(@optvalue) in ("true", @true)
1525 begin
1526         /*
1527         **  If this the option to make the database read only,
1528         **  we need to do some checking first.
1529         **  Unless it's the master db, no one can be using it.
1530         **  If it's the master db, only the SA may be using it.
1531         */
1532         if (@statvalue = 1024) and (select count(*)
1533                                         from master.dbo.sysprocesses
1534                                                 where dbid = @dbid) > 0
1535         begin
1536                 /*
1537                 ** 17429, "The database is currently in use -- 'read only' option disallowed."                                       
1538                 */
1539                 raiserror 17429
1540                 return (1)
1541         end
1542
1543         /* 
1544         ** If this is the option to set 'abort tran on log full' to 
1545         ** true for sybsecurit database, then don't allow.
1546         */
1547         if (db_name(@dbid) = "sybsecurity" 
1548                 and @stattype = "D2" and @statvalue = 1)
1549         begin
1550                 /*
1551                 ** AUDIT_CHANGE: New error message needs to be reserved and
1552                 ** the print statement needs to be removed.
1553                 */
1554                 print "You cannot set 'abort tran on log full' to true for sybsecurity database."
1555                 return (1)
1556         end
1557
1558         /*
1559         ** Disallow DDL IN TRAN 
1560         **      if proxydb option is set (@@crthaproxy = 1)
1561         **      if this server is a HA server (@@cmpstate >= 0)
1562         */
1563         if ((@statvalue = 512) and (@@crthaproxy = 1) and (@@cmpstate >= 0))
1564         begin
1565                 /*
1566                 ** Cannot set DDL_IN_TRAN option for HA servers
1567                 ** configured with proxy_db option.
1568                 */
1569                 select @name = db_name(@dbid)
1570                 exec sp_getmessage 17439, @msg output
1571                 print @msg, @success_msg, @name
1572                 return (1)
1573         end
1574
1575         if (@stattype = "D")
1576                 update master.dbo.sysdatabases
1577                         set status = status | @statvalue
1578                                 where dbid = @dbid
1579         else
1580                 update master.dbo.sysdatabases
1581                         set status2 = status2 | @statvalue
1582                                 where dbid = @dbid
1583         /*
1584         ** 17433, "Database option %1! turned ON for database %2!."
1585         */
1586         exec sp_getmessage 17433, @msg output
1587         select @name = db_name(@dbid)
1588         print @msg, @success_msg, @name
1589 end
1590
1591 /*
1592 **  We want to turn it off.
1593 */
1594 else
1595 begin
1596
1597         if (@stattype = "D")
1598                 update master.dbo.sysdatabases
1599                         set status = status & ~@statvalue
1600                                 where dbid = @dbid
1601         else
1602                 update master.dbo.sysdatabases
1603                         set status2 = status2 & ~@statvalue
1604                                 where dbid = @dbid
1605         /*
1606         ** 17434, "Database option %1! turned OFF for database %2!."
1607         */
1608         exec sp_getmessage 17434, @msg output
1609         select @name = db_name(@dbid)
1610         print @msg, @success_msg, @name
1611 end
1612
1613 /*
1614 **  Advise the user to run the CHECKPOINT command in the database that
1615 **  was changed.
1616 */
1617 /*
1618 ** 17430, "Run the CHECKPOINT command in the database that was changed."
1619 */
1620 exec sp_getmessage 17430, @msg output
1621 print @msg
1622
1623 return (0)
1624
1625 go
1626
1627 IF OBJECT_ID('dbo.sp_dboption') IS NOT NULL
1628 BEGIN
1629     GRANT EXECUTE ON dbo.sp_dboption TO public
1630 END
1631 go
1632 /* Procedure sp_dbupgrade, owner dbo */
1633
1634 setuser 'dbo'
1635 go
1636
1637 /* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */
1638
1639
1640 create procedure sp_dbupgrade
1641 as
1642
1643 if @@trancount = 0
1644 begin
1645         set chained off
1646 end
1647
1648 set transaction isolation level 1
1649
1650 /* 
1651 **      Do the sysindexes column names update which is part of the 38 upgrade.
1652 **      The sysgams updates have already been taken care of by pg_gamalloc.
1653 */
1654 print "Upgrading Sysindexes columns in the database"
1655 if not exists (select name from syscolumns where id = 2 and name = 'doampg')
1656 begin
1657         update syscolumns
1658         set name = 'doampg'
1659         where id = 2 and name = 'dpages'
1660         update syscolumns
1661         set name = 'ioampg'
1662         where id = 2 and name = 'reserved'
1663         update syscolumns
1664         set name = 'spare1'
1665         where id = 2 and name = 'used'
1666         update syscolumns
1667         set name = 'spare2'
1668         where id = 2 and name = 'rows'
1669 end
1670
1671 /*      The following updates are part of the 42 upgrade. */
1672
1673 /*
1674 **      Tweak the Syscolumns entries for the Sysindexes table so that 
1675 **      "soid" and "csid" replace half of "usagecnt".
1676 */
1677 if not exists (select * from syscolumns where id = 2  and  name = 'soid') 
1678 begin
1679         begin transaction 
1680         update syscolumns
1681                 set type = 52, length = 2, usertype = 6, offset = 42
1682                 where id = 2  and  colid = 13
1683         insert into syscolumns
1684                 (id, number, colid, status, type, length, offset,
1685                  usertype, cdefault, domain, name, printfmt)
1686                 values (2, 0, 23, 0, 48, 1, 40, 5, 0, 0, 'soid', '')
1687         insert into syscolumns
1688                 (id, number, colid, status, type, length, offset,
1689                  usertype, cdefault, domain, name, printfmt)
1690                 values (2, 0, 24, 0, 48, 1, 41, 5, 0, 0, 'csid', '')
1691         commit transaction
1692 end
1693
1694 print "Adding new datatypes to the database."
1695 if exists (select * from systypes where name = 'text')
1696  begin 
1697         delete systypes where name = 'text'
1698  end
1699 insert systypes (uid, usertype, variable, allownulls, type, length, 
1700                         tdefault, domain, name, printfmt)
1701         values (1, 19, 0, 1, 35, 16, 0, 0, 'text', null) 
1702 if exists (select * from systypes where name = 'image')
1703  begin 
1704         delete systypes where name = 'image'
1705  end
1706 insert systypes (uid, usertype, variable, allownulls, type, length, 
1707                         tdefault, domain, name, printfmt)
1708         values (1, 20, 0, 1, 34, 16, 0, 0, 'image', null) 
1709 if exists (select * from systypes where name = 'timestamp')
1710  begin 
1711         delete systypes where name = 'timestamp'
1712  end
1713 insert systypes (uid, usertype, variable, allownulls, type, length, 
1714                         tdefault, domain, name, printfmt)
1715         values (1, 80, 0, 1, 37, 8, 0, 0, 'timestamp', null) 
1716 if exists (select * from systypes where name = 'smallmoney')
1717  begin 
1718         delete systypes where name = 'smallmoney'
1719  end
1720 insert systypes (uid, usertype, variable, allownulls, type, length, 
1721                         tdefault, domain, name, printfmt)
1722         values (1, 21, 0, 1, 122, 4, 0, 0, 'smallmoney', null) 
1723 if exists (select * from systypes where name = 'smalldatetime')
1724  begin 
1725         delete systypes where name = 'smalldatetime'
1726  end
1727 insert systypes (uid, usertype, variable, allownulls, type, length, 
1728                         tdefault, domain, name, printfmt)
1729         values (1, 22, 0, 1, 58, 4, 0, 0, 'smalldatetime', null) 
1730 if exists (select * from systypes where name = 'real')
1731  begin 
1732         delete systypes where name = 'real'
1733  end
1734 insert systypes (uid, usertype, variable, allownulls, type, length, 
1735                         tdefault, domain, name, printfmt)
1736         values (1, 23, 0, 1, 59, 4, 0, 0, 'real', null) 
1737
1738 /* 4.9 user types for national character */
1739 if exists (select * from systypes where name = 'nchar')
1740  begin 
1741         delete systypes where name = 'nchar'
1742  end
1743 insert systypes (uid, usertype, variable, allownulls, type, length, 
1744                         tdefault, domain, name, printfmt )
1745         values (1, 24, 0, 1, 47, 255, 0, 0, 'nchar', null)
1746
1747 if exists (select * from systypes where name = 'nvarchar')
1748  begin 
1749         delete systypes where name = 'nvarchar'
1750  end
1751 insert systypes (uid, usertype, variable, allownulls, type, length, 
1752                         tdefault, domain, name, printfmt)
1753         values (1, 25, 1, 1, 39, 255, 0, 0, 'nvarchar', null) 
1754
1755 if exists (select * from systypes where name = 'NULL')
1756  begin 
1757         delete systypes where name = 'NULL'
1758  end
1759 insert systypes (uid, usertype, variable, allownulls, type, length, 
1760                         tdefault, domain, name, printfmt)
1761         values (1, 0, 0, 1, 0, 0, 0, 0, 'NULL', null) 
1762
1763 /* 4.9 system table creation */
1764 print "Creating system catalog: sysusermessages and its indexes."
1765 if not exists (select * from sysobjects where name='sysusermessages')
1766     begin
1767         execute sp_configure 'allow updates', 1
1768         reconfigure with override
1769
1770         dbcc traceon(3701)
1771         begin
1772             create table sysusermessages(error int,uid smallint,
1773                 description varchar(255), langid smallint null) lock allpages
1774             create clustered index csysusermessages
1775                 on sysusermessages (error)
1776             create unique nonclustered index ncsysusermessages
1777                 on sysusermessages (error, langid)
1778         end
1779         dbcc traceoff(3701)
1780
1781         execute sp_configure 'allow updates', 0
1782         reconfigure with override
1783     end
1784
1785 print "Shutting down SQL Server"
1786 shutdown
1787
1788 return (0)
1789
1790 go
1791
1792 /* Procedure sp_getmessage, owner dbo */
1793
1794 setuser 'dbo'
1795 go
1796
1797
1798 /* generic/sproc/getmessage     14.2    4/25/91 */
1799
1800 /* Messages from sysmessages
1801 ** 17200, "Message number must be greater than or equal to 17000."
1802 ** 17201, "'%1!' is not an official language name from Syslanguages."
1803 ** 17202, "Message number %1! does not exist in the %2! language."
1804 */
1805
1806 create procedure sp_getmessage
1807 @message_num int,
1808 @result varchar(255) output,
1809 @language varchar(30) = NULL
1810 as
1811
1812         declare @lang_id smallint 
1813         declare @msg varchar(255)
1814         declare @returncode smallint
1815
1816         if @@trancount = 0
1817         begin
1818                 set chained off
1819         end
1820
1821         set transaction isolation level 1
1822
1823         /* 
1824         ** Use default language if none specified, 
1825         ** and initialize result 
1826         */
1827         select @lang_id = @@langid, @result = NULL
1828
1829         /* Only retrieve external errors */
1830         if @message_num < 17000
1831         BEGIN
1832                 /* 17200   "Message number must be greater than or equal to 17000." */
1833                  select @msg = description from master.dbo.sysmessages
1834                                     where error = 17200
1835                  print @msg
1836                  return (1)
1837         END
1838
1839         /*
1840         ** Check that language is valid.
1841         */
1842         if @language is not NULL
1843         BEGIN
1844             execute @returncode = sp_validlang @language
1845             if @returncode != 0
1846             begin
1847                     /* Us_english is always valid */
1848                     if @language != "us_english"
1849                     BEGIN
1850                             /* 
1851                             ** 17201, "'%1!' is not an official language
1852                             **  name from Syslanguages."
1853                             */
1854                             select @msg = description from master.dbo.sysmessages
1855                                                 where error = 17201
1856                                                 and langid = @@langid
1857
1858                             /* Get english if the current language is missing */
1859                             if @msg is null
1860                                 select @msg = description from master.dbo.sysmessages
1861                                                 where error = 17201
1862                                                 and langid is NULL
1863
1864                             print @msg, @language
1865                             return @returncode
1866                     END
1867
1868                     /* set to us_english */
1869                     select @lang_id = NULL
1870             end
1871
1872             else
1873             select @lang_id = langid from master.dbo.syslanguages
1874                         where @language = name
1875
1876         END
1877
1878         /* The langid is assigned 0 since it gets its value from @@langid. */
1879         /* For us_english, we have to insert it as NULL and not 0, this is */
1880         /* to maintain compatibility with the current conventions          */
1881         if @lang_id = 0 
1882         begin 
1883                 select @lang_id = NULL        
1884         end   
1885
1886         /* Get message from the proper place */
1887         /* System messages */
1888         if @message_num < 20000
1889         BEGIN
1890                 select @result = description from master.dbo.sysmessages
1891                            where  langid = @lang_id 
1892                              and   error = @message_num 
1893
1894                 /* Get english if the current language is missing */
1895                 if @result is null
1896                         select @result = description from master.dbo.sysmessages
1897                                         where error = @message_num
1898                                         and (langid is NULL or langid =0)
1899         END
1900         else
1901         /* User messages */
1902         BEGIN
1903                 /* There is no proper alternate language for user messages */
1904                 select @result = description from sysusermessages
1905                            where  langid = @lang_id 
1906                              and   error = @message_num 
1907
1908                 /* this is in here for compatibility with older revs which */
1909                 /* by mistake used to add langid as 0 in sysusermessages   */
1910                 if @result is null and @lang_id is NULL
1911                         select @result = description from sysusermessages
1912                                         where (langid = 0 or langid is NULL)
1913                                         and error = @message_num
1914
1915         END
1916
1917         /* Warn the user if the message can't be found */
1918         if @result is null
1919         begin
1920                 /* 17202, "Message number %1! does not exist in the %2! language." */
1921                 select @msg = description from master.dbo.sysmessages
1922                         where error = 17202
1923                         and langid = @@langid
1924
1925                 if @language is null
1926                         select @language = @@language
1927
1928                 if @msg is null
1929                         select @msg = "Message number %1! does not exist in the %2! language."
1930
1931                 print @msg, @message_num, @language
1932
1933                 return (1)
1934         end
1935
1936 return (0)
1937
1938 go
1939
1940 IF OBJECT_ID('dbo.sp_getmessage') IS NOT NULL
1941 BEGIN
1942     GRANT EXECUTE ON dbo.sp_getmessage TO public
1943 END
1944 go
1945 /* Procedure sp_loaddbupgrade, owner dbo */
1946
1947 setuser 'dbo'
1948 go
1949
1950 /* Sccsid = "%Z% generic/sproc/src/%M% %I% %G%" */
1951
1952 create procedure sp_loaddbupgrade 
1953 @databasename varchar(30), 
1954 @devname varchar(30)
1955 as
1956
1957
1958 if @@trancount = 0
1959 begin
1960         set chained off
1961 end
1962
1963 set transaction isolation level 1
1964
1965 dbcc traceon(3402)
1966
1967 /* The recovery which is part of load database will perform the upgrade */
1968 load database @databasename from @devname
1969
1970 dbcc traceoff(3402)
1971
1972 return (0)
1973
1974 go
1975
1976 /* Procedure sp_procxmode, owner dbo */
1977
1978 setuser 'dbo'
1979 go
1980
1981 /* Sccsid = "%Z% generic/sproc/src/%M% %I% %G%" */
1982
1983 /*
1984 ** 17756, "The execution of the stored procedure '%1!' in database
1985 **         '%2!' was aborted because there was an error in writing the
1986 **         replication log record."
1987 */
1988
1989 create procedure sp_procxmode
1990 @procname varchar(255) = null,
1991 @tranmode varchar(30) = null
1992 as
1993
1994 declare @uid smallint
1995 declare @oid int
1996 declare @msg varchar(250)       /* message text */
1997 declare @dbname varchar(30)
1998
1999 if @@trancount = 0
2000 begin
2001         set chained off
2002 end
2003
2004 set transaction isolation level 1
2005
2006 /* If either parameter is null we will be joining with a temporary table
2007 ** to convert transaction mode numbers (0, 1, 2) to strings ("Unchained",
2008 ** "Chained", "Any Mode").
2009 */
2010 if ((@procname is null) or (@tranmode is null))
2011 begin
2012         create table #tranmode (intval integer, charval varchar(15))
2013         insert into #tranmode values(0, "Unchained")
2014         insert into #tranmode values(1, "Chained")
2015         insert into #tranmode values(2, "Any Mode")
2016 end
2017
2018 /* If the first parameter is null, we're to report the transaction-modes
2019 ** of every stored procedure in the current database.
2020 */
2021 if (@procname is null)
2022 begin
2023         select "procedure name" = o.name, "user name" = user_name(o.uid),
2024                 "transaction mode" = t.charval 
2025         from sysobjects o, #tranmode t
2026         where ((o.type = "P") or (o.type = "XP")) and (t.intval = ((o.sysstat2 / 16) & 3))
2027         order by o.name
2028
2029         return(0)
2030 end
2031
2032 /* If only the second parameter is null, we're to report the
2033 ** transaction-mode of the specified stored procedure.
2034 */
2035 if ((@procname is not  null) and (@tranmode is null))
2036 begin
2037         if (not exists (select name from sysobjects 
2038                                     where ((type = "P") or (type = "XP")) and
2039                                           (name = @procname)))
2040         begin
2041                 /*
2042                 ** Force an error message, since we haven't
2043                 ** installed sp_getmessage yet.
2044                 */
2045                 dbcc update_tmode(@procname, "Chained")
2046                 return (1)
2047         end
2048         select "procedure name" = o.name, "user name" = user_name(o.uid),
2049                 "transaction mode" = t.charval
2050         from sysobjects o, #tranmode t
2051         where ((o.type = "P") or (o.type = "XP")) and (@procname = o.name) and
2052                 (t.intval = ((o.sysstat2 / 16) & 3))
2053
2054         return(0)
2055 end
2056
2057 /* If neither parameter is null, we're to set the transaction-mode
2058 ** of the specified procedure to the specified value.
2059 */
2060 if ((@procname is not  null) and (@tranmode is not  null))
2061 begin
2062         /* Start the transaction to log the execution of this procedure.
2063         **
2064         ** IMPORTANT: The name "rs_logexec is significant and is used by 
2065         **            Replication Server
2066         */
2067         begin transaction rs_logexec
2068
2069         /*
2070         ** Update transaction-mode in both sysobjects and DES.
2071         */
2072         dbcc update_tmode(@procname, @tranmode)
2073
2074         /* If dbcc update_tmode returned an error, return
2075         ** an error now.
2076         */
2077         if (@@error != 0)
2078         begin
2079                 rollback transaction rs_logexec
2080                 return(1)
2081         end
2082
2083         /*
2084         ** Write the log record to replicate this invocation 
2085         ** of the stored procedure.
2086         */
2087         if (logexec() != 1)
2088         begin
2089                 /*
2090                 ** 17756, "The execution of the stored procedure '%1!'
2091                 **         in database '%2!' was aborted because there
2092                 **         was an error in writing the replication log
2093                 **         record."
2094                 */
2095                 select @dbname = db_name()
2096                 raiserror 17756, "sp_procxmode", @dbname
2097                         
2098                 rollback transaction rs_logexec
2099                 return(1)
2100         end
2101                 
2102         commit transaction
2103
2104 end
2105
2106 go
2107
2108 IF OBJECT_ID('dbo.sp_procxmode') IS NOT NULL
2109 BEGIN
2110     GRANT EXECUTE ON dbo.sp_procxmode TO public
2111 END
2112 go
2113 /* Procedure sp_prtsybsysmsgs, owner dbo */
2114
2115 setuser 'dbo'
2116 go
2117
2118 /* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */
2119
2120 /* 
2121 ** This procedure is needed to extract messages for the batch that creates
2122 ** the sybsystemprocs database. When return parameters are used in an execute
2123 ** statement that is a part of a SQL batch, the return values are printed 
2124 ** with a heading before subsequent statements in the batch are executed.
2125 ** These headings could be confusing to a user that is looking at the results
2126 ** of the batch. Hence we print the message in a stored procedure
2127 */
2128
2129 create procedure sp_prtsybsysmsgs 
2130 @i int, @size int = NULL, @size2 int = NULL
2131 as
2132
2133 declare @msg varchar(250)
2134
2135 exec sp_getmessage @i, @msg out
2136 print @msg, @size, @size2
2137
2138 go
2139
2140 /* Procedure sp_validlang, owner dbo */
2141
2142 setuser 'dbo'
2143 go
2144
2145 /* Sccsid = "%Z% generic/sproc/src/%M% %I% %G%" */
2146 /*      4.8     1.1     06/14/90        sproc/src/serveroption */
2147 create procedure sp_validlang
2148 @name   varchar(30)
2149 as
2150
2151
2152 if @@trancount = 0
2153 begin
2154         set chained off
2155 end
2156
2157 set transaction isolation level 1
2158
2159 /* Check to see if this language is in Syslanguages. */
2160 if exists (select *
2161                 from master.dbo.syslanguages
2162                 where name = @name)
2163 begin
2164         return 0
2165 end
2166 return 1
2167
2168 go
2169
2170 IF OBJECT_ID('dbo.sp_validlang') IS NOT NULL
2171 BEGIN
2172     GRANT EXECUTE ON dbo.sp_validlang TO public
2173 END
2174 go
2175
2176 /* Triggers... */
2177
2178 /* No triggers found. */