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