home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a025 / 9.ddi / INSTALL / INST48.SQL < prev    next >
Encoding:
Text File  |  1992-08-24  |  60.3 KB  |  1,924 lines

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