home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a061 / 8.img / INSTCAT.SQ@ / INSTCAT.bin
Encoding:
Text File  |  1992-02-26  |  59.7 KB  |  1,907 lines

  1. /* 
  2. NOTE:  you MUST change the last row inserted into MSserver_info
  3. to be version number of this file.  the convention is VV.vvvv, where
  4. VV is major version number ("00" until we ship), and vvvv is minor
  5. version number in the form of month and day (mmdd) of the date you 
  6. check in this file.  add 12 to the month to keep in sync with the
  7. driver version numbers.  e.g. checking in on feb 5 would mean setting
  8. the value to 00.1405.
  9. */
  10.  
  11. /****************************************************************************/
  12. /* This portion sets up the ability to perform all the functions in this    */
  13. /* script                                                                    */
  14. /****************************************************************************/
  15. use master
  16. go
  17. dump tran master with truncate_only
  18. go
  19.  
  20. if exists (select * from sysobjects
  21.            where name = 'sp_configure' and sysstat & 7 = 4)
  22. begin
  23.     execute sp_configure 'update',1
  24. end
  25. reconfigure with override
  26. go
  27. if (exists (select * from sysobjects where name = 'MSdatatype_info'))
  28.     drop table MSdatatype_info
  29. go
  30. if (exists (select * from sysobjects where name = 'MStable_types'))
  31.     drop table MStable_types
  32. go
  33. if (exists (select * from sysobjects where name = 'MSserver_info'))
  34.     drop table MSserver_info
  35. go
  36. if (exists (select * from sysobjects where name = 'sp_tables'))
  37.     drop proc sp_tables
  38. go
  39. if (exists (select * from sysobjects where name = 'sp_statistics'))
  40.     drop proc sp_statistics
  41. go
  42. if (exists (select * from sysobjects where name = 'sp_columns'))
  43.     drop proc sp_columns
  44. go
  45. if (exists (select * from sysobjects where name = 'sp_fkeys'))
  46.     drop proc sp_fkeys
  47. go
  48. if (exists (select * from sysobjects where name = 'sp_pkeys'))
  49.     drop proc sp_pkeys
  50. dump tran master with truncate_only
  51. go
  52.  
  53. go
  54. if (exists (select * from sysobjects where name = 'sp_stored_procedures'))
  55.     drop proc sp_stored_procedures
  56. go
  57. if (exists (select * from sysobjects where name = 'sp_sproc_columns'))
  58.     drop proc sp_sproc_columns
  59. go
  60. if (exists (select * from sysobjects where name = 'sp_table_privileges'))
  61.     drop proc sp_table_privileges
  62. go
  63. if (exists (select * from sysobjects where name = 'sp_column_privileges'))
  64.     drop proc sp_column_privileges
  65. go
  66. if (exists (select * from sysobjects where name = 'sp_server_info'))
  67.     drop proc sp_server_info
  68. go
  69. if (exists (select * from sysobjects where name = 'sp_datatype_info'))
  70.     drop proc sp_datatype_info
  71. go
  72. if (exists (select * from sysobjects where name = 'sp_special_columns'))
  73.     drop proc sp_special_columns
  74. go
  75. if (exists (select * from sysobjects where name = 'sp_databases'))
  76.     drop proc sp_databases
  77. go
  78.  
  79. dump tran master with truncate_only
  80. go
  81.  
  82. print "creating table MSdatatype_info"
  83. go
  84. create table MSdatatype_info (ss_dtype       tinyint      not null,
  85.                             type_name        varchar(32)  not null,
  86.                             data_type        smallint     not null,
  87.                             data_precision   int          null,
  88.                             numeric_scale    smallint     null,
  89.                             numeric_radix    smallint     null,
  90.                             length           int          null,
  91.                             literal_prefix   varchar(32)  null,
  92.                             literal_suffix   varchar(32)  null,
  93.                             create_params    varchar(32)  null,
  94.                             nullable         smallint     not null,
  95.                             case_sensitive   smallint     not null,
  96.                             searchable       smallint     not null)
  97. go
  98.  
  99. insert into MSdatatype_info
  100. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf, cp,        nul,case,srch */
  101. values (45,"binary",-2,   null,null,null,255,"0x",null,"max length",1,  1,   1)
  102.  
  103. insert into MSdatatype_info
  104. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf, cp, nul,case,srch */
  105. values (50,"bit",-7,        1,   0,   2,null, null,null,null,0,   1,   1)
  106.  
  107. insert into MSdatatype_info
  108. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf, cp,       nul,case,srch */
  109. values (47,"char",1,      null,null,null,255,"'", "'","max length",1, 1,   1)
  110.  
  111. insert into MSdatatype_info
  112. /* ss_type,name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  113. values (61,"datetime",11,   23,   3,  10,null,"'","'",null,1,  1,   1)
  114.  
  115. insert into MSdatatype_info
  116. /* ss_type,name,    data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  117. values (58,"smalldatetime",11, 23,   3,  10,null,"'","'",null,1,   1,  1)
  118.  
  119. insert into MSdatatype_info  /* sql server type is "datetimn" */
  120. /* ss_type,name,    data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  121. values (111,"datetime",11,     23,   3,   10,null,"'","'",null,1,  1,  1)
  122.  
  123. insert into MSdatatype_info
  124. /* ss_type, name,data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  125. values (62,"float",6,       15,  null,10,null,null,null,null,1,   1,  1)
  126.  
  127. insert into MSdatatype_info  /* sql server type is "floatn" */
  128. /* ss_type, name,data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  129. values (109,"float",6,     15,  null, 10,null, null,null,null,1,  1,  1)
  130.  
  131. insert into MSdatatype_info
  132. /* ss_type, name,data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  133. values (59,"real",7,        7,  null, 10,null, null,null,null,1,  1,  1)
  134.  
  135. insert into MSdatatype_info
  136. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  137. values(122,"smallmoney",3,    7,   4,   10,null, null,null,null,1,  1,  1)
  138.  
  139. insert into MSdatatype_info
  140. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  141. values (56,"int",  4,        10,   0,   10,null,null,null,null,1, 1,  1)
  142.  
  143. insert into MSdatatype_info  /* sql server type is "intn" */
  144. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  145. values (38,"int",   4,      10,   0,   10,null,null,null,null,1, 1,  1)
  146.  
  147. insert into MSdatatype_info
  148. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  149. values (60,"money", 3,       18,  4,    10,null,null,null,null,1, 1,  1)
  150.  
  151. insert into MSdatatype_info   /* sql server type is "moneyn" */
  152. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  153. values (110,"money", 3,     18,   4,   10,null,null,null,null,1, 1,  1)
  154.  
  155. insert into MSdatatype_info
  156. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  157. values (52,"smallint",5,      5,   0,   10,null,null,null,null,1,1,   1)
  158.  
  159. insert into MSdatatype_info
  160. /* ss_type,name,data_type,prec,scale,rdx,len,        prf, suf,cp,nul,case,srch */
  161. values (35,"text",-1,     null, null,null,2147483647,"'","'",null,1,  1,   1)
  162.  
  163. insert into MSdatatype_info
  164. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,     nul,case,srch */
  165. values (37,"varbinary",-3,null,null,null,254,"0x",null,"max length",1,1,   1)
  166.  
  167. insert into MSdatatype_info
  168. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,nul,case,srch */
  169. values (48,"tinyint",-6,   3,    0,   10,null,null,null,null,1,  1,   1)
  170.  
  171. insert into MSdatatype_info
  172. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,    nul,case,srch */
  173. values (39,"varchar",12,   null,null,null,254,"'","'","max length",1,1,   1)
  174.  
  175. insert into MSdatatype_info
  176. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,nul,case,srch */
  177. values (34,"image",-4,     null,null,null,2147483647,"0x",null,null,1,1,1)
  178. go
  179.  
  180. print "creating table MStable_types"
  181. go
  182. create table MStable_types (ss_type char(1),sag_type varchar(32))
  183. go
  184.  
  185. insert into MStable_types
  186.     values ('S','SYSTEM TABLE')
  187. insert into MStable_types
  188.     values ('U','TABLE')
  189. insert into MStable_types
  190.     values ('V','VIEW')
  191. go
  192.  
  193. dump tran master with truncate_only
  194. go
  195.  
  196. print "creating table MSserver_info"
  197. go
  198. create table MSserver_info (attribute_id      int,
  199.                           attribute_name  varchar(60),
  200.                           attribute_value varchar(255))
  201. go
  202.  
  203. insert into MSserver_info
  204.     values (1,"DBMS_NAME","Microsoft SQL SERVER")
  205. insert into MSserver_info
  206.     values (2,"DBMS_VER","4.2.0")
  207. insert into MSserver_info
  208.     values (6,"DBE_NAME","")
  209. insert into MSserver_info
  210.     values (10,"OWNER_TERM","owner")
  211. insert into MSserver_info
  212.     values (11,"TABLE_TERM","table")
  213. insert into MSserver_info
  214.     values (12,"MAX_OWNER_NAME_LENGTH","30")
  215. insert into MSserver_info
  216.     values (16,"IDENTIFIER_CASE","MIXED")
  217. insert into MSserver_info
  218.     values (15,"COLUMN_LENGTH","30")
  219. insert into MSserver_info
  220.     values (13,"TABLE_LENGTH","30")
  221. insert into MSserver_info
  222.     values (100,"USERID_LENGTH","30")
  223. insert into MSserver_info
  224.     values (17,"TX_ISOLATION","2")
  225. insert into MSserver_info
  226.     values (18,"COLLATION_SEQ","")
  227. insert into MSserver_info
  228.     values (14,"MAX_QUAL_LENGTH","30")
  229. insert into MSserver_info
  230.     values (101,"QUALIFIER_TERM","database")
  231. insert into MSserver_info
  232.     values (19,"SAVEPOINT_SUPPORT","Y")
  233. insert into MSserver_info
  234.     values (20,"MULTI_RESULT_SETS","Y")
  235. insert into MSserver_info
  236.     values (102,"NAMED_TRANSACTIONS","Y")
  237. insert into MSserver_info
  238.     values (103,"SPROC_AS_LANGUAGE","Y")
  239. insert into MSserver_info
  240.     values (103,"REMOTE_SPROC","Y")
  241. insert into MSserver_info
  242.     values (22,"ACCESSIBLE_TABLES","Y")
  243. insert into MSserver_info
  244.     values (104,"ACCESSIBLE_SPROC","Y")
  245. insert into MSserver_info
  246.     values (105,"MAX_INDEX_COLS","16")
  247. insert into MSserver_info
  248.     values (106,"RENAME_TABLE","Y")
  249. insert into MSserver_info
  250.     values (107,"RENAME_COLUMN","Y")
  251. insert into MSserver_info
  252.     values (108,"DROP_COLUMN","N")
  253. insert into MSserver_info
  254.     values (109,"INCREASE_COLUMN_LENGTH","N")
  255. insert into MSserver_info
  256.     values (110,"DDL_IN_TRANSACTION","N")
  257. insert into MSserver_info
  258.     values (111,"DESCENDING_INDEXES","N")
  259. insert into MSserver_info
  260.     values (112,"SP_RENAME","Y")
  261. insert into MSserver_info
  262.     values (500,"SYS_SPROC_VERSION","00.1405")
  263. go
  264.  
  265. print "creating sp_tables"
  266. go
  267.  
  268. create procedure sp_tables(@table_name        varchar(32)  = null,
  269.                            @table_owner     varchar(32)  = null,
  270.                            @table_qualifier varchar(32)  = null,
  271.                            @table_type        varchar(100) = null)
  272. as
  273.     declare @type1 char(1),
  274.             @type2 char(1),
  275.             @type3 char(1),
  276.             @tableindex int
  277.     if @table_qualifier is not null
  278.     begin
  279.         if db_name() != @table_qualifier
  280.         begin
  281.             print "Table qualifier must be name of current database"
  282.             return
  283.         end
  284.     end
  285.     if @table_type is null
  286.     begin
  287.         select @type1 = 'U'
  288.         select @type2 = 'V'
  289.         select @type3 = 'S'
  290.     end
  291.     else
  292.     begin
  293.         if (charindex("'TABLE'",@table_type) != 0)
  294.             select @type1 = 'U'
  295.         else
  296.             select @type1 = 'X'
  297.         if (charindex("'SYSTEM TABLE'",@table_type) != 0)
  298.             select @type2 = 'S'
  299.         else
  300.             select @type2 = 'X'
  301.         if (charindex("'VIEW'",@table_type) != 0)
  302.             select @type3 = 'V'
  303.         else
  304.             select @type3 = 'X'
  305.     end
  306.     
  307.     if @table_name is null
  308.     begin
  309.         select @table_name = '%'
  310.     end 
  311.     else begin
  312.         if (@table_owner is null) and (charindex('%', @table_name) = 0)
  313.         begin
  314.             if exists (select * from sysobjects 
  315.                         where uid = user_id()
  316.                         and name = @table_name
  317.                         and (type = 'U' or type = 'V' or type = 'S'))
  318.             begin
  319.                 select @table_owner = user_name()
  320.             end
  321.         end
  322.     end
  323.     if @table_owner is null
  324.         select @table_owner = '%'
  325.     select
  326.         table_qualifier = db_name(),
  327.         table_owner=user_name(o.uid),
  328.         table_name=o.name,
  329.         table_type = t.sag_type,
  330.         remarks = convert(varchar(254),null)
  331.     from
  332.         sysusers u,sysobjects o,master.dbo.MStable_types t
  333.     where
  334.         o.name like @table_name
  335.         and user_name(o.uid) like @table_owner
  336.         and o.type = t.ss_type
  337.         and ( o.type = @type1 or o.type = @type2 or o.type = @type3)
  338.         and u.uid=user_id() /* constrain sysusers uid for use in subquery */
  339.         and (suser_id()=1    /* User is the System Administrator */
  340.              or o.uid=user_id()   /* User created the object */
  341.              /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  342.              or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  343.                   from sysprotects p
  344.                   /* outer join to correlate with all rows in sysobjects */
  345.                   where p.id =* o.id
  346.                       /* get rows for public,current user,user's group */
  347.                       and (p.uid=0 or p.uid=user_id() or p.uid=*u.gid)
  348.                       /* check for SELECT,EXECUTE privilege */
  349.                       and (action in (193,224)))&1     /* more magic...normalize GRANT */
  350.                 )=1    /* final magic...compare Grants    */
  351.             )
  352. go
  353.  
  354. grant execute on sp_tables to public
  355. go
  356.  
  357. dump tran master with truncate_only
  358. go
  359.  
  360. print "creating sp_statistics"
  361. go
  362.  
  363. dump tran master with truncate_only
  364. go
  365.  
  366.  
  367. CREATE PROCEDURE sp_statistics (@table_name      varchar(32),
  368.                              @table_owner      varchar(32) = null,
  369.                              @table_qualifier varchar(32) = null,
  370.                              @index_name      varchar(32) = '%',
  371.                              @is_unique       char(1) = 'N')
  372. AS
  373.     DECLARE @indid                int
  374.     DECLARE @lastindid            int
  375.     DECLARE @table_id            int
  376.     DECLARE @full_table_name    char(70)
  377.     if @table_qualifier is not null
  378.     begin
  379.         if db_name() != @table_qualifier
  380.         begin
  381.             print "Table qualifier must be name of current database"
  382.             return
  383.         end
  384.     end
  385.     if @@trancount != 0
  386.     begin
  387.         raiserror 20001 "stored procedure sp_statistics can not be run while in a transaction"
  388.         return
  389.     end
  390.     create table #TmpIndex( table_qualifier varchar(32),
  391.                             table_owner     varchar(32),
  392.                             table_name        varchar(32),
  393.                             index_qualifier varchar(32) null,
  394.                             index_name        varchar(32) null,
  395.                             non_unique        smallint null,
  396.                             type            smallint,
  397.                             seq_in_index    smallint null,
  398.                             column_name     varchar(32) null,
  399.                             collation        char(1) null,
  400.                             remarks         varchar(255) null,
  401.                             index_id        int null,
  402.                             cardinality     int,
  403.                              pages            int,
  404.                              status         smallint)
  405.     if @table_owner is null
  406.     begin
  407.         SELECT @full_table_name = @table_name
  408.     end
  409.     else
  410.     begin
  411.         SELECT @full_table_name = @table_owner + '.' + @table_name
  412.     end
  413.     SELECT @table_id = object_id(@full_table_name)
  414.     SELECT    @indid=min(indid)
  415.     FROM sysindexes
  416.     WHERE id=@table_id
  417.         AND indid > 0
  418.         AND indid < 255
  419.     
  420.     WHILE @indid != NULL
  421.     BEGIN
  422.         INSERT #TmpIndex
  423.         SELECT
  424.             DB_NAME(),                               /* table_qualifier */
  425.             USER_NAME(o.uid),                         /* table_owner     */
  426.             o.name,                                   /* table_name      */
  427.             null,                                     /* index_qualifier */
  428.             x.name,                                   /* index_name      */
  429.             0,                                       /* non_unique      */
  430.             3,                                       /* type            */
  431.             colid,                                   /* seq_in_index    */
  432.             INDEX_COL(@full_table_name,indid,colid), /* column_name     */
  433.             "A",                                     /* collation       */
  434.             "",                                       /*  remarks         */
  435.             @indid,                                   /*  index_id        */
  436.              x.rows,                                 /*  cardinality     */
  437.              x.dpages,                               /* pages            */
  438.             x.status                                 /* status           */
  439.         FROM sysindexes x,syscolumns c,sysobjects o
  440.         WHERE x.id=@table_id
  441.             AND x.id = o.id
  442.             AND x.id=c.id
  443.             AND c.colid<keycnt+ (x.status&16)/16
  444.             AND x.indid=@indid
  445.         /*
  446.         **    Now move @indid to the next index.
  447.         */
  448.         SELECT @lastindid = @indid
  449.         SELECT @indid = NULL
  450.         SELECT @indid = min(indid)
  451.         FROM sysindexes
  452.         WHERE id = @table_id
  453.             AND indid > @lastindid
  454.             AND indid < 255
  455.     END
  456.    
  457.     UPDATE #TmpIndex
  458.         SET non_unique = 1
  459.         WHERE  status&0x2 != 2
  460.     UPDATE #TmpIndex
  461.         SET type = 1
  462.         WHERE  index_id = 1
  463.  
  464.  
  465.      /* now add row for table statistics */
  466.  
  467.  
  468.       INSERT #TmpIndex  
  469.         SELECT
  470.             DB_NAME(),         /* table_qualifier */
  471.             USER_NAME(o.uid),   /* table_owner     */
  472.             o.name,             /* table_name      */
  473.             null,               /* index_qualifier */ 
  474.             null,               /* index_name      */
  475.             0,                 /* non_unique      */
  476.             0,                 /* type            */
  477.             0,                 /* seq_in_index    */
  478.             null,               /* column_name     */
  479.             null,               /* collation       */
  480.             "",                 /*  remarks         */
  481.             0,                 /*  index_id        */
  482.              x.rows,           /*  cardinality     */
  483.              x.dpages,         /* pages            */
  484.             0                   /* status           */
  485.         FROM sysindexes x, sysobjects o
  486.         WHERE o.id=@table_id
  487.             AND x.id = o.id
  488.             AND (x.indid=0 or x.indid=1)
  489.  
  490.  
  491.     if @is_unique != 'Y'
  492.         SELECT
  493.             table_qualifier, 
  494.             table_owner,     
  495.             table_name,       
  496.             non_unique,       
  497.             index_qualifier, 
  498.             index_name,       
  499.             type,             
  500.             seq_in_index,     
  501.             column_name,     
  502.             collation,       
  503.             cardinality,     
  504.             pages             
  505.          FROM #TmpIndex       
  506.         WHERE index_name like @index_name or index_name is null   
  507.         ORDER BY non_unique,type,index_name,seq_in_index         
  508.     else
  509.         SELECT
  510.             table_qualifier,
  511.             table_owner,
  512.             table_name,
  513.             non_unique,
  514.             index_qualifier,
  515.             index_name,
  516.             type,
  517.             seq_in_index,
  518.             column_name,
  519.             collation,
  520.             cardinality,
  521.             pages
  522.         FROM #TmpIndex
  523.         WHERE non_unique = 0
  524.             and (index_name like @index_name or index_name is null)
  525.         ORDER BY non_unique,type,index_name,seq_in_index
  526.     DROP TABLE #TmpIndex
  527. go
  528.  
  529. grant execute on sp_statistics to public
  530. go
  531.  
  532. dump tran master with truncate_only
  533. go
  534.  
  535. print "creating sp_columns"
  536. go
  537.  
  538. CREATE PROCEDURE sp_columns (@table_name varchar(32),
  539.                              @table_owner varchar(32) = null,
  540.                              @table_qualifier varchar(32) = null,
  541.                              @column_name varchar(32) = null )
  542. AS
  543.     if @column_name is null
  544.         select @column_name = '%'
  545.     DECLARE @full_table_name    char(70)
  546.     if @table_qualifier is not null
  547.     begin
  548.         if db_name() != @table_qualifier
  549.         begin
  550.             print "Table qualifier must be name of current database"
  551.             return
  552.         end
  553.     end
  554.         if @table_name is null
  555.         begin
  556.             select @table_name='%'
  557.         end
  558.     if @table_owner is null
  559.     begin
  560.         SELECT @full_table_name = @table_name
  561.     end
  562.     else
  563.     begin
  564.         SELECT @full_table_name = @table_owner + '.' + @table_name
  565.     end
  566.     if (charindex('%',@full_table_name) = 0)
  567.     begin
  568.         SELECT
  569.             table_qualifier = DB_NAME(),
  570.             table_owner = USER_NAME(o.uid),
  571.             table_name = o.name,
  572.             column_name = c.name,
  573.             d.data_type,
  574.             type_name = t.name,
  575.             data_precision=isnull(d.data_precision, convert(int,c.length)),
  576.             length=convert(int,c.length),
  577.             d.numeric_scale,
  578.             d.numeric_radix,
  579.             nullable=convert(smallint, convert(bit, c.status&8)),
  580.             remarks = c.name,
  581.             ss_data_type = c.type
  582.         FROM
  583.         syscolumns c,sysobjects o,master.dbo.MSdatatype_info d, systypes t
  584.         WHERE
  585.             o.id=object_id(@full_table_name)
  586.             AND c.id = o.id
  587.             AND c.type = d.ss_dtype
  588.         AND c.usertype *= t.usertype
  589.             AND c.name like @column_name
  590.     end else
  591.     begin
  592.         if @table_owner is null
  593.             select @table_owner = '%'
  594.         SELECT
  595.             table_qualifier = DB_NAME(),
  596.             table_owner = USER_NAME(o.uid),
  597.             table_name = o.name,
  598.             column_name = c.name,
  599.             d.data_type,
  600.             d.type_name,
  601.             data_precision=isnull(d.data_precision, convert(int, c.length)),
  602.             length=convert(int,c.length),
  603.             d.numeric_scale,
  604.             d.numeric_radix,
  605.             nullable=convert(smallint, convert(bit, c.status&8)),
  606.             remarks = null,
  607.             ss_data_type = c.type
  608.         FROM
  609.             syscolumns c,sysobjects o,master.dbo.MSdatatype_info d, systypes t
  610.  
  611.         WHERE
  612.             o.name like @table_name
  613.             AND user_name(o.uid) like @table_owner
  614.             AND c.id = o.id
  615.         AND c.usertype *= t.usertype
  616.             AND c.type = d.ss_dtype
  617.             AND c.name like @column_name
  618.     end
  619.  
  620.     
  621. go
  622.  
  623. grant execute on sp_columns to public
  624. go
  625.  
  626. dump tran master with truncate_only
  627. go
  628.  
  629. print "creating sp_fkeys"
  630. go
  631.  
  632. CREATE PROCEDURE sp_fkeys( @pktable_name      varchar(32) = null,
  633.                            @pktable_owner      varchar(32) = null,
  634.                            @pktable_qualifier varchar(32) = null,
  635.                            @fktable_name      varchar(32) = null,
  636.                            @fktable_owner      varchar(32) = null,
  637.                            @fktable_qualifier varchar(32) = null )
  638. as
  639.     if (@pktable_name is null) and (@fktable_name is null)
  640.     begin
  641.         print "pk table name or fk table name must be given"
  642.         return
  643.     end
  644.     if @fktable_qualifier is not null
  645.     begin
  646.         if db_name() != @fktable_qualifier
  647.         begin
  648.             print "Foriegn Key Table qualifier must be name of current database"
  649.             return
  650.         end
  651.     end
  652.     if @pktable_qualifier is not null
  653.     begin
  654.         if db_name() != @pktable_qualifier
  655.         begin
  656.             print "Primary Key Table qualifier must be name of current database"
  657.             return
  658.         end
  659.     end
  660.  
  661.     if @pktable_name is null
  662.         select @pktable_name = '%'
  663.     if @pktable_owner is null
  664.         select @pktable_owner = '%'
  665.     if @fktable_name is null
  666.         select @fktable_name = '%'
  667.     if @fktable_owner is null
  668.         select @fktable_owner = '%'
  669.  
  670.     if @@trancount != 0
  671.     begin
  672.         raiserror 20001 "catalog procedure sp_fkeys can not be run in a transaction"
  673.         return
  674.     end
  675.     create table #fkeys( pktable_qualifier varchar(32),
  676.                          pktable_owner       varchar(32),
  677.                          pktable_name       varchar(32),
  678.                          pkcolumn_name      varchar(32),
  679.                          fktable_qualifier varchar(32),
  680.                          fktable_owner       varchar(32),
  681.                          fktable_name       varchar(32),
  682.                          fkcolumn_name       varchar(32),
  683.                          key_seq           smallint)
  684.     insert into #fkeys
  685.         select
  686.             db_name(),
  687.             (select user_name(uid) from sysobjects o where o.id = k.depid),
  688.             object_name(k.depid),
  689.             (    select name 
  690.                     from syscolumns
  691.                     where id = k.depid
  692.                     and colid = k.key1),
  693.              db_name(),
  694.             (select user_name(uid) from sysobjects o where o.id = k.id),
  695.             object_name(k.id),
  696.             c.name,
  697.             1
  698.         from
  699.             syskeys k,syscolumns c
  700.         where
  701.             c.id = k.id
  702.             and k.type = 2
  703.             and c.colid = depkey1
  704.         if (@@rowcount    = 0)
  705.             goto done
  706.         insert into #fkeys
  707.             select
  708.                 db_name(),
  709.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  710.                 object_name(k.depid),
  711.                 (select name 
  712.                     from syscolumns
  713.                     where id = k.depid
  714.                     and colid = k.key2),
  715.                 db_name(),
  716.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  717.                 object_name(k.id),
  718.                 c.name,
  719.                 2
  720.             from
  721.                 syskeys k,syscolumns c
  722.             where
  723.                 c.id = k.id
  724.                 and k.type = 2
  725.                 and c.colid = depkey2
  726.         if (@@rowcount = 0)
  727.             goto done
  728.         insert into #fkeys
  729.             select
  730.                 db_name(),
  731.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  732.                 object_name(k.depid),
  733.                 (select name 
  734.                     from syscolumns
  735.                     where id = k.depid
  736.                     and colid = k.key3),
  737.                  db_name(),
  738.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  739.                 object_name(k.id),
  740.                 c.name,
  741.                 3
  742.             from
  743.                 syskeys k,syscolumns c
  744.             where
  745.                 c.id = k.id
  746.                 and k.type = 2
  747.                 and c.colid = depkey3
  748.         if (@@rowcount = 0)
  749.             goto done
  750.         insert into #fkeys
  751.         select
  752.             db_name(),
  753.             (select user_name(uid) from sysobjects o where o.id = k.depid),
  754.                 object_name(k.depid),
  755.                 (select name 
  756.                     from syscolumns
  757.                     where id = k.depid
  758.                     and colid = k.key4),
  759.                  db_name(),
  760.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  761.                 object_name(k.id),
  762.                 c.name,
  763.                 4
  764.             from
  765.                 syskeys k,syscolumns c
  766.             where
  767.                 c.id = k.id
  768.                 and k.type = 2
  769.                 and c.colid = depkey4
  770.         if (@@rowcount = 0)
  771.             goto done
  772.         insert into #fkeys
  773.             select
  774.                 db_name(),
  775.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  776.                 object_name(k.depid),
  777.                 (select name 
  778.                     from syscolumns
  779.                     where id = k.depid
  780.                     and colid = k.key5),
  781.                  db_name(),
  782.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  783.                 object_name(k.id),
  784.                 c.name,
  785.                 5
  786.             from
  787.                 syskeys k,syscolumns c
  788.             where
  789.                 c.id = k.id
  790.                 and k.type = 2
  791.                 and c.colid = depkey5
  792.         if (@@rowcount = 0)
  793.             goto done
  794.         insert into #fkeys
  795.             select
  796.                 db_name(),
  797.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  798.                 object_name(k.depid),
  799.                 (select name 
  800.                     from syscolumns
  801.                     where id = k.depid
  802.                     and colid = k.key6),
  803.                  db_name(),
  804.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  805.                 object_name(k.id),
  806.                 c.name,
  807.                 6
  808.             from
  809.                 syskeys k,syscolumns c
  810.             where
  811.                 c.id = k.id
  812.                 and k.type = 2
  813.                 and c.colid = depkey6
  814.         if (@@rowcount = 0)
  815.             goto done
  816.         insert into #fkeys
  817.             select
  818.                 db_name(),
  819.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  820.                 object_name(k.depid),
  821.                 (select name 
  822.                     from syscolumns
  823.                     where id = k.depid
  824.                     and colid = k.key7),
  825.                  db_name(),
  826.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  827.                 object_name(k.id),
  828.                 c.name,
  829.                 7
  830.             from
  831.                 syskeys k,syscolumns c
  832.             where
  833.                 c.id = k.id
  834.                 and k.type = 2
  835.                 and c.colid = depkey7
  836.         if (@@rowcount    = 0)
  837.             goto done
  838.         insert into #fkeys
  839.             select
  840.                 db_name(),
  841.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  842.                 object_name(k.depid),
  843.                 (select name 
  844.                     from syscolumns
  845.                     where id = k.depid
  846.                     and colid = k.key8),
  847.                  db_name(),
  848.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  849.                 object_name(k.id),
  850.                 c.name,
  851.                 8
  852.             from
  853.                 syskeys k,syscolumns c
  854.             where
  855.                 c.id = k.id
  856.                 and k.type = 2
  857.                 and c.colid = depkey8
  858.     done:
  859.     select
  860.         pktable_qualifier,
  861.         pktable_owner,
  862.         pktable_name,
  863.         pkcolumn_name,
  864.         fktable_qualifier,
  865.         fktable_owner,
  866.         fktable_name,
  867.         fkcolumn_name,
  868.         key_seq,
  869.         update_delete_rule = 1
  870.     from #fkeys
  871.     where fktable_name like @fktable_name
  872.         and fktable_owner like @fktable_owner
  873.         and pktable_name  like @pktable_name
  874.         and pktable_owner like @pktable_owner
  875.     order by fktable_name,fktable_owner,key_seq
  876. go
  877.  
  878. grant execute on sp_fkeys to public
  879. go
  880. dump tran master with truncate_only
  881. go
  882.  
  883.  
  884.  
  885. print "creating sp_pkeys"
  886. go
  887.  
  888. CREATE PROCEDURE sp_pkeys( @table_name    varchar(32),
  889.                            @table_owner varchar(32) = null,
  890.                            @table_qualifier varchar(32)= null )
  891. as
  892.     if @table_qualifier is not null
  893.     begin
  894.         if db_name() != @table_qualifier
  895.         begin
  896.             print "Table qualifier must be name of current database"
  897.             return
  898.         end
  899.     end
  900.     if @table_owner is null
  901.         select @table_owner = '%'
  902.     if @@trancount != 0
  903.     begin
  904.         raiserror 20001 "catalog procedure sp_pkeys can not be run in a transaction"
  905.         return
  906.     end
  907.     create table #pkeys( table_qualifier varchar(32),
  908.                          table_owner     varchar(32),
  909.                          table_name      varchar(32),
  910.                          column_name     varchar(32),
  911.                          key_seq         smallint,
  912.                          update_delete_rule smallint null )
  913.     insert into #pkeys
  914.         select
  915.             db_name(),
  916.             (select user_name(uid) from sysobjects o where o.id = k.id),
  917.             object_name(k.id),
  918.             c.name,
  919.             1,
  920.             null
  921.         from
  922.             syskeys k,syscolumns c
  923.         where
  924.             c.id = k.id
  925.             and k.type = 1
  926.             and c.colid = k.key1
  927.     if (@@rowcount = 0)
  928.         goto done
  929.     insert into #pkeys
  930.         select
  931.             db_name(),
  932.             (select user_name(uid) from sysobjects o where o.id = k.id),
  933.             object_name(k.id),
  934.             c.name,
  935.             2,
  936.             null
  937.         from
  938.             syskeys k,syscolumns c
  939.         where
  940.             c.id = k.id
  941.             and k.type = 1
  942.             and c.colid = key2
  943.     if (@@rowcount = 0)
  944.         goto done
  945.     insert into #pkeys
  946.         select
  947.             db_name(),
  948.             (select user_name(uid) from sysobjects o where o.id = k.id),
  949.             object_name(k.id),
  950.             c.name,
  951.             3,
  952.             null
  953.         from
  954.             syskeys k,syscolumns c
  955.         where
  956.             c.id = k.id
  957.             and k.type = 1
  958.             and c.colid = key3
  959.     if (@@rowcount = 0)
  960.         goto done
  961.     insert into #pkeys
  962.         select
  963.             db_name(),
  964.             (select user_name(uid) from sysobjects o where o.id = k.id),
  965.             object_name(k.id),
  966.             c.name,
  967.             4,
  968.             null
  969.         from
  970.             syskeys k,syscolumns c
  971.         where
  972.             c.id = k.id
  973.             and k.type = 1
  974.             and c.colid = key4
  975.     if (@@rowcount = 0)
  976.         goto done
  977.     insert into #pkeys
  978.         select
  979.                 db_name(),
  980.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  981.                 object_name(k.id),
  982.                 c.name,
  983.                 5,
  984.             null
  985.         from
  986.             syskeys k,syscolumns c
  987.         where
  988.             c.id = k.id
  989.             and k.type = 1
  990.             and c.colid = key5
  991.     if (@@rowcount = 0)
  992.         goto done
  993.     insert into #pkeys
  994.         select
  995.             db_name(),
  996.             (select user_name(uid) from sysobjects o where o.id = k.id),
  997.             object_name(k.id),
  998.             c.name,
  999.             6,
  1000.             null
  1001.         from
  1002.             syskeys k,syscolumns c
  1003.         where
  1004.             c.id = k.id
  1005.             and k.type = 1
  1006.             and c.colid = key6
  1007.     if (@@rowcount = 0)
  1008.         goto done
  1009.     insert into #pkeys
  1010.         select
  1011.             db_name(),
  1012.             (select user_name(uid) from sysobjects o where o.id = k.id),
  1013.             object_name(k.id),
  1014.             c.name,
  1015.             7,
  1016.             null
  1017.         from
  1018.             syskeys k,syscolumns c
  1019.         where
  1020.             c.id = k.id
  1021.             and k.type = 1
  1022.             and c.colid = key7
  1023.     if (@@rowcount = 0)
  1024.         goto done
  1025.     insert into #pkeys
  1026.         select
  1027.             db_name(),
  1028.             (select user_name(uid) from sysobjects o where o.id = k.id),
  1029.             object_name(k.id),
  1030.             c.name,
  1031.             8,
  1032.             null
  1033.         from
  1034.             syskeys k,syscolumns c
  1035.         where
  1036.             c.id = k.id
  1037.             and k.type = 1
  1038.             and c.colid = key8
  1039.     done:
  1040.     select 
  1041.             table_qualifier,
  1042.             table_owner,
  1043.             table_name,
  1044.             column_name,
  1045.             key_seq
  1046.             from #pkeys
  1047.     where table_name like  @table_name
  1048.     and table_owner like @table_owner
  1049.     order by table_name,table_owner,key_seq
  1050. go
  1051.  
  1052. grant execute on sp_pkeys to public
  1053. go
  1054.  
  1055. dump tran master with truncate_only
  1056. go
  1057.  
  1058. print "creating sp_stored_procedures"
  1059. go
  1060.  
  1061. create procedure sp_stored_procedures( @sp_name      varchar(32) = null,
  1062.                                        @sp_owner     varchar(32) = null,
  1063.                                        @sp_qualifier varchar(32) = null)
  1064. as
  1065.     if @sp_qualifier is not null
  1066.     begin
  1067.         if db_name() != @sp_qualifier
  1068.         begin
  1069.             print "stored procedure qualifier must be name of current database"
  1070.             return
  1071.         end
  1072.     end
  1073.     
  1074.     if @sp_name is null
  1075.     begin
  1076.         select @sp_name = '%'
  1077.     end 
  1078.     else begin
  1079.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  1080.         begin
  1081.             if exists (select * from sysobjects 
  1082.                         where uid = user_id()
  1083.                         and name = @sp_name
  1084.                         and type = 'P')
  1085.             begin
  1086.                 select @sp_owner = user_name()
  1087.             end
  1088.         end
  1089.     end
  1090.     if @sp_owner is null
  1091.         select @sp_owner = '%'
  1092.  
  1093.     select distinct
  1094.             sp_qualifier = db_name(),
  1095.             sp_owner = user_name(o.uid),
  1096.             sp_name = o.name,
  1097.             sp_number = p.number,
  1098.             sp_num_input_params = -1,
  1099.             sp_num_output_params = -1,
  1100.             sp_num_result_sets = -1,
  1101.             remarks = null
  1102.         from
  1103.             sysobjects o,sysprocedures p,sysusers u
  1104.         where
  1105.             o.name like @sp_name
  1106.             and user_name(o.uid) like @sp_owner
  1107.             and o.type = 'P'
  1108.             and p.id = o.id
  1109.             and u.uid=user_id()   /* constrain sysusers uid for use in subquery */
  1110.             and (suser_id()=1      /* User is the System Administrator */
  1111.                     or o.uid=user_id()     /* User created the object */
  1112.                     /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  1113.                     or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  1114.                          from sysprotects p
  1115.                          /* outer join to correlate with all rows in sysobjects */
  1116.                          where p.id =* o.id
  1117.                              /*  get rows for public,current user,user's group */
  1118.                              and (p.uid=0 or p.uid=user_id() or p.uid=*u.gid)
  1119.                              /* check for SELECT,EXECUTE privilege */
  1120.                              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  1121.                         )=1    /* final magic...compare Grants    */
  1122.                 )
  1123. go
  1124. grant execute on sp_stored_procedures to public
  1125. go
  1126.  
  1127. dump tran master with truncate_only
  1128. go
  1129.  
  1130.  
  1131. print "creating sp_sproc_columns"
  1132. go
  1133.  
  1134. CREATE PROCEDURE sp_sproc_columns( @sp_name      varchar(32),
  1135.                                    @sp_owner     varchar(32) = null,
  1136.                                    @sp_qualifier varchar(32) = null,
  1137.                            @sp_number  smallint = 1,
  1138.                                    @column_name  varchar(32) = null )
  1139. AS
  1140.     DECLARE @full_sp_name char(70)
  1141.     if @sp_qualifier is not null
  1142.     begin
  1143.         if db_name() != @sp_qualifier
  1144.         begin
  1145.             print "Table qualifier must be name of current database"
  1146.             return
  1147.         end
  1148.     end
  1149.     if @sp_owner is null
  1150.         SELECT @full_sp_name = @sp_name
  1151.     else
  1152.         SELECT @full_sp_name = @sp_owner + '.' + @sp_name
  1153.     SELECT
  1154.         sp_qualifier = DB_NAME(),
  1155.         sp_owner = USER_NAME(o.uid),
  1156.         sp_name = o.name,
  1157.         sp_number  = c.number,
  1158.         column_name = c.name,
  1159.         d.data_type,
  1160.         type_name = t.name,
  1161.         c.length,
  1162.         d.data_precision,
  1163.         d.numeric_scale,
  1164.         d.numeric_radix,
  1165.         d.nullable,
  1166.         remarks = null,
  1167.         ss_data_type = c.type,
  1168.         column_type = 1
  1169.     FROM
  1170.         syscolumns c,sysobjects o,master.dbo.MSdatatype_info d, systypes t
  1171.     WHERE o.id=object_id(@full_sp_name)
  1172.         AND c.id = o.id
  1173.         AND c.type = d.ss_dtype
  1174.     AND c.usertype *= t.usertype
  1175.         AND c.number = @sp_number
  1176. go
  1177.  
  1178. grant execute on sp_sproc_columns to public
  1179. go
  1180.  
  1181.                 
  1182. print "creating sp_table_privileges"
  1183. go
  1184.  
  1185.  
  1186. CREATE PROCEDURE sp_table_privileges ( 
  1187.                         @table_name  varchar(32),
  1188.                         @table_owner varchar(32) = null,
  1189.                         @table_qualifier varchar(32)= null)
  1190. as        
  1191.  
  1192.     declare @table_id    int,
  1193.             @owner_id    int,
  1194.             @full_table_name char(70)
  1195.     
  1196.         
  1197.     if @table_qualifier is not null
  1198.     begin
  1199.         if db_name() != @table_qualifier
  1200.         begin
  1201.             print "Table qualifier must be name of current database"
  1202.             return
  1203.         end
  1204.     end
  1205.     if @table_owner is null
  1206.     begin
  1207.         SELECT @full_table_name = @table_name
  1208.     end
  1209.     else
  1210.     begin
  1211.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1212.     end
  1213.     SELECT @table_id = object_id(@full_table_name)
  1214.     if @table_id = 0
  1215.     begin
  1216.         print "table not found"
  1217.         return
  1218.     end
  1219.  
  1220.     if @@trancount != 0
  1221.     begin
  1222.         raiserror 20001 "catalog procedure sp_table_privileges can not be run in a transaction"
  1223.         return
  1224.     end
  1225.     create table #table_privileges 
  1226.             (table_qualifier         varchar(32),
  1227.             table_owner         varchar(32),    
  1228.             table_name     varchar(32),
  1229.             grantor            varchar(32),
  1230.             grantee            varchar(32),
  1231.             select_privilege    int,
  1232.             select_grantable    int,
  1233.             insert_privilege    int,
  1234.             insert_grantable    int,
  1235.             update_privilege    int,
  1236.             update_grantable    int,
  1237.             delete_privilege    int,
  1238.             delete_grantable    int,
  1239.             references_privilege    int null,
  1240.             references_grantable    int null,
  1241.             uid                 int,
  1242.             gid                 int)
  1243.  
  1244.     insert into #table_privileges
  1245.     select distinct db_name(),
  1246.         user_name(o.uid),
  1247.         o.name,
  1248.         user_name(o.uid),
  1249.         u.name,
  1250.         0,
  1251.         0,
  1252.         0,
  1253.         0,
  1254.         0,
  1255.         0,
  1256.         0,
  1257.         0,
  1258.         null,
  1259.         null,
  1260.         u.uid,
  1261.         u.gid
  1262.     from sysusers u, sysobjects o
  1263.     where o.id = @table_id
  1264.     and u.uid != u.gid
  1265.     
  1266.     /* 
  1267.     ** now add row for table owner
  1268.     */    
  1269.     if exists (
  1270.         select * 
  1271.             from #table_privileges
  1272.             where grantor = grantee)
  1273.     begin
  1274.         update #table_privileges
  1275.             set select_privilege = 1,
  1276.                 select_grantable = 1,
  1277.                 update_privilege = 1,
  1278.                 update_grantable = 1,
  1279.                 insert_privilege = 1,
  1280.                 insert_grantable = 1,
  1281.                 delete_privilege = 1,
  1282.                 delete_grantable = 1
  1283.             where grantor = grantee
  1284.     end
  1285.     else 
  1286.     begin
  1287.         insert into #table_privileges
  1288.         select  db_name(),
  1289.             user_name(o.uid),
  1290.             o.name,
  1291.             user_name(o.uid),
  1292.             user_name(o.uid),
  1293.             1,
  1294.             1,
  1295.             1,
  1296.             1,
  1297.             1,
  1298.             1,
  1299.             1,
  1300.             1,
  1301.             null,
  1302.             null,
  1303.             o.uid,
  1304.             u.gid
  1305.         from sysobjects o, sysusers u
  1306.         where o.id = @table_id
  1307.         and u.uid = o.uid
  1308.     end     
  1309.              
  1310.     update #table_privileges 
  1311.         set select_privilege = 1 
  1312.         where exists (select * from sysprotects
  1313.             where id = @table_id
  1314.             and (#table_privileges.uid = uid
  1315.                 or #table_privileges.gid = uid
  1316.                 or uid = 0)
  1317.             and protecttype = 205
  1318.             and action = 193)
  1319.         and not exists (select * from sysprotects
  1320.             where id = @table_id
  1321.             and (#table_privileges.uid = uid
  1322.                 or #table_privileges.gid = uid
  1323.                 or uid = 0)
  1324.             and protecttype = 206
  1325.             and action = 193)
  1326.  
  1327.  
  1328.     update #table_privileges 
  1329.         set insert_privilege = 1 
  1330.         where exists (select * from sysprotects 
  1331.             where id = @table_id
  1332.             and (#table_privileges.uid = uid
  1333.                 or #table_privileges.gid = uid
  1334.                 or uid = 0)
  1335.             and protecttype = 205
  1336.             and action = 195)
  1337.         and not exists (select * from sysprotects
  1338.             where id = @table_id
  1339.             and (#table_privileges.uid = uid
  1340.                 or #table_privileges.gid = uid
  1341.                 or uid = 0)
  1342.             and protecttype = 206
  1343.             and action = 195)
  1344.  
  1345.     update #table_privileges 
  1346.         set delete_privilege = 1 
  1347.         where exists (select * from sysprotects 
  1348.             where id = @table_id
  1349.             and (#table_privileges.uid = uid
  1350.                 or #table_privileges.gid = uid
  1351.                 or uid = 0)
  1352.             and protecttype = 205
  1353.             and action = 196)
  1354.         and not exists (select * from sysprotects
  1355.             where id = @table_id
  1356.             and (#table_privileges.uid = uid
  1357.                 or #table_privileges.gid = uid
  1358.                 or uid = 0)
  1359.             and protecttype = 206
  1360.             and action = 196)
  1361.  
  1362.   
  1363.  
  1364.  
  1365.     update #table_privileges 
  1366.         set update_privilege = 1 
  1367.         where exists (select * from sysprotects 
  1368.             where id = @table_id
  1369.             and (#table_privileges.uid = uid
  1370.                 or #table_privileges.gid = uid
  1371.                 or uid = 0)
  1372.             and protecttype = 205
  1373.             and action = 197)
  1374.         and not exists (select * from sysprotects
  1375.             where id = @table_id
  1376.             and (#table_privileges.uid = uid
  1377.                 or #table_privileges.gid = uid
  1378.                 or uid = 0)
  1379.             and protecttype = 206
  1380.             and action = 197)
  1381.  
  1382.     select 
  1383.             table_qualifier,
  1384.             table_owner,
  1385.             table_name,
  1386.             grantor,
  1387.             grantee,
  1388.             select_privilege,
  1389.             select_grantable,
  1390.             insert_privilege,
  1391.             insert_grantable,
  1392.             update_privilege,
  1393.             update_grantable,
  1394.             delete_privilege,
  1395.             delete_grantable,
  1396.             references_privilege,
  1397.             references_grantable
  1398.     from #table_privileges
  1399.         where select_privilege != 0
  1400.         or insert_privilege != 0
  1401.         or delete_privilege != 0
  1402.         or update_privilege != 0
  1403.  
  1404.  
  1405. go
  1406. grant execute on sp_table_privileges to public
  1407. go
  1408. dump tran master with truncate_only
  1409. go
  1410.                
  1411. print "creating sp_column_privileges"
  1412. go
  1413.  
  1414. CREATE PROCEDURE sp_column_privileges ( 
  1415.                         @table_name  varchar(32),
  1416.                         @table_owner varchar(32) = null,
  1417.                         @table_qualifier varchar(32)= null,
  1418.                         @column_name varchar(32) = null)
  1419. as        
  1420.  
  1421.     declare @table_id    int,
  1422.         @owner_id    int
  1423.     DECLARE @full_table_name    char(70)
  1424.  
  1425.     declare @low int            /* range of userids to check */
  1426.     declare @high int
  1427.     declare @objid int            /* id of @name if object */
  1428.     declare @owner_name varchar(32)
  1429.  
  1430.     select @low = 0, @high = 32767
  1431.  
  1432.     if @column_name is null
  1433.         select @column_name = '%'
  1434.  
  1435.     if @table_qualifier is not null
  1436.     begin
  1437.         if db_name() != @table_qualifier
  1438.         begin
  1439.             print "Table qualifier must be name of current database"
  1440.             return
  1441.         end
  1442.     end
  1443.     if @table_owner is null
  1444.     begin
  1445.         SELECT @full_table_name = @table_name
  1446.     end
  1447.     else
  1448.     begin
  1449.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1450.     end
  1451.     select @table_id = 0    
  1452.     select @table_id = object_id(@full_table_name) 
  1453.     if @table_id = 0
  1454.     begin
  1455.         print "table not found"
  1456.         return
  1457.     end
  1458.  
  1459.     if @@trancount != 0
  1460.     begin
  1461.         raiserror 20001 "catalog procedure sp_column_privileges can not be run in a transaction"
  1462.         return
  1463.     end
  1464.  
  1465.     /*
  1466.     ** We need to create a table which will contain a row for every row to
  1467.     ** be returned to the client.  
  1468.     */
  1469.  
  1470.     create table #column_privileges 
  1471.             (table_qualifier     varchar(32),
  1472.             table_owner         varchar(32),    
  1473.             table_name         varchar(32),
  1474.             column_name         varchar(32),
  1475.             grantor            varchar(32),
  1476.             grantee            varchar(32),
  1477.             select_privilege    int,
  1478.             select_grantable    int,
  1479.             insert_privilege    int,
  1480.             insert_grantable    int,
  1481.             update_privilege    int,
  1482.             update_grantable    int,
  1483.             delete_privilege    int,
  1484.             delete_grantable    int,
  1485.             references_privilege    int null,
  1486.             references_grantable    int null,
  1487.             uid                 int,
  1488.             gid                 int)
  1489.  
  1490. /* 
  1491. ** insert a row for the table owner (who has all permissions)
  1492. */
  1493.     select @owner_name = (select user_name(uid) 
  1494.                             from sysobjects 
  1495.                                 where id = @table_id)
  1496.  
  1497.  
  1498.     insert into #column_privileges
  1499.     select
  1500.             db_name(),
  1501.             @owner_name,
  1502.             @table_name,
  1503.             name,
  1504.             @owner_name,
  1505.             @owner_name,
  1506.             1,
  1507.             1,
  1508.             1,
  1509.             1,
  1510.             1,
  1511.             1,
  1512.             1,
  1513.             1,
  1514.             null,
  1515.             null,
  1516.             user_id(@owner_name),
  1517.             0
  1518.         from syscolumns 
  1519.         where id = @table_id
  1520.  
  1521. /* 
  1522. ** now stick a row in the table for every user in the database        
  1523. ** we will need to weed out those who have no permissions later
  1524. ** (and yes this is a cartesion product: the uid field in sysprotects
  1525. ** can also have a group id, in which case we need to extend those 
  1526. ** privileges to all group members).
  1527. */
  1528.     
  1529.     insert into #column_privileges
  1530.     select distinct 
  1531.         db_name(),
  1532.         user_name(o.uid),
  1533.         @table_name,
  1534.         c.name,
  1535.         user_name(o.uid),
  1536.         u.name,
  1537.         0,
  1538.         0,
  1539.         0,
  1540.         0,
  1541.         0,
  1542.         0,
  1543.         0,
  1544.         0,
  1545.         null,
  1546.         null,
  1547.         u.uid,
  1548.         u.gid
  1549.     from sysusers u, syscolumns c, sysobjects o
  1550.     where o.id = @table_id
  1551.         and c.id = o.id
  1552.         and u.gid != u.uid
  1553.         and u.name != @owner_name
  1554.  
  1555.     /*
  1556.     ** we need to create another temporary table to contain all the various
  1557.     ** protection information for the table in question
  1558.     */
  1559.     create table #protects (uid smallint,
  1560.                             action tinyint,
  1561.                             protecttype tinyint,
  1562.                             name    varchar(32))
  1563.     insert into #protects
  1564.     select  p.uid,
  1565.             p.action,
  1566.             p.protecttype,
  1567.             isnull(col_name(id, c.number), "All")
  1568.         from sysprotects p, master.dbo.spt_values c,
  1569.                 master.dbo.spt_values a, master.dbo.spt_values b
  1570.             where convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1571.                 & c.high != 0
  1572.                 and c.number <=
  1573.                     (select count(*)
  1574.                         from syscolumns
  1575.                         where id = @table_id)
  1576.                 and a.type = "T"
  1577.                 and a.number = p.action
  1578.                 and b.type = "T"
  1579.                 and b.number = p.protecttype
  1580.                 and p.id = @table_id
  1581.                 and p.uid between @low and @high
  1582.  
  1583.  
  1584.     update #column_privileges 
  1585.         set select_privilege = 1 
  1586.         where exists 
  1587.             (select * from #protects
  1588.             where protecttype = 205
  1589.             and action = 193
  1590.             and ( name = #column_privileges.column_name
  1591.                   or name = 'All')
  1592.             and ( uid = 0 
  1593.                   or uid = #column_privileges.gid
  1594.                   or uid = #column_privileges.uid))
  1595.         and not exists (select * from #protects
  1596.             where protecttype = 206
  1597.             and action = 193
  1598.             and ( name = #column_privileges.column_name
  1599.                   or name = 'All')
  1600.             and ( uid = 0 
  1601.                   or uid = #column_privileges.gid
  1602.                   or uid = #column_privileges.uid))
  1603.  
  1604.   
  1605.           
  1606.     update #column_privileges 
  1607.         set insert_privilege = 1 
  1608.         where exists (select * from #protects 
  1609.             where protecttype = 205
  1610.             and action = 195
  1611.             and ( name = #column_privileges.column_name
  1612.                   or name = 'All')
  1613.             and  ( uid = 0 
  1614.                   or uid = #column_privileges.gid
  1615.                   or uid = #column_privileges.uid))
  1616.         and not exists (select * from #protects
  1617.             where protecttype = 206
  1618.             and action = 195
  1619.             and ( name = #column_privileges.column_name
  1620.                   or name = 'All')
  1621.             and ( uid = 0 
  1622.                   or uid = #column_privileges.gid
  1623.                   or uid = #column_privileges.uid))
  1624.  
  1625.  
  1626.             
  1627.     update #column_privileges 
  1628.         set insert_privilege = 1 
  1629.         where exists (select * from #protects 
  1630.             where protecttype = 205
  1631.             and action = 196
  1632.             and ( name = #column_privileges.column_name
  1633.                   or name = 'All')
  1634.             and ( uid = 0 
  1635.                   or uid = #column_privileges.gid
  1636.                   or uid = #column_privileges.uid))
  1637.         and not exists (select * from #protects
  1638.             where protecttype = 206
  1639.             and action = 196
  1640.             and ( name = #column_privileges.column_name
  1641.                   or name = 'All')
  1642.             and ( uid = 0 
  1643.                   or uid = #column_privileges.gid
  1644.                   or uid = #column_privileges.uid))
  1645.  
  1646.  
  1647.     update #column_privileges
  1648.         set update_privilege = 1 
  1649.         where exists (select * from #protects 
  1650.             where protecttype = 205
  1651.             and action = 197
  1652.             and ( name = #column_privileges.column_name
  1653.                   or name = 'All')
  1654.             and ( uid = 0 
  1655.                   or uid = #column_privileges.gid
  1656.                   or uid = #column_privileges.uid))
  1657.         and not exists (select * from #protects
  1658.             where protecttype = 206
  1659.             and action = 197
  1660.             and ( name = #column_privileges.column_name
  1661.                   or name = 'All')
  1662.             and ( uid = 0 
  1663.                   or uid = #column_privileges.gid
  1664.                   or uid = #column_privileges.uid))
  1665.  
  1666.  
  1667.           
  1668.     select 
  1669.             table_qualifier,
  1670.             table_owner,
  1671.             table_name,
  1672.             column_name,
  1673.             grantor,
  1674.             grantee,
  1675.             select_privilege,
  1676.             select_grantable,
  1677.             insert_privilege,
  1678.             insert_grantable,
  1679.             update_privilege,
  1680.             update_grantable,
  1681.             delete_privilege,
  1682.             delete_grantable,
  1683.             references_privilege,
  1684.             references_grantable
  1685.     from #column_privileges
  1686.         where column_name like @column_name
  1687.         and ( select_privilege != 0
  1688.         or insert_privilege != 0
  1689.         or delete_privilege != 0
  1690.         or update_privilege != 0)
  1691.  
  1692.  
  1693.  
  1694.  
  1695. drop table #column_privileges
  1696. drop table #protects
  1697. go
  1698. grant execute on sp_column_privileges to public
  1699. go
  1700. dump tran master with truncate_only
  1701.  
  1702.  
  1703.  
  1704. print "creating sp_server_info"
  1705. if (exists (select * from sysobjects where name = 'sp_server_info'))
  1706.     drop proc sp_server_info
  1707. go
  1708.  
  1709. create proc sp_server_info (@attribute_id  int = null)
  1710. as
  1711.     if @attribute_id is not null
  1712.         select *
  1713.         from master.dbo.MSserver_info
  1714.         where attribute_id = @attribute_id
  1715.     else
  1716.         select *
  1717.         from master.dbo.MSserver_info
  1718.  
  1719. go
  1720.  
  1721. grant execute on sp_server_info to public
  1722. go
  1723.  
  1724. print "creating sp_datatype_info"
  1725. if (exists (select * from sysobjects where name = 'sp_datatype_info'))
  1726.     drop proc sp_datatype_info
  1727. go
  1728.  
  1729. /* the messiness of 'data_type' was to get around the problem of
  1730. returning the correct lengths for user defined types.  the join
  1731. on the type name ensures all user defined types are returned, but
  1732. this puts a null in the data_type column.  by forcing an embedded
  1733. select and correlating it with the current row in systypes, we get
  1734. the correct data_type mapping even for user defined types  (kwg) */
  1735.  
  1736. create proc sp_datatype_info (@data_type int = 0)
  1737. as
  1738.     if @data_type = 0
  1739.         select
  1740.             type_name = t.name,
  1741.             d.data_type,
  1742.             t.length,
  1743.             d.literal_prefix,
  1744.             d.literal_suffix,
  1745.             d.create_params,
  1746.             d.nullable,
  1747.             d.case_sensitive,
  1748.             d.searchable
  1749.         from master.dbo.MSdatatype_info d, systypes t
  1750.         where d.ss_dtype = t.type and
  1751.           t.type not in (111,109,38,110)  /* get rid of nullable types */
  1752.     else
  1753.         select
  1754.             type_name = t.name,
  1755.             d.data_type,
  1756.             t.length,
  1757.             d.literal_prefix,
  1758.             d.literal_suffix,
  1759.             d.create_params,
  1760.             d.nullable,
  1761.             d.case_sensitive,
  1762.             d.searchable
  1763.         from master.dbo.MSdatatype_info d, systypes t
  1764.         where data_type = @data_type and
  1765.              d.ss_dtype = t.type and
  1766.           t.type not in (111,109,38,110)
  1767. go
  1768.  
  1769. grant execute on sp_datatype_info to public
  1770. go
  1771.  
  1772. dump tran master with truncate_only
  1773. go
  1774.  
  1775. print "creating sp_special_columns"
  1776. if (exists (select * from sysobjects where name = 'sp_special_columns'))
  1777.     drop proc sp_special_columns
  1778. go
  1779.  
  1780. dump tran master with truncate_only
  1781. go
  1782.  
  1783.  
  1784. CREATE PROCEDURE sp_special_columns (@table_name      varchar(32),
  1785.                              @table_owner      varchar(32) = null,
  1786.                              @table_qualifier varchar(32) = null,
  1787.                              @col_type       char(1) = 'R')
  1788. AS
  1789.     DECLARE @indid                int
  1790.     DECLARE @table_id            int
  1791.     DECLARE @full_table_name    char(70)
  1792.     DECLARE @msg          char(70)
  1793.  
  1794.     if @table_qualifier is not null
  1795.     begin
  1796.         if db_name() != @table_qualifier
  1797.         begin
  1798.             print "Table qualifier must be name of current database"
  1799.             return
  1800.         end
  1801.     end
  1802.     if @table_owner is null
  1803.     begin
  1804.         SELECT @full_table_name = @table_name
  1805.     end
  1806.     else
  1807.     begin
  1808.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1809.     end
  1810.     SELECT @table_id = object_id(@full_table_name)
  1811.     
  1812.   /* if we just need ROWVER, just run that query */
  1813.     if @col_type = 'V'
  1814.   BEGIN
  1815.     SELECT   
  1816.         scope = 1,
  1817.         column_name = c.name,
  1818.         d.data_type,
  1819.             type_name = t.name,
  1820.             data_precision=isnull(d.data_precision, c.length),
  1821.             c.length,
  1822.             d.numeric_scale
  1823.       FROM
  1824.             systypes t,syscolumns c,  master.dbo.MSdatatype_info d
  1825.       WHERE
  1826.           c.id=@table_id
  1827.           AND c.type = d.ss_dtype
  1828.       AND t.type = c.type 
  1829.       AND t.usertype = 80 
  1830.     RETURN
  1831.   END
  1832.   /* now find the id of the 'best' index for this table */
  1833.  
  1834.   SELECT @indid = (
  1835.     SELECT MIN(indid) 
  1836.       FROM sysindexes
  1837.           WHERE  status&2 = 2
  1838.       AND id = @table_id
  1839.       AND indid > 0)
  1840.  
  1841.    SELECT
  1842.       scope = 3,
  1843.       column_name = INDEX_COL(@full_table_name,indid,colid),
  1844.       d.data_type,
  1845.        d.type_name,
  1846.       data_precision=isnull(d.data_precision, c.length),
  1847.       c.length,
  1848.       d.numeric_scale
  1849.     FROM sysindexes x,syscolumns c,  master.dbo.MSdatatype_info d
  1850.     WHERE x.id=@table_id
  1851.         AND x.id=c.id
  1852.         AND c.colid<keycnt+ (x.status&16)/16
  1853.         AND x.indid=@indid
  1854.       AND c.type = d.ss_dtype
  1855.  
  1856. go  
  1857. grant execute on sp_special_columns to public
  1858. go
  1859.  
  1860.  
  1861.  
  1862.  
  1863. print "creating sp_databases"
  1864. if (exists (select * from sysobjects where name = 'sp_databases'))
  1865.   drop proc sp_databases
  1866. go
  1867.  
  1868. create procedure sp_databases
  1869.         as
  1870.         create table #databases ( database_name varchar(32),
  1871.                                   size int)
  1872.         insert into #databases                          
  1873.         select  name,
  1874.             (select sum(size) from master.dbo.sysusages
  1875.           where dbid = d.dbid )
  1876.         from master.dbo.sysdatabases d
  1877.         select database_name, database_size = size*2, remarks = null
  1878.         from #databases
  1879. go
  1880. grant execute on sp_databases to public
  1881. go
  1882.  
  1883. dump tran master with truncate_only
  1884. go
  1885.  
  1886.  
  1887.  
  1888.  
  1889.  
  1890.  
  1891. /*******************************************************************************/
  1892. /* This portion returns everything back to normal                   */
  1893. /*******************************************************************************/
  1894. use master
  1895. go
  1896.  
  1897. if exists (select * from sysobjects
  1898.            where name = 'sp_configure' and sysstat & 7 = 4)
  1899. begin
  1900.     execute sp_configure 'update',0
  1901. end
  1902. reconfigure with override
  1903. go
  1904. checkpoint
  1905. go
  1906.  
  1907.