home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / ppc / object60.sql < prev    next >
Encoding:
Text File  |  1995-09-29  |  44.9 KB  |  1,240 lines

  1. /***************************************************************************/
  2. /* OBJECT2.SQL - SQL Object Manager INSTALL FOR SQL SERVER VERSION 4.2x    */
  3. /***************************************************************************/
  4. /* latest revision - 3/16/93                                               */
  5. /* Copyright (C) 1993 - Microsoft Corporation                              */
  6. /***************************************************************************/
  7. print ''
  8. print 'Install SQL Object Manager Scripts (SQL Server version 4.2x)'
  9. print ''
  10. go
  11.  
  12. /************* DUMP THE TRANSACTION LOG **************************************/
  13. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  14. /* script periodically, you will run out of transaction log space.           */
  15. print 'dumping transaction log'
  16. dump tran master with truncate_only
  17. go
  18. /************* END DUMP THE TRANSACTION LOG **********************************/
  19.  
  20. use master
  21. go
  22.  
  23. /*******************************************************************************/
  24. /*        DROP ALL EXISTING PROCEDURE AND TABLES FIRST               */
  25. /*******************************************************************************/
  26. print ''
  27. print 'dropping existing procedures'
  28.  
  29. if exists (select * from sysobjects where name = 'sp_MScheck_OM' and sysstat & 7 = 4)
  30. begin
  31.   drop procedure sp_MScheck_OM
  32. end
  33.  
  34. if exists (select * from sysobjects where name = 'sp_MSuser_info' and sysstat & 7 = 4)
  35. begin
  36.   drop procedure sp_MSuser_info
  37. end
  38. go
  39.  
  40. if exists (select * from sysobjects where name = 'sp_MStable_properties' and sysstat & 7 = 4)
  41. begin
  42.   drop procedure sp_MStable_properties
  43. end
  44. go
  45.  
  46. if exists (select * from sysobjects where name = 'sp_MScolumn_properties' and sysstat & 7 = 4)
  47. begin
  48.   drop procedure sp_MScolumn_properties
  49. end
  50. go
  51.  
  52. if exists (select * from sysobjects where name = 'sp_MSobjecttype_name' and sysstat & 7 = 4)
  53. begin
  54.   drop procedure sp_MSobjecttype_name
  55. end
  56. go
  57.  
  58. if exists (select * from sysobjects where name = 'sp_MSobject_list' and sysstat & 7 = 4)
  59. begin
  60.   drop procedure sp_MSobject_list
  61. end
  62. go
  63.  
  64. if exists (select * from sysobjects where name = 'sp_MStable_permissions' and sysstat & 7 = 4)
  65. begin
  66.   drop procedure sp_MStable_permissions
  67. end
  68. go
  69.  
  70. if exists (select * from sysobjects where name = 'sp_MSobject_dependencies' and sysstat & 7 = 4)
  71. begin
  72.   drop procedure sp_MSobject_dependencies
  73. end
  74. go
  75.  
  76. if exists (select * from sysobjects where name = 'sp_MSOM_version' and sysstat & 7 = 4)
  77. begin
  78.   drop procedure sp_MSOM_version
  79. end
  80. go
  81.  
  82. /****************************************************************************/
  83. /* This stored procedure returns all relevant user info for current login.  */
  84. /****************************************************************************/
  85.  
  86. print ''
  87. print 'Creating sp_MSuser_info'
  88.  
  89. go
  90.  
  91. create procedure sp_MSuser_info as
  92.    declare @perms int
  93.    select @perms = 0
  94.    set nocount on
  95.  
  96.    /* CREATE DATABASE */
  97.    if exists (select * from sysprotects where protecttype = 205 and action = 203 and uid = user_id())
  98.    begin
  99.       select @perms = @perms | 1
  100.    end
  101.  
  102.    /* CREATE DEFAULT */
  103.    if exists (select * from sysprotects where protecttype = 205 and action = 233 and uid = user_id())
  104.    begin
  105.       select @perms = @perms | 2
  106.    end
  107.  
  108.    /* CREATE PROCEDURE */
  109.    if exists (select * from sysprotects where protecttype = 205 and action = 222 and uid = user_id())
  110.    begin
  111.       select @perms = @perms | 4
  112.    end
  113.  
  114.    /* CREATE RULE */
  115.    if exists (select * from sysprotects where protecttype = 205 and action = 236 and uid = user_id())
  116.    begin
  117.       select @perms = @perms | 8
  118.    end
  119.  
  120.    /* CREATE TABLE */
  121.    if exists (select * from sysprotects where protecttype = 205 and action = 198 and uid = user_id())
  122.    begin
  123.       select @perms = @perms | 16
  124.    end
  125.  
  126.    /* CREATE VIEW */
  127.    if exists (select * from sysprotects where protecttype = 205 and action = 207 and uid = user_id())
  128.    begin
  129.       select @perms = @perms | 32
  130.    end
  131.  
  132.    set nocount off
  133.    select user_name(), user_id(), @perms
  134. go
  135.  
  136. /************* DUMP THE TRANSACTION LOG **************************************/
  137. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  138. /* script periodically, you will run out of transaction log space.           */
  139. dump tran master with truncate_only
  140. go
  141. /************* END DUMP THE TRANSACTION LOG **********************************/
  142.  
  143. /****************************************************************************/
  144. /* This stored procedure is an updated version designed to tell users with  */
  145. /* old .exe's to upgrade to the newer ones.                                 */
  146. /****************************************************************************/
  147.  
  148. print ''
  149. print 'Creating sp_MScheck_OM'
  150.  
  151. go
  152.  
  153. create procedure sp_MScheck_OM as
  154.    raiserror 99999 'You must upgrade your SQL Object Manager executable to work with the scripts on this server.'
  155. go
  156.  
  157. /************* DUMP THE TRANSACTION LOG **************************************/
  158. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  159. /* script periodically, you will run out of transaction log space.           */
  160. dump tran master with truncate_only
  161. go
  162. /************* END DUMP THE TRANSACTION LOG **********************************/
  163.  
  164. /****************************************************************************/
  165. /* This stored procedure will get the table properties for the passed       */
  166. /* table name, according to the type desired in @proptype:                  */
  167. /*    header   = Header information                                         */
  168. /*    key      = Key information                                            */
  169. /*    index    = Index information                                          */
  170. /*    trigger  = Trigger information                                        */
  171. /*    all      = All of the above                                           */
  172. /****************************************************************************/
  173.  
  174. print ''
  175. print 'Creating sp_MStable_properties'
  176.  
  177. go
  178.  
  179. create procedure sp_MStable_properties
  180. @proptype varchar(20) = 'all', @tabname varchar(92) = NULL
  181. as
  182.    declare @my_tabid int
  183.     select @my_tabid = object_id(@tabname)
  184.    if (@my_tabid is null)
  185.    begin
  186.       return (1)
  187.    end
  188.  
  189.    declare @dash char(5)
  190.    select @dash = '  ---'
  191.    declare @sort_ind int
  192.    declare @sort_name varchar(30)
  193.  
  194.    /******************* HEADER INFO ****************/
  195.    if (@proptype = 'header' or @proptype = 'all')
  196.    begin
  197.       create table #tempheader (
  198.      indid int null,
  199.      crdate datetime null,
  200.      uid int null,
  201.      tablesize int null,
  202.      tableres int null,
  203.      tableseg int null,
  204.      numcols int null,
  205.      numrows int null,
  206.      rowlen int null)
  207.  
  208.       declare @t_crdate datetime
  209.       declare @t_uid int
  210.       declare @t_tablesize int
  211.       declare @t_tableres int
  212.       declare @t_numcols int
  213.  
  214.       /* Size multiplier */
  215.       declare @size_mult int
  216.       select @size_mult = low/1024 from master.dbo.spt_values
  217.      where number = 1 and type = 'E'
  218.  
  219.       /* Start things off.  Delete just in case there were two rows. */
  220.       insert #tempheader(indid, tableseg, numrows, rowlen)
  221.      select indid, segment, rows, maxlen
  222.         from sysindexes where id = @my_tabid and indid < 2
  223.       delete #tempheader where indid > (select max(indid) from #tempheader)
  224.  
  225.       select @t_crdate = crdate, @t_uid = uid from sysobjects
  226.         where id = @my_tabid
  227.       update #tempheader set crdate = @t_crdate, uid = @t_uid
  228.  
  229.       select @t_tablesize = sum(used) * @size_mult from sysindexes
  230.         where id = @my_tabid and indid in (0, 1, 255)
  231.       update #tempheader set tablesize = @t_tablesize
  232.  
  233.       select @t_tableres = sum(reserved) * @size_mult from sysindexes
  234.         where id = @my_tabid and indid in (0, 1, 255)
  235.       update #tempheader set tableres = @t_tableres
  236.  
  237.       select @t_numcols = count(*) from syscolumns where id = @my_tabid
  238.       update #tempheader set numcols = @t_numcols
  239.  
  240.       /* output the results */
  241.       select t.crdate, user_name(t.uid), t.tablesize, t.tableres,
  242.          s.name, t.numcols, t.numrows, t.rowlen
  243.      from #tempheader t, syssegments s
  244.      where s.segment = t.tableseg
  245.    end
  246.  
  247.    /******************* KEYS ***********************/
  248.    if (@proptype = 'key' or @proptype = 'all')
  249.    begin
  250.  
  251.       /*
  252.        * Get all keys for this table into a temp table.
  253.        */
  254.       select * into #tempsyskeys 
  255.      from syskeys
  256.         where depid = @my_tabid or id = @my_tabid
  257.       if not exists (select * from #tempsyskeys)
  258.       begin
  259.      return (1)
  260.       end
  261.  
  262.       create table #tempkeys (
  263.      rel_tabid int null,
  264.      keytype int null,
  265.      my_col1 int null,
  266.      my_col2 int null,
  267.      my_col3 int null,
  268.      my_col4 int null,
  269.      my_col5 int null,
  270.      my_col6 int null,
  271.      my_col7 int null,
  272.      my_col8 int null,
  273.      rel_col1 int null,
  274.      rel_col2 int null,
  275.      rel_col3 int null,
  276.      rel_col4 int null,
  277.      rel_col5 int null,
  278.      rel_col6 int null,
  279.      rel_col7 int null,
  280.      rel_col8 int null)
  281.  
  282.       /* 
  283.        * This loads #tempkeys with a my_table ---> related_table combination,
  284.        * which is what sp_helpkey really ought to do.  First get all syskeys
  285.        * rows where this table is the related object.
  286.        */
  287.       insert #tempkeys
  288.      select id, type, 
  289.         depkey1, depkey2, depkey3, depkey4, depkey5, depkey6, depkey7, depkey8,
  290.         key1, key2, key3, key4, key5, key6, key7, key8
  291.         from #tempsyskeys
  292.         where depid = @my_tabid
  293.  
  294.       /* 
  295.        * Now we'll change all foreign keys from the above step to primary
  296.        * keys, because they illustrate all key relationships where this is
  297.        * the primary table.
  298.        */
  299.       update #tempkeys set keytype = 1 where keytype = 2
  300.  
  301.       /* 
  302.        * Now get all syskeys rows where this table is the main object.
  303.        * These will be either common keys or keys where this is the foreign
  304.        * table.
  305.        */
  306.       insert #tempkeys
  307.      select depid, type,
  308.         key1, key2, key3, key4, key5, key6, key7, key8,
  309.         depkey1, depkey2, depkey3, depkey4, depkey5, depkey6, depkey7, depkey8
  310.         from #tempsyskeys
  311.      where id = @my_tabid
  312.  
  313.       /* 
  314.        * If there are rows where a foreign table is shown for this table's
  315.        * primary key, then the row which declares this table's primary key
  316.        * with no foreign key table is redundant -- therefore, remove it.
  317.        */
  318.       if exists (select * from #tempkeys
  319.            where keytype = 1 and rel_tabid is not null)
  320.       begin
  321.      delete #tempkeys where keytype = 1 and rel_tabid is null
  322.       end
  323.  
  324.       /* 
  325.        * Now copy them into the format we want -- each row consists of a
  326.        * matching My_col <--> Rel_col pair, and only the first returned row
  327.        * has Key_type and Related_table; others are @dash.
  328.        *
  329.        * Add 10 to #tempkeys.keytype to flag each row after it's processed.
  330.        * @sort_ind, and @sort_name are to keep columns in order within
  331.        * #tempkeyout.
  332.        */
  333.       create table #tempkeyout (
  334.      keytype int null,
  335.      sort_ind int null,
  336.      rel_tabid int null,
  337.      sort_name varchar(30) null,
  338.      rel_tabname varchar(30) null,
  339.      key_typename varchar(30) null,
  340.      my_col int null,
  341.      rel_col int null)
  342.  
  343.       declare @sort_tabid int
  344.       declare @more_rows int
  345.       select @sort_ind = 1
  346.       select @more_rows = count(*),
  347.          @sort_tabid = min(rel_tabid) from #tempkeys where keytype < 10
  348.       select @sort_name = object_name(@sort_tabid)
  349.  
  350.       while @more_rows > 0
  351.       begin
  352.      insert #tempkeyout
  353.         select keytype, @sort_ind, @sort_tabid, @sort_name, @sort_name, v.name,
  354.            t.my_col1, t.rel_col1
  355.         from #tempkeys t, master.dbo.spt_values v
  356.         where t.rel_tabid = @sort_tabid
  357.            and t.keytype = v.number and v.type = 'K'
  358.      select @sort_ind = @sort_ind + 1
  359.      
  360.      insert #tempkeyout
  361.         select keytype, @sort_ind, @sort_tabid, @sort_name, @dash, @dash, my_col2, rel_col2
  362.         from #tempkeys
  363.         where rel_tabid = @sort_tabid
  364.      select @sort_ind = @sort_ind + 1
  365.  
  366.      insert #tempkeyout
  367.         select keytype, @sort_ind, @sort_tabid, @sort_name, @dash, @dash, my_col3, rel_col3
  368.         from #tempkeys
  369.         where rel_tabid = @sort_tabid
  370.      select @sort_ind = @sort_ind + 1
  371.  
  372.      insert #tempkeyout
  373.         select keytype, @sort_ind, @sort_tabid, @sort_name, @dash, @dash, my_col4, rel_col4
  374.         from #tempkeys
  375.         where rel_tabid = @sort_tabid
  376.      select @sort_ind = @sort_ind + 1
  377.  
  378.      insert #tempkeyout
  379.         select keytype, @sort_ind, @sort_tabid, @sort_name, @dash, @dash, my_col5, rel_col5
  380.         from #tempkeys
  381.         where rel_tabid = @sort_tabid
  382.      select @sort_ind = @sort_ind + 1
  383.  
  384.      insert #tempkeyout
  385.         select keytype, @sort_ind, @sort_tabid, @sort_name, @dash, @dash, my_col6, rel_col6
  386.         from #tempkeys
  387.         where rel_tabid = @sort_tabid
  388.      select @sort_ind = @sort_ind + 1
  389.  
  390.      insert #tempkeyout
  391.         select keytype, @sort_ind, @sort_tabid, @sort_name, @dash, @dash, my_col7, rel_col7
  392.         from #tempkeys
  393.         where rel_tabid = @sort_tabid
  394.      select @sort_ind = @sort_ind + 1
  395.  
  396.      insert #tempkeyout
  397.         select keytype, @sort_ind, @sort_tabid, @sort_name, @dash, @dash, my_col8, rel_col8
  398.         from #tempkeys
  399.         where rel_tabid = @sort_tabid
  400.      select @sort_ind = @sort_ind + 1
  401.  
  402.      /* Flag this row as processed in #tempkeys and set up for the next. */
  403.      update #tempkeys set keytype = keytype + 10 where rel_tabid = @sort_tabid
  404.      select @sort_ind = 1
  405.      select @more_rows = count(*),
  406.         @sort_tabid = min(rel_tabid) from #tempkeys where keytype < 10
  407.      select @sort_name = object_name(@sort_tabid)
  408.       end
  409.  
  410.       /* Now output it all. */
  411.       select Key_type = key_typename,
  412.          My_Col = col_name(@my_tabid, my_col),
  413.          Related_Table = isnull(rel_tabname, @dash),
  414.          Rel_Col = isnull(col_name(rel_tabid, rel_col), @dash)
  415.      from #tempkeyout
  416.      where my_col is not null
  417.      order by keytype, sort_name, sort_ind
  418.    end
  419.  
  420.    /******************* INDEXES ********************/
  421.    if (@proptype = 'index' or @proptype = 'all')
  422.    begin
  423.       select i.name, clstr = i.status & 16, uniq = i.status & 2, igduprow = i.status & 4,
  424.          alduprow = i.status & 64, igdupkey = i.status & 1, seg = g.name,
  425.          index_col(@tabname, i.indid, 1), index_col(@tabname, i.indid, 2),
  426.          index_col(@tabname, i.indid, 3), index_col(@tabname, i.indid, 4),
  427.          index_col(@tabname, i.indid, 5), index_col(@tabname, i.indid, 6),
  428.          index_col(@tabname, i.indid, 7), index_col(@tabname, i.indid, 8),
  429.          index_col(@tabname, i.indid, 9), index_col(@tabname, i.indid, 10),
  430.          index_col(@tabname, i.indid, 11), index_col(@tabname, i.indid, 12),
  431.          index_col(@tabname, i.indid, 13), index_col(@tabname, i.indid, 14),
  432.          index_col(@tabname, i.indid, 15), index_col(@tabname, i.indid, 16)
  433.       from sysindexes i, syssegments g where id = object_id(@tabname) and g.segment = i.segment
  434.          and i.indid > 0 and i.indid < 255
  435.       order by clstr DESC, i.name
  436.    end
  437.  
  438.    /******************* TRIGGERS *******************/
  439.    if (@proptype = 'trigger' or @proptype = 'all')
  440.    begin
  441.       /* Create a temp table with this table's triggers. */
  442.       create table #temptrig (
  443.      trig_id int null,
  444.      isinsert int null,
  445.      isupdate int null,
  446.      isdelete int null)
  447.       
  448.       declare @ins_id int, @upd_id int, @del_id int
  449.       select @ins_id = instrig, @upd_id = updtrig, @del_id = deltrig
  450.      from sysobjects where id = @my_tabid
  451.  
  452.       /* INSERT */
  453.       insert #temptrig values (@ins_id, 1, 0, 0)
  454.  
  455.       /* UPDATE */
  456.       if exists (select trig_id from #temptrig where trig_id = @upd_id)
  457.       begin
  458.      update #temptrig set isupdate = 1 where trig_id = @upd_id
  459.       end
  460.       else
  461.       begin
  462.      insert #temptrig values (@upd_id, 0, 1, 0)
  463.       end
  464.  
  465.       /* DELETE */
  466.       if exists (select trig_id from #temptrig where trig_id = @del_id)
  467.       begin
  468.      update #temptrig set isdelete = 1 where trig_id = @del_id
  469.       end
  470.       else
  471.       begin
  472.      insert #temptrig values (@del_id, 0, 0, 1)
  473.       end
  474.  
  475.       select Name = object_name(trig_id), isinsert, isupdate, isdelete
  476.      from #temptrig where trig_id is not null and trig_id > 0
  477.      order by Name
  478.    end
  479.  
  480. go
  481.  
  482. /************* DUMP THE TRANSACTION LOG **************************************/
  483. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  484. /* script periodically, you will run out of transaction log space.           */
  485. dump tran master with truncate_only
  486. go
  487. /************* END DUMP THE TRANSACTION LOG **********************************/
  488.  
  489. /****************************************************************************/
  490. /* This stored procedure will get the column properties for the passed      */
  491. /* table name.  It is a close subset copy from sp_help.                     */
  492. /****************************************************************************/
  493.  
  494. print ''
  495. print 'Creating sp_MScolumn_properties'
  496.  
  497. go
  498.  
  499. create procedure sp_MScolumn_properties
  500. @object_name varchar(92)
  501. as
  502.     create table #sphelptab
  503.     (
  504.         col_name        char (30),
  505.       col_id   tinyint,
  506.         col_type        char (30),      
  507.         col_len         tinyint,        
  508.         col_status      tinyint,
  509.         col_def         char (30)  null,
  510.         col_dom         char (30)  null,
  511.       usr_type    smallint,
  512.       var_len     bit,
  513.       def_own     char (30) null,
  514.       dom_own     char (30) null
  515.     )
  516.  
  517.     insert into #sphelptab
  518.         select c.name, c.colid, t.name, c.length, c.status,
  519.         def.name, dom.name,
  520.        t.usertype, 0, user_name(def.uid), user_name(dom.uid)
  521.         from syscolumns c, systypes t, sysobjects def, sysobjects dom
  522.         where c.id = object_id(@object_name)
  523.             and c.usertype *= t.usertype
  524.      and def.id =* c.cdefault and dom.id =* c.domain
  525.     and (def.category & 0x0800 = 0)
  526.  
  527.     update #sphelptab
  528.         set col_type = 'money'
  529.         where col_type = 'moneyn'
  530.  
  531.     update #sphelptab
  532.         set col_type = 'float'
  533.         where col_type = 'floatn'
  534.  
  535.     update #sphelptab
  536.         set col_type = 'int'
  537.         where col_type = 'intn'
  538.  
  539.     update #sphelptab
  540.         set col_type = 'datetime'
  541.         where col_type = 'datetimn'
  542.  
  543.    /* These types should match spt_datatype_info_ext. */
  544.    update #sphelptab
  545.       set var_len = 1
  546.       where usr_type in (1,2,3,4,18)
  547.  
  548.     select t.col_name, t.col_type, t.col_len, Nulls = convert(bit, (t.col_status & 8)),
  549.            t.col_def, t.col_dom, t.var_len, t.def_own, t.dom_own, UDTOwner = user_name(n.uid)
  550.    from #sphelptab t, systypes n
  551.       where t.col_type *= n.name and n.usertype > 99
  552.    order by t.col_id
  553. go
  554.  
  555. /************* DUMP THE TRANSACTION LOG **************************************/
  556. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  557. /* script periodically, you will run out of transaction log space.           */
  558. dump tran master with truncate_only
  559. go
  560. /************* END DUMP THE TRANSACTION LOG **********************************/
  561.  
  562. /****************************************************************************/
  563. /* This stored procedure will get the name of the passed object type_id.    */
  564. /****************************************************************************/
  565.  
  566. print ''
  567. print 'Creating sp_MSobjecttype_name'
  568.  
  569. go
  570.  
  571. create procedure sp_MSobjecttype_name
  572. @type_id int
  573. as
  574.    if @type_id = -1
  575.    begin
  576.       /*
  577.        * Get all object type names.  Use a temp table because spt_values
  578.        * doesn't have a type 5 and we need to keep this in order.
  579.        */
  580.       create table #tempType (
  581.      number int,
  582.      name char(30)
  583.       )
  584.       insert #tempType
  585.      select number & 7, name
  586.      from master.dbo.spt_values
  587.      where type = 'O' and number >= 0 and number < 9
  588.  
  589.       /*
  590.        * Insert the mysterious 'logs'; this string could be internationalized
  591.        * but is never used anywhere (yet); it is just a placeholder.
  592.        */
  593.       if not exists (select number from #tempType where number = 5)
  594.       begin
  595.      insert #tempType values (5, 'Not Found')
  596.       end
  597.  
  598.       /*
  599.        * Insert datatypes as these are not in sysobjects.
  600.        * This string should be internationalized.
  601.        */
  602.       if not exists (select number from #tempType where number = 8)
  603.       begin
  604.      insert #tempType values (8, 'user data type')
  605.       end
  606.  
  607.       /* Final select to output results */
  608.       select name from #tempType order by number
  609.    end
  610.  
  611.    else
  612.    begin
  613.       /* Get a specific object type name */
  614.       select name from master.dbo.spt_values
  615.      where type = 'O' and number = @type_id
  616.    end
  617. go
  618.  
  619. /************* DUMP THE TRANSACTION LOG **************************************/
  620. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  621. /* script periodically, you will run out of transaction log space.           */
  622. dump tran master with truncate_only
  623. go
  624. /************* END DUMP THE TRANSACTION LOG **********************************/
  625.  
  626. /****************************************************************************/
  627. /* This stored procedure will get the name, type, owner, and creation date  */
  628. /* for all objects in the current database.                                 */
  629. /****************************************************************************/
  630.  
  631. print ''
  632. print 'Creating sp_MSobject_list'
  633.  
  634. go
  635.  
  636. create procedure sp_MSobject_list @flags int = 0xffff
  637. as
  638.    /*
  639.     * @flags is a bitmask of power(2, object type number(s)) to return
  640.     * in results set:
  641.     *    0 (1     - 0x0001)    - trigger
  642.     *    1 (2     - 0x0002)    - system table / system objects
  643.     *    2 (4     - 0x0004)    - view
  644.     *    3 (8     - 0x0008)    - user table
  645.     *    4 (16    - 0x0010)    - procedure
  646.     *    5 (32    - 0x0020)    - log
  647.     *    6 (64    - 0x0040)    - default
  648.     *    7 (128   - 0x0080)    - rule
  649.     *    8 (256   - 0x0100)    - datatype
  650.     * If system objects not specified and we're in master, eliminate
  651.     * internal system objects.
  652.     */
  653.     select Name = o.name, Type = v.name, Owner = user_name(o.uid), Created = o.crdate
  654.    from sysobjects o, master.dbo.spt_values v
  655.    where (power(2, o.sysstat & 7) & @flags != 0)
  656.       and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  657.       and o.sysstat & 7 = v.number
  658.       and v.type = 'O'
  659.       and ((db_id() != 1 or @flags & 2 != 0) or not
  660.          (((o.name = 'helpsql' or o.name like 'MS%'
  661.             or o.name like 'spt[_]%') and o.sysstat & 7 = 3)
  662.           or ((o.name like 'sp[_]%' or o.name like 'xp[_]%'
  663.             or o.name like 'MS[_]%') and o.sysstat & 7 = 4)))
  664.    order by Type, Name
  665. go
  666.  
  667. /************* DUMP THE TRANSACTION LOG **************************************/
  668. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  669. /* script periodically, you will run out of transaction log space.           */
  670. dump tran master with truncate_only
  671. go
  672. /************* END DUMP THE TRANSACTION LOG **********************************/
  673.  
  674. /*****************************************************************************/
  675. /* This stored procedure gets all the users that have particular permissions */
  676. /*****************************************************************************/
  677.  
  678. print ''
  679. print 'Creating sp_MStable_permissions'
  680.  
  681. go
  682.  
  683. create procedure sp_MStable_permissions
  684. @objname varchar(92),
  685. @permit int,
  686. @insertinto bit,
  687. @deletefrom bit,
  688. @executeit bit,
  689. @selcolarray varbinary(32),
  690. @updcolarray varbinary(32)
  691. as
  692.  
  693. set nocount on
  694.  
  695. create table #tmp_permiss
  696. (userid int,
  697.  ins     bit,
  698.  del     bit,
  699.  execit  bit,
  700.  selectcols varbinary(32) null,
  701.  updatecols varbinary(32) null
  702. )
  703.  
  704. declare @objid int
  705. select @objid = object_id(@objname)
  706. declare @granttype int
  707. select @granttype = number from master.dbo.spt_values where name = 'Grant'
  708.  
  709. /*
  710.  * @permit values:
  711.  *    0 = Revoke  (all users with the specified privileges revoked for @objname)
  712.  *    1 = Grant   (all users with the specified privileges granted for @objname)
  713.  *    2 = None    (all users with no privileges granted for @objname)
  714.  */
  715. if @permit = 2
  716. begin
  717.    select distinct x = t.name, user_name(u.gid) from sysusers t, sysusers u
  718.       where t.uid != 1 and t.uid not in
  719.          (select uid from sysprotects where protecttype = @granttype
  720.          and id = @objid)
  721.       and t.uid = u.uid
  722.       order by x
  723. end
  724. else
  725. begin
  726.    declare @permtype int
  727.    if @permit = 1
  728.        select @permtype = @granttype
  729.    else
  730.        select @permtype = number from master.dbo.spt_values where name = 'Revoke'
  731.    
  732.    insert into #tmp_permiss
  733.       select distinct uid,0,0,0,NULL,NULL from sysprotects
  734.      where id = @objid and protecttype = @permtype
  735.    
  736.    update #tmp_permiss set del = 1 from sysprotects where
  737.    userid = uid and protecttype = @permtype and id = @objid
  738.    and action = (select number from master.dbo.spt_values where name = 'Delete')
  739.    
  740.    update #tmp_permiss set ins = 1 from sysprotects where
  741.    userid = uid and protecttype = @permtype and id = @objid
  742.    and action = (select number from master.dbo.spt_values where name = 'Insert')
  743.    
  744.    update #tmp_permiss set execit = 1 from sysprotects where
  745.    userid = uid and protecttype = @permtype and id = @objid
  746.    and action = (select number from master.dbo.spt_values where name = 'Execute')
  747.    
  748.    update #tmp_permiss set updatecols = columns from sysprotects where
  749.    userid = uid and protecttype = @permtype and id = @objid
  750.    and action = (select number from master.dbo.spt_values where name = 'Update')
  751.    and columns = @updcolarray
  752.    
  753.    update #tmp_permiss set selectcols = columns from sysprotects where
  754.    userid = uid and protecttype = @permtype and id = @objid
  755.    and action = (select number from master.dbo.spt_values where name = 'Select')
  756.    and columns = @selcolarray
  757.    
  758.    set nocount off
  759.    
  760.    select x = name, user_name(gid) from #tmp_permiss, sysusers
  761.       where del = @deletefrom and ins = @insertinto and execit= @executeit
  762.       and selectcols=@selcolarray and updatecols=@updcolarray
  763.       and userid = uid
  764.       order by x
  765. end
  766. go
  767.  
  768. /************* DUMP THE TRANSACTION LOG **************************************/
  769. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  770. /* script periodically, you will run out of transaction log space.           */
  771. dump tran master with truncate_only
  772. go
  773. /************* END DUMP THE TRANSACTION LOG **********************************/
  774.  
  775. /*****************************************************************************/
  776. /* This procedure takes a single object, an object type, or a request        */
  777. /* for all objects and returns the ordered hierarchy of parent objects       */
  778. /* (objects which must be created first) or child objects (objects which     */
  779. /* rely on this object being created first).  It iteratively evaluates       */
  780. /* the dependencies of every object it finds, so that every object it        */
  781. /* returns has its entire dependency tree included in the return set.        */
  782. /*****************************************************************************/
  783.  
  784. print ''
  785. print 'Creating sp_MSobject_dependencies'
  786.  
  787. go
  788.  
  789. create procedure sp_MSobject_dependencies
  790. @objname varchar(92) = null, @objtype int = null, @flags int = 0x01fd
  791. as
  792.    if (@objname = '?')
  793.    begin
  794.       print 'sp_MSobject_dependencies name = NULL, type = NULL, flags = 0x01fd'
  795.       print '  name:  name or null (all objects of type)'
  796.       print '  type:  type number (see below) or null'
  797.       print '     if both null, get all objects in database'
  798.       print '  flags is a bitmask of the following values:'
  799.       print '     0x1000  = return multiple parent/child rows per object'
  800.       print '     0x2000  = descending return order'
  801.       print '     0x4000  = return children instead of parents'
  802.       print '     0x8000  = do not return input object'
  803.       print '     power(2, object type number(s))  to return in results set:'
  804.       print '        0 (1     - 0x0001)    - trigger'
  805.       print '        1 (2     - 0x0002)    - system table / system objects'
  806.       print '        2 (4     - 0x0004)    - view'
  807.       print '        3 (8     - 0x0008)    - user table'
  808.       print '        4 (16    - 0x0010)    - procedure'
  809.       print '        5 (32    - 0x0020)    - log'
  810.       print '        6 (64    - 0x0040)    - default'
  811.       print '        7 (128   - 0x0080)    - rule'
  812.       print '        8 (256   - 0x0100)    - datatype'
  813.       print '     shortcuts:'
  814.       print '        29    (0x001d) - trig, view, user table, procedure'
  815.       print '        448   (0x01c0) - rule, default, datatype'
  816.       print '        511   (0x01ff) - all'
  817.       return 0
  818.    end
  819.  
  820.    if (@objtype in (5, 6, 7, 8))
  821.    begin
  822.       print 'Rules, defaults, and datatypes do not have dependencies.'
  823.       return (1)
  824.    end
  825.  
  826.    /*
  827.     * Create #t1 and #t2 as temp object holding areas.  Columns are:
  828.     *    tid      - temp object id
  829.     *    ttype    - temp object type
  830.     *    pid      - parent or child object id
  831.     *    ptype    - parent or child object type
  832.     *    bDone    - NULL means dependencies not yet evaluated, else nonNULL.
  833.     */
  834.    declare @curid int
  835.    declare @allobjs int
  836.    declare @delinputobj int
  837.    select @allobjs = 0, @delinputobj = 0, @curid = NULL
  838.    create table #t1 (tid int NULL, ttype smallint NULL, pid int NULL, ptype smallint NULL, bDone smallint NULL)
  839.    create table #t2 (tid int NULL, ttype smallint NULL, pid int NULL, ptype smallint NULL, bDone smallint NULL)
  840.    create table #tempudt (dtype int)
  841.  
  842.    /*
  843.     * If both name and type are null, this means get every object in the
  844.     * database matching the specification they passed in.  Otherwise,
  845.     * find the passed object or all objects of the passed type.  Start off
  846.     * loading parent info (pid, tid); these will be put into child as needed.
  847.     */
  848.    if (@objname is null and @objtype is null)
  849.    begin
  850.       set nocount on
  851.       select @allobjs = 1
  852.       insert #t1 (pid, ptype) select o.id, o.sysstat & 7 from sysobjects o
  853.          where o.sysstat & 7 not in (6, 7)
  854.          and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  855.    end else begin
  856.       if (@objname is not null)
  857.       begin
  858.          select @curid = object_id(@objname)
  859.          if (@curid is null)
  860.             return (1)
  861.          if (@flags & 0x8000 != 0)
  862.             select @delinputobj = @curid
  863.          select @objtype = o.sysstat & 7 from sysobjects o where id = @curid
  864.       end
  865.  
  866.       set nocount on
  867.       if (@curid is null)
  868.          insert #t1 (pid, ptype) select o.id, o.sysstat & 7 from sysobjects o
  869.             where o.sysstat & 7 = @objtype
  870.             and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  871.       else
  872.          insert #t1 (pid, ptype) values (@curid, @objtype)
  873.    end
  874.  
  875.    /* 
  876.     * All initial objects are loaded as parents/children.  Now we loop, creating
  877.     * rows of child/parent relationships.  Use #t2 as a temp area for the selects
  878.     * to simulate recursion; when they find no rows, we're done with this step.
  879.     *
  880.     * Note that triggers are weird; they're part of a table definition but can
  881.     * also reference other tables, so we need to evaluate them both ways.  SQL
  882.     * Server stores the table for a trigger object as its deltrig; if a trigger
  883.     * references another table, that relationship is stored in sysdepends.
  884.     * This peculiarity of triggers requires separating the object-retrieval pass
  885.     * from the creation-sequence pass (below).
  886.     */
  887.    while (select count(*) from #t1 where bDone is null) > 0
  888.    begin
  889.       /* 
  890.        * Remove Microsoft-specific or other system objects from #t1, unless
  891.        * @flags specified including system tables.  We do this here so that
  892.        * cascaded system dependencies are not included unless specifically
  893.        * requested.  For other restrictions, we wait until below so that all
  894.        * cascaded object types are fully evaluated.
  895.        */
  896.       if (@flags & power(2, 1) = 0)
  897.          delete #t1 where ttype = 1 or (db_id() = 1 and
  898.             ((object_name(tid) like 'MS%' and ttype = 3)
  899.             or (object_name(tid) = 'helpsql' and ttype = 3)
  900.             or (object_name(tid) like 'spt[_]%' and ttype = 3)
  901.             or (object_name(tid) like 'sp[_]%' and ttype = 4)
  902.             or (object_name(tid) like 'xp[_]%' and ttype = 4)
  903.             or (object_name(tid) like 'MS[_]%' and ttype = 4)
  904.  
  905.             or (object_name(pid) like 'MS%' and ptype = 3)
  906.             or (object_name(pid) = 'helpsql' and ttype = 3)
  907.             or (object_name(pid) like 'spt[_]%' and ptype = 3)
  908.             or (object_name(pid) like 'sp[_]%' and ptype = 4)
  909.             or (object_name(pid) like 'xp[_]%' and ptype = 4)
  910.             or (object_name(pid) like 'MS[_]%' and ptype = 4)
  911.             ))
  912.  
  913.       /* Table --> Triggers */
  914.       if (@flags & power(2, 0) != 0)
  915.          insert #t2 (tid, ttype, pid, ptype)
  916.             select distinct t.pid, t.ptype, o.id, 0 from #t1 t, sysobjects o
  917.                where t.bDone is null and t.ptype = 3 and o.deltrig = t.pid
  918.  
  919.       if (@flags & 0x4000 != 0)
  920.       begin
  921.          /* Object --> sysdepends children */
  922.          insert #t2 (tid, ttype, pid, ptype)
  923.             select distinct t.pid, t.ptype, d.id, o.sysstat & 7
  924.             from #t1 t, sysdepends d, sysobjects o
  925.             where t.bDone is null and d.depid = t.pid and d.id = o.id
  926.       end else begin
  927.          /* Trigger --> Table */
  928.          if (@flags & power(2, 3) != 0)
  929.             insert #t2 (tid, ttype, pid, ptype)
  930.                select distinct t.pid, t.ptype, o.deltrig, 3 from #t1 t, sysobjects o
  931.                   where t.bDone is null and t.ptype = 0 and o.id = t.pid and o.deltrig != 0
  932.  
  933.          /* Object --> sysdepends parents */
  934.          insert #t2 (tid, ttype, pid, ptype)
  935.             select distinct t.pid, t.ptype, d.depid, o.sysstat & 7
  936.             from #t1 t, sysdepends d, sysobjects o
  937.             where t.bDone is null and d.id = t.pid and d.depid = o.id
  938.       end
  939.  
  940.       /*
  941.        * We have this generation of parents in #t2, so clear the current
  942.        * child generation's bDone flags.  Then insert from #t2; the current
  943.        * parent generation becomes the next loop's child generation, with
  944.        * bDone = null until next loop's dependencies are selected.
  945.        */
  946.       update #t1 set bDone = 1
  947.       insert #t1 select * from #t2 where #t2.tid not in
  948.          (select tid from #t1 where #t1.tid = #t2.tid and #t1.pid = #t2.pid)
  949.       truncate table #t2
  950.    end
  951.  
  952.    /*
  953.     * Because triggers can go in both directions, we'll need to check for
  954.     * circular dependencies on parent evaluation.  Since any tables referenced
  955.     * by the trigger must exist before the trigger can be created, remove rows
  956.     * where the trigger is the parent.
  957.     */
  958.    if (@flags & 0x4000 = 0)
  959.       delete #t1 where ptype = 0
  960.  
  961.    /* 
  962.     * The inner loop above did not put parents with no parents into the
  963.     * child (tid) list.  Do that now, then remove all rows where tid is
  964.     * NULL, because these were initial objects which now have a tid row.
  965.     * Just in case, remove self-refs from #t1, and also remove rows from #t1
  966.     * with NULL pid if a row exists for that tid where the pid is nonNULL.
  967.     */
  968.    insert #t1 (tid, ttype) select distinct pid, ptype from #t1 t 
  969.       /* where t.pid not in (select tid from #t1) */
  970.       where not exists (select tid from #t1 where tid = t.pid)
  971.    delete from #t1 where tid is null or tid = pid or (pid is null and tid in
  972.       (select tid from #t1 where pid is not null))
  973.  
  974.    /*
  975.     * Now get datatypes, which aren't in sysobjects.  Get any types that
  976.     * are referenced by objects in #t1.  We don't care about specific
  977.     * datatype dependencies, we just want to know which ones are needed.
  978.     */
  979.    if (@flags & power(2, 8) != 0)
  980.       insert #tempudt select distinct usertype from syscolumns
  981.          where usertype > 99 and id in (select tid from #t1)
  982.  
  983.    /*
  984.     * Load rules and defaults needed by datatypes and other #t1 objects
  985.     * into #t2.  Don't track specific object dependencies with these;
  986.     * we just want to know which ones are needed.
  987.     */
  988.    if (@flags & power(2, 7) != 0)
  989.    begin
  990.       insert #t2 (tid, ttype)
  991.          select distinct s.domain, 7 from systypes s, #tempudt t
  992.             where s.domain != 0 and s.usertype = t.dtype
  993.                and s.domain not in (select tid from #t1)
  994.       insert #t2 (tid, ttype)
  995.          select distinct s.domain, 7 from syscolumns s, #t1 t
  996.             where s.domain != 0 and s.id = t.tid
  997.                and s.domain not in (select tid from #t1)
  998.    end
  999.    if (@flags & power(2, 6) != 0)
  1000.    begin
  1001.       insert #t2 (tid, ttype)
  1002.          select distinct s.tdefault, 6 from systypes s, #tempudt t
  1003.             where s.tdefault != 0 and s.usertype = t.dtype
  1004.                and s.tdefault not in (select tid from #t1)
  1005.       insert #t2 (tid, ttype)
  1006.          select distinct s.cdefault, 6 from syscolumns s, #t1 t
  1007.             where s.cdefault != 0 and s.id = t.tid
  1008.                and s.cdefault not in (select tid from #t1)
  1009.    end
  1010.  
  1011.    /*
  1012.     * Now that we've got all objects we want, eliminate those we don't
  1013.     * want to return.  If @inputobj and they don't want it returned,
  1014.     * remove it from the table.  Then eliminate object types they don't
  1015.     * want returned.  Make sure that in doing so we retain all parent
  1016.     * objects of the types we do want -- it is possible at this point
  1017.     * that a tid we want has no rows except those with pids we don't want.
  1018.     */
  1019.    if (@flags & 0x01ff != 0x01ff or @delinputobj != 0)
  1020.    begin
  1021.       delete #t1 where @flags & power(2, ttype) = 0 or tid = @delinputobj
  1022.       insert #t1 (tid, ttype) select distinct tid, ttype from #t1
  1023.          where (@flags & power(2, ptype) = 0 or pid = @delinputobj)
  1024.             and tid not in (select tid from #t1 where ptype is null or
  1025.                @flags & power(2, ptype) != 0)
  1026.       delete #t1 where @flags & power(2, ptype) = 0 or pid = @delinputobj
  1027.    end
  1028.  
  1029.    /* 
  1030.     * To determine creation order, find all objects which are not yet bDone
  1031.     * and have no parents or whose parents are all bDone, and set their bDone
  1032.     * to the next @curid.  This will leave bDone as the ascending order in
  1033.     * which objects must be created (topological sort).
  1034.     */
  1035.    update #t1 set bDone = 0
  1036.    select @curid = 1
  1037.    while (select count(*) from #t1 where bDone = 0) > 0
  1038.    begin
  1039.       if (@flags & 0x4000 != 0)
  1040.          update #t1 set bDone = @curid from #t1 tx where bDone = 0
  1041.             and not exists
  1042.                (select * from #t1 t2 where bDone = 0 and tx.tid = t2.pid)
  1043.       else
  1044.          update #t1 set bDone = @curid from #t1 tx where bDone = 0
  1045.              and not exists 
  1046.                (select * from #t1 t2 where bDone = 0 and tx.pid = t2.tid)
  1047.       select @curid = @curid + 1
  1048.    end
  1049.  
  1050.    /* 
  1051.     * Finally, return the objects.  Rules/Defaults must be created first
  1052.     * so they're returned first (with a dummy bDone), followed by Datatypes,
  1053.     * followed by all other (sysdepends) dependencies.
  1054.     *
  1055.     * NOTE:  'user data type' should be internationalized to match the
  1056.     * string in sp_MSobjecttype_names.
  1057.     */
  1058.    if (@flags & (power(2, 7) | power(2, 6)) != 0)
  1059.       select distinct o.sysstat & 7, o.name, user_name(o.uid), 0
  1060.          from sysobjects o, #t2 t
  1061.          where o.id = t.tid
  1062.          and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  1063.          order by o.sysstat & 7, o.name
  1064.    if (@flags & power(2, 8) != 0)
  1065.       select distinct 8, c.name, user_name(c.uid), 0
  1066.          from systypes c, #tempudt t 
  1067.          where c.usertype = t.dtype
  1068.          order by c.name
  1069.  
  1070.    /*
  1071.     * Select dependency-style objects, returning parents if desired.
  1072.     * @curid will control sort direction.
  1073.     */
  1074.    /* select @curid = -((@flags & 0x2000) - 1) */
  1075.    if (@flags & 0x1000 != 0)
  1076.    begin
  1077.       if (@flags & 0x02000 != 0)
  1078.       begin
  1079.          select distinct o.sysstat & 7, o.name, user_name(o.uid), p.sysstat & 7, p.name, user_name(p.uid), t.bDone
  1080.             from sysobjects o, sysobjects p, #t1 t
  1081.             where o.id = t.tid and p.id =* t.pid
  1082.             and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  1083.             order by t.bDone DESC, o.sysstat & 7, o.name
  1084.       end else begin
  1085.          select distinct o.sysstat & 7, o.name, user_name(o.uid), p.sysstat & 7, p.name, user_name(p.uid), t.bDone
  1086.             from sysobjects o, sysobjects p, #t1 t
  1087.             where o.id = t.tid and p.id =* t.pid
  1088.             and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  1089.             order by t.bDone, o.sysstat & 7, o.name
  1090.       end
  1091.    end else begin
  1092.       if (@flags & 0x02000 != 0)
  1093.       begin
  1094.          select distinct o.sysstat & 7, o.name, user_name(o.uid), t.bDone
  1095.             from sysobjects o, sysobjects p, #t1 t
  1096.             where o.id = t.tid
  1097.             and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  1098.             order by t.bDone DESC, o.sysstat & 7, o.name
  1099.       end else begin
  1100.          select distinct o.sysstat & 7, o.name, user_name(o.uid), t.bDone
  1101.             from sysobjects o, sysobjects p, #t1 t
  1102.             where o.id = t.tid
  1103.             and o.sysstat & 0xf < 9 and (o.sysstat != 6 or o.category & 0x0800 = 0)
  1104.             order by t.bDone, o.sysstat & 7, o.name
  1105.       end
  1106.    end
  1107. go
  1108.  
  1109. /************* DUMP THE TRANSACTION LOG **************************************/
  1110. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1111. /* script periodically, you will run out of transaction log space.           */
  1112. dump tran master with truncate_only
  1113. go
  1114. /************* END DUMP THE TRANSACTION LOG **********************************/
  1115.  
  1116. /************* SQL OM VERSION CONTROL ****************************************/
  1117. /*    4.20.xx.yy where yy is script version.                                 */
  1118. /*****************************************************************************/
  1119.  
  1120. create procedure sp_MSOM_version as
  1121.    select 'Microsoft SQL Object Manager script version 6.00.00.1'
  1122. go
  1123.  
  1124. /* Set the 'MS-installed object' category bit.  Must be done AFTER all objects created. */
  1125. print ''
  1126. print 'Reconfiguring to set the category bit'
  1127. print ''
  1128. go
  1129. sp_configure 'allow updates',1
  1130. go
  1131. reconfigure with override
  1132. go
  1133.  
  1134. update master..sysobjects set category = category | 0x02 where sysstat & 7 = 4 and name in (
  1135.    'sp_MScheck_OM',
  1136.    'sp_MSuser_info',
  1137.    'sp_MStable_properties',
  1138.    'sp_MScolumn_properties',
  1139.    'sp_MSobject_list',
  1140.    'sp_MSobjecttype_name',
  1141.    'sp_MStable_permissions',
  1142.    'sp_MSobject_dependencies',
  1143.    'sp_MSOM_version')
  1144. go
  1145.  
  1146. sp_configure 'allow updates',0
  1147. go
  1148. reconfigure with override
  1149. go
  1150.  
  1151. set nocount on
  1152. create table #spmissing
  1153. (name varchar(30),
  1154.  type varchar(10)
  1155. )
  1156. go
  1157.  
  1158. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_info' and sysstat & 7 = 4)
  1159. begin
  1160.   insert into #spmissing values('sp_MSuser_info', 'procedure')
  1161. end
  1162.  
  1163. if not exists (select * from master.dbo.sysobjects where name = 'sp_MStable_properties' and sysstat & 7 = 4)
  1164. begin
  1165.   insert into #spmissing values('sp_MStable_properties', 'procedure')
  1166. end
  1167.  
  1168. if not exists (select * from master.dbo.sysobjects where name = 'sp_MScolumn_properties' and sysstat & 7 = 4)
  1169. begin
  1170.   insert into #spmissing values('sp_MScolumn_properties', 'procedure')
  1171. end
  1172.  
  1173. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSobject_list' and sysstat & 7 = 4)
  1174. begin
  1175.   insert into #spmissing values('sp_MSobject_list', 'procedure')
  1176. end
  1177.  
  1178. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSobjecttype_name' and sysstat & 7 = 4)
  1179. begin
  1180.   insert into #spmissing values('sp_MSobjecttype_name', 'procedure')
  1181. end
  1182.  
  1183. if not exists (select * from master.dbo.sysobjects where name = 'sp_MStable_permissions' and sysstat & 7 = 4)
  1184. begin
  1185.   insert into #spmissing values('sp_MStable_permissions', 'procedure')
  1186. end
  1187.  
  1188. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSobject_dependencies' and sysstat & 7 = 4)
  1189. begin
  1190.   insert into #spmissing values('sp_MSobject_dependencies', 'procedure')
  1191. end
  1192. go
  1193.  
  1194. if exists (select * from #spmissing)
  1195. begin
  1196.    print ''
  1197.    print ''
  1198.    print ' =====================  ERRORS!  ===================='
  1199.    print '       The following objects were not created.'
  1200.    print ' Sql Object Manager will not run against this server.'
  1201.    print ''
  1202.    select * from #spmissing
  1203.    drop procedure sp_MSOM_version
  1204. end
  1205. else
  1206. begin
  1207.    print ''
  1208.    print 'Granting execute permissions on procedures'
  1209.  
  1210.    grant execute on sp_MScheck_OM to public
  1211.    grant execute on sp_MSuser_info to public
  1212.    grant execute on sp_MStable_properties to public
  1213.    grant execute on sp_MScolumn_properties to public
  1214.    grant execute on sp_MSobject_list to public
  1215.    grant execute on sp_MSobjecttype_name to public
  1216.    grant execute on sp_MStable_permissions to public
  1217.    grant execute on sp_MSobject_dependencies to public
  1218.    grant execute on sp_MSOM_version to public
  1219.  
  1220.    print ''
  1221.    print ''
  1222.    print ' Successful installation.'
  1223.    exec sp_MSOM_version
  1224. end
  1225. go
  1226. drop table #spmissing
  1227. go
  1228. set nocount off
  1229. go
  1230.  
  1231. /************* DUMP THE TRANSACTION LOG **************************************/
  1232. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1233. /* script periodically, you will run out of transaction log space.           */
  1234. dump tran master with truncate_only
  1235. go
  1236. /************* END DUMP THE TRANSACTION LOG **********************************/
  1237.  
  1238. checkpoint
  1239. go
  1240.