home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / alpha / sqlole42.sql < prev    next >
Encoding:
Text File  |  1995-12-13  |  55.4 KB  |  2,864 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. /* Preprocessor directives, will be blank space in output .sql file. */
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. /* status values for these. */
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /* bitmask values for same; power(2, DRI_*). */
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. /* DRI-generated index masks, to apply to sysindexes.status */
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. /* sysobjects.category bit that indicates this is an MS-internal object. */
  113.  
  114.  
  115. /* sysobjects.category bit for an sp_ that indicates it's a startup proc, or an xp that should ImpersonateClient. */
  116.  
  117.  
  118.  
  119. /* BIT_CLUSTERED indicates the key is clustered. */
  120. /* EXCLUDE REPLICATION value in sysconstraints.status, and system-generated name. */
  121.  
  122.  
  123.  
  124.  
  125. /* sysobjects.sysstat bits (lower 4) that mask off the object type. */
  126.  
  127.  
  128. /* bit for DEFAULTS which are really DRI-created. */
  129.  
  130.  
  131. /* bits for columns - don't conflict with bit_sysgenname for DRIDefaults. */
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /* sysdatabases.category bits */
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /* From SQLOLE.H, MUST BE SYNC'd!!! */
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. /* From perm.h */
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. /* Localized error-string files (PRE95_ERRMSG_*) for PRE95 servers. */
  201. /* Set the include path to determine which lang-specific version is picked up. */
  202.  
  203. /* USA-localized strings for 4.21 localization of sqlole.cxx */
  204. /* Should match the localized string in sysmessages (see sqlole.cxx for message number). */
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. use master
  218. go
  219.  
  220. /************* DUMP THE TRANSACTION LOG **************************************/
  221. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  222. /* script periodically, you will run out of transaction log space.           */
  223. print ''
  224. print 'Dumping transaction log...'
  225. print ''
  226. go
  227. dump tran master with no_log
  228. go
  229. /************* END DUMP THE TRANSACTION LOG **********************************/
  230.  
  231. /********************* Delete existing objects *********************************/
  232. print ''
  233. print 'Deleting existing objects...'
  234. print ''
  235. go
  236.  
  237.  
  238.  
  239.  
  240.  
  241. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MShelpcolumns')
  242.     drop procedure sp_MShelpcolumns
  243. go
  244. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MShelpindex')
  245.     drop procedure sp_MShelpindex
  246. go
  247. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MShelptype')
  248.     drop procedure sp_MShelptype
  249. go
  250. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MSdependencies')
  251.     drop procedure sp_MSdependencies
  252. go
  253. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MStablespace')
  254.     drop procedure sp_MStablespace
  255. go
  256. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MSindexspace')
  257.     drop procedure sp_MSindexspace
  258. go
  259. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MSuniquename')
  260.     drop procedure sp_MSuniquename
  261. go
  262. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MSkilldb')
  263.     drop procedure sp_MSkilldb
  264. go
  265. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MSdbuserprofile')
  266.     drop procedure sp_MSdbuserprofile
  267. go
  268. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MSobjectprivs')
  269.     drop procedure sp_MSobjectprivs
  270. go
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. if exists (select * from master..sysobjects where sysstat & 0x0f = 4 and name = 'sp_MSSQLOLE_version')
  320.     drop procedure sp_MSSQLOLE_version
  321. go
  322.  
  323. /********************* Create new objects *********************************/
  324.  
  325. /*******************************************************************************/
  326. print ''
  327. print 'Creating sp_MShelpcolumns'
  328. print ''
  329. go
  330. create procedure sp_MShelpcolumns
  331. @tablename varchar(92), @flags int = 0, @orderby varchar(10) = null
  332. as
  333.     if (@tablename = '?')
  334.     begin
  335.         print ''
  336.         print 'Usage:  sp_MShelpcolumns @tablename, @flags int = 0'
  337.         print ' where @flags is a bitmask of:'
  338.         print ' 0x0200        = No DRI (ignore Checks, Primary/Foreign/Unique Keys, etc.)'
  339.         print ' 0x0400        = UDDTs --> Base type'
  340.         print ' 0x0800        = 42Syntax (convert numeric/decimal, no identity)'
  341.         print ' 0x80000        = TimestampToBinary (convert timestamp cols to binary(8))'
  342.         print ' 0x40000000    = No Identity attribute'
  343.         return 0
  344.     end
  345.  
  346.     declare @42errmsg varchar(100)
  347.     if (object_id(@tablename) is null)
  348.     begin
  349.         select @42errmsg = @tablename + ' does not exist.'
  350.         RAISERROR 4215001 @42errmsg
  351.         return 1
  352.     end
  353.  
  354.     set nocount on
  355.  
  356.     create table #sphelpcols
  357.     (
  358.         col_name        varchar (30),
  359.         col_id            tinyint,
  360.         col_type        smallint,
  361.         col_len            tinyint,
  362.         col_prec        tinyint    null,
  363.         col_scale        tinyint    null,
  364.         col_def            int        null,
  365.         col_rul            int        null,
  366.         col_null        bit,                /* status & 8 */
  367.         col_identity    bit,                /* status & 128 */
  368.         col_defname        varchar(61) null,    /* fully-qual'd default name, or NULL */
  369.         col_rulname        varchar(61) null,    /* fully-qual'd rule name, or NULL */
  370.         col_basetype    int        null,
  371.         col_flags        int        null,
  372.         col_seed        int        null,
  373.         col_increment    int     null,
  374.         col_dridefname    varchar(30) null,    /* DRI DEFAULT name */
  375.         col_drideftext    varchar(255) null    /* DRI DEFAULT text */
  376.     )
  377.  
  378.     /* First load stuff so we can blot off inappropriate info and massage as per @flags */
  379.     insert #sphelpcols
  380.     select name, colid, usertype, length, NULL, NULL, cdefault, domain,
  381.             convert(bit, status & 0x0008), convert(bit, status & 0x0080), NULL, NULL, type, 0, null, null, null, null
  382.     from syscolumns
  383.     where id = object_id(@tablename)
  384.  
  385.     /* Convert any timestamp column to binary(8) if they asked. */
  386.     if (@flags is not null and @flags & 0x80000 != 0)
  387.         update #sphelpcols set col_type = 3, col_len = 8 where col_type = 80
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.     /* Convert from the dblib-specific 'nullable' type to something people and the server recognize. */
  401.     /* datetime --> datetimn */
  402.     update #sphelpcols set col_type = 12 where col_type = 15
  403.     /* decimal --> decimaln */
  404.     update #sphelpcols set col_type = 24 where col_type = 26
  405.     /* float --> floatn */
  406.     update #sphelpcols set col_type = 8 where col_type = 14
  407.     /* int --> intn*/
  408.     update #sphelpcols set col_type = 7 where col_type = 13
  409.     /* money --> moneyn*/
  410.     update #sphelpcols set col_type = 11 where col_type = 17
  411.     /* numeric --> numericn */
  412.     update #sphelpcols set col_type = 10 where col_type = 25
  413.  
  414.     /* Set the physical base datatype to col_type if it's not a usertype, else to the actual physical basetype. */
  415.     update #sphelpcols set col_basetype = col_type where col_type < 100
  416.     update #sphelpcols set col_basetype = bt.usertype
  417.         from systypes bt
  418.         where col_type > 100 and bt.type = col_basetype and bt.usertype < 99 and bt.usertype not in (18, 80)
  419.  
  420.     /* Now do the same conversion for the physical base datatype. */
  421.     /* datetime --> datetimn */
  422.     update #sphelpcols set col_basetype = 12 where col_basetype = 15
  423.     /* decimal --> decimaln */
  424.     update #sphelpcols set col_basetype = 24 where col_basetype = 26
  425.     /* float --> floatn */
  426.     update #sphelpcols set col_basetype = 8 where col_basetype = 14
  427.     /* int --> intn*/
  428.     update #sphelpcols set col_basetype = 7 where col_basetype = 13
  429.     /* money --> moneyn*/
  430.     update #sphelpcols set col_basetype = 11 where col_basetype = 17
  431.     /* numeric --> numericn */
  432.     update #sphelpcols set col_basetype = 10 where col_basetype = 25
  433.  
  434.     /* Make a nice, presentable qualified rule/default name for those which are non-null */
  435.     update #sphelpcols set col_defname = user_name(d.uid) + '.' + d.name
  436.             from #sphelpcols c, sysobjects d where c.col_def is not null and d.id = c.col_def
  437.     update #sphelpcols set col_rulname = user_name(r.uid) + '.' + r.name
  438.             from #sphelpcols c, sysobjects r where c.col_rul is not null and r.id = c.col_rul
  439.  
  440.     /* Now see what our flags are, if anything.  Ignore the physical base type here -- these */
  441.     /* flags are just for scripting, which doesn't use that. */
  442.     if (@flags is not null and @flags != 0)
  443.     begin
  444.         if (@flags & 0x0400 != 0)
  445.         begin
  446.             /* Track from usertype --> b.<base>type --> u.usertype in systypes */
  447.             update #sphelpcols set col_type = u.usertype from #sphelpcols c, systypes b, systypes u
  448.             where c.col_type > 100 and b.usertype = c.col_type and b.type = u.type and u.usertype < 100 and u.usertype not in (18, 80)
  449.         end
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.     end
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.     /* For scripting, set the col_flags */
  498.     update #sphelpcols set col_flags = col_flags | 0x0001 where col_type in (1,2,3,4)
  499.     update #sphelpcols set col_flags = col_flags | 0x0002 where col_type in (10, 24)
  500.  
  501.     /* OK, now put out the data */
  502.     set nocount off
  503.     if (@orderby is null or @orderby = 'id')
  504.     begin
  505.         select c.col_name, c.col_id, DataType = t.name, c.col_len, c.col_prec, c.col_scale,
  506.                 BaseType = b.name, c.col_defname, c.col_rulname, c.col_null, c.col_identity,
  507.                 c.col_flags, c.col_seed, c.col_increment, c.col_dridefname, c.col_drideftext
  508.         from #sphelpcols c, systypes t, systypes b
  509.         where t.usertype = c.col_type and b.usertype = c.col_basetype
  510.         order by c.col_id
  511.     end else begin
  512.         select c.col_name, c.col_id, DataType = t.name, c.col_len, c.col_prec, c.col_scale,
  513.                 BaseType = b.name, c.col_defname, c.col_rulname, c.col_null, c.col_identity,
  514.                 c.col_flags, c.col_seed, c.col_increment, c.col_dridefname, c.col_drideftext
  515.         from #sphelpcols c, systypes t, systypes b
  516.         where t.usertype = c.col_type and b.usertype = c.col_basetype
  517.         order by c.col_name
  518.     end
  519. go
  520. /* End sp_MShelpcolumns */
  521.  
  522. /*******************************************************************************/
  523. print ''
  524. print 'Creating sp_MShelpindex'
  525. print ''
  526. go
  527. create procedure sp_MShelpindex
  528. @tablename varchar(92), @indexname varchar(30) = null
  529. as
  530.  
  531.  
  532.  
  533.     select i.name, i.status, i.indid, 0,
  534.  
  535.         index_col(@tablename, i.indid, 1), index_col(@tablename, i.indid, 2),
  536.         index_col(@tablename, i.indid, 3), index_col(@tablename, i.indid, 4),
  537.         index_col(@tablename, i.indid, 5), index_col(@tablename, i.indid, 6),
  538.         index_col(@tablename, i.indid, 7), index_col(@tablename, i.indid, 8),
  539.         index_col(@tablename, i.indid, 9), index_col(@tablename, i.indid, 10),
  540.         index_col(@tablename, i.indid, 11), index_col(@tablename, i.indid, 12),
  541.         index_col(@tablename, i.indid, 13), index_col(@tablename, i.indid, 14),
  542.         index_col(@tablename, i.indid, 15), index_col(@tablename, i.indid, 16)
  543.     from sysindexes i where id = object_id(@tablename) and i.indid > 0 and i.indid < 255
  544.     and (@indexname is null or i.name = @indexname)
  545.     order by i.name
  546. go
  547. /* End sp_MShelpindex */
  548.  
  549. /*******************************************************************************/
  550. print ''
  551. print 'Creating sp_MShelptype'
  552. print ''
  553. go
  554. create procedure sp_MShelptype
  555. @typename varchar(92) = null, @flags varchar(10) = null
  556. as
  557.     if (@typename = '?')
  558.     begin
  559.         print ''
  560.         print 'Usage:  sp_MShelptype @typeename = null, @flags varchar(10) = null'
  561.         print ' where @flags is either:'
  562.         print ' sdt        = look in system datatypes'
  563.         print ' uddt      = look in user defined datatypes'
  564.         print ' null    = look wherever its found'
  565.         print ''
  566.         return 0
  567.     end
  568.  
  569.     /* Catch typos... */
  570.     if (@flags is not null and @flags not in ('sdt', 'uddt'))
  571.         select @flags = null
  572.  
  573.     declare @42errmsg varchar(100)
  574.  
  575.     /* Find out what type we're gonna be looking in, if they gave us a name. */
  576.     if (@typename is not null)
  577.     begin
  578.         declare @usertype int
  579.         select @usertype = usertype from systypes where name = @typename
  580.         if (@usertype is not null)
  581.         begin
  582.             if (@usertype < 100)
  583.             begin
  584.                 if (@flags is null)
  585.                     select @flags = 'sdt'
  586.                 if (@flags != 'sdt')
  587.                     select @usertype = null
  588.             end else begin
  589.                 if (@flags is null)
  590.                     select @flags = 'uddt'
  591.                 if (@flags != 'uddt')
  592.                     select @usertype = null
  593.             end
  594.         end
  595.         if (@usertype is null)
  596.         begin
  597.             select @42errmsg = @typename + ' does not exist.'
  598.             RAISERROR 4215001 @42errmsg
  599.             return 1
  600.         end
  601.     end
  602.  
  603.     /* Now go get the info, depending on the type they gave us. */
  604.     if (@flags is null or @flags = 'sdt')
  605.     begin
  606.         /* Exclude the 'xxxxn' dblib-specific nullable types, and hardcode a check for variable length and numeric usertypes. */
  607.         select     SystemDatatypeName = t.name,
  608.                 ifvarlen_max = y.length,
  609.                 allownulls = t.allownulls,
  610.  
  611.  
  612.  
  613.  
  614.                 isnumeric = 0, allowidentity = 0
  615.  
  616.             from systypes t, systypes y
  617.             where t.usertype < 100 and t.usertype not in (15,26,14,13,17,25) and (@typename is null or t.name like @typename)
  618.             and y.usertype =* t.usertype and y.usertype in (1,2,3,4)
  619.             order by t.name
  620.     end
  621.  
  622.     /* Need a temp table so we can ownerqualify nonNULL rules/defaults. */
  623.     create table #sphelptype (
  624.         dt_usertype    int        null,
  625.         dt_basetype int        null,
  626.         dt_rul         int        null,
  627.         dt_def         int        null,
  628.         dt_rulname     varchar(61) null,
  629.         dt_defname    varchar(61) null,
  630.         dt_flags    int        null
  631.     )
  632.  
  633.     if (@flags is null or @flags = 'uddt')
  634.     begin
  635.         set nocount on
  636.         insert #sphelptype (dt_usertype, dt_basetype, dt_rul, dt_def, dt_flags)
  637.             select t.usertype,
  638.             (select distinct b.usertype from systypes b where b.type = t.type and b.usertype < 100 and b.usertype not in (18, 80)),
  639.             t.domain, t.tdefault, 0
  640.             from systypes t
  641.             where t.usertype > 99 and (@typename is null or t.name like @typename)
  642.  
  643.         /* Make a nice, presentable qualified rule/default name for those which are non-null */
  644.         update #sphelptype set dt_defname = user_name(d.uid) + '.' + d.name
  645.                 from #sphelptype c, sysobjects d where c.dt_def is not null and d.id = c.dt_def
  646.         update #sphelptype set dt_rulname = user_name(r.uid) + '.' + r.name
  647.                 from #sphelptype c, sysobjects r where c.dt_rul is not null and r.id = c.dt_rul
  648.  
  649.         /* For scripting, set the dt_flags -- these apply to the BASE datatype. */
  650.         update #sphelptype set dt_flags = dt_flags | 0x0001 where dt_basetype in (1,2,3,4)
  651.         update #sphelptype set dt_flags = dt_flags | 0x0002 where dt_basetype in (10, 24)
  652.  
  653.         set nocount off
  654.         select distinct UserDatatypeName = t.name,
  655.                 owner = user_name(t.uid),
  656.                 basetypename = (select distinct b.name from systypes b where b.usertype = s.dt_basetype),
  657.                 defaultname = dt_defname,
  658.                 rulename = dt_rulname,
  659.                 tid = t.usertype,
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.                 length = t.length, nullable = t.allownulls,    dt_prec = null, dt_scale = null, dt_flags, allowidentity = 0
  669.  
  670.             from systypes t, #sphelptype s
  671.             where t.usertype > 99 and (@typename is null or t.name like @typename)
  672.                 and dt_usertype = t.usertype
  673.             order by t.name
  674.     end
  675. go
  676. /* End sp_MShelptype */
  677.  
  678. /*******************************************************************************/
  679. print ''
  680. print 'Creating sp_MSdependencies'
  681. print ''
  682. go
  683.  
  684. create procedure sp_MSdependencies
  685. @objname varchar(92) = null, @objtype int = null, @flags int = 0x01fd
  686. as
  687.     if (@objname = '?')
  688.     begin
  689.         print 'sp_MSobject_dependencies name = NULL, type = NULL, flags = 0x01fd'
  690.         print '  name:  name or null (all objects of type)'
  691.         print '  type:  type number (see below) or null'
  692.         print '      if both null, get all objects in database'
  693.         print '  flags is a bitmask of the following values:'
  694.         print '      0x10000  = return multiple parent/child rows per object'
  695.         print '      0x20000  = descending return order'
  696.         print '      0x40000  = return children instead of parents'
  697.         print '      0x80000  = Include input object in output result set'
  698.         print '      0x100000 = return only firstlevel (immediate) parents/children'
  699.         print '      0x200000 = return only DRI dependencies'
  700.         print '      power(2, object type number(s))  to return in results set:'
  701.         print '          0 (1      - 0x0001)     - datatype'
  702.         print '          1 (2      - 0x0002)     - system tables or MS-internal objects'
  703.         print '          2 (4      - 0x0004)     - view'
  704.         print '          3 (8      - 0x0008)     - user table'
  705.         print '          4 (16        - 0x0010)     - procedure'
  706.         print '          5 (32        - 0x0020)     - log'
  707.         print '          6 (64     - 0x0040)     - default'
  708.         print '          7 (128    - 0x0080)     - rule'
  709.         print '          8 (256    - 0x0100)     - trigger'
  710.         print '      shortcuts:'
  711.         print '          29     (0x011c) - trig, view, user table, procedure'
  712.         print '          448    (0x00c1) - rule, default, datatype'
  713.         print '          509    (0x01fd) - all but systables/objects'
  714.         print '          511    (0x01ff) - all'
  715.         return 0
  716.     end
  717.  
  718.     /* If they want SQLOLEDep_DRIOnly, remove all but usertable objects from @flags */
  719.  
  720.  
  721.  
  722.  
  723.  
  724.     if (@objtype in (0, 5, 6, 7))
  725.     begin
  726.         /* Print only, do not raiserror as we may be calling this blindly and this is not a real error. */
  727.         print 'Rules, defaults, and datatypes do not have dependencies.'
  728.         return (0)
  729.     end
  730.  
  731.     /*
  732.      * Create #t1 and #t2 as temp object holding areas.  Columns are:
  733.      *     tid        - temp object id
  734.      *     ttype     - temp object type
  735.      *     pid        - parent or child object id
  736.      *     ptype     - parent or child object type
  737.      *     bDone     - NULL means dependencies not yet evaluated, else nonNULL.
  738.      */
  739.     declare @curid int, @curcat int, @rowsaffected int
  740.     declare @allobjs int
  741.     declare @delinputobj int
  742.     select @allobjs = 0, @delinputobj = 0, @curid = NULL, @curcat = NULL
  743.     create table #t1 (tid int NULL, ttype smallint NULL, tcat smallint NULL, pid int NULL, ptype smallint NULL, pcat smallint NULL, bDone smallint NULL)
  744.     create table #t2 (tid int NULL, ttype smallint NULL, tcat smallint NULL, pid int NULL, ptype smallint NULL, pcat smallint NULL, bDone smallint NULL)
  745.     create table #tempudt (dtype int)
  746.  
  747.     /* Worktables we'll use for optimization.  #t3 and #t4 push us over the 64 2K page limit in 4.21, */
  748.     /* but the real gain is from #temptrig anyway. */
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.     create table #temptrig(id int, deltrig int, sysstat smallint, category int)
  756.     create clustered index #ci_temptrig on #temptrig (deltrig) with allow_dup_row
  757.  
  758.     declare @42errmsg varchar(100)
  759.  
  760.     /*
  761.      * If both name and type are null, this means get every object in the
  762.      * database matching the specification they passed in.  Otherwise,
  763.      * find the passed object or all objects of the passed type.  Start off
  764.      * loading parent info (pid, tid); these will be put into child as needed.
  765.      */
  766.     if (@objname is null and @objtype is null)
  767.     begin
  768.         set nocount on
  769.         select @allobjs = 1
  770.         insert #t1 (pid, ptype, pcat) select o.id, o.sysstat & 0x0f, o.category & 0x0002 from sysobjects o
  771.             where ((power(2, o.sysstat & 0x0f) & 0x01ff) <> 0) and (o.sysstat & 0x0f not in (6, 7))
  772.     end else begin
  773.         if (@objname is not null)
  774.         begin
  775.             select @curid = id, @objtype = o.sysstat & 0x0f, @curcat = o.category & 0x0002 from sysobjects o where id = object_id(@objname)
  776.             if (@curid is null)
  777.             begin
  778.                 select @42errmsg = @objname + ' does not exist.'
  779.                 RAISERROR 4215001 @42errmsg
  780.                 return 1
  781.             end
  782.             if (@flags & 0x80000 = 0)
  783.                 select @delinputobj = @curid
  784.         end
  785.  
  786.         set nocount on
  787.         if (@curid is null)
  788.             insert #t1 (pid, ptype, pcat) select o.id, o.sysstat & 0x0f, o.category & 0x0002 from sysobjects o
  789.                 where o.sysstat & 0x0f = @objtype
  790.         else
  791.             insert #t1 (pid, ptype, pcat) values (@curid, @objtype, @curcat)
  792.     end
  793.  
  794.     /*
  795.      * All initial objects are loaded as parents/children.  Now we loop, creating
  796.      * rows of child/parent relationships.  Use #t2 as a temp area for the selects
  797.      * to simulate recursion; when they find no rows, we're done with this step.
  798.      *
  799.      * Note that triggers are weird; they're part of a table definition but can
  800.      * also reference other tables, so we need to evaluate them both ways.  SQL
  801.      * Server stores the table for a trigger object as its deltrig; if a trigger
  802.      * references another table, that relationship is stored in sysdepends.
  803.      * This peculiarity of triggers requires separating the object-retrieval pass
  804.      * from the creation-sequence pass (below).  Also, the fact that trigger tables
  805.      * are stored in a non-indexed column (deltrig) requires us to use a worktable
  806.      * if we're returning triggers, so we don't continually tablescan sysobjects.
  807.      */
  808.  
  809.     if (@flags & power(2, 8) != 0)
  810.         insert #temptrig select d.id, d.deltrig, d.sysstat, d.category from sysobjects d where d.sysstat & 0x0f = 8
  811.  
  812.     while (select count(*) from #t1 where bDone is null) > 0
  813.     begin
  814.         /*
  815.          * Remove Microsoft-internal or other system objects from #t1, unless
  816.          * @flags specified including system tables.  We do this here so that
  817.          * cascaded system dependencies are not included unless specifically
  818.          * requested.  For other restrictions, we wait until below so that all
  819.          * cascaded object types are fully evaluated.
  820.          */
  821.         if (@flags & power(2, 1) = 0)
  822.             delete #t1 where ttype = 1 or tcat = 0x0002 or pcat = 0x0002
  823.  
  824.         if (@flags & 0x40000 != 0)
  825.         begin
  826.             
  827.                 /* Table --> Triggers */
  828.                 if (@flags & power(2, 8) != 0)
  829.                     insert #t2 (tid, ttype, tcat, pid, ptype, pcat)
  830.                         select distinct t.pid, t.ptype, t.pcat, o.id, o.sysstat & 0x0f, o.category & 0x0002 from #t1 t, #temptrig o
  831.                             where t.bDone is null and t.ptype = 3 and o.deltrig = t.pid
  832.  
  833.                 /* Object --> sysdepends children */
  834.                 insert #t2 (tid, ttype, tcat, pid, ptype, pcat)
  835.                     select distinct t.pid, t.ptype, t.pcat, d.id, o.sysstat & 0x0f, o.category & 0x0002
  836.                     from #t1 t, sysdepends d, sysobjects o
  837.                     where t.bDone is null and d.depid = t.pid and d.id = o.id
  838.             
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.         end else begin
  847.             
  848.                 /* Trigger --> Table */
  849.                 if (@flags & power(2, 3) != 0)
  850.                     insert #t2 (tid, ttype, tcat, pid, ptype, pcat)
  851.                         select distinct t.pid, t.ptype, t.pcat, o.deltrig, u.sysstat & 0x0f, u.category & 0x0002
  852.                               from #t1 t, sysobjects o, sysobjects u
  853.                             where t.bDone is null and t.ptype = 8 and o.id = t.pid and o.deltrig != 0 and u.id = o.deltrig
  854.  
  855.                 /* Object --> sysdepends parents */
  856.                 insert #t2 (tid, ttype, tcat, pid, ptype, pcat)
  857.                     select distinct t.pid, t.ptype, t.pcat, d.depid, o.sysstat & 0x0f, o.category & 0x0002
  858.                     from #t1 t, sysdepends d, sysobjects o
  859.                     where t.bDone is null and d.id = t.pid and d.depid = o.id
  860.             
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.         end
  869.  
  870.         /*
  871.          * We have this generation of parents in #t2, so clear the current
  872.          * child generation's bDone flags.  Then insert from #t2; the current
  873.          * parent generation becomes the next loop's child generation, with
  874.          * bDone = null until next loop's dependencies are selected.
  875.          */
  876.         update #t1 set bDone = 1
  877.         insert #t1 select * from #t2 where #t2.tid not in
  878.             (select tid from #t1 where #t1.tid = #t2.tid and #t1.pid = #t2.pid)
  879.         truncate table #t2
  880.  
  881.         /* If they only want one level, we're done.    */
  882.         if (@flags & 0x100000 <> 0)
  883.             update #t1 set bDone = 1
  884.     end
  885.  
  886.     /*
  887.      * The inner loop above did not put parents with no parents into the
  888.      * child (tid) list.  Do that now, then remove all rows where tid is
  889.      * NULL, because these were initial objects which now have a tid row.
  890.      * Just in case, remove self-refs from #t1, and also remove rows from #t1
  891.      * with NULL pid if a row exists for that tid where the pid is nonNULL.
  892.      * Avoid nested self-joins by using worktables.
  893.      */
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.     insert #t1 (tid, ttype, tcat) select distinct pid, ptype, pcat from #t1 t
  902.         where not exists (select tid from #t1 where tid = t.pid)
  903.  
  904.  
  905.     /*
  906.      * Because triggers can go in both directions, we'll need to check for
  907.      * circular dependencies on parent evaluation.  Since any tables referenced
  908.      * by the trigger must exist before the trigger can be created, remove rows
  909.      * where the trigger is the parent.
  910.      */
  911.     if (@flags & 0x40000 = 0)
  912.         delete #t1 where ptype = 8
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.     delete from #t1 where tid is null or tid = pid or (pid is null and tid in
  922.         (select tid from #t1 where pid is not null))
  923.  
  924.  
  925.     /*
  926.      * If we're to get all objects, get all UDDTs (which aren't in sysobjects)
  927.      * and Rules/Defaults, assuming we're returning those types.
  928.      */
  929.     if (@allobjs <> 0)
  930.     begin
  931.         if (@flags & power(2, 0) != 0)
  932.             insert #tempudt
  933.                 select usertype from systypes where usertype > 99
  934.         if (@flags & (power(2, 7) | power(2, 6)) != 0)
  935.             insert #t2 (tid, ttype, tcat)
  936.                 select id, sysstat & 0x0f, 0 from sysobjects
  937.                 where sysstat in (7, 6)
  938.                 
  939.     end else begin
  940.         /*
  941.          * Not getting all objects.  Get any datatypes that
  942.          * are referenced by objects in #t1.  We don't care about specific
  943.          * datatype dependencies, we just want to know which ones are needed.
  944.          */
  945.         if (@flags & power(2, 0) != 0)
  946.             insert #tempudt select distinct usertype from syscolumns
  947.                 where usertype > 99 and id in (select tid from #t1)
  948.  
  949.         /*
  950.          * Load rules and defaults needed by datatypes and other #t1 objects
  951.          * into #t2.  Don't track specific object dependencies with these;
  952.          * we just want to know which ones are needed.  For defaults only, eliminate
  953.          * those which are constraints.
  954.          */
  955.         if (@flags & power(2, 7) != 0)
  956.         begin
  957.             insert #t2 (tid, ttype, tcat)
  958.                 select distinct s.domain, 7, 0 from systypes s, #tempudt t
  959.                     where s.domain != 0 and s.usertype = t.dtype
  960.                         and s.domain not in (select tid from #t1)
  961.             insert #t2 (tid, ttype, tcat)
  962.                 select distinct s.domain, 7, 0 from syscolumns s, #t1 t
  963.                     where s.domain != 0 and s.id = t.tid
  964.                         and s.domain not in (select tid from #t1)
  965.         end
  966.         if (@flags & power(2, 6) != 0)
  967.         begin
  968.             insert #t2 (tid, ttype, tcat)
  969.                 select distinct s.tdefault, 6, 0 from systypes s, #tempudt t
  970.                     where s.tdefault != 0 and s.usertype = t.dtype
  971.                         and s.tdefault not in (select tid from #t1)
  972.                         
  973.             insert #t2 (tid, ttype, tcat)
  974.                 select distinct s.cdefault, 6, 0 from syscolumns s, #t1 t
  975.                     where s.cdefault != 0 and s.id = t.tid
  976.                         and s.cdefault not in (select tid from #t1)
  977.                         
  978.         end
  979.     end        /* Not getting all objects */
  980.  
  981.     /*
  982.      * Now that we've got all objects we want, eliminate those we don't
  983.      * want to return.  If @inputobj and they don't want it returned,
  984.      * remove it from the table.  Then eliminate object types they don't
  985.      * want returned.  Make sure that in doing so we retain all parent
  986.      * objects of the types we do want -- it is possible at this point
  987.      * that a tid we want has no rows except those with pids we don't want.
  988.      */
  989.     if (@flags & 0x01ff != 0x01ff or @delinputobj != 0)
  990.     begin
  991.         delete #t1 where @flags & power(2, ttype) = 0 or tid = @delinputobj
  992.  
  993.         /*
  994.          * Be sure that the insert does not duplicate rows that will survive the
  995.          * following delete -- these are rows where the pid is not @delinputobj
  996.          * and ptype is either null or a type we'll keep (if ptype is null then
  997.          * pid hasn't been set so no need for more complex checking).
  998.          */
  999.         insert #t1 (tid, ttype, tcat) select distinct tid, ttype, tcat from #t1
  1000.             where (@flags & power(2, ptype) = 0 or pid = @delinputobj)
  1001.                 and tid not in (select tid from #t1 where ptype is null or
  1002.                     (pid != @delinputobj and @flags & power(2, ptype) != 0))
  1003.         delete #t1 where @flags & power(2, ptype) = 0 or pid = @delinputobj
  1004.     end
  1005.  
  1006.     /*
  1007.      * To determine creation order, find all objects which are not yet bDone
  1008.      * and have no parents or whose parents are all bDone, and set their bDone
  1009.      * to the next @curid.  This will leave bDone as the ascending order in
  1010.      * which objects must be created (topological sort).  Again, use worktables
  1011.      * to remove nested self-joins.
  1012.      */
  1013.     update #t1 set bDone = 0
  1014.     select @curid = 1, @rowsaffected = 1
  1015.     while (@rowsaffected <> 0)
  1016.     begin
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.         if (@flags & 0x40000 != 0)
  1036.             update #t1 set bDone = @curid from #t1 where bDone = 0
  1037.                 and tid not in (select pid from #t1 where pid is not null and bDone = 0)
  1038.         else
  1039.             update #t1 set bDone = @curid from #t1 where bDone = 0
  1040.                 and tid not in (select tid from #t1 where pid is not null and pid in (select tid from #t1 where bDone = 0))
  1041.  
  1042.  
  1043.         select @rowsaffected = @@rowcount, @curid = @curid + 1
  1044.     end
  1045.  
  1046.     /* For SQL60 only, we need to check circular dependencies (DRI for tables is the only way to get them). */
  1047.     /* This will have occurred if we still have any rows in #t1 where bDone = 0, after the above loop. */
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.     /*
  1056.      * Finally, return the objects.  Rules/Defaults must be created first so they're returned first,
  1057.      * followed by UDDTs. followed by all other (sysdepends/DRI) dependencies.  @curid is the bDone
  1058.      * value; we need to increment the #t1 value so our multi-result-set is in the proper sequence.
  1059.      * Of course, these never have parents, so don't return them if asking for children.
  1060.      */
  1061.     if (@flags & 0x40000 = 0) begin
  1062.         select @curid = 1
  1063.         if ((@flags & (power(2, 7) | power(2, 6)) != 0) and exists (select * from #t2)) begin
  1064.             update #t1 set bDone = bDone + 1
  1065.             select distinct oType = power(2, o.sysstat & 0x0f), oRuleDefName = o.name, oOwner = user_name(o.uid), oSequence = convert(smallint, @curid)
  1066.                 from sysobjects o, #t2 t
  1067.                 where o.id = t.tid
  1068.                 order by power(2, o.sysstat & 0x0f), o.name
  1069.             select @curid = @curid + 1
  1070.         end
  1071.         if ((@flags & power(2, 0) != 0) and exists (select * from #tempudt)) begin
  1072.             update #t1 set bDone = bDone + 1
  1073.             select distinct oType = power(2, 0), oUDDTName = c.name, oOwner = user_name(c.uid), oSequence = convert(smallint, @curid)
  1074.                 from systypes c, #tempudt t, sysobjects p
  1075.                 where c.usertype = t.dtype
  1076.                 order by c.name
  1077.             select @curid = @curid + 1
  1078.         end
  1079.     end
  1080.  
  1081.     /*
  1082.      * Select dependency-style objects, returning parents if desired.
  1083.      * Normally sorting is in terms of who must be created first, i.e. ascending:  parent-->child-->grandchild.
  1084.      * Descending order (child-->parent-->grandparent) would be used for a graphical-dependencies evaluator showing
  1085.      * the parents.  Therefore we invert bDone if descending sort.  bDone is 1-based; min + max - bDone gives inversion.
  1086.      * Note:  Always return at least this empty set.
  1087.      */
  1088.     if (@flags & 0x20000 != 0) begin
  1089.         select @curid = max(bDone) + min(bDone) from #t1
  1090.         update #t1 set bDone = convert(smallint, @curid) - bDone
  1091.     end
  1092.     if (@flags & 0x10000 != 0)
  1093.         select distinct oType = power(2, o.sysstat & 0x0f), oObjName = o.name, oOwner = user_name(o.uid),
  1094.                 RelType = power(2, p.sysstat & 0x0f), RelName = p.name, RelOwner = user_name(p.uid),
  1095.                 oSequence = t.bDone
  1096.             from sysobjects o, sysobjects p, #t1 t
  1097.             where o.id = t.tid and p.id =* t.pid
  1098.             order by t.bDone, power(2, o.sysstat & 0x0f), o.name
  1099.     else
  1100.         select distinct oType = power(2, o.sysstat & 0x0f), oObjName = o.name, oOwner = user_name(o.uid),
  1101.                 oSequence = t.bDone
  1102.             from sysobjects o, #t1 t
  1103.             where o.id = t.tid
  1104.             order by t.bDone, power(2, o.sysstat & 0x0f), o.name
  1105. go
  1106. /* End sp_MSdependencies */
  1107.  
  1108. /************* DUMP THE TRANSACTION LOG **************************************/
  1109. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1110. /* script periodically, you will run out of transaction log space.           */
  1111. print ''
  1112. print 'Dumping transaction log...'
  1113. print ''
  1114. go
  1115. dump tran master with no_log
  1116. go
  1117. /************* END DUMP THE TRANSACTION LOG **********************************/
  1118.  
  1119. /*******************************************************************************/
  1120. print ''
  1121. print 'Creating sp_MStablespace'
  1122. print ''
  1123. go
  1124.  
  1125. create procedure sp_MStablespace
  1126. @name varchar(92), @id int = null
  1127. as
  1128.     declare @rows int, @datasizeused int, @indexsizeused int, @pagesize int
  1129.     declare @dbname varchar(30)
  1130.     select @dbname = db_name()
  1131.     declare @42errmsg varchar(100)
  1132.  
  1133.     if (@id is null)
  1134.         select @id = id from sysobjects where id = object_id(@name) and sysstat & 0x0f in (1, 3)
  1135.     if (@id is null)
  1136.     begin
  1137.         select @42errmsg = 'Object ''' + @name + ''' is not a table in database ''' + @dbname + ''''
  1138.         RAISERROR 4215009 @42errmsg
  1139.         return 1
  1140.     end
  1141.  
  1142.     /* rows */
  1143.     SELECT @rows = rows
  1144.         FROM sysindexes
  1145.         WHERE indid < 2 and id = @id
  1146.  
  1147.     /* data */
  1148.     SELECT @datasizeused =
  1149.     (SELECT sum(dpages)
  1150.      FROM sysindexes
  1151.      WHERE indid < 2 and id = @id)
  1152.     +
  1153.     (SELECT isnull(sum(used), 0)
  1154.      FROM sysindexes
  1155.      WHERE indid = 255 and id = @id)
  1156.  
  1157.     /* index */
  1158.     SELECT @indexsizeused =
  1159.     (SELECT sum(used)
  1160.      FROM sysindexes
  1161.      WHERE indid in (0, 1, 255) and id = @id)
  1162.      - @datasizeused
  1163.  
  1164.     /* Pagesize on this server (sysindexes stores size info in pages) */
  1165.     select @pagesize = v.low / 1024 from master..spt_values v where v.number=1 and v.type='E'
  1166.  
  1167.     select Rows = @rows, DataSpaceUsed = @datasizeused * @pagesize, IndexSpaceUsed = @indexsizeused    * @pagesize
  1168. go
  1169.  
  1170. /* End sp_MStablespace */
  1171.  
  1172. /*******************************************************************************/
  1173. print ''
  1174. print 'Creating sp_MSindexspace'
  1175. print ''
  1176. go
  1177.  
  1178. CREATE PROCEDURE sp_MSindexspace
  1179.     @tablename varchar(92), @index_name varchar(30) = NULL
  1180. AS
  1181. BEGIN
  1182.   DECLARE @table_id int
  1183.   DECLARE @index_id int
  1184.   DECLARE @msg varchar(255)
  1185.   declare @42errmsg varchar(100)
  1186.  
  1187.   /* Make sure @tablename is local to the current database */
  1188.   IF (@tablename like '%.%.%') AND
  1189.      (substring(@tablename, 1, charindex('.', @tablename) - 1) <> db_name())
  1190.   BEGIN
  1191.     select @42errmsg = 'Table or view name must be in the ''current'' database.'
  1192.     RAISERROR 4215078 @42errmsg
  1193.     RETURN(1)
  1194.   END
  1195.   /* Make sure that @tablename and @index_name exist */
  1196.   SELECT @table_id = id
  1197.   FROM sysobjects
  1198.   WHERE (id = object_id(@tablename))
  1199.     AND (sysstat & 0xf = 3)
  1200.   IF (@table_id = NULL)
  1201.   BEGIN
  1202.     select @42errmsg = @tablename + ' does not exist.'
  1203.     RAISERROR 4215001 @42errmsg
  1204.     RETURN(1)
  1205.   END
  1206.   IF (@index_name <> NULL)
  1207.   BEGIN
  1208.     SELECT @index_id = indid
  1209.     FROM sysindexes
  1210.     WHERE (name = @index_name)
  1211.       AND (id = object_id(@tablename))
  1212.     IF (@index_id = NULL)
  1213.     BEGIN
  1214.       SELECT @msg = @tablename + '.' + @index_name
  1215.       select @42errmsg = @msg + ' does not exist.'
  1216.       RAISERROR 4215001 @42errmsg
  1217.       RETURN(1)
  1218.     END
  1219.   END
  1220.   /* Ok, we're good to go */
  1221.   IF (user_id() = 1) 
  1222.     CHECKPOINT
  1223.   IF (@index_name = NULL)
  1224.   BEGIN
  1225.     CREATE TABLE #IndexSizeTemp
  1226.     (IndexID tinyint, IndexName varchar(30), IndexSize int, Comments varchar(28))  
  1227.     INSERT INTO #IndexSizeTemp
  1228.     SELECT indid, name, 0, ''
  1229.     FROM sysindexes 
  1230.     WHERE (id = object_id(@tablename))
  1231.       AND ((indid > 0) AND (indid < 255)) 
  1232.     UPDATE #IndexSizeTemp
  1233.     SET IndexSize = used * 2,
  1234.         Comments = '(None)' 
  1235.     FROM sysindexes si, #IndexSizeTemp ist
  1236.     WHERE (id = object_id(@tablename))
  1237.       AND (indid > 1) AND (indid < 255)
  1238.       AND (si.indid = ist.IndexID)
  1239.     UPDATE #IndexSizeTemp
  1240.     SET IndexSize = (used - dpages - isnull((SELECT sum(used) 
  1241.                                              FROM sysindexes
  1242.                                              WHERE (indid > 1) AND (indid < 255) 
  1243.                                                AND (id = object_id(@tablename))), 0)) * 2,
  1244.         Comments = 'Size excludes actual data.' 
  1245.     FROM sysindexes si, #IndexSizeTemp ist
  1246.     WHERE (id = object_id(@tablename))
  1247.       AND (indid = 1)
  1248.       AND (si.indid = ist.IndexID)
  1249.     SELECT 'Index ID' = IndexID, 'Index Name' = IndexName, 'Size (KB)' = IndexSize, Comments 
  1250.     FROM #IndexSizeTemp
  1251.     ORDER BY IndexID
  1252.     DROP TABLE #IndexSizeTemp
  1253.   END
  1254.   ELSE
  1255.   BEGIN 
  1256.     DECLARE @indid int
  1257.     SELECT @indid = indid 
  1258.     FROM sysindexes 
  1259.     WHERE (id = object_id(@tablename))
  1260.       AND (name = @index_name)
  1261.     /* The non-clustered index case */
  1262.     IF ((@indid > 1) AND (@indid < 255))
  1263.     BEGIN
  1264.       SELECT 'Size (KB)' = used * 2 
  1265.       FROM sysindexes 
  1266.       WHERE (id = object_id(@tablename))
  1267.         AND (name = @index_name)
  1268.       RETURN(0) 
  1269.     END
  1270.     /* The clustered index case */
  1271.     IF (@indid = 1)
  1272.     BEGIN
  1273.       SELECT 'Size (KB)' =
  1274.              (used - dpages - isnull((SELECT sum(used) 
  1275.                                       FROM sysindexes
  1276.                                       WHERE (indid > 1) AND (indid < 255) 
  1277.                                         AND (id = object_id(@tablename))), 0)) * 2 
  1278.       FROM sysindexes 
  1279.       WHERE (id = object_id(@tablename))
  1280.         AND (name = @index_name)
  1281.     END
  1282.   END
  1283.   RETURN(0)
  1284. END
  1285. go
  1286. /* End sp_MSindexspace */
  1287.  
  1288. /*******************************************************************************/
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460.  
  1461.  
  1462.  
  1463.  
  1464.  
  1465.  
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524.  
  1525.  
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.  
  1532.  
  1533.  
  1534.  
  1535.  
  1536.  
  1537.  
  1538.  
  1539.  
  1540.  
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.  
  1562.  
  1563.  
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589.  
  1590.  
  1591.  
  1592.  
  1593.  
  1594.  
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721.  
  1722.  
  1723.  
  1724.  
  1725.  
  1726.  
  1727.  
  1728.  
  1729.  
  1730.  
  1731.  
  1732.  
  1733.  
  1734.  
  1735.  
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747.  
  1748.  
  1749.  
  1750.  
  1751.  
  1752.  
  1753.  
  1754.  
  1755.  
  1756.  
  1757.  
  1758.  
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.  
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794.  
  1795.  
  1796.  
  1797.  
  1798.  
  1799.  
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811.  
  1812.  
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855.  
  1856.  
  1857.  
  1858.  
  1859.  
  1860.  
  1861.  
  1862.  
  1863.  
  1864.  
  1865.  
  1866.  
  1867.  
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874.  
  1875.  
  1876.  
  1877.  
  1878.  
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.  
  1885.  
  1886.  
  1887.  
  1888.  
  1889.  
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921.  
  1922.  
  1923.  
  1924.  
  1925.  
  1926.  
  1927.  
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990.  
  1991.  
  1992.  
  1993.  
  1994.  
  1995.  
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006.  
  2007.  
  2008.  
  2009.  
  2010.  
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018.  
  2019.  
  2020.  
  2021.  
  2022.  
  2023.  
  2024.  
  2025.  
  2026.  
  2027.  
  2028.  
  2029.  
  2030.  
  2031.  
  2032.  
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057.  
  2058.  
  2059.  
  2060.  
  2061.  
  2062.  
  2063.  
  2064.  
  2065.  
  2066.  
  2067.  
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089.  
  2090.  
  2091.  
  2092.  
  2093.  
  2094.  
  2095.  
  2096.  
  2097.  
  2098.  
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.  
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.  
  2117.  
  2118.  
  2119.  
  2120.  
  2121.  
  2122.  
  2123.  
  2124.  
  2125.  
  2126.  
  2127.  
  2128.  
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134.  
  2135.  
  2136.  
  2137.  
  2138.  
  2139.  
  2140.  
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149.  
  2150.  
  2151.  
  2152.  
  2153.  
  2154.  
  2155.  
  2156.  
  2157.  
  2158.  
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190.  
  2191.  
  2192.  
  2193.  
  2194.  
  2195.  
  2196.  
  2197.  
  2198.  
  2199.  
  2200.  
  2201.  
  2202.  
  2203.  
  2204.  
  2205.  
  2206.  
  2207.  
  2208.  
  2209.  
  2210.  
  2211.  
  2212.  
  2213.  
  2214.  
  2215.  
  2216.  
  2217.  
  2218.  
  2219.  
  2220.  
  2221.  
  2222.  
  2223.  
  2224.  
  2225.  
  2226.  
  2227.  
  2228.  
  2229.  
  2230.  
  2231.  
  2232.  
  2233.  
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239.  
  2240.  
  2241.  
  2242.  
  2243.  
  2244. /*******************************************************************************/
  2245. print ''
  2246. print 'Creating sp_MSuniquename'
  2247. print ''
  2248. go
  2249.  
  2250. create procedure sp_MSuniquename 
  2251.     @seed varchar(30), @start int = null
  2252. as
  2253.     /* Return a unique name for sysobjects, based on a passed-in seed. */
  2254.     set nocount on
  2255.     declare @i int, @append varchar(10), @seedlen int, @temp varchar(30), @recalcseedlen int
  2256.     select @i = 1, @seedlen = datalength(@seed), @recalcseedlen = 1
  2257.     if (@start is not null and @start >= 0)
  2258.         select @i = @start
  2259.     while 1 < 2
  2260.     begin
  2261.         /* This is probably overkill, but start at max length of seed name, leaving room under SYSNAMELEN for @append. */
  2262.         /* We'll work our way back along the string if more room needed (pathological user). */
  2263.         select @append = ltrim(str(@i)) + '__' + ltrim(str(@@spid))
  2264.         if (@recalcseedlen = @i)
  2265.         begin
  2266.             select @recalcseedlen = @recalcseedlen * 10
  2267.             if ((@seedlen + datalength(@append)) > 30)
  2268.                 select @seedlen = 30 - datalength(@append)
  2269.         end
  2270.         select @temp = substring(@seed, 1, @seedlen) + @append        
  2271.  
  2272.         /* If I don't set a limit somewhere, it's gonna look hung -- I'd rather get a nonunique error. */
  2273.         if object_id(@temp) is null or @i > 999999        /* if increased, watch out for overflow of @recalcseedlen */
  2274.         begin
  2275.             set nocount off
  2276.             select Name = @temp, Next = @i + 1
  2277.             return 0
  2278.         end
  2279.         select @i = @i + 1
  2280.     end
  2281. go
  2282. /* End sp_MSuniquename */
  2283.  
  2284. /*******************************************************************************/
  2285. print ''
  2286. print 'Creating sp_MSkilldb'
  2287. print ''
  2288. go
  2289.  
  2290. sp_configure updat, 1
  2291. go
  2292. reconfigure with override
  2293. go
  2294.  
  2295. create proc sp_MSkilldb
  2296.     @dbname varchar(30)
  2297. as
  2298.     declare @42errmsg varchar(100)
  2299.     if (@@trancount > 0) begin
  2300.         select @42errmsg = 'The procedure ''sp_MSkilldb'' cannot be executed from within a transaction.'
  2301.         RAISERROR 4215002 @42errmsg
  2302.         return 1
  2303.     end
  2304.  
  2305.     if (suser_id() <> 1) begin
  2306.         select @42errmsg = 'You must be System Administrator (SA) to execute this stored procedure.'
  2307.         RAISERROR 4215003 @42errmsg
  2308.         return 1
  2309.     end
  2310.  
  2311.     /* Set this db to suspect, then let dbcc dbrepair kill it for us. */
  2312.     update master..sysdatabases set status = status | 0x0100
  2313.         where name = @dbname
  2314.     if (@@rowcount = 0) begin
  2315.         select @42errmsg = 'Database ''' + @dbname + ''' not found.'
  2316.         RAISERROR 4214211 @42errmsg
  2317.         return 1
  2318.     end
  2319.     dbcc dbrepair(@dbname, dropdb)
  2320.     return 0
  2321. go
  2322.  
  2323. sp_configure updat, 0
  2324. go
  2325. reconfigure with override
  2326. go
  2327.  
  2328. /* End sp_MSkilldb */
  2329.  
  2330. /*******************************************************************************/
  2331. print ''
  2332. print 'Creating sp_MSdbuserprofile'
  2333. print ''
  2334. go
  2335.  
  2336. create proc sp_MSdbuserprofile
  2337.     @mode varchar(10) = 'init', @qual varchar(30) = '%'
  2338. as
  2339.  
  2340. /* Order of privilege evaluation is:  user granted/revoked, then group granted/revoked, then public granted/revoked */
  2341.  
  2342.  
  2343.  
  2344.  
  2345.  
  2346.  
  2347.  
  2348.     /* If 'srv', we're selecting the server (master db) user profile - currently, just create db priv. */
  2349.     declare @bits int, @gid int, @prot int
  2350.     if (lower(@mode) like 'serv%') begin
  2351.         select @bits = 0x0000
  2352.         if (suser_id() = 1) begin
  2353.             /* sa has everything */
  2354.             select @bits = 0x0007
  2355.         end else begin
  2356.             select @prot = null select @prot = protecttype from sysprotects where action = 203 and uid = user_id()
  2357.             if (@prot is null) select @prot = protecttype from sysprotects where action = 203 and uid = (select gid from sysusers where uid = user_id())
  2358.             if (@prot is null) select @prot = protecttype from sysprotects where action = 203 and uid = user_id('public')
  2359.             if (@prot = 205) select @bits = @bits | 0x0002
  2360.  
  2361.             select @prot = null select @prot = protecttype from sysprotects where action = 224 and uid = user_id() and id = object_id('sp_addextendedproc')
  2362.             if (@prot is null) select @prot = protecttype from sysprotects where action = 224 and uid = (select gid from sysusers where uid = user_id()) and id = object_id('sp_addextendedproc')
  2363.             if (@prot is null) select @prot = protecttype from sysprotects where action = 224 and uid = user_id('public') and id = object_id('sp_addextendedproc')
  2364.             if (@prot = 205) select @bits = @bits | 0x0004
  2365.         end
  2366.         select @bits
  2367.         return 0
  2368.     end
  2369.  
  2370.  
  2371.  
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.  
  2381.  
  2382.  
  2383.  
  2384.  
  2385.  
  2386.  
  2387.  
  2388.  
  2389.  
  2390.  
  2391.  
  2392.  
  2393.  
  2394.  
  2395.  
  2396.  
  2397.  
  2398.  
  2399.  
  2400.  
  2401.  
  2402.  
  2403.  
  2404.  
  2405.  
  2406.  
  2407.  
  2408.  
  2409.  
  2410.  
  2411.  
  2412.  
  2413.  
  2414.  
  2415.  
  2416.  
  2417.  
  2418.  
  2419.  
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425.  
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.  
  2433.  
  2434.  
  2435.  
  2436.  
  2437.  
  2438.  
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.  
  2448.  
  2449.  
  2450.  
  2451.     if (lower(@mode) like 'ini%')
  2452.         select @mode = 'select'
  2453.  
  2454.  
  2455.     /* All other modes require a creation of a profile bitmask. */
  2456.     if (user_id() = 1) begin
  2457.         /* sa/Dbo has everything. */
  2458.         select @bits = 0x00ff
  2459.     end else begin
  2460.         /* Not dbo so get individual privileges */
  2461.         select @bits = 0x0000, @gid = gid from sysusers where uid = user_id()
  2462.         select @prot = null select @prot = protecttype from sysprotects where action = 198 and uid = user_id()
  2463.         if (@prot is null) select @prot = protecttype from sysprotects where action = 198 and uid = (select gid from sysusers where uid = user_id())
  2464.         if (@prot is null) select @prot = protecttype from sysprotects where action = 198 and uid = user_id('public')
  2465.         if (@prot = 205) select @bits = @bits | 0x0002
  2466.  
  2467.         select @prot = null select @prot = protecttype from sysprotects where action = 207 and uid = user_id()
  2468.         if (@prot is null) select @prot = protecttype from sysprotects where action = 207 and uid = (select gid from sysusers where uid = user_id())
  2469.         if (@prot is null) select @prot = protecttype from sysprotects where action = 207 and uid = user_id('public')
  2470.         if (@prot = 205) select @bits = @bits | 0x0004
  2471.  
  2472.         select @prot = null select @prot = protecttype from sysprotects where action = 222 and uid = user_id()
  2473.         if (@prot is null) select @prot = protecttype from sysprotects where action = 222 and uid = (select gid from sysusers where uid = user_id())
  2474.         if (@prot is null) select @prot = protecttype from sysprotects where action = 222 and uid = user_id('public')
  2475.         if (@prot = 205) select @bits = @bits | 0x0008
  2476.  
  2477.         select @prot = null select @prot = protecttype from sysprotects where action = 228 and uid = user_id()
  2478.         if (@prot is null) select @prot = protecttype from sysprotects where action = 228 and uid = (select gid from sysusers where uid = user_id())
  2479.         if (@prot is null) select @prot = protecttype from sysprotects where action = 228 and uid = user_id('public')
  2480.         if (@prot = 205) select @bits = @bits | 0x0010
  2481.  
  2482.         select @prot = null select @prot = protecttype from sysprotects where action = 233 and uid = user_id()
  2483.         if (@prot is null) select @prot = protecttype from sysprotects where action = 233 and uid = (select gid from sysusers where uid = user_id())
  2484.         if (@prot is null) select @prot = protecttype from sysprotects where action = 233 and uid = user_id('public')
  2485.         if (@prot = 205) select @bits = @bits | 0x0020
  2486.  
  2487.         select @prot = null select @prot = protecttype from sysprotects where action = 235 and uid = user_id()
  2488.         if (@prot is null) select @prot = protecttype from sysprotects where action = 235 and uid = (select gid from sysusers where uid = user_id())
  2489.         if (@prot is null) select @prot = protecttype from sysprotects where action = 235 and uid = user_id('public')
  2490.         if (@prot = 205) select @bits = @bits | 0x0040
  2491.  
  2492.         select @prot = null select @prot = protecttype from sysprotects where action = 236 and uid = user_id()
  2493.         if (@prot is null) select @prot = protecttype from sysprotects where action = 236 and uid = (select gid from sysusers where uid = user_id())
  2494.         if (@prot is null) select @prot = protecttype from sysprotects where action = 236 and uid = user_id('public')
  2495.         if (@prot = 205) select @bits = @bits | 0x0080
  2496.     end
  2497.  
  2498.  
  2499.  
  2500.  
  2501.  
  2502.  
  2503.  
  2504.  
  2505.  
  2506.     if (lower(@mode) like '%sel%') begin
  2507.         if (lower(@qual) like 'bin%')        /* For visual debugging */
  2508.             select 'profile bitmask' = convert(binary(4), @bits)
  2509.         else
  2510.             select 'profile bitmask' = @bits
  2511.     end 
  2512. go
  2513. /* End sp_MSdbuserprofile */
  2514.  
  2515. /*******************************************************************************/
  2516. print ''
  2517. print 'Creating sp_MSobjectprivs'
  2518. print ''
  2519. go
  2520.  
  2521. create proc sp_MSobjectprivs
  2522.     @objname varchar(92) = null, @mode varchar(10) = 'object'    /* or 'user' */
  2523. as
  2524.     set nocount on
  2525.  
  2526.     /* 
  2527.      * To get around a 4.21 subquery bug where returning count(*) of 0 (for proc cols)
  2528.      * causes the result set to return no rows, we need two passes; one to get the
  2529.      * objects, and another to explicitly use a value (@cols) instead of a subquery.
  2530.      */
  2531.     declare @id int, @uid int, @cols int
  2532.     select @id = null, @uid = null
  2533.     if (@mode like 'us%')
  2534.         select @uid = user_id(@objname)
  2535.     else
  2536.         select @id = object_id(@objname)
  2537.     if (@id is null and @uid is null) begin
  2538.         declare @42errmsg varchar(100)
  2539.         select @42errmsg = @objname + ' does not exist.'
  2540.         RAISERROR 4215001 @42errmsg
  2541.         return 1
  2542.     end
  2543.  
  2544.     /* Get a temp list of objects we're interested in.  Do not include repl_* users. */
  2545.     create table #objs(
  2546.         id int
  2547.     )
  2548.     insert #objs select distinct p.id from sysprotects p 
  2549.         where (@id is null or p.id = @id)
  2550.         and (@uid is null or p.uid = @uid)
  2551.         and p.action in (193, 195, 196, 197, 224, 26) and p.uid not in (16382, 16383)
  2552.  
  2553.     /* Temp table will hold output for final select */
  2554.     create table #output (
  2555.         action int,
  2556.         colid int null,
  2557.         uid int,
  2558.         protecttype int,
  2559.         id int
  2560.     )
  2561.  
  2562.     /* Use a "fake cursor" by deleting successive id's from #objs, as this must run on 4.21 */
  2563.     select @id = min(id) from #objs
  2564.     while (@id is not null) begin
  2565.         select @cols = count(*) from syscolumns c where c.id = @id
  2566.         insert #output select p.action, a.number, p.uid, p.protecttype, p.id
  2567.             from master.dbo.spt_values a, sysprotects p
  2568.             where convert(tinyint, substring( isnull(p.columns, 0x01), a.low, 1)) & a.high !=0
  2569.             and (p.id = @id)
  2570.             and (@uid is null or p.uid = @uid)
  2571.             and a.number <= @cols
  2572.             and a.type = 'P'
  2573.         /* Increment our "fake cursor" column and get the next one. */
  2574.         delete #objs where id = @id
  2575.         select @id = min(id) from #objs
  2576.     end
  2577.  
  2578.     /*
  2579.      * Organize so that the non-collist privileges are returned first.. this allows
  2580.      * scripting to combine them.  sysprotects.action is tinyint, so the hibyte won't conflict.
  2581.      */
  2582.  
  2583.     update #output set action = action | 0x10000000 where colid <> 0
  2584.  
  2585.     /*
  2586.      * Order output by uid so Public will script before other groups (we need to script privs for public before
  2587.      * other groups, before users; otherwise sysprotects doesn't hold onto things right).  Sub-order is by object id
  2588.      * so we know when we're done with one object and onto the next, then by protecttype to group all GRANTs and
  2589.      * REVOKEs together, and lastly by action (including ORDER_ACTION_BIT so scripting can be more efficient)
  2590.      * because we may have multiple rows for columns.
  2591.      */
  2592.     set nocount off
  2593.     select p.action & ~convert(int, 0x10000000), 'column' = col_name(p.id, p.colid), p.uid, 'username' = user_name(p.uid), 
  2594.             p.protecttype, o.name, 'owner' = user_name(o.uid), p.id 
  2595.         from #output p, sysobjects o 
  2596.         where o.id = p.id 
  2597.         order by p.uid, p.id, p.protecttype, p.action
  2598. go
  2599. /* End sp_MSobjectprivs */
  2600.  
  2601. /*******************************************************************************/
  2602. /* Need to create the version proc here so we can set its category bit */
  2603. go
  2604. create procedure sp_MSSQLOLE_version
  2605. as
  2606.     /* Values for this are same as @@microsoft_version:  0xrraaiibb (reserved, major, minor, build). */
  2607.     declare @i int
  2608.     select @i = 0x00063201    /* Must be in hex! */
  2609.     /* Select the numeric value, and a conversion to make it readable */
  2610.     select 'Microsoft SQLOLE Scripts' = @i, 'Version' = convert(binary(4), @i)
  2611. go
  2612.  
  2613. /********************* Grant privileges *********************************/
  2614. print ''
  2615. print 'Granting execute permissions on procedures'
  2616.  
  2617. grant execute on sp_MShelpcolumns to public
  2618. grant execute on sp_MShelpindex to public
  2619. grant execute on sp_MShelptype to public
  2620. grant execute on sp_MSdependencies to public
  2621. grant execute on sp_MStablespace to public
  2622. grant execute on sp_MSindexspace to public
  2623. grant execute on sp_MSuniquename to public
  2624. grant execute on sp_MSkilldb to public
  2625. grant execute on sp_MSdbuserprofile to public
  2626. grant execute on sp_MSobjectprivs to public
  2627.  
  2628.  
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634.  
  2635.  
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644. grant execute on xp_msver to public
  2645.  
  2646. grant execute on sp_MSSQLOLE_version to public
  2647. go
  2648.  
  2649. /********************** Verify object creation and update category bit for objects *********************************/
  2650.  
  2651. /***************************************************
  2652.  ******************* SQL60 *************************
  2653.  ***************************************************
  2654.  */
  2655.  
  2656.  
  2657.  
  2658.  
  2659.  
  2660.  
  2661.  
  2662.  
  2663.  
  2664.  
  2665.  
  2666.  
  2667.  
  2668.  
  2669.  
  2670.  
  2671.  
  2672.  
  2673.  
  2674.  
  2675.  
  2676. /************* DUMP THE TRANSACTION LOG **************************************/
  2677. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2678. /* script periodically, you will run out of transaction log space.           */
  2679. print ''
  2680. print 'Dumping transaction log...'
  2681. print ''
  2682. go
  2683. dump tran master with no_log
  2684. go
  2685.  
  2686. /***************************************************
  2687.  ******************* Pre-SQL60 *********************
  2688.  ***************************************************
  2689.  */
  2690. print ''
  2691. print 'Setting category bit for objects'
  2692. print ''
  2693. go
  2694. sp_configure 'allow updates', 1
  2695. go
  2696. reconfigure with override
  2697. go
  2698.  
  2699. update master..sysobjects set category = category | 0x0002 where sysstat & 0x0f = 4 and name in (
  2700.     'sp_MShelpcolumns',
  2701.     'sp_MShelpindex',
  2702.     'sp_MShelptype',
  2703.     'sp_MSdependencies',
  2704.     'sp_MStablespace',
  2705.     'sp_MSindexspace',
  2706.     'sp_MSuniquename',
  2707.     'sp_MSkilldb',
  2708.     'sp_MSdbuserprofile',
  2709.     'sp_MSobjectprivs',
  2710.     'sp_MSSQLOLE_version'
  2711.     )
  2712. go
  2713.  
  2714. sp_configure 'allow updates', 0
  2715. go
  2716. reconfigure with override
  2717. go
  2718.  
  2719. /********************* Check for successful install *********************************/
  2720.  
  2721. set nocount on
  2722. create table #spmissing
  2723. (name varchar(30),
  2724.  type varchar(10)
  2725. )
  2726. go
  2727.  
  2728. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpcolumns' and sysstat & 0x0f = 4)
  2729.     insert into #spmissing values('sp_MShelpcolumns', 'procedure')
  2730. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpindex' and sysstat & 0x0f = 4)
  2731.     insert into #spmissing values('sp_MShelpindex', 'procedure')
  2732. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelptype' and sysstat & 0x0f = 4)
  2733.     insert into #spmissing values('sp_MShelptype', 'procedure')
  2734. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdependencies' and sysstat & 0x0f = 4)
  2735.     insert into #spmissing values('sp_MSdependencies', 'procedure')
  2736. if not exists (select * from master.dbo.sysobjects where name = 'sp_MStablespace' and sysstat & 0x0f = 4)
  2737.     insert into #spmissing values('sp_MStablespace', 'procedure')
  2738. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSindexspace' and sysstat & 0x0f = 4)
  2739.     insert into #spmissing values('sp_MSindexspace', 'procedure')
  2740. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuniquename' and sysstat & 0x0f = 4)
  2741.     insert into #spmissing values('sp_MSuniquename', 'procedure')
  2742. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSkilldb' and sysstat & 0x0f = 4)
  2743.     insert into #spmissing values('sp_MSkilldb', 'procedure')
  2744. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdbuserprofile' and sysstat & 0x0f = 4)
  2745.     insert into #spmissing values('sp_MSdbuserprofile', 'procedure')
  2746. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSobjectprivs' and sysstat & 0x0f = 4)
  2747.     insert into #spmissing values('sp_MSobjectprivs', 'procedure')
  2748.  
  2749. if exists (select * from #spmissing)
  2750. begin
  2751.     print ''
  2752.     print ''
  2753.     print ' =====================  ERRORS!  ===================='
  2754.     print '       The following objects were not created.'
  2755.     print '       SQLOLE will not run against this server.'
  2756.     print ''
  2757.     select * from #spmissing
  2758.     drop procedure sp_MSSQLOLE_version
  2759. end
  2760. go
  2761. drop table #spmissing
  2762. go
  2763. set nocount off
  2764. go
  2765.  
  2766. /************************** Check for admin/OM objects on a 4.21 server. *****************************/
  2767.  
  2768. print ''
  2769. print 'Pre-SQL60 server:  Setting category bit for objects supporting prior versions of GUI tools.'
  2770. print ''
  2771. go
  2772. sp_configure 'allow updates', 1
  2773. go
  2774. reconfigure with override
  2775. go
  2776.  
  2777. print ''
  2778. print 'SQL Administrator objects...'
  2779. print ''
  2780. go
  2781. update master..sysobjects set category = category | 0x0002 where sysstat & 0x0f = 4 and name in (
  2782.         'sp_MSdevice_list',
  2783.         'sp_MScheck_admin',
  2784.         'sp_MSdev_description',
  2785.         'sp_MSdev_databases',
  2786.         'sp_MSsys_monitor',
  2787.         'sp_MSnext_devnumber',
  2788.         'sp_MSdatabase_list',
  2789.         'sp_MSdatabase_avail',
  2790.         'sp_MSbackup_now',
  2791.         'sp_MSuser_list',
  2792.         'sp_MSmonitor',
  2793.         'sp_MSdb_devices',
  2794.         'sp_MSdb_options',
  2795.         'sp_MSdb_properties',
  2796.         'sp_MSdb_space',
  2797.         'sp_MSbackup_schedule',
  2798.         'sp_MScmd_permissions',
  2799.         'sp_MSuser_cmd_permissions',
  2800.         'sp_MSdb_options_bits',
  2801.         'sp_MSdev_mirror',
  2802.         'sp_MSdb_devices_segments',
  2803.         'sp_MSsegment_list',
  2804.         'sp_MSsegment_devices',
  2805.         'sp_MSseg_properties',
  2806.         'sp_MSseg_tables',
  2807.         'sp_MSthread_list',
  2808.         'sp_MShelpdb',
  2809.         'sp_MShelpremotelogin',
  2810.         'sp_MSlogdevice',
  2811.         'sp_MSAdmin_version',
  2812.         'sp_MShelpserver')
  2813. go
  2814. update master..sysobjects set category = category | 0x0002 where sysstat & 0x0f = 3 and name in (
  2815.         'MSscheduled_backups',
  2816.         'MSscheduled_backups_log',
  2817.         'MSsystem_monitor',
  2818.         'MSlast_monitor')
  2819. go
  2820.  
  2821. print ''
  2822. print 'SQL Object Manager objects...'
  2823. print ''
  2824. go
  2825. update master..sysobjects set category = category | 0x0002 where sysstat & 0x0f = 4 and name in (
  2826.         'sp_MScheck_OM',
  2827.         'sp_MSuser_info',
  2828.         'sp_MStable_properties',
  2829.         'sp_MScolumn_properties',
  2830.         'sp_MSobject_list',
  2831.         'sp_MSobjecttype_name',
  2832.         'sp_MStable_permissions',
  2833.         'sp_MSobject_dependencies',
  2834.         'sp_MSOM_version')
  2835. go
  2836.  
  2837. sp_configure 'allow updates', 0
  2838. go
  2839. reconfigure with override
  2840. go
  2841.  
  2842.  
  2843.  
  2844. if (object_id('sp_MSSQLOLE_version') is not null) begin
  2845.     print ''
  2846.     print ''
  2847.     print ' Successful installation.'
  2848.     exec sp_MSSQLOLE_version
  2849. end
  2850.  
  2851. /************* DUMP THE TRANSACTION LOG **************************************/
  2852. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2853. /* script periodically, you will run out of transaction log space.           */
  2854. print ''
  2855. print 'Dumping transaction log...'
  2856. print ''
  2857. go
  2858. dump tran master with no_log
  2859. go
  2860. checkpoint
  2861. go
  2862. /************* END DUMP THE TRANSACTION LOG **********************************/
  2863.  
  2864.