home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / access / update11 / englisch / diskinst / a11_4e.exe / INSTCAT.SQL < prev    next >
Text File  |  1993-05-26  |  94KB  |  3,162 lines

  1. /*
  2. NOTE:  you MUST change the last row inserted into spt_server_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. NOTE:  The version number will be set automatically when you run
  10.        BUILD VER from the root of the SLM tree.
  11. */
  12.  
  13. /****************************************************************************/
  14. /* This portion sets up the ability to perform all the functions in this    */
  15. /* script                                                                    */
  16. /****************************************************************************/
  17. use master
  18. go
  19. dump tran master with truncate_only
  20. go
  21.  
  22. if exists (select * from sysobjects
  23.        where name = 'sp_configure' and sysstat & 7 = 4)
  24. begin
  25.     execute sp_configure 'update',1
  26. end
  27. reconfigure with override
  28. go
  29.  
  30. /*
  31. ** If old versions of tables exist, drop them.
  32. */
  33. if (exists (select * from sysobjects where name = 'MSdatatype_info'))
  34.     drop table MSdatatype_info
  35. go
  36. if (exists (select * from sysobjects where name = 'MSdatatype_info_ext'))
  37.     drop table MSdatatype_info_ext
  38. go
  39. if (exists (select * from sysobjects where name = 'MStable_types'))
  40.     drop table MStable_types
  41. go
  42. if (exists (select * from sysobjects where name = 'MSserver_info'))
  43.     drop table MSserver_info
  44. go
  45.  
  46. /*
  47. ** If tables already exist, drop them.
  48. */
  49.  
  50. if (exists (select * from sysobjects where name = 'spt_datatype_info'))
  51.     drop table spt_datatype_info
  52. go
  53. if (exists (select * from sysobjects where name = 'spt_datatype_info_ext'))
  54.     drop table spt_datatype_info_ext
  55. go
  56. if (exists (select * from sysobjects where name = 'spt_table_types'))
  57.     drop table spt_table_types
  58. go
  59. if (exists (select * from sysobjects where name = 'spt_server_info'))
  60.     drop table spt_server_info
  61. go
  62. if (exists (select * from sysobjects where name = 'sp_tables'))
  63.     drop proc sp_tables
  64. go
  65. if (exists (select * from sysobjects where name = 'sp_statistics'))
  66.     drop proc sp_statistics
  67. go
  68. if (exists (select * from sysobjects where name = 'sp_columns'))
  69.     drop proc sp_columns
  70. go
  71. if (exists (select * from sysobjects where name = 'sp_fkeys'))
  72.     drop proc sp_fkeys
  73. go
  74. if (exists (select * from sysobjects where name = 'sp_pkeys'))
  75.     drop proc sp_pkeys
  76. dump tran master with truncate_only
  77. go
  78.  
  79. go
  80. if (exists (select * from sysobjects where name = 'sp_stored_procedures'))
  81.     drop proc sp_stored_procedures
  82. go
  83. if (exists (select * from sysobjects where name = 'sp_sproc_columns'))
  84.     drop proc sp_sproc_columns
  85. go
  86. if (exists (select * from sysobjects where name = 'sp_table_privileges'))
  87.     drop proc sp_table_privileges
  88. go
  89. if (exists (select * from sysobjects where name = 'sp_column_privileges'))
  90.     drop proc sp_column_privileges
  91. go
  92. if (exists (select * from sysobjects where name = 'sp_server_info'))
  93.     drop proc sp_server_info
  94. go
  95. if (exists (select * from sysobjects where name = 'sp_datatype_info'))
  96.     drop proc sp_datatype_info
  97. go
  98. if (exists (select * from sysobjects where name = 'sp_special_columns'))
  99.     drop proc sp_special_columns
  100. go
  101. if (exists (select * from sysobjects where name = 'sp_databases'))
  102.     drop proc sp_databases
  103. go
  104.  
  105. dump tran master with truncate_only
  106. go
  107.  
  108. print "creating table spt_datatype_info_ext"
  109. go
  110. create table spt_datatype_info_ext (user_type  smallint     not null,
  111.                 create_params    varchar(32)  null)
  112. go
  113.  
  114. grant select on spt_datatype_info_ext to public
  115. go
  116.  
  117.  
  118. insert into spt_datatype_info_ext
  119. /* CHAR      user_type, create_params */
  120. values           (1,    "length" )
  121.  
  122. insert into spt_datatype_info_ext
  123. /* VARCHAR   user_type, create_params */
  124. values           (2,    "max length" )
  125.  
  126. insert into spt_datatype_info_ext
  127. /* BINARY    user_type, create_params */
  128. values           (3,    "length" )
  129.  
  130. insert into spt_datatype_info_ext
  131. /* VARBINARY user_type, create_params */
  132. values           (4,    "max length" )
  133.  
  134. insert into spt_datatype_info_ext
  135. /* SYSNAME   user_type, create_params */
  136. values           (18,   "max length" )
  137. go
  138.  
  139. print "creating table spt_datatype_info"
  140. go
  141. create table spt_datatype_info (ss_dtype         tinyint      not null,
  142.                 type_name          varchar(32)  not null,
  143.                 data_type          smallint     not null,
  144.                 data_precision     int          null,
  145.                 numeric_scale      smallint     null,
  146.                 numeric_radix      smallint     null,
  147.                 length             int          null,
  148.                 literal_prefix     varchar(32)  null,
  149.                 literal_suffix     varchar(32)  null,
  150.                 create_params      varchar(32)  null,
  151.                 nullable           smallint     not null,
  152.                 case_sensitive     smallint     not null,
  153.                 searchable         smallint     not null,
  154.                 unsigned_attribute smallint     null,
  155.                 money              smallint     not null,
  156.                 auto_increment     smallint     null,
  157.                 local_type_name    varchar(128) not null,
  158.                 aux                int          null)
  159. go
  160.  
  161. grant select on spt_datatype_info to public
  162. go
  163.  
  164. /* Get case sensitivity */
  165. if 'A' = 'A'
  166. begin
  167. declare @case smallint
  168.  
  169. select @case = 0
  170. select @case = 1 where 'a' != 'A'
  171.  
  172. insert into spt_datatype_info
  173. /* ss_type,name,   data_type,prec,scale,rdx, len, prf, suf, cp,          nul,case,srch,unsigned,money,auto, local,                       aux */
  174. values (45,"binary",-2,      null,null, null,null,"0x",null,"length",1,  0,   2,   null,    0,    null, /* Local Binary */ "binary", 0)
  175.  
  176. insert into spt_datatype_info
  177. /* ss_type,name, data_type, prec,scale,rdx,len,  prf, suf, cp,  nul, case,srch,unsigned,money,auto, local,                 aux */
  178. values (50,"bit",-7,        1,   0,    2,  null, null,null,null,0,   0,   2,   null,    0,    null, /* Local Bit */ "bit", 0)
  179.  
  180. insert into spt_datatype_info
  181. /* ss_type,name,  data_type,prec,scale,rdx, len, prf, suf, cp,          nul,case,    srch,unsigned,money,auto, local,                   aux */
  182. values (47,"char",1,        null,null, null,null,"'", "'", "length",1,  @case,   3,   null,    0,    null, /* Local Char */ "char", 0)
  183.  
  184. insert into spt_datatype_info
  185. /* ss_type,name,      data_type,prec,scale,rdx,len, prf, suf, cp,  nul,case,srch,unsigned,money,auto, local,                           aux */
  186. values (61,"datetime",11,       23,  3,    10, 16,  "'", "'", null, 1,  0,   3,   null,    0,    null, /* Local Datetime */ "datetime", 0)
  187.  
  188. insert into spt_datatype_info
  189. /* ss_type,name,           data_type,prec,scale,rdx,len, prf, suf, cp,  nul, case,srch,unsigned,money,auto, local,                                     aux */
  190. values (58,"smalldatetime",11,       16,  0,    10, 16,  "'", "'", null, 1,   0,   3,   null,    0,    null, /* Local Smalldatetime */ "smalldatetime", 0)
  191.  
  192. insert into spt_datatype_info  /* sql server type is "datetimn" */
  193. /* ss_type, name,      data_type,prec, scale,rdx,len, prf, suf, cp,  nul,case,srch,unsigned,money,auto, local,                           aux */
  194. values (111,"smalldatetime",0,    0,   0,    10,  0,  "'", "'", null, 1,  0,   3,   null,    0,    null, /* Local Datetimn */ "datetime", null)
  195.  
  196. insert into spt_datatype_info
  197. /* ss_type, name,  data_type,prec,scale,rdx,len, prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                     aux */
  198. values (62,"float",6,        15,  null, 10, null,null,null,null,1,  0,   2,   0,       0,    0,   /* Local Float */ "float", 0)
  199.  
  200. insert into spt_datatype_info  /* sql server type is "floatn" */
  201. /* ss_type, name,             data_type,prec,scale,rdx,len,  prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                                   aux */
  202. values (109,"float        real",0,        0,  null, 10, 0,   null,null,null, 1,  0,   2,   0,       0,    0, /* Local RealFloat */ "real      float", null)
  203.  
  204. insert into spt_datatype_info
  205. /* ss_type, name,  data_type,prec,scale,rdx,len,  prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                   aux */
  206. values (59, "real",7,        7,   null, 10, null, null,null,null, 1,  0,   2,   0,       0,    0,   /* Local Real */ "real", 0)
  207.  
  208. insert into spt_datatype_info
  209. /* ss_type, name,        data_type,prec, scale,rdx,len,  prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                               aux */
  210. values(122, "smallmoney",3,        10,   4,    10, null, "$", null,null, 1,  0,   2,   0,       1,    0,   /* Local Smallmoney */ "smallmoney", 0)
  211.  
  212. insert into spt_datatype_info
  213. /* ss_type, name,   data_type,prec, scale,rdx,len, prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                 aux */
  214. values (56, "int",  4,        10,   0,    10, null,null,null,null, 1,  0,   2,   0,       0,    0,   /* Local Int */ "int", 0)
  215.  
  216. insert into spt_datatype_info  /* sql server type is "intn" */
  217. /* ss_type, name,                data_type,prec,scale,rdx,len, prf, suf, cp,   nul,case,srch,unsigned,money,auto,local,                                 aux */
  218. values (38, "smallint     tinyint",0,      0,   0,    10,  0,  null,null,null,  1,  0,   2,   0,       0,    0,   /* Local Intn */ "tinyint   smallint", null)
  219.  
  220. insert into spt_datatype_info
  221. /* ss_type, name,    data_type,prec,scale,rdx,len, prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                     aux */
  222. values (60, "money", 3,        19,  4,    10, null,"$", null,null, 1,  0,   2,   0,       1,    0,   /* Local Money */ "money", 0)
  223.  
  224. insert into spt_datatype_info   /* sql server type is "moneyn" */
  225. /* ss_type, name,        data_type,prec,scale,rdx,len, prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                                aux */
  226. values (110,"smallmoney",  0,        0,  4,    10, 0,  "$", null,null, 1,  0,   2,   0,       1,    0,   /* Local Moneyn */ "smallmoneymoney", null)
  227.  
  228. insert into spt_datatype_info
  229. /* ss_type, name,      data_type,prec,scale,rdx, len, prf, suf, cp,  nul,case,srch,unsigned,money,auto,local,                           aux */
  230. values (52, "smallint", 5,        5,   0,    10, null,null,null,null, 1,  0,   2,   0,       0,    0,   /* Local Smallint */ "smallint", 0)
  231.  
  232. insert into spt_datatype_info
  233. /* ss_type,name,  data_type,prec,      scale,rdx,  len,       prf, suf,cp,  nul,case,    srch,unsigned,money,auto, local,                   aux */
  234. values (35,"text",-1,       2147483647,null, null, 2147483647,"'", "'",null, 1, @case,    1,  null,    0,    null, /* Local Text */ "text", 0)
  235.  
  236. insert into spt_datatype_info
  237. /* ss_type,name,       data_type,prec,scale,rdx, len, prf, suf,  cp,          nul,case,srch,unsigned,money,auto, local,                             aux */
  238. values (37,"varbinary",-3,       null,null, null,null,"0x",null, "max length", 1,  0,   2,  null,    0,    null, /* Local Varbinary */ "varbinary", 0)
  239.  
  240. insert into spt_datatype_info
  241. /* ss_type, name,     data_type,prec, scale,rdx,len, prf, suf,  cp,  nul,case,srch,unsigned,money,auto,local,                         aux */
  242. values (48, "tinyint",-6,       3,    0,    10, null,null,null, null, 1,  0,   2,   1,       0,    0,   /* Local Tinyint */ "tinyint", 0)
  243.  
  244. insert into spt_datatype_info
  245. /* ss_type,name,     data_type,prec,scale,rdx, len, prf, suf, cp,          nul,case, srch,unsigned,money,auto, local,                         aux */
  246. values (39,"varchar",12,       null,null, null,null,"'", "'", "max length", 1, @case, 3,   null,    0,    null, /* Local Varchar */ "varchar", 0)
  247.  
  248. insert into spt_datatype_info
  249. /* ss_type,name,   data_type,prec,      scale,rdx, len,       prf, suf, cp,  nul,case,srch,unsigned,money,auto, local,                     aux */
  250. values (34,"image",-4,       2147483647,null, null,2147483647,"0x",null,null,1,  0,   1,   null,    0,    null, /* Local Image */ "image", 0)
  251. end
  252. go
  253.  
  254. print "creating table spt_table_types"
  255. go
  256. create table spt_table_types (ss_type char(1),sag_type varchar(32))
  257. go
  258.  
  259. insert into spt_table_types
  260.     values ('S','SYSTEM TABLE')
  261. insert into spt_table_types
  262.     values ('U','TABLE')
  263. insert into spt_table_types
  264.     values ('V','VIEW')
  265. go
  266.  
  267. dump tran master with truncate_only
  268. go
  269.  
  270. print "creating table spt_server_info"
  271. go
  272. create table spt_server_info (attribute_id      int,
  273.               attribute_name  varchar(60),
  274.               attribute_value varchar(255))
  275. go
  276.  
  277. insert into spt_server_info
  278.     values (1,"DBMS_NAME","SQL Server")
  279. insert into spt_server_info
  280.     values (2,"DBMS_VER",@@version)
  281. insert into spt_server_info
  282.     values (6,"DBE_NAME","")
  283. insert into spt_server_info
  284.     values (10,"OWNER_TERM","owner")
  285. insert into spt_server_info
  286.     values (11,"TABLE_TERM","table")
  287. insert into spt_server_info
  288.     values (12,"MAX_OWNER_NAME_LENGTH","30")
  289. insert into spt_server_info
  290.     values (16,"IDENTIFIER_CASE","MIXED")
  291. insert into spt_server_info
  292.     values (15,"COLUMN_LENGTH","30")
  293. insert into spt_server_info
  294.     values (13,"TABLE_LENGTH","30")
  295. insert into spt_server_info
  296.     values (100,"USERID_LENGTH","30")
  297. insert into spt_server_info
  298.     values (17,"TX_ISOLATION","2")
  299. insert into spt_server_info
  300.     values (18,"COLLATION_SEQ","")
  301. insert into spt_server_info
  302.     values (14,"MAX_QUAL_LENGTH","30")
  303. insert into spt_server_info
  304.     values (101,"QUALIFIER_TERM","database")
  305. insert into spt_server_info
  306.     values (19,"SAVEPOINT_SUPPORT","Y")
  307. insert into spt_server_info
  308.     values (20,"MULTI_RESULT_SETS","Y")
  309. insert into spt_server_info
  310.     values (102,"NAMED_TRANSACTIONS","Y")
  311. insert into spt_server_info
  312.     values (103,"SPROC_AS_LANGUAGE","Y")
  313. insert into spt_server_info
  314.     values (103,"REMOTE_SPROC","Y")
  315. insert into spt_server_info
  316.     values (22,"ACCESSIBLE_TABLES","Y")
  317. insert into spt_server_info
  318.     values (104,"ACCESSIBLE_SPROC","Y")
  319. insert into spt_server_info
  320.     values (105,"MAX_INDEX_COLS","16")
  321. insert into spt_server_info
  322.     values (106,"RENAME_TABLE","Y")
  323. insert into spt_server_info
  324.     values (107,"RENAME_COLUMN","Y")
  325. insert into spt_server_info
  326.     values (108,"DROP_COLUMN","N")
  327. insert into spt_server_info
  328.     values (109,"INCREASE_COLUMN_LENGTH","N")
  329. insert into spt_server_info
  330.     values (110,"DDL_IN_TRANSACTION","N")
  331. insert into spt_server_info
  332.     values (111,"DESCENDING_INDEXES","N")
  333. insert into spt_server_info
  334.     values (112,"SP_RENAME","Y")
  335. insert into spt_server_info
  336.     values (500,"SYS_SPROC_VERSION","01.01.2807")
  337. go
  338.  
  339. grant select on spt_server_info to public
  340. go
  341.  
  342. print "creating sp_tables"
  343. go
  344.  
  345. create procedure sp_tables(@table_name        varchar(32)  = null,
  346.                @table_owner     varchar(32)  = null,
  347.                @table_qualifier varchar(32)  = null,
  348.                @table_type        varchar(100) = null)
  349. as
  350.     declare @type1 char(1),
  351.         @type2 char(1),
  352.         @type3 char(1),
  353.         @tableindex int
  354.  
  355.  
  356.             /* Special feature #1:  enumerate databases when owner and name
  357.                  are blank but qualifier is explicitly '%'.  */
  358.         if @table_qualifier = '%' and
  359.              @table_owner = '' and
  360.              @table_name = ''
  361.             begin
  362.                 select
  363.         table_qualifier = name,
  364.           table_owner=null,
  365.         table_name=null,
  366.       table_type = 'Database',
  367.         remarks = convert(varchar(254),null)
  368.              from master..sysdatabases
  369.             where name != 'model'
  370.             end
  371.             /* Special feature #2:  enumerate owners when qualifier and name
  372.                  are blank but owner is explicitly '%'.  */
  373.         else if @table_qualifier = '' and
  374.                       @table_owner = '%' and
  375.                         @table_name = ''
  376.             begin
  377.                 select distinct
  378.         table_qualifier = null,
  379.           table_owner=user_name(uid),
  380.         table_name=null,
  381.       table_type = 'Owner',
  382.         remarks = convert(varchar(254),null)
  383.              from sysobjects
  384.             end
  385.         else  /* end of special features -- do normal processing */
  386.             begin
  387.     if @table_qualifier is not null
  388.     begin
  389.     if db_name() != @table_qualifier
  390.     begin
  391.     if @table_qualifier = ''
  392.     begin
  393.         /* in this case, we need to return an empty result set */
  394.         /* because the user has requested a database with an empty name */
  395.         select @table_name = ''
  396.         select @table_owner = ''
  397.     end else
  398.     begin
  399.         print "Table qualifier must be name of current database"
  400.         return
  401.     end
  402.     end
  403.     end
  404.     if @table_type is null
  405.     begin
  406.     select @type1 = 'U'
  407.     select @type2 = 'V'
  408.     select @type3 = 'S'
  409.     end
  410.     else
  411.     begin
  412.     if (charindex("'TABLE'",@table_type) != 0)
  413.         select @type1 = 'U'
  414.     else
  415.         select @type1 = 'X'
  416.     if (charindex("'SYSTEM TABLE'",@table_type) != 0)
  417.         select @type2 = 'S'
  418.     else
  419.         select @type2 = 'X'
  420.     if (charindex("'VIEW'",@table_type) != 0)
  421.         select @type3 = 'V'
  422.     else
  423.         select @type3 = 'X'
  424.     end
  425.  
  426.     if @table_name is null
  427.     begin
  428.     select @table_name = '%'
  429.     end
  430.     else begin
  431.     if (@table_owner is null) and (charindex('%', @table_name) = 0)
  432.     begin
  433.         if exists (select * from sysobjects
  434.             where uid = user_id()
  435.             and name = @table_name
  436.             and (type = 'U' or type = 'V' or type = 'S'))
  437.         begin
  438.         select @table_owner = user_name()
  439.         end
  440.     end
  441.     end
  442.     if @table_owner is null
  443.     select @table_owner = '%'
  444.     select
  445.     table_qualifier = db_name(),
  446.     table_owner=user_name(o.uid),
  447.     table_name=o.name,
  448.     table_type = t.sag_type,
  449.     remarks = convert(varchar(254),null)
  450.     from
  451.     sysusers u,sysobjects o,master.dbo.spt_table_types t
  452.     where
  453.     o.name like @table_name
  454.     and user_name(o.uid) like @table_owner
  455.     and o.type = t.ss_type
  456.     and ( o.type = @type1 or o.type = @type2 or o.type = @type3)
  457.     and u.uid=user_id() /* constrain sysusers uid for use in subquery */
  458.     and (suser_id()=1    /* User is the System Administrator */
  459.          or o.uid=user_id()   /* User created the object */
  460.          /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  461.          or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  462.           from sysprotects p
  463.           /* outer join to correlate with all rows in sysobjects */
  464.           where p.id =* o.id
  465.               /* get rows for public,current user,user's group */
  466.               and (p.uid=0 or p.uid=user_id() or p.uid=*u.gid)
  467.               /* check for SELECT,EXECUTE privilege */
  468.               and (action in (193,224)))&1     /* more magic...normalize GRANT */
  469.         )=1    /* final magic...compare Grants    */
  470.         )
  471.     order by table_type, table_qualifier, table_owner, table_name
  472.         end
  473. go
  474.  
  475. grant execute on sp_tables to public
  476. go
  477.  
  478. dump tran master with truncate_only
  479. go
  480.  
  481. print "creating sp_statistics"
  482. go
  483.  
  484.  
  485. if  (charindex('4.8', @@version) = 0 
  486.     and charindex('4.9', @@version) = 0)
  487. begin
  488.     print ""
  489.     print ""
  490.     print "Warning:"
  491.     print "you are installing the stored procedures "
  492.     print "on SQL Server with version less than 4.8, "
  493.     print  "ignore the following error"
  494. end
  495. go
  496.  
  497. CREATE PROCEDURE sp_statistics (@table_name      varchar(32),
  498.                  @table_owner      varchar(32) = null,
  499.                  @table_qualifier varchar(32) = null,
  500.                  @index_name      varchar(32) = '%',
  501.                  @is_unique       char(1) = 'N')
  502. AS
  503.     DECLARE @indid                int
  504.     DECLARE @lastindid            int
  505.     DECLARE @table_id            int
  506.     DECLARE @full_table_name    char(70)
  507.     if @table_qualifier is not null
  508.     begin
  509.     if db_name() != @table_qualifier
  510.     begin
  511.         print "Table qualifier must be name of current database"
  512.         return
  513.     end
  514.     end
  515.     if @@trancount != 0
  516.     begin
  517.     raiserror 20001 "stored procedure sp_statistics can not be run while in a transaction"
  518.     return
  519.     end
  520.     create table #TmpIndex( table_qualifier varchar(32),
  521.                 table_owner     varchar(32),
  522.                 table_name      varchar(32),
  523.                 index_qualifier varchar(32) null,
  524.                 index_name      varchar(32) null,
  525.                 non_unique      smallint null,
  526.                 type            smallint,
  527.                 seq_in_index    smallint null,
  528.                 column_name     varchar(32) null,
  529.                 collation       char(1) null,
  530.                 remarks         varchar(255) null,
  531.                 index_id        int null,
  532.                 cardinality     int null,
  533.                 pages           int null,
  534.                 status          smallint)
  535.     if @table_owner is null
  536.     begin
  537.     SELECT @full_table_name = @table_name
  538.     end
  539.     else
  540.     begin
  541.     SELECT @full_table_name = @table_owner + '.' + @table_name
  542.     end
  543.     SELECT @table_id = object_id(@full_table_name)
  544.  
  545.  
  546.     SELECT    @indid=min(indid)
  547.     FROM sysindexes
  548.     WHERE id=@table_id
  549.     AND indid > 0
  550.     AND indid < 255
  551.     
  552.     WHILE @indid != NULL
  553.     BEGIN
  554.     INSERT #TmpIndex
  555.     SELECT
  556.         DB_NAME(),                               /* table_qualifier */
  557.         USER_NAME(o.uid),                        /* table_owner     */
  558.         o.name,                                  /* table_name      */
  559.         o.name,                                     /* index_qualifier */
  560.         x.name,                                  /* index_name      */
  561.         0,                                       /* non_unique      */
  562.         1,                                       /* type            */
  563.         colid,                                   /* seq_in_index    */
  564.         INDEX_COL(@full_table_name,indid,colid), /* column_name     */
  565.         "A",                                     /* collation       */
  566.         "",                                      /* remarks         */
  567.         @indid,                                  /* index_id        */
  568.         rowcnt(x.doampg),                                  /* cardinality     */
  569.         data_pgs(x.id,doampg),                                /* pages           */
  570.         x.status                                 /* status          */
  571.     FROM sysindexes x,syscolumns c,sysobjects o
  572.     WHERE x.id=@table_id
  573.         AND x.id = o.id
  574.         AND x.id=c.id
  575.         AND c.colid<keycnt+ (x.status&16)/16
  576.         AND x.indid=@indid
  577.     /*
  578.     **    Now move @indid to the next index.
  579.     */
  580.     SELECT @lastindid = @indid
  581.     SELECT @indid = NULL
  582.     SELECT @indid = min(indid)
  583.     FROM sysindexes
  584.     WHERE id = @table_id
  585.         AND indid > @lastindid
  586.         AND indid < 255
  587.     END
  588.    
  589.     UPDATE #TmpIndex
  590.     SET non_unique = 1
  591.     WHERE  status&2 != 2
  592.     UPDATE #TmpIndex
  593.     SET type = 3, cardinality = NULL, pages = NULL
  594.     WHERE  index_id > 1
  595.  
  596.  
  597.      /* now add row for table statistics */
  598.  
  599.  
  600.       INSERT #TmpIndex  
  601.     SELECT
  602.         DB_NAME(),          /* table_qualifier */
  603.         USER_NAME(o.uid),   /* table_owner     */
  604.         o.name,             /* table_name      */
  605.         null,               /* index_qualifier */ 
  606.         null,               /* index_name      */
  607.         null,               /* non_unique      */
  608.         0,                  /* type            */
  609.         null,               /* seq_in_index    */
  610.         null,               /* column_name     */
  611.         null,               /* collation       */
  612.         "",                 /* remarks         */
  613.         0,                  /* index_id        */
  614.         rowcnt(x.doampg),                                  /* cardinality     */
  615.         data_pgs(x.id,doampg),                                /* pages           */
  616.         0                   /* status          */
  617.     FROM sysindexes x, sysobjects o
  618.     WHERE o.id=@table_id
  619.         AND x.id = o.id
  620.         AND (x.indid=0 or x.indid=1)
  621.  
  622.  
  623.     if @is_unique != 'Y'
  624.     SELECT
  625.         table_qualifier, 
  626.         table_owner,     
  627.         table_name,       
  628.         non_unique,       
  629.         index_qualifier, 
  630.         index_name,       
  631.         type,             
  632.         seq_in_index,     
  633.         column_name,     
  634.         collation,
  635.         cardinality,     
  636.         pages             
  637.      FROM #TmpIndex       
  638.     WHERE index_name like @index_name or index_name is null   
  639.     ORDER BY non_unique,type,index_name,seq_in_index         
  640.     else
  641.     SELECT
  642.         table_qualifier,
  643.         table_owner,
  644.         table_name,
  645.         non_unique,
  646.         index_qualifier,
  647.         index_name,
  648.         type,
  649.         seq_in_index,
  650.         column_name,
  651.         collation,
  652.         cardinality,
  653.         pages
  654.     FROM #TmpIndex
  655.     WHERE (non_unique = 0 or non_unique is NULL)
  656.         and (index_name like @index_name or index_name is null)
  657.     ORDER BY non_unique,type,index_name,seq_in_index
  658.     DROP TABLE #TmpIndex
  659. go
  660.  
  661. if object_id('#TmpIndex') != null
  662.     drop table #TmpIndex
  663.  
  664. dump tran master with truncate_only
  665. go
  666.  
  667.  
  668. if  (charindex('4.8', @@version) != 0 
  669.     or charindex('4.9', @@version) != 0)
  670. begin
  671.     print ""
  672.     print ""
  673.     print "Warning:"
  674.     print "you are installing the stored procedures "
  675.     print "on SQL Server with version 4.8 or greater. "
  676.     print  "Ignore the following error."
  677. end
  678. go
  679.  
  680.  
  681. CREATE PROCEDURE sp_statistics (@table_name      varchar(32),
  682.                  @table_owner      varchar(32) = null,
  683.                  @table_qualifier varchar(32) = null,
  684.                  @index_name      varchar(32) = '%',
  685.                  @is_unique       char(1) = 'N')
  686. AS
  687.     DECLARE @indid                int
  688.     DECLARE @lastindid            int
  689.     DECLARE @table_id            int
  690.     DECLARE @full_table_name    char(70)
  691.     if @table_qualifier is not null
  692.     begin
  693.     if db_name() != @table_qualifier
  694.     begin
  695.         print "Table qualifier must be name of current database"
  696.         return
  697.     end
  698.     end
  699.     if @@trancount != 0
  700.     begin
  701.     raiserror 20001 "stored procedure sp_statistics can not be run while in a transaction"
  702.     return
  703.     end
  704.     create table #TmpIndex( table_qualifier varchar(32),
  705.                 table_owner     varchar(32),
  706.                 table_name      varchar(32),
  707.                 index_qualifier varchar(32) null,
  708.                 index_name      varchar(32) null,
  709.                 non_unique      smallint null,
  710.                 type            smallint,
  711.                 seq_in_index    smallint null,
  712.                 column_name     varchar(32) null,
  713.                 collation       char(1) null,
  714.                 remarks         varchar(255) null,
  715.                 index_id        int null,
  716.                 cardinality     int null,
  717.                 pages           int null,
  718.                 status          smallint)
  719.     if @table_owner is null
  720.     begin
  721.     SELECT @full_table_name = @table_name
  722.     end
  723.     else
  724.     begin
  725.     SELECT @full_table_name = @table_owner + '.' + @table_name
  726.     end
  727.     SELECT @table_id = object_id(@full_table_name)
  728.  
  729.  
  730.     SELECT    @indid=min(indid)
  731.     FROM sysindexes
  732.     WHERE id=@table_id
  733.     AND indid > 0
  734.     AND indid < 255
  735.  
  736.     WHILE @indid != NULL
  737.     BEGIN
  738.     INSERT #TmpIndex
  739.     SELECT
  740.         DB_NAME(),                               /* table_qualifier */
  741.         USER_NAME(o.uid),                        /* table_owner     */
  742.         o.name,                                  /* table_name      */
  743.         o.name,                                     /* index_qualifier */
  744.         x.name,                                  /* index_name      */
  745.         0,                                       /* non_unique      */
  746.         1,                                       /* type            */
  747.         colid,                                   /* seq_in_index    */
  748.         INDEX_COL(@full_table_name,indid,colid), /* column_name     */
  749.         "A",                                     /* collation       */
  750.         "",                                      /* remarks         */
  751.         @indid,                                  /* index_id        */
  752.         x.rows,                                  /* cardinality     */
  753.         x.dpages,                                /* pages           */
  754.         x.status                                 /* status          */
  755.     FROM sysindexes x,syscolumns c,sysobjects o
  756.     WHERE x.id=@table_id
  757.         AND x.id = o.id
  758.         AND x.id=c.id
  759.         AND c.colid<keycnt+ (x.status&16)/16
  760.         AND x.indid=@indid
  761.     /*
  762.     **    Now move @indid to the next index.
  763.     */
  764.     SELECT @lastindid = @indid
  765.     SELECT @indid = NULL
  766.     SELECT @indid = min(indid)
  767.     FROM sysindexes
  768.     WHERE id = @table_id
  769.         AND indid > @lastindid
  770.         AND indid < 255
  771.     END
  772.  
  773.     UPDATE #TmpIndex
  774.     SET non_unique = 1
  775.     WHERE  status&2 != 2
  776.     UPDATE #TmpIndex
  777.     SET type = 3, cardinality = NULL, pages = NULL
  778.     WHERE  index_id > 1
  779.  
  780.  
  781.      /* now add row for table statistics */
  782.  
  783.  
  784.       INSERT #TmpIndex
  785.     SELECT
  786.         DB_NAME(),          /* table_qualifier */
  787.         USER_NAME(o.uid),   /* table_owner     */
  788.         o.name,             /* table_name      */
  789.         null,               /* index_qualifier */
  790.         null,               /* index_name      */
  791.         null,               /* non_unique      */
  792.         0,                  /* type            */
  793.         null,               /* seq_in_index    */
  794.         null,               /* column_name     */
  795.         null,               /* collation       */
  796.         "",                 /* remarks         */
  797.         0,                  /* index_id        */
  798.         x.rows,             /* cardinality     */
  799.         x.dpages,           /* pages           */
  800.         0                   /* status          */
  801.     FROM sysindexes x, sysobjects o
  802.     WHERE o.id=@table_id
  803.         AND x.id = o.id
  804.         AND (x.indid=0 or x.indid=1)
  805.  
  806.  
  807.     if @is_unique != 'Y'
  808.     SELECT
  809.         table_qualifier,
  810.         table_owner,
  811.         table_name,
  812.         non_unique,
  813.         index_qualifier,
  814.         index_name,
  815.         type,
  816.         seq_in_index,
  817.         column_name,
  818.         collation,
  819.         cardinality,
  820.         pages
  821.      FROM #TmpIndex
  822.     WHERE index_name like @index_name or index_name is null
  823.     ORDER BY non_unique,type,index_name,seq_in_index
  824.     else
  825.     SELECT
  826.         table_qualifier,
  827.         table_owner,
  828.         table_name,
  829.         non_unique,
  830.         index_qualifier,
  831.         index_name,
  832.         type,
  833.         seq_in_index,
  834.         column_name,
  835.         collation,
  836.         cardinality,
  837.         pages
  838.     FROM #TmpIndex
  839.     WHERE (non_unique = 0 or non_unique is NULL)
  840.         and (index_name like @index_name or index_name is null)
  841.     ORDER BY non_unique,type,index_name,seq_in_index
  842.     DROP TABLE #TmpIndex
  843. go
  844.  
  845. grant execute on sp_statistics to public
  846. go
  847.  
  848. dump tran master with truncate_only
  849. go
  850.  
  851.  
  852. print "creating sp_columns"
  853. go
  854.  
  855. /* this is the version for servers  without support for UNION */
  856. CREATE PROCEDURE sp_columns (@table_name varchar(32),
  857.                  @table_owner varchar(32) = null,
  858.                  @table_qualifier varchar(32) = null,
  859.                  @column_name varchar(32) = null )
  860. AS
  861.     if @column_name is null
  862.     select @column_name = '%'
  863.     DECLARE @full_table_name    char(70)
  864.     DECLARE @table_id int
  865.     if @table_qualifier is not null
  866.     begin
  867.     if db_name() != @table_qualifier
  868.     begin
  869.         print "Table qualifier must be name of current database"
  870.         return
  871.     end
  872.     end
  873.         if @table_name is null
  874.         begin
  875.             select @table_name='%'
  876.         end
  877.     if @table_owner is null
  878.     begin
  879.     SELECT @full_table_name = @table_name
  880.     end
  881.     else
  882.     begin
  883.     SELECT @full_table_name = @table_owner + '.' + @table_name
  884.     end
  885.     SELECT @table_id = 0
  886.     SELECT @table_id = object_id(@full_table_name)
  887.     if ((charindex('%',@full_table_name) = 0) and
  888.        (charindex('_',@full_table_name) = 0)  and
  889.     @table_id != 0)
  890.     begin
  891.     /* this block is for the case where there is no pattern
  892.          matching required for the table name */
  893.     SELECT
  894.         table_qualifier = DB_NAME(),
  895.         table_owner = USER_NAME(o.uid),
  896.         table_name = o.name,
  897.         column_name = c.name,
  898.         data_type=d.data_type+convert(smallint,
  899.                     isnull(d.aux,
  900.                     ascii(substring("666AAA@@@CB??GG",
  901.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  902.                     -60)),
  903.         type_name=rtrim(substring(d.type_name,
  904.                     1+isnull(d.aux,
  905.                     ascii(substring("III<<<MMMI<<A<A",
  906.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  907.                     1))-60), 13)),
  908.         precision=isnull(d.data_precision, convert(int,c.length))
  909.                 +isnull(d.aux, convert(int,
  910.                 ascii(substring("???AAAFFFCKFOLS",
  911.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  912.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  913.                     isnull(d.aux,
  914.                     ascii(substring("AAA<BB<DDDHJSPP",
  915.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  916.                     1))-64)),
  917.         scale = d.numeric_scale +convert(smallint,
  918.                     isnull(d.aux,
  919.                     ascii(substring("<<<<<<<<<<<<<<?",
  920.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  921.                     1))-60)),
  922.         radix=d.numeric_radix,
  923.         nullable=convert(smallint, convert(bit, c.status&8)),
  924.         remarks = convert(varchar(254),null),
  925.         ss_data_type = c.type
  926.     FROM
  927.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  928.     WHERE
  929.         o.id=@table_id
  930.         AND c.id = o.id
  931.         AND c.type = d.ss_dtype
  932.     AND c.usertype *= t.usertype
  933.         AND c.name like @column_name
  934.  
  935.     end else
  936.     begin
  937.     if @table_owner is null
  938.         select @table_owner = '%'
  939.                 /* this block is for the case where there IS pattern
  940.          matching done on the table name */
  941.     SELECT
  942.         table_qualifier = DB_NAME(),
  943.         table_owner = USER_NAME(o.uid),
  944.         table_name = o.name,
  945.         column_name = c.name,
  946.         data_type=d.data_type+convert(smallint,
  947.                     isnull(d.aux,
  948.                     ascii(substring("666AAA@@@CB??GG",
  949.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  950.                     -60)),
  951.         type_name=rtrim(substring(d.type_name,
  952.                     1+isnull(d.aux,
  953.                     ascii(substring("III<<<MMMI<<A<A",
  954.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  955.                     1))-60), 13)),
  956.         precision=isnull(d.data_precision, convert(int,c.length))
  957.                 +isnull(d.aux, convert(int,
  958.                 ascii(substring("???AAAFFFCKFOLS",
  959.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  960.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  961.                     isnull(d.aux,
  962.                     ascii(substring("AAA<BB<DDDHJSPP",
  963.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  964.                     1))-64)),
  965.         scale = d.numeric_scale +convert(smallint,
  966.                     isnull(d.aux,
  967.                     ascii(substring("<<<<<<<<<<<<<<?",
  968.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  969.                     1))-60)),
  970.         radix=d.numeric_radix,
  971.         nullable=convert(smallint, convert(bit, c.status&8)),
  972.         remarks = convert(varchar(254),null),
  973.         ss_data_type = c.type,
  974.         c.colid
  975.     FROM
  976.         syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  977.  
  978.     WHERE
  979.         o.name like @table_name
  980.         AND user_name(o.uid) like @table_owner
  981.         AND c.id = o.id
  982.     AND c.usertype *= t.usertype
  983.         AND c.type = d.ss_dtype
  984.         AND o.type != 'P'
  985.         AND c.name like @column_name
  986.     ORDER BY table_qualifier, table_owner, table_name, colid
  987.     end
  988. go
  989.  
  990.  
  991. /* if this is a 4.2 or later SQL Server, then we want to delete the stored
  992. procedure we just created, and create a new one which uses UNION
  993. (this fixes some bugs involving UDT names and char NULL datatype
  994. names)
  995. */
  996.  
  997. if  (charindex('1.1', @@version) = 0
  998.     and charindex('4.0', @@version) = 0)
  999. begin
  1000.     drop proc sp_columns
  1001.     dump tran master with truncate_only
  1002. end
  1003. else
  1004. begin
  1005.     print ""
  1006.     print ""
  1007.     print "Installing catalog procedures on a 1.x or 4.0 server:"
  1008.     print "Ignore the following error messages"
  1009.     print ""
  1010.     print ""
  1011. end
  1012. go
  1013.  
  1014.  
  1015.  
  1016. /* this is the version for servers which support UNION */
  1017. CREATE PROCEDURE sp_columns (@table_name varchar(32),
  1018.                  @table_owner varchar(32) = null,
  1019.                  @table_qualifier varchar(32) = null,
  1020.                  @column_name varchar(32) = null )
  1021. AS
  1022.     if @column_name is null
  1023.     select @column_name = '%'
  1024.     DECLARE @full_table_name    char(70)
  1025.     DECLARE @table_id int
  1026.     if @table_qualifier is not null
  1027.     begin
  1028.     if db_name() != @table_qualifier
  1029.     begin
  1030.         print "Table qualifier must be name of current database"
  1031.         return
  1032.     end
  1033.     end
  1034.         if @table_name is null
  1035.         begin
  1036.             select @table_name='%'
  1037.         end
  1038.     if @table_owner is null
  1039.     begin
  1040.     SELECT @full_table_name = @table_name
  1041.     end
  1042.     else
  1043.     begin
  1044.     SELECT @full_table_name = @table_owner + '.' + @table_name
  1045.     end
  1046.     SELECT @table_id = 0
  1047.     SELECT @table_id = object_id(@full_table_name)
  1048.     if ((charindex('%',@full_table_name) = 0) and
  1049.        (charindex('_',@full_table_name) = 0)  and
  1050.     @table_id != 0)
  1051.     begin
  1052.     /* this block is for the case where there is no pattern
  1053.          matching required for the table name */
  1054.     SELECT  /* INTn, FLOATn, DATETIMEn and MONEYn types */
  1055.         table_qualifier = DB_NAME(),
  1056.         table_owner = USER_NAME(o.uid),
  1057.         table_name = o.name,
  1058.         column_name = c.name,
  1059.         data_type=d.data_type+convert(smallint,
  1060.                     isnull(d.aux,
  1061.                     ascii(substring("666AAA@@@CB??GG",
  1062.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  1063.                     -60)),
  1064.         type_name=rtrim(substring(d.type_name,
  1065.                     1+isnull(d.aux,
  1066.                     ascii(substring("III<<<MMMI<<A<A",
  1067.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1068.                     1))-60), 13)),
  1069.         precision=isnull(d.data_precision, convert(int,c.length))
  1070.                 +isnull(d.aux, convert(int,
  1071.                 ascii(substring("???AAAFFFCKFOLS",
  1072.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  1073.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  1074.                     isnull(d.aux,
  1075.                     ascii(substring("AAA<BB<DDDHJSPP",
  1076.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1077.                     1))-64)),
  1078.         scale = d.numeric_scale +convert(smallint,
  1079.                     isnull(d.aux,
  1080.                     ascii(substring("<<<<<<<<<<<<<<?",
  1081.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1082.                     1))-60)),
  1083.         radix=d.numeric_radix,
  1084.         nullable=convert(smallint, convert(bit, c.status&8)),
  1085.         remarks = convert(varchar(254),null),
  1086.         ss_data_type = c.type,
  1087.         colid = c.colid
  1088.     FROM
  1089.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  1090.     WHERE
  1091.         o.id=@table_id
  1092.         AND c.id = o.id
  1093.         AND c.type = d.ss_dtype
  1094.         AND c.name like @column_name
  1095.         AND d.ss_dtype IN (111, 109, 38, 110)
  1096.         AND c.usertype < 100
  1097.     UNION
  1098.     SELECT  /* All other types including user data types */
  1099.         table_qualifier = DB_NAME(),
  1100.         table_owner = USER_NAME(o.uid),
  1101.         table_name = o.name,
  1102.         column_name = c.name,
  1103.         data_type=convert(smallint,
  1104.                         /*      SS-Type                                          1                */
  1105.                         /*                       33 3 3         4 44 5 5 2 5 55666*/
  1106.                         /*                       45 7 9         5 78 0 2 2 6 89012*/
  1107.                     ascii(substring("8;<9<H<<<<<:<=6<5<A<?<@<GC?GD",
  1108.                     t.type%34+1,1))-60),
  1109.         type_name=t.name,
  1110.         precision=isnull(d.data_precision, convert(int,c.length))
  1111.                 +isnull(d.aux, convert(int,
  1112.                 ascii(substring("???AAAFFFCKFOLS",
  1113.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  1114.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  1115.                     isnull(d.aux,
  1116.                     ascii(substring("AAA<BB<DDDHJSPP",
  1117.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1118.                     1))-64)),
  1119.         scale = d.numeric_scale +convert(smallint,
  1120.                     isnull(d.aux,
  1121.                     ascii(substring("<<<<<<<<<<<<<<?",
  1122.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1123.                     1))-60)),
  1124.         radix=d.numeric_radix,
  1125.         nullable=convert(smallint, convert(bit, c.status&8)),
  1126.         remarks = c.name,
  1127.         ss_data_type = c.type,
  1128.         colid = c.colid
  1129.     FROM
  1130.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  1131.     WHERE
  1132.         o.id=@table_id
  1133.         AND c.id = o.id
  1134.         AND c.type = d.ss_dtype
  1135.        AND c.usertype *= t.usertype
  1136.        AND c.name like @column_name
  1137.       AND (d.ss_dtype NOT IN (111, 109, 38, 110)
  1138.         OR c.usertype >= 100)
  1139.       ORDER BY colid
  1140.     end else
  1141.     begin
  1142.     if @table_owner is null
  1143.         select @table_owner = '%'
  1144.     /* this block is for the case where there IS pattern
  1145.          matching done on the table name */
  1146.     SELECT  /* INTn, FLOATn, DATETIMEn and MONEYn types */
  1147.         table_qualifier = DB_NAME(),
  1148.         table_owner = USER_NAME(o.uid),
  1149.         table_name = o.name,
  1150.         column_name = c.name,
  1151.         data_type=d.data_type+convert(smallint,
  1152.                     isnull(d.aux,
  1153.                     ascii(substring("666AAA@@@CB??GG",
  1154.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  1155.                     -60)),
  1156.         type_name=rtrim(substring(d.type_name,
  1157.                     1+isnull(d.aux,
  1158.                     ascii(substring("III<<<MMMI<<A<A",
  1159.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1160.                     1))-60), 13)),
  1161.         precision=isnull(d.data_precision, convert(int,c.length))
  1162.                 +isnull(d.aux, convert(int,
  1163.                 ascii(substring("???AAAFFFCKFOLS",
  1164.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  1165.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  1166.                     isnull(d.aux,
  1167.                     ascii(substring("AAA<BB<DDDHJSPP",
  1168.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1169.                     1))-64)),
  1170.         scale = d.numeric_scale +convert(smallint,
  1171.                     isnull(d.aux,
  1172.                     ascii(substring("<<<<<<<<<<<<<<?",
  1173.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1174.                     1))-60)),
  1175.         radix=d.numeric_radix,
  1176.         nullable=convert(smallint, convert(bit, c.status&8)),
  1177.         remarks = convert(varchar(254),null),
  1178.         ss_data_type = c.type,
  1179.         colid = c.colid
  1180.     FROM
  1181.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  1182.     WHERE
  1183.         o.name like @table_name
  1184.         AND user_name(o.uid) like @table_owner
  1185.         AND o.id = c.id
  1186.         AND c.type = d.ss_dtype
  1187.         AND o.type != 'P'
  1188.         AND c.name like @column_name
  1189.         AND d.ss_dtype IN (111, 109, 38, 110)
  1190.     UNION
  1191.     SELECT  /* All other types including user data types */
  1192.         table_qualifier = DB_NAME(),
  1193.         table_owner = USER_NAME(o.uid),
  1194.         table_name = o.name,
  1195.         column_name = c.name,
  1196.         data_type=convert(smallint,
  1197.                         /*      SS-Type                                          1                */
  1198.                         /*                       33 3 3         4 44 5 5 2 5 55666*/
  1199.                         /*                       45 7 9         5 78 0 2 2 6 89012*/
  1200.                     ascii(substring("8;<9<H<<<<<:<=6<5<A<?<@<GC?GD",
  1201.                     t.type%34+1,1))-60),
  1202.         type_name=t.name,
  1203.         precision=isnull(d.data_precision, convert(int,c.length))
  1204.                 +isnull(d.aux, convert(int,
  1205.                 ascii(substring("???AAAFFFCKFOLS",
  1206.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  1207.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  1208.                     isnull(d.aux,
  1209.                     ascii(substring("AAA<BB<DDDHJSPP",
  1210.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1211.                     1))-64)),
  1212.         scale = d.numeric_scale +convert(smallint,
  1213.                     isnull(d.aux,
  1214.                     ascii(substring("<<<<<<<<<<<<<<?",
  1215.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1216.                     1))-60)),
  1217.         radix=d.numeric_radix,
  1218.         nullable=convert(smallint, convert(bit, c.status&8)),
  1219.         remarks =convert(varchar(254),null),
  1220.         ss_data_type = c.type,
  1221.         colid = c.colid
  1222.     FROM
  1223.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  1224.     WHERE
  1225.       o.name like @table_name
  1226.         AND user_name(o.uid) like @table_owner
  1227.         AND o.id = c.id
  1228.         AND c.type = d.ss_dtype
  1229.        AND c.usertype *= t.usertype
  1230.         AND o.type != 'P'
  1231.        AND c.name like @column_name
  1232.       AND d.ss_dtype NOT IN (111, 109, 38, 110)
  1233.       ORDER BY table_owner, table_name, colid
  1234.     end
  1235.  
  1236.  
  1237. go
  1238.  
  1239. grant execute on sp_columns to public
  1240. go
  1241.  
  1242. dump tran master with truncate_only
  1243. go
  1244. print "creating sp_fkeys"
  1245. go
  1246.  
  1247. CREATE PROCEDURE sp_fkeys( @pktable_name      varchar(32) = null,
  1248.                @pktable_owner      varchar(32) = null,
  1249.                @pktable_qualifier varchar(32) = null,
  1250.                @fktable_name      varchar(32) = null,
  1251.                @fktable_owner      varchar(32) = null,
  1252.                @fktable_qualifier varchar(32) = null )
  1253. as
  1254.     declare     @order_by_pk int
  1255.     select  @order_by_pk = 0
  1256.  
  1257.     if (@pktable_name is null) and (@fktable_name is null)
  1258.     begin
  1259.     print "pk table name or fk table name must be given"
  1260.     return
  1261.     end
  1262.     if @fktable_qualifier is not null
  1263.     begin
  1264.     if db_name() != @fktable_qualifier
  1265.     begin
  1266.         print "Foreign Key Table qualifier must be name of current database"
  1267.         return
  1268.     end
  1269.     end
  1270.     if @pktable_qualifier is not null
  1271.     begin
  1272.     if db_name() != @pktable_qualifier
  1273.     begin
  1274.         print "Primary Key Table qualifier must be name of current database"
  1275.         return
  1276.     end
  1277.     end
  1278.  
  1279.     if @pktable_name is null
  1280.     begin
  1281.     select @pktable_name = '%'
  1282.     select @order_by_pk = 1
  1283.     end
  1284.     if @pktable_owner is null
  1285.     select @pktable_owner = '%'
  1286.     if @fktable_name is null
  1287.     select @fktable_name = '%'
  1288.     if @fktable_owner is null
  1289.     select @fktable_owner = '%'
  1290.  
  1291.     if @@trancount != 0
  1292.     begin
  1293.     raiserror 20001 "catalog procedure sp_fkeys can not be run in a transaction"
  1294.     return
  1295.     end
  1296.     create table #fkeys( pktable_qualifier varchar(32),
  1297.              pktable_owner       varchar(32),
  1298.              pktable_name       varchar(32),
  1299.              pkcolumn_name      varchar(32),
  1300.              fktable_qualifier varchar(32),
  1301.              fktable_owner       varchar(32),
  1302.              fktable_name       varchar(32),
  1303.              fkcolumn_name       varchar(32),
  1304.              key_seq           smallint)
  1305.     insert into #fkeys
  1306.     select
  1307.         db_name(),
  1308.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1309.         object_name(k.depid),
  1310.          (    select name
  1311.             from syscolumns
  1312.             where id = k.depid
  1313.             and colid = k.depkey1),
  1314.     db_name(),
  1315.        (select user_name(uid) from sysobjects o where o.id = k.id),
  1316.         object_name(k.id),
  1317.         c.name,
  1318.         1
  1319.     from
  1320.         syskeys k,syscolumns c
  1321.     where
  1322.         c.id = k.id
  1323.         and k.type = 2
  1324.         and c.colid = key1
  1325.     if (@@rowcount    = 0)
  1326.         goto done
  1327.     insert into #fkeys
  1328.         select
  1329.         db_name(),
  1330.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1331.         object_name(k.depid),
  1332.         (select name
  1333.             from syscolumns
  1334.             where id = k.depid
  1335.             and colid = k.depkey2),
  1336.         db_name(),
  1337.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1338.         object_name(k.id),
  1339.         c.name,
  1340.         2
  1341.         from
  1342.         syskeys k,syscolumns c
  1343.         where
  1344.         c.id = k.id
  1345.         and k.type = 2
  1346.         and c.colid = key2
  1347.     if (@@rowcount = 0)
  1348.         goto done
  1349.     insert into #fkeys
  1350.        select
  1351.         db_name(),
  1352.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1353.         object_name(k.depid),
  1354.         (select name
  1355.             from syscolumns
  1356.             where id = k.depid
  1357.             and colid = k.depkey3),
  1358.         db_name(),
  1359.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1360.         object_name(k.id),
  1361.         c.name,
  1362.         3
  1363.         from
  1364.         syskeys k,syscolumns c
  1365.         where
  1366.         c.id = k.id
  1367.         and k.type = 2
  1368.         and c.colid = key3
  1369.     if (@@rowcount = 0)
  1370.         goto done
  1371.     insert into #fkeys
  1372.     select
  1373.         db_name(),
  1374.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1375.         object_name(k.depid),
  1376.         (select name
  1377.             from syscolumns
  1378.             where id = k.depid
  1379.             and colid = k.depkey4),
  1380.         db_name(),
  1381.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1382.         object_name(k.id),
  1383.         c.name,
  1384.         4
  1385.         from
  1386.         syskeys k,syscolumns c
  1387.         where
  1388.         c.id = k.id
  1389.         and k.type = 2
  1390.         and c.colid = key4
  1391.     if (@@rowcount = 0)
  1392.         goto done
  1393.     insert into #fkeys
  1394.        select
  1395.         db_name(),
  1396.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1397.         object_name(k.depid),
  1398.         (select name
  1399.             from syscolumns
  1400.             where id = k.depid
  1401.             and colid = k.depkey5),
  1402.         db_name(),
  1403.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1404.         object_name(k.id),
  1405.         c.name,
  1406.         5
  1407.         from
  1408.         syskeys k,syscolumns c
  1409.         where
  1410.         c.id = k.id
  1411.         and k.type = 2
  1412.         and c.colid = key5
  1413.  
  1414.     if (@@rowcount = 0)
  1415.         goto done
  1416.     insert into #fkeys
  1417.         select
  1418.         db_name(),
  1419.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1420.         object_name(k.depid),
  1421.         (select name
  1422.             from syscolumns
  1423.             where id = k.depid
  1424.             and colid = k.depkey6),
  1425.         db_name(),
  1426.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1427.         object_name(k.id),
  1428.         c.name,
  1429.         6
  1430.         from
  1431.         syskeys k,syscolumns c
  1432.         where
  1433.         c.id = k.id
  1434.         and k.type =2
  1435.         and c.colid = key6
  1436.     if (@@rowcount = 0)
  1437.         goto done
  1438.     insert into #fkeys
  1439.         select
  1440.         db_name(),
  1441.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1442.         object_name(k.depid),
  1443.         (select name
  1444.             from syscolumns
  1445.             where id = k.depid
  1446.             and colid = k.depkey7),
  1447.         db_name(),
  1448.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1449.         object_name(k.id),
  1450.         c.name,
  1451.         7
  1452.         from
  1453.         syskeys k,syscolumns c
  1454.         where
  1455.         c.id = k.id
  1456.         and k.type = 2
  1457.         and c.colid = key7
  1458.     if (@@rowcount    = 0)
  1459.         goto done
  1460.     insert into #fkeys
  1461.         select
  1462.         db_name(),
  1463.         (select user_name(uid) from sysobjects o where o.id = k.depid),
  1464.         object_name(k.depid),
  1465.         (select name
  1466.             from syscolumns
  1467.             where id = k.depid
  1468.             and colid = k.depkey8),
  1469.         db_name(),
  1470.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1471.         object_name(k.id),
  1472.         c.name,
  1473.         8
  1474.         from
  1475.         syskeys k,syscolumns c
  1476.         where
  1477.         c.id = k.id
  1478.         and k.type = 2
  1479.         and c.colid = key8
  1480.     done:
  1481.     if @order_by_pk = 1
  1482.     select
  1483.         pktable_qualifier,
  1484.         pktable_owner,
  1485.         pktable_name,
  1486.         pkcolumn_name,
  1487.         fktable_qualifier,
  1488.         fktable_owner,
  1489.         fktable_name,
  1490.         fkcolumn_name,
  1491.         key_seq,
  1492.         update_rule = convert(smallint, null),
  1493.     delete_rule = convert(smallint,null)
  1494.     from #fkeys
  1495.     where fktable_name like @fktable_name
  1496.         and fktable_owner like @fktable_owner
  1497.         and pktable_name  like @pktable_name
  1498.         and pktable_owner like @pktable_owner
  1499.     order by pktable_qualifier,pktable_owner,pktable_name, key_seq
  1500.     else
  1501.     select
  1502.         pktable_qualifier,
  1503.         pktable_owner,
  1504.         pktable_name,
  1505.         pkcolumn_name,
  1506.         fktable_qualifier,
  1507.         fktable_owner,
  1508.         fktable_name,
  1509.         fkcolumn_name,
  1510.         key_seq,
  1511.         update_rule = convert(smallint,null),
  1512.     delete_rule = convert(smallint,null)
  1513.     from #fkeys
  1514.     where fktable_name like @fktable_name
  1515.         and fktable_owner like @fktable_owner
  1516.         and pktable_name  like @pktable_name
  1517.         and pktable_owner like @pktable_owner
  1518.     order by fktable_qualifier,fktable_owner,fktable_name, key_seq
  1519.  
  1520. go
  1521.  
  1522. grant execute on sp_fkeys to public
  1523. go
  1524. dump tran master with truncate_only
  1525. go
  1526.  
  1527. print "creating sp_pkeys"
  1528. go
  1529.  
  1530. CREATE PROCEDURE sp_pkeys( @table_name    varchar(32),
  1531.                @table_owner varchar(32) = null,
  1532.                @table_qualifier varchar(32)= null )
  1533. as
  1534.     if @table_qualifier is not null
  1535.     begin
  1536.     if db_name() != @table_qualifier
  1537.     begin
  1538.         print "Table qualifier must be name of current database"
  1539.         return
  1540.     end
  1541.     end
  1542.     if @table_owner is null
  1543.     select @table_owner = '%'
  1544.     if @@trancount != 0
  1545.     begin
  1546.     raiserror 20001 "catalog procedure sp_pkeys can not be run in a transaction"
  1547.     return
  1548.     end
  1549.     create table #pkeys( table_qualifier varchar(32),
  1550.              table_owner     varchar(32),
  1551.              table_name      varchar(32),
  1552.              column_name     varchar(32),
  1553.              key_seq         smallint,
  1554.              update_delete_rule smallint null )
  1555.     insert into #pkeys
  1556.     select
  1557.         db_name(),
  1558.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1559.         object_name(k.id),
  1560.         c.name,
  1561.         1,
  1562.         null
  1563.     from
  1564.         syskeys k,syscolumns c
  1565.     where
  1566.         c.id = k.id
  1567.         and k.type = 1
  1568.         and c.colid = k.key1
  1569.     if (@@rowcount = 0)
  1570.     goto done
  1571.     insert into #pkeys
  1572.     select
  1573.         db_name(),
  1574.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1575.         object_name(k.id),
  1576.         c.name,
  1577.         2,
  1578.         null
  1579.     from
  1580.         syskeys k,syscolumns c
  1581.     where
  1582.         c.id = k.id
  1583.         and k.type = 1
  1584.         and c.colid = key2
  1585.     if (@@rowcount = 0)
  1586.     goto done
  1587.     insert into #pkeys
  1588.     select
  1589.         db_name(),
  1590.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1591.         object_name(k.id),
  1592.         c.name,
  1593.         3,
  1594.         null
  1595.     from
  1596.         syskeys k,syscolumns c
  1597.     where
  1598.         c.id = k.id
  1599.         and k.type = 1
  1600.         and c.colid = key3
  1601.     if (@@rowcount = 0)
  1602.     goto done
  1603.     insert into #pkeys
  1604.     select
  1605.         db_name(),
  1606.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1607.         object_name(k.id),
  1608.         c.name,
  1609.         4,
  1610.         null
  1611.     from
  1612.         syskeys k,syscolumns c
  1613.     where
  1614.         c.id = k.id
  1615.         and k.type = 1
  1616.         and c.colid = key4
  1617.     if (@@rowcount = 0)
  1618.     goto done
  1619.     insert into #pkeys
  1620.     select
  1621.         db_name(),
  1622.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1623.         object_name(k.id),
  1624.         c.name,
  1625.         5,
  1626.         null
  1627.     from
  1628.         syskeys k,syscolumns c
  1629.     where
  1630.         c.id = k.id
  1631.         and k.type = 1
  1632.         and c.colid = key5
  1633.     if (@@rowcount = 0)
  1634.     goto done
  1635.     insert into #pkeys
  1636.     select
  1637.         db_name(),
  1638.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1639.         object_name(k.id),
  1640.         c.name,
  1641.         6,
  1642.         null
  1643.     from
  1644.         syskeys k,syscolumns c
  1645.     where
  1646.         c.id = k.id
  1647.         and k.type = 1
  1648.         and c.colid = key6
  1649.     if (@@rowcount = 0)
  1650.     goto done
  1651.     insert into #pkeys
  1652.     select
  1653.         db_name(),
  1654.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1655.         object_name(k.id),
  1656.         c.name,
  1657.         7,
  1658.         null
  1659.     from
  1660.         syskeys k,syscolumns c
  1661.     where
  1662.         c.id = k.id
  1663.         and k.type = 1
  1664.         and c.colid = key7
  1665.     if (@@rowcount = 0)
  1666.     goto done
  1667.     insert into #pkeys
  1668.     select
  1669.         db_name(),
  1670.         (select user_name(uid) from sysobjects o where o.id = k.id),
  1671.         object_name(k.id),
  1672.         c.name,
  1673.         8,
  1674.         null
  1675.     from
  1676.         syskeys k,syscolumns c
  1677.     where
  1678.         c.id = k.id
  1679.         and k.type = 1
  1680.         and c.colid = key8
  1681.     done:
  1682.     select
  1683.         table_qualifier,
  1684.         table_owner,
  1685.         table_name,
  1686.         column_name,
  1687.         key_seq
  1688.         from #pkeys
  1689.     where table_name like  @table_name
  1690.     and table_owner like @table_owner
  1691.     order by table_qualifier, table_owner, table_name,key_seq
  1692. go
  1693.  
  1694. grant execute on sp_pkeys to public
  1695. go
  1696.  
  1697. dump tran master with truncate_only
  1698. go
  1699.  
  1700. print "creating sp_stored_procedures"
  1701. go
  1702.  
  1703. create procedure sp_stored_procedures( @sp_name      varchar(36) = null,
  1704.                        @sp_owner     varchar(32) = null,
  1705.                        @sp_qualifier varchar(32) = null)
  1706. as
  1707.     if @sp_qualifier is not null
  1708.     begin
  1709.     if db_name() != @sp_qualifier
  1710.     begin
  1711.     if @sp_qualifier = ''
  1712.     begin
  1713.         /* in this case, we need to return an empty result set */
  1714.         /* because the user has requested a database with an empty name */
  1715.         select @sp_name = ''
  1716.         select @sp_owner = ''
  1717.     end else
  1718.     begin
  1719.         print "Stored procedure qualifier must be name of current database"
  1720.         return
  1721.     end
  1722.     end
  1723.     end
  1724.  
  1725.     if @sp_name is null
  1726.     begin
  1727.     select @sp_name = '%'
  1728.     end
  1729.     else begin
  1730.     if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  1731.     begin
  1732.         if exists (select * from sysobjects
  1733.             where uid = user_id()
  1734.             and name = @sp_name
  1735.             and type = 'P')
  1736.         begin
  1737.         select @sp_owner = user_name()
  1738.         end
  1739.     end
  1740.     end
  1741.     if @sp_owner is null
  1742.     select @sp_owner = '%'
  1743.  
  1744.     select
  1745.         procedure_qualifier = db_name(),
  1746.         procedure_owner = user_name(o.uid),
  1747.         procedure_name = o.name +';'+ ltrim(str(p.number,5)),
  1748.         num_input_params = -1,
  1749.         num_output_params = -1,
  1750.         num_result_sets = -1,
  1751.         remarks = convert(varchar(254),null)
  1752.     from
  1753.         sysobjects o,sysprocedures p,sysusers u
  1754.     where
  1755.         o.name like @sp_name
  1756.         and p.sequence = 0
  1757.         and user_name(o.uid) like @sp_owner
  1758.         and o.type = 'P'
  1759.         and p.id = o.id
  1760.         and u.uid=user_id()   /* constrain sysusers uid for use in subquery */
  1761.         and (suser_id()=1      /* User is the System Administrator */
  1762.             or o.uid=user_id()     /* User created the object */
  1763.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  1764.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  1765.              from sysprotects p
  1766.              /* outer join to correlate with all rows in sysobjects */
  1767.              where p.id =* o.id
  1768.                  /*  get rows for public,current user,user's group */
  1769.                  and (p.uid=0 or p.uid=user_id() or p.uid=*u.gid)
  1770.                  /* check for SELECT,EXECUTE privilege */
  1771.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  1772.             )=1    /* final magic...compare Grants    */
  1773.         )
  1774.     order by procedure_qualifier, procedure_owner, procedure_name
  1775. go
  1776. grant execute on sp_stored_procedures to public
  1777. go
  1778.  
  1779. dump tran master with truncate_only
  1780. go
  1781.  
  1782.  
  1783. print "creating sp_sproc_columns"
  1784. go
  1785.  
  1786. /* this is the version for servers  without support for UNION */
  1787. CREATE PROCEDURE sp_sproc_columns (@procedure_name varchar(36) = '%',
  1788.                  @procedure_owner varchar(32) = null,
  1789.                  @procedure_qualifier varchar(32) = null,
  1790.                  @column_name varchar(32) = null )
  1791. AS
  1792.  
  1793.     DECLARE @group_num int
  1794.     DECLARE @semi_position int
  1795.     if @column_name is null
  1796.     select @column_name = '%'
  1797.     DECLARE @full_procedure_name    char(70)
  1798.     DECLARE @procedure_id int
  1799.     if @procedure_qualifier is not null
  1800.     begin
  1801.     if db_name() != @procedure_qualifier
  1802.     begin
  1803.     if @procedure_qualifier = ''
  1804.     begin
  1805.         /* in this case, we need to return an empty result set */
  1806.         /* because the user has requested a database with an empty name */
  1807.         select @procedure_name = ''
  1808.         select @procedure_owner = ''
  1809.     end else
  1810.     begin
  1811.         print "Procedure qualifier must be name of current database"
  1812.         return
  1813.     end
  1814.     end
  1815.     end
  1816.     /* first we need to extract the procedure group number, if one exists */
  1817.     select @semi_position = charindex(';',@procedure_name)
  1818.     if (@semi_position > 0 )
  1819.     begin
  1820.     select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  1821.     select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  1822.     end
  1823.     else
  1824.     begin
  1825.     select @group_num = 1
  1826.     end
  1827.     if @procedure_owner is null
  1828.     begin
  1829.     SELECT @full_procedure_name = @procedure_name
  1830.     end
  1831.     else
  1832.     begin
  1833.     SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  1834.     end
  1835.     SELECT @procedure_id = 0
  1836.     SELECT @procedure_id = object_id(@full_procedure_name)
  1837.     if ((charindex('%',@full_procedure_name) = 0) and
  1838.        (charindex('_',@full_procedure_name) = 0)  and
  1839.     @procedure_id != 0)
  1840.     begin
  1841.     /* this block is for the case where there is no pattern
  1842.          matching required for the table name */
  1843.     SELECT
  1844.         procedure_qualifier = DB_NAME(),
  1845.         procedure_owner = USER_NAME(o.uid),
  1846.         procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  1847.         column_name = c.name,
  1848.         column_type = convert(smallint, 0),
  1849.         data_type=d.data_type+convert(smallint,
  1850.                     isnull(d.aux,
  1851.                     ascii(substring("666AAA@@@CB??GG",
  1852.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  1853.                     -60)),
  1854.         type_name=rtrim(substring(d.type_name,
  1855.                     1+isnull(d.aux,
  1856.                     ascii(substring("III<<<MMMI<<A<A",
  1857.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1858.                     1))-60), 13)),
  1859.         precision=isnull(d.data_precision, convert(int,c.length))
  1860.                 +isnull(d.aux, convert(int,
  1861.                 ascii(substring("???AAAFFFCKFOLS",
  1862.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  1863.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  1864.                     isnull(d.aux,
  1865.                     ascii(substring("AAA<BB<DDDHJSPP",
  1866.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1867.                     1))-64)),
  1868.         scale = d.numeric_scale +convert(smallint,
  1869.                     isnull(d.aux,
  1870.                     ascii(substring("<<<<<<<<<<<<<<?",
  1871.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1872.                     1))-60)),
  1873.         radix=d.numeric_radix,
  1874.         nullable=convert(smallint, convert(bit, c.status&8)),
  1875.         remarks = convert(varchar(254),null),
  1876.         ss_data_type = c.type
  1877.     FROM
  1878.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  1879.     WHERE
  1880.         o.id=@procedure_id
  1881.         AND c.id = o.id
  1882.         AND c.type = d.ss_dtype
  1883.         AND c.usertype *= t.usertype
  1884.         AND c.name like @column_name
  1885.         AND c.number = @group_num
  1886.  
  1887.     end else
  1888.     begin
  1889.     if @procedure_owner is null
  1890.         select @procedure_owner = '%'
  1891.                 /* this block is for the case where there IS pattern
  1892.          matching done on the table name */
  1893.     SELECT
  1894.         procedure_qualifier = DB_NAME(),
  1895.         procedure_owner = USER_NAME(o.uid),
  1896.         procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  1897.         column_name = c.name,
  1898.         column_type = convert(smallint, 0),
  1899.         data_type=d.data_type+convert(smallint,
  1900.                     isnull(d.aux,
  1901.                     ascii(substring("666AAA@@@CB??GG",
  1902.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  1903.                     -60)),
  1904.         type_name=rtrim(substring(d.type_name,
  1905.                     1+isnull(d.aux,
  1906.                     ascii(substring("III<<<MMMI<<A<A",
  1907.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1908.                     1))-60), 13)),
  1909.         precision=isnull(d.data_precision, convert(int,c.length))
  1910.                 +isnull(d.aux, convert(int,
  1911.                 ascii(substring("???AAAFFFCKFOLS",
  1912.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  1913.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  1914.                     isnull(d.aux,
  1915.                     ascii(substring("AAA<BB<DDDHJSPP",
  1916.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1917.                     1))-64)),
  1918.         scale = d.numeric_scale +convert(smallint,
  1919.                     isnull(d.aux,
  1920.                     ascii(substring("<<<<<<<<<<<<<<?",
  1921.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  1922.                     1))-60)),
  1923.         radix=d.numeric_radix,
  1924.         nullable=convert(smallint, convert(bit, c.status&8)),
  1925.         remarks = convert(varchar(254),null),
  1926.         ss_data_type = c.type,
  1927.         c.colid
  1928.     FROM
  1929.         syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  1930.     WHERE
  1931.         o.name like @procedure_name
  1932.         AND user_name(o.uid) like @procedure_owner
  1933.         AND c.id = o.id
  1934.         AND c.usertype *= t.usertype
  1935.         AND c.type = d.ss_dtype
  1936.         AND c.name like @column_name
  1937.     AND o.type = 'P'
  1938.     ORDER BY procedure_qualifier, procedure_owner, procedure_name, colid
  1939.     end
  1940. go
  1941.  
  1942.  
  1943. /* if this is a 4.2 or later SQL Server, then we want to delete the stored
  1944. procedure we just created, and create a new one which uses UNION
  1945. (this fixes some bugs involving UDT names and char NULL datatype
  1946. names)
  1947. */
  1948.  
  1949. if  (charindex('1.1', @@version) = 0
  1950.     and charindex('4.0', @@version) = 0)
  1951. begin
  1952.     drop proc sp_sproc_columns
  1953.     dump tran master with truncate_only
  1954. end
  1955. else
  1956. begin
  1957.     print ""
  1958.     print ""
  1959.     print "Installing catalog procedures on a 1.x or 4.0 server:"
  1960.     print "Ignore the following error messages"
  1961.     print ""
  1962.     print ""
  1963. end
  1964. go
  1965.  
  1966.  
  1967.  
  1968. /* this is the version for servers which support UNION */
  1969. CREATE PROCEDURE sp_sproc_columns (@procedure_name varchar(36) = '%',
  1970.                  @procedure_owner varchar(32) = null,
  1971.                  @procedure_qualifier varchar(32) = null,
  1972.                  @column_name varchar(32) = null )
  1973. AS
  1974.     DECLARE @group_num int
  1975.     DECLARE @semi_position int
  1976.     if @column_name is null
  1977.     select @column_name = '%'
  1978.     DECLARE @full_procedure_name    char(70)
  1979.     DECLARE @procedure_id int
  1980.      if @procedure_qualifier is not null
  1981.     begin
  1982.     if db_name() != @procedure_qualifier
  1983.     begin
  1984.     if @procedure_qualifier = ''
  1985.     begin
  1986.         /* in this case, we need to return an empty result set */
  1987.         /* because the user has requested a database with an empty name */
  1988.         select @procedure_name = ''
  1989.         select @procedure_owner = ''
  1990.     end else
  1991.     begin
  1992.         print "Procedure qualifier must be name of current database"
  1993.         return
  1994.     end
  1995.     end
  1996.     end
  1997.  
  1998.  
  1999.      /* first we need to extract the procedure group number, if one exists */
  2000.     select @semi_position = charindex(';',@procedure_name)
  2001.     if (@semi_position > 0 )
  2002.     begin
  2003.     select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  2004.     select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  2005.     end
  2006.     else
  2007.     begin
  2008.     select @group_num = 1
  2009.     end
  2010.  
  2011.     if @procedure_owner is null
  2012.     begin
  2013.     SELECT @full_procedure_name = @procedure_name
  2014.     end
  2015.     else
  2016.     begin
  2017.     SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  2018.     end
  2019.     SELECT @procedure_id = 0
  2020.     SELECT @procedure_id = object_id(@full_procedure_name)
  2021.     if ((charindex('%',@full_procedure_name) = 0) and
  2022.        (charindex('_',@full_procedure_name) = 0)  and
  2023.     @procedure_id != 0)
  2024.     begin
  2025.     /* this block is for the case where there is no pattern
  2026.          matching required for the table name */
  2027.     SELECT  /* INTn, FLOATn, DATETIMEn and MONEYn types */
  2028.         procedure_qualifier = DB_NAME(),
  2029.         procedure_owner = USER_NAME(o.uid),
  2030.         procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2031.         column_name = c.name,
  2032.         column_type = convert(smallint, 0),
  2033.         data_type=d.data_type+convert(smallint,
  2034.                     isnull(d.aux,
  2035.                     ascii(substring("666AAA@@@CB??GG",
  2036.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  2037.                     -60)),
  2038.         type_name=rtrim(substring(d.type_name,
  2039.                     1+isnull(d.aux,
  2040.                     ascii(substring("III<<<MMMI<<A<A",
  2041.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2042.                     1))-60), 13)),
  2043.         precision=isnull(d.data_precision, convert(int,c.length))
  2044.                 +isnull(d.aux, convert(int,
  2045.                 ascii(substring("???AAAFFFCKFOLS",
  2046.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  2047.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  2048.                     isnull(d.aux,
  2049.                     ascii(substring("AAA<BB<DDDHJSPP",
  2050.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2051.                     1))-64)),
  2052.         scale = d.numeric_scale +convert(smallint,
  2053.                     isnull(d.aux,
  2054.                     ascii(substring("<<<<<<<<<<<<<<?",
  2055.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2056.                     1))-60)),
  2057.         radix=d.numeric_radix,
  2058.         nullable=convert(smallint, convert(bit, c.status&8)),
  2059.         remarks = convert(varchar(254),null),
  2060.         ss_data_type = c.type,
  2061.         colid = c.colid
  2062.     FROM
  2063.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t, sysprocedures p
  2064.     WHERE
  2065.         o.id=@procedure_id
  2066.         AND c.id = o.id
  2067.         AND c.type = d.ss_dtype
  2068.         AND c.name like @column_name
  2069.         AND d.ss_dtype IN (111, 109, 38, 110)
  2070.         AND c.number = @group_num
  2071.     UNION
  2072.  SELECT         /* All other types including user data types */
  2073.         procedure_qualifier = DB_NAME(),
  2074.         procedure_owner = USER_NAME(o.uid),
  2075.         procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2076.         column_name = c.name,
  2077.         column_type = convert(smallint, 0),
  2078.         data_type=convert(smallint,
  2079.                         /*      SS-Type                                          1                */
  2080.                         /*                       33 3 3         4 44 5 5 2 5 55666*/
  2081.                         /*                       45 7 9         5 78 0 2 2 6 89012*/
  2082.                     ascii(substring("8;<9<H<<<<<:<=6<5<A<?<@<GC?GD",
  2083.                     t.type%34+1,1))-60),
  2084.         type_name=t.name,
  2085.         precision=isnull(d.data_precision, convert(int,c.length))
  2086.                 +isnull(d.aux, convert(int,
  2087.                 ascii(substring("???AAAFFFCKFOLS",
  2088.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  2089.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  2090.                     isnull(d.aux,
  2091.                     ascii(substring("AAA<BB<DDDHJSPP",
  2092.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2093.                     1))-64)),
  2094.         scale = d.numeric_scale +convert(smallint,
  2095.                     isnull(d.aux,
  2096.                     ascii(substring("<<<<<<<<<<<<<<?",
  2097.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2098.                     1))-60)),
  2099.         radix=d.numeric_radix,
  2100.         nullable=convert(smallint, convert(bit, c.status&8)),
  2101.         remarks = convert(varchar(254),null),
  2102.         ss_data_type = c.type,
  2103.          colid = c.colid
  2104.     FROM
  2105.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  2106.     WHERE
  2107.         o.id=@procedure_id
  2108.         AND c.id = o.id
  2109.         AND c.type = d.ss_dtype
  2110.        AND c.usertype *= t.usertype
  2111.        AND c.name like @column_name
  2112.       AND d.ss_dtype NOT IN (111, 109, 38, 110)
  2113.         AND c.number = @group_num
  2114.       ORDER BY colid
  2115.     end else
  2116.     begin
  2117.     if @procedure_owner is null
  2118.         select @procedure_owner = '%'
  2119.                 /* this block is for the case where there IS pattern
  2120.          matching done on the table name */
  2121.     SELECT  /* INTn, FLOATn, DATETIMEn and MONEYn types */
  2122.         procedure_qualifier = DB_NAME(),
  2123.         procedure_owner = USER_NAME(o.uid),
  2124.         procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2125.         column_name = c.name,
  2126.         column_type = convert(smallint, 0),
  2127.         data_type=d.data_type+convert(smallint,
  2128.                     isnull(d.aux,
  2129.                     ascii(substring("666AAA@@@CB??GG",
  2130.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  2131.                     -60)),
  2132.         type_name=rtrim(substring(d.type_name,
  2133.                     1+isnull(d.aux,
  2134.                     ascii(substring("III<<<MMMI<<A<A",
  2135.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2136.                     1))-60), 13)),
  2137.         precision=isnull(d.data_precision, convert(int,c.length))
  2138.                 +isnull(d.aux, convert(int,
  2139.                 ascii(substring("???AAAFFFCKFOLS",
  2140.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  2141.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  2142.                     isnull(d.aux,
  2143.                     ascii(substring("AAA<BB<DDDHJSPP",
  2144.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2145.                     1))-64)),
  2146.         scale = d.numeric_scale +convert(smallint,
  2147.                     isnull(d.aux,
  2148.                     ascii(substring("<<<<<<<<<<<<<<?",
  2149.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2150.                     1))-60)),
  2151.         radix=d.numeric_radix,
  2152.         nullable=convert(smallint, convert(bit, c.status&8)),
  2153.         remarks = convert(varchar(254),null),
  2154.         ss_data_type = c.type,
  2155.         colid = c.colid
  2156.     FROM
  2157.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  2158.     WHERE
  2159.         o.name like @procedure_name
  2160.         AND user_name(o.uid) like @procedure_owner
  2161.         AND o.id = c.id
  2162.         AND c.type = d.ss_dtype
  2163.         AND c.name like @column_name
  2164.         AND o.type = 'P'
  2165.         AND d.ss_dtype IN (111, 109, 38, 110)
  2166.  UNION
  2167.  SELECT         /* All other types including user data types */
  2168.         procedure_qualifier = DB_NAME(),
  2169.         procedure_owner = USER_NAME(o.uid),
  2170.         procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2171.         column_name = c.name,
  2172.         column_type = convert(smallint, 0),
  2173.         data_type=convert(smallint,
  2174.                         /*      SS-Type                                          1                */
  2175.                         /*                       33 3 3         4 44 5 5 2 5 55666*/
  2176.                         /*                       45 7 9         5 78 0 2 2 6 89012*/
  2177.                     ascii(substring("8;<9<H<<<<<:<=6<5<A<?<@<GC?GD",
  2178.                     t.type%34+1,1))-60),
  2179.         type_name=t.name,
  2180.         precision=isnull(d.data_precision, convert(int,c.length))
  2181.                 +isnull(d.aux, convert(int,
  2182.                 ascii(substring("???AAAFFFCKFOLS",
  2183.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  2184.         length=isnull(d.length, convert(int,c.length)) +convert(int,
  2185.                     isnull(d.aux,
  2186.                     ascii(substring("AAA<BB<DDDHJSPP",
  2187.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2188.                     1))-64)),
  2189.         scale = d.numeric_scale +convert(smallint,
  2190.                     isnull(d.aux,
  2191.                     ascii(substring("<<<<<<<<<<<<<<?",
  2192.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  2193.                     1))-60)),
  2194.         radix=d.numeric_radix,
  2195.         nullable=convert(smallint, convert(bit, c.status&8)),
  2196.         remarks = convert(varchar(254),null),
  2197.         ss_data_type = c.type,
  2198.         colid = c.colid
  2199.     FROM
  2200.     syscolumns c,sysobjects o,master.dbo.spt_datatype_info d, systypes t
  2201.     WHERE
  2202.       o.name like @procedure_name
  2203.         AND user_name(o.uid) like @procedure_owner
  2204.         AND o.id = c.id
  2205.         AND c.type = d.ss_dtype
  2206.        AND c.usertype *= t.usertype
  2207.     AND o.type = 'P'
  2208.        AND c.name like @column_name
  2209.       AND d.ss_dtype NOT IN (111, 109, 38, 110)
  2210.     ORDER BY procedure_owner, procedure_name, colid
  2211.     end
  2212.  
  2213.  
  2214. go
  2215.  
  2216.  
  2217. grant execute on sp_sproc_columns to public
  2218. go
  2219.  
  2220.  
  2221. print "creating sp_table_privileges"
  2222. go
  2223.  
  2224.  
  2225. CREATE PROCEDURE sp_table_privileges (
  2226.             @table_name  varchar(32),
  2227.             @table_owner varchar(32) = null,
  2228.             @table_qualifier varchar(32)= null)
  2229. as
  2230.  
  2231.     declare @table_id    int,
  2232.         @owner_id    int,
  2233.         @full_table_name char(70)
  2234.  
  2235.  
  2236.     if @table_qualifier is not null
  2237.     begin
  2238.     if db_name() != @table_qualifier
  2239.     begin
  2240.         print "Table qualifier must be name of current database"
  2241.         return
  2242.     end
  2243.     end
  2244.     if @table_owner is null
  2245.     begin
  2246.     SELECT @full_table_name = @table_name
  2247.     end
  2248.     else
  2249.     begin
  2250.     SELECT @full_table_name = @table_owner + '.' + @table_name
  2251.     end
  2252.     SELECT @table_id = object_id(@full_table_name)
  2253.  
  2254.     
  2255.  
  2256.     if @@trancount != 0
  2257.     begin
  2258.     raiserror 20001 "catalog procedure sp_table_privileges can not be run in a transaction"
  2259.     return
  2260.     end
  2261.     create table #table_privileges
  2262.         (table_qualifier         varchar(32),
  2263.         table_owner         varchar(32),
  2264.         table_name     varchar(32),
  2265.         grantor            varchar(32),
  2266.         grantee            varchar(32),
  2267.         select_privilege    int,
  2268.          insert_privilege    int,
  2269.          update_privilege    int,
  2270.          delete_privilege    int,
  2271.     is_grantable    varchar(3),
  2272.          uid                 int,
  2273.         gid                 int)
  2274.  
  2275.     insert into #table_privileges
  2276.     select distinct db_name(),
  2277.     user_name(o.uid),
  2278.     o.name,
  2279.     user_name(o.uid),
  2280.     u.name,
  2281.     0,
  2282.     0,
  2283.     0,
  2284.     0,
  2285.     'no',
  2286.     u.uid,
  2287.     u.gid
  2288.     from sysusers u, sysobjects o
  2289.     where o.id = @table_id
  2290.     and u.uid != u.gid
  2291.  
  2292.     /*
  2293.     ** now add row for table owner
  2294.     */
  2295.     if exists (
  2296.     select *
  2297.         from #table_privileges
  2298.         where grantor = grantee)
  2299.     begin
  2300.     update #table_privileges
  2301.         set select_privilege = 1,
  2302.            update_privilege = 1,
  2303.         insert_privilege = 1,
  2304.          delete_privilege = 1,
  2305.        is_grantable = 'yes'
  2306.           where grantor = grantee
  2307.     end
  2308.     else
  2309.     begin
  2310.     insert into #table_privileges
  2311.     select  db_name(),
  2312.         user_name(o.uid),
  2313.         o.name,
  2314.         user_name(o.uid),
  2315.         user_name(o.uid),
  2316.         1,
  2317.         1,
  2318.         1,
  2319.         1,
  2320.         'yes',
  2321.         o.uid,
  2322.         u.gid
  2323.     from sysobjects o, sysusers u
  2324.     where o.id = @table_id
  2325.     and u.uid = o.uid
  2326.     end
  2327.  
  2328.     update #table_privileges
  2329.     set select_privilege = 1
  2330.     where exists (select * from sysprotects
  2331.         where id = @table_id
  2332.         and (#table_privileges.uid = uid
  2333.         or #table_privileges.gid = uid
  2334.         or uid = 0)
  2335.         and protecttype = 205
  2336.         and action = 193)
  2337.     and not exists (select * from sysprotects
  2338.         where id = @table_id
  2339.         and (#table_privileges.uid = uid
  2340.         or #table_privileges.gid = uid
  2341.         or uid = 0)
  2342.         and protecttype = 206
  2343.         and action = 193)
  2344.  
  2345.  
  2346.     update #table_privileges
  2347.     set insert_privilege = 1
  2348.     where exists (select * from sysprotects
  2349.         where id = @table_id
  2350.         and (#table_privileges.uid = uid
  2351.         or #table_privileges.gid = uid
  2352.         or uid = 0)
  2353.         and protecttype = 205
  2354.         and action = 195)
  2355.     and not exists (select * from sysprotects
  2356.         where id = @table_id
  2357.         and (#table_privileges.uid = uid
  2358.         or #table_privileges.gid = uid
  2359.         or uid = 0)
  2360.         and protecttype = 206
  2361.         and action = 195)
  2362.  
  2363.     update #table_privileges
  2364.     set delete_privilege = 1
  2365.     where exists (select * from sysprotects
  2366.         where id = @table_id
  2367.         and (#table_privileges.uid = uid
  2368.         or #table_privileges.gid = uid
  2369.         or uid = 0)
  2370.         and protecttype = 205
  2371.         and action = 196)
  2372.     and not exists (select * from sysprotects
  2373.         where id = @table_id
  2374.         and (#table_privileges.uid = uid
  2375.         or #table_privileges.gid = uid
  2376.         or uid = 0)
  2377.         and protecttype = 206
  2378.         and action = 196)
  2379.  
  2380.  
  2381.  
  2382.  
  2383.     update #table_privileges
  2384.     set update_privilege = 1
  2385.     where exists (select * from sysprotects
  2386.         where id = @table_id
  2387.         and (#table_privileges.uid = uid
  2388.         or #table_privileges.gid = uid
  2389.         or uid = 0)
  2390.         and protecttype = 205
  2391.         and action = 197)
  2392.     and not exists (select * from sysprotects
  2393.         where id = @table_id
  2394.         and (#table_privileges.uid = uid
  2395.         or #table_privileges.gid = uid
  2396.         or uid = 0)
  2397.         and protecttype = 206
  2398.         and action = 197)
  2399.  
  2400.     create table #table_priv2
  2401.     (table_qualifier         varchar(32),
  2402.         table_owner         varchar(32),
  2403.         table_name     varchar(32),
  2404.         grantor            varchar(32),
  2405.         grantee            varchar(32),
  2406.         privilege            varchar(32),
  2407.     is_grantable    varchar(3))
  2408.  
  2409.      insert into #table_priv2
  2410.      select
  2411.     table_qualifier,
  2412.         table_owner,
  2413.         table_name,
  2414.         grantor,
  2415.         grantee,
  2416.         'SELECT',
  2417.     is_grantable
  2418.     from #table_privileges
  2419.     where select_privilege = 1
  2420.  
  2421.  
  2422.      insert into #table_priv2
  2423.      select
  2424.     table_qualifier,
  2425.         table_owner,
  2426.         table_name,
  2427.         grantor,
  2428.         grantee,
  2429.         'INSERT',
  2430.     is_grantable
  2431.     from #table_privileges
  2432.     where insert_privilege = 1
  2433.  
  2434.  
  2435.      insert into #table_priv2
  2436.      select
  2437.     table_qualifier,
  2438.         table_owner,
  2439.         table_name,
  2440.         grantor,
  2441.         grantee,
  2442.         'DELETE',
  2443.     is_grantable
  2444.     from #table_privileges
  2445.     where delete_privilege = 1
  2446.  
  2447.  
  2448.      insert into #table_priv2
  2449.      select
  2450.     table_qualifier,
  2451.         table_owner,
  2452.         table_name,
  2453.         grantor,
  2454.         grantee,
  2455.         'UPDATE',
  2456.     is_grantable
  2457.     from #table_privileges
  2458.     where update_privilege = 1
  2459.  
  2460.  
  2461.     select * from #table_priv2
  2462.     order by privilege
  2463. go
  2464. grant execute on sp_table_privileges to public
  2465. go
  2466. dump tran master with truncate_only
  2467. go
  2468.  
  2469. print "creating sp_column_privileges"
  2470. go
  2471.  
  2472.  
  2473. CREATE PROCEDURE sp_column_privileges (
  2474.             @table_name  varchar(32),
  2475.             @table_owner varchar(32) = null,
  2476.             @table_qualifier varchar(32)= null,
  2477.             @column_name varchar(32) = null)
  2478. as
  2479.  
  2480.     declare @table_id    int,
  2481.     @owner_id    int
  2482.     DECLARE @full_table_name    char(70)
  2483.  
  2484.     declare @low int                    /* range of userids to check */
  2485.     declare @high int
  2486.     declare @objid int                  /* id of @name if object */
  2487.     declare @owner_name varchar(32)
  2488.  
  2489.     select @low = 0, @high = 32767
  2490.  
  2491.     if @column_name is null
  2492.     select @column_name = '%'
  2493.  
  2494.     if @table_qualifier is not null
  2495.     begin
  2496.     if db_name() != @table_qualifier
  2497.     begin
  2498.         print "Table qualifier must be name of current database"
  2499.         return
  2500.     end
  2501.     end
  2502.     if @table_owner is null
  2503.     begin
  2504.     SELECT @full_table_name = @table_name
  2505.     end
  2506.     else
  2507.     begin
  2508.     SELECT @full_table_name = @table_owner + '.' + @table_name
  2509.     end
  2510.     select @table_id = object_id(@full_table_name)
  2511.  
  2512.     
  2513.  
  2514.     if @@trancount != 0
  2515.     begin
  2516.     raiserror 20001 "catalog procedure sp_column_privileges can not be run in a transaction"
  2517.     return
  2518.     end
  2519.  
  2520.     /*
  2521.     ** We need to create a table which will contain a row for every row to
  2522.     ** be returned to the client.
  2523.     */
  2524.  
  2525.     create table #column_privileges
  2526.         (table_qualifier     varchar(32),
  2527.         table_owner         varchar(32),
  2528.         table_name         varchar(32),
  2529.         column_name         varchar(32),
  2530.         grantor            varchar(32),
  2531.         grantee            varchar(32),
  2532.         select_privilege    int,
  2533.         select_grantable    int,
  2534.         insert_privilege    int,
  2535.         insert_grantable    int,
  2536.         update_privilege    int,
  2537.         update_grantable    int,
  2538.         delete_privilege    int,
  2539.         delete_grantable    int,
  2540.         references_privilege    int null,
  2541.         references_grantable    int null,
  2542.         uid                 int,
  2543.         gid                 int,
  2544.         is_grantable        varchar(3))
  2545.  
  2546. /*
  2547. ** insert a row for the table owner (who has all permissions)
  2548. */
  2549.     select @owner_name = (select user_name(uid)
  2550.                 from sysobjects
  2551.                 where id = @table_id)
  2552.  
  2553.  
  2554.     insert into #column_privileges
  2555.     select
  2556.         db_name(),
  2557.         @owner_name,
  2558.         @table_name,
  2559.         name,
  2560.         @owner_name,
  2561.         @owner_name,
  2562.         1,
  2563.         1,
  2564.         1,
  2565.         1,
  2566.         1,
  2567.         1,
  2568.         1,
  2569.         1,
  2570.         null,
  2571.         null,
  2572.         user_id(@owner_name),
  2573.         0,
  2574.     'yes'
  2575.     from syscolumns
  2576.     where id = @table_id
  2577.  
  2578. /*
  2579. ** now stick a row in the table for every user in the database
  2580. ** we will need to weed out those who have no permissions later
  2581. ** (and yes this is a cartesion product: the uid field in sysprotects
  2582. ** can also have a group id, in which case we need to extend those
  2583. ** privileges to all group members).
  2584. */
  2585.  
  2586.     insert into #column_privileges
  2587.     select distinct
  2588.     db_name(),
  2589.     user_name(o.uid),
  2590.     @table_name,
  2591.     c.name,
  2592.     user_name(o.uid),
  2593.     u.name,
  2594.     0,
  2595.     0,
  2596.     0,
  2597.     0,
  2598.     0,
  2599.     0,
  2600.     0,
  2601.     0,
  2602.     null,
  2603.     null,
  2604.     u.uid,
  2605.     u.gid,
  2606.     'no'
  2607.     from sysusers u, syscolumns c, sysobjects o
  2608.     where o.id = @table_id
  2609.     and c.id = o.id
  2610.     and u.gid != u.uid
  2611.     and u.name != @owner_name
  2612.  
  2613.     /*
  2614.     ** we need to create another temporary table to contain all the various
  2615.     ** protection information for the table in question
  2616.     */
  2617.     create table #protects (uid smallint,
  2618.                 action tinyint,
  2619.                 protecttype tinyint,
  2620.                 name    varchar(32))
  2621.     insert into #protects
  2622.     select  p.uid,
  2623.         p.action,
  2624.         p.protecttype,
  2625.             isnull(col_name(id, c.number), "All")
  2626.         from sysprotects p, master.dbo.spt_values c,
  2627.                 master.dbo.spt_values a, master.dbo.spt_values b
  2628.             where convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  2629.                 & c.high != 0
  2630.                 and c.number <=
  2631.                     (select count(*)
  2632.                         from syscolumns
  2633.                         where id = @table_id)
  2634.                 and a.type = "T"
  2635.                 and a.number = p.action
  2636.                 and b.type = "T"
  2637.                 and b.number = p.protecttype
  2638.                 and p.id = @table_id
  2639.                 and p.uid between @low and @high
  2640.  
  2641.  
  2642.     update #column_privileges
  2643.     set select_privilege = 1
  2644.     where exists
  2645.         (select * from #protects
  2646.         where protecttype = 205
  2647.         and action = 193
  2648.         and ( name = #column_privileges.column_name
  2649.           or name = 'All')
  2650.         and ( uid = 0
  2651.           or uid = #column_privileges.gid
  2652.           or uid = #column_privileges.uid))
  2653.     and not exists (select * from #protects
  2654.         where protecttype = 206
  2655.         and action = 193
  2656.         and ( name = #column_privileges.column_name
  2657.           or name = 'All')
  2658.         and ( uid = 0
  2659.           or uid = #column_privileges.gid
  2660.           or uid = #column_privileges.uid))
  2661.  
  2662.  
  2663.  
  2664.     update #column_privileges
  2665.     set insert_privilege = 1
  2666.     where exists (select * from #protects
  2667.         where protecttype = 205
  2668.         and action = 195
  2669.         and ( name = #column_privileges.column_name
  2670.           or name = 'All')
  2671.         and  ( uid = 0
  2672.           or uid = #column_privileges.gid
  2673.           or uid = #column_privileges.uid))
  2674.     and not exists (select * from #protects
  2675.         where protecttype = 206
  2676.         and action = 195
  2677.         and ( name = #column_privileges.column_name
  2678.           or name = 'All')
  2679.         and ( uid = 0
  2680.           or uid = #column_privileges.gid
  2681.           or uid = #column_privileges.uid))
  2682.  
  2683.  
  2684.  
  2685.     update #column_privileges
  2686.     set insert_privilege = 1
  2687.     where exists (select * from #protects
  2688.         where protecttype = 205
  2689.         and action = 196
  2690.         and ( name = #column_privileges.column_name
  2691.           or name = 'All')
  2692.         and ( uid = 0
  2693.           or uid = #column_privileges.gid
  2694.           or uid = #column_privileges.uid))
  2695.     and not exists (select * from #protects
  2696.         where protecttype = 206
  2697.         and action = 196
  2698.         and ( name = #column_privileges.column_name
  2699.           or name = 'All')
  2700.         and ( uid = 0
  2701.           or uid = #column_privileges.gid
  2702.           or uid = #column_privileges.uid))
  2703.  
  2704.  
  2705.     update #column_privileges
  2706.     set update_privilege = 1
  2707.     where exists (select * from #protects
  2708.         where protecttype = 205
  2709.         and action = 197
  2710.         and ( name = #column_privileges.column_name
  2711.           or name = 'All')
  2712.         and ( uid = 0
  2713.           or uid = #column_privileges.gid
  2714.           or uid = #column_privileges.uid))
  2715.     and not exists (select * from #protects
  2716.         where protecttype = 206
  2717.         and action = 197
  2718.         and ( name = #column_privileges.column_name
  2719.           or name = 'All')
  2720.         and ( uid = 0
  2721.           or uid = #column_privileges.gid
  2722.           or uid = #column_privileges.uid))
  2723.  
  2724. create table #column_priv2
  2725.     (table_qualifier         varchar(32),
  2726.         table_owner         varchar(32),
  2727.         table_name     varchar(32),
  2728.     column_name varchar(32),
  2729.         grantor            varchar(32),
  2730.         grantee            varchar(32),
  2731.         privilege            varchar(32),
  2732.     is_grantable    varchar(3))
  2733.  
  2734.   insert into #column_priv2
  2735.      select
  2736.     table_qualifier,
  2737.         table_owner,
  2738.         table_name,
  2739.         column_name,
  2740.         grantor,
  2741.         grantee,
  2742.         'SELECT',
  2743.     is_grantable
  2744.     from #column_privileges
  2745.     where select_privilege = 1
  2746.  
  2747.  
  2748.      insert into #column_priv2
  2749.      select
  2750.     table_qualifier,
  2751.         table_owner,
  2752.         table_name,
  2753.         column_name,
  2754.         grantor,
  2755.         grantee,
  2756.         'INSERT',
  2757.     is_grantable
  2758.     from #column_privileges
  2759.     where insert_privilege = 1
  2760.  
  2761.  
  2762.  
  2763.      insert into #column_priv2
  2764.      select
  2765.     table_qualifier,
  2766.         table_owner,
  2767.         table_name,
  2768.         column_name,
  2769.         grantor,
  2770.         grantee,
  2771.         'UPDATE',
  2772.     is_grantable
  2773.     from #column_privileges
  2774.     where update_privilege = 1
  2775.  
  2776.  
  2777.     select * from #column_priv2
  2778.     where column_name like @column_name
  2779.      order by column_name, privilege
  2780. go
  2781.  
  2782. grant execute on sp_column_privileges to public
  2783. go
  2784. dump tran master with truncate_only
  2785. go
  2786.  
  2787.  
  2788. print "creating sp_server_info"
  2789. if (exists (select * from sysobjects where name = 'sp_server_info'))
  2790. begin
  2791.     drop proc sp_server_info
  2792. end
  2793. go
  2794.  
  2795. create proc sp_server_info (@attribute_id  int = null)
  2796. as
  2797.     if @attribute_id is not null
  2798.     select *
  2799.     from master.dbo.spt_server_info
  2800.     where attribute_id = @attribute_id
  2801.     else
  2802.     select *
  2803.     from master.dbo.spt_server_info
  2804.  
  2805. go
  2806.  
  2807. grant execute on sp_server_info to public
  2808. go
  2809.  
  2810. print "creating sp_datatype_info"
  2811. if (exists (select * from sysobjects where name = 'sp_datatype_info'))
  2812. begin
  2813.     drop proc sp_datatype_info
  2814. end
  2815. go
  2816.  
  2817. /* the messiness of 'data_type' was to get around the problem of
  2818. returning the correct lengths for user defined types.  the join
  2819. on the type name ensures all user defined types are returned, but
  2820. this puts a null in the data_type column.  by forcing an embedded
  2821. select and correlating it with the current row in systypes, we get
  2822. the correct data_type mapping even for user defined types  (kwg) */
  2823.  
  2824. /* this is the version for servers without support for UNION */
  2825. create proc sp_datatype_info (@data_type int = 0)
  2826. as
  2827.     if @data_type = 0
  2828.     select
  2829.         type_name = t.name,
  2830.         d.data_type,
  2831.         precision=isnull(d.data_precision, convert(int,t.length)),
  2832.         d.literal_prefix,
  2833.         d.literal_suffix,
  2834.         e.create_params,
  2835.         d.nullable,
  2836.         d.case_sensitive,
  2837.         d.searchable,
  2838.              d.unsigned_attribute,
  2839.              d.money,
  2840.              d.auto_increment,
  2841.              d.local_type_name
  2842.     from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2843.     where d.ss_dtype = t.type and
  2844.       t.usertype *= e.user_type and
  2845.       t.type not in (111,109,38,110)  /* get rid of nullable types */
  2846.     order by d.data_type, type_name
  2847.     else
  2848.     select
  2849.         type_name = t.name,
  2850.         d.data_type,
  2851.         precision=isnull(d.data_precision, convert(int,t.length)),
  2852.         d.literal_prefix,
  2853.         d.literal_suffix,
  2854.         e.create_params,
  2855.         d.nullable,
  2856.         d.case_sensitive,
  2857.         d.searchable,
  2858.              d.unsigned_attribute,
  2859.              d.money,
  2860.              d.auto_increment,
  2861.              d.local_type_name
  2862.     from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2863.     where data_type = @data_type and
  2864.          d.ss_dtype = t.type and
  2865.       t.usertype *= e.user_type and
  2866.       t.type not in (111,109,38,110)
  2867.     order by type_name
  2868. go
  2869.  
  2870. /* if this is a 4.2 or later SQL Server, then we want to delete the stored
  2871. procedure we just created, and create a new one which uses UNION
  2872. (this fixes some bugs involving UDT names)
  2873. */
  2874.  
  2875. if  (charindex('1.1', @@version) = 0
  2876.     and charindex('4.0', @@version) = 0)
  2877. begin
  2878.     drop proc sp_datatype_info
  2879.     dump tran master with truncate_only
  2880. end
  2881. else
  2882. begin
  2883.     print ""
  2884.     print ""
  2885.     print "Installing catalog procedures on a 1.x or 4.0 server:"
  2886.     print "Ignore the following error messages"
  2887.     print ""
  2888.     print ""
  2889. end
  2890. go
  2891.  
  2892. /* this is the version for servers which support UNION */
  2893. create proc sp_datatype_info (@data_type int = 0)
  2894. as
  2895.   if @data_type = 0
  2896.     select  /* Real SQL Server data types */
  2897.         type_name = t.name,
  2898.         d.data_type,
  2899.         precision=isnull(d.data_precision, convert(int,t.length)),
  2900.         d.literal_prefix,
  2901.         d.literal_suffix,
  2902.         e.create_params,
  2903.         d.nullable,
  2904.         d.case_sensitive,
  2905.         d.searchable,
  2906.         d.unsigned_attribute,
  2907.         d.money,
  2908.         d.auto_increment,
  2909.         d.local_type_name
  2910.     from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2911.     where d.ss_dtype = t.type and
  2912.       t.usertype *= e.user_type and
  2913.       t.usertype not in (80,18) and
  2914.       t.usertype < 100 and
  2915.       t.type not in (111,109,38,110)  /* get rid of nullable types */
  2916.           and t.name != 'nchar'   /* get rid of bogus 4.9.1 types */
  2917.       and t.name != 'nvarchar'  /* get rid of more bogus 4.9.1 types */
  2918.     UNION
  2919.     select  /* SQL Server SYSNAME, TIMESTAMP and user data types */
  2920.         type_name = t.name,
  2921.         d.data_type,
  2922.         precision=isnull(d.data_precision, convert(int,t.length)),
  2923.         d.literal_prefix,
  2924.         d.literal_suffix,
  2925.         e.create_params,
  2926.         d.nullable,
  2927.         d.case_sensitive,
  2928.         d.searchable,
  2929.         d.unsigned_attribute,
  2930.         d.money,
  2931.         d.auto_increment,
  2932.         t.name
  2933.     from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2934.     where d.ss_dtype = t.type and
  2935.       t.usertype *= e.user_type and
  2936.       (t.usertype in (80,18) or
  2937.        t.usertype >= 100) and
  2938.       t.type not in (111,109,38,110)  /* get rid of nullable types */
  2939.     order by d.data_type, type_name
  2940.   else
  2941.     select  /* Real SQL Server data types */
  2942.         type_name = t.name,
  2943.         d.data_type,
  2944.         precision=isnull(d.data_precision, convert(int,t.length)),
  2945.         d.literal_prefix,
  2946.         d.literal_suffix,
  2947.         e.create_params,
  2948.         d.nullable,
  2949.         d.case_sensitive,
  2950.         d.searchable,
  2951.         d.unsigned_attribute,
  2952.         d.money,
  2953.         d.auto_increment,
  2954.         d.local_type_name
  2955.     from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2956.     where data_type = @data_type and
  2957.          d.ss_dtype = t.type and
  2958.       t.usertype *= e.user_type and
  2959.       t.usertype not in (80,18) and
  2960.       t.usertype < 100 and
  2961.       t.type not in (111,109,38,110)
  2962.     UNION
  2963.     select  /* SQL Server SYSNAME, TIMESTAMP and user data types */
  2964.         type_name = t.name,
  2965.         d.data_type,
  2966.         precision=isnull(d.data_precision, convert(int,t.length)),
  2967.         d.literal_prefix,
  2968.         d.literal_suffix,
  2969.         e.create_params,
  2970.         d.nullable,
  2971.         d.case_sensitive,
  2972.         d.searchable,
  2973.         d.unsigned_attribute,
  2974.         d.money,
  2975.         d.auto_increment,
  2976.         t.name
  2977.     from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2978.     where data_type = @data_type and
  2979.          d.ss_dtype = t.type and
  2980.       t.usertype *= e.user_type and
  2981.       (t.usertype in (80,18) or
  2982.        t.usertype >= 100) and
  2983.       t.type not in (111,109,38,110)
  2984.     order by type_name
  2985. go
  2986.  
  2987. grant execute on sp_datatype_info to public
  2988. go
  2989.  
  2990. dump tran master with truncate_only
  2991. go
  2992.  
  2993. print "creating sp_special_columns"
  2994. if (exists (select * from sysobjects where name = 'sp_special_columns'))
  2995. begin
  2996.     drop proc sp_special_columns
  2997. end
  2998. go
  2999.  
  3000. dump tran master with truncate_only
  3001. go
  3002.  
  3003.  
  3004. CREATE PROCEDURE sp_special_columns (@table_name      varchar(32),
  3005.                  @table_owner      varchar(32) = null,
  3006.                  @table_qualifier varchar(32) = null,
  3007.                  @col_type       char(1) = 'R')
  3008. AS
  3009.     DECLARE @indid                int
  3010.     DECLARE @table_id            int
  3011.     DECLARE @full_table_name    char(70)
  3012.     DECLARE @msg          char(70)
  3013.  
  3014.     if @table_qualifier is not null
  3015.     begin
  3016.     if db_name() != @table_qualifier
  3017.     begin
  3018.         print "Table qualifier must be name of current database"
  3019.         return
  3020.     end
  3021.     end
  3022.     if @table_owner is null
  3023.     begin
  3024.     SELECT @full_table_name = @table_name
  3025.     end
  3026.     else
  3027.     begin
  3028.     SELECT @full_table_name = @table_owner + '.' + @table_name
  3029.     end
  3030.     SELECT @table_id = object_id(@full_table_name)
  3031.  
  3032.   /* if we just need ROWVER, just run that query */
  3033.     if @col_type = 'V'
  3034.   BEGIN
  3035.     SELECT
  3036.     scope = convert(smallint, 0),
  3037.     column_name = c.name,
  3038.     data_type=d.data_type+convert(smallint,
  3039.                     isnull(d.aux,
  3040.                     ascii(substring("666AAA@@@CB??GG",
  3041.                     2*(d.ss_dtype%35+1)+2-8/c.length,1))
  3042.                     -60)),
  3043.     type_name=t.name,
  3044.     precision=isnull(d.data_precision, convert(int,c.length))
  3045.                 +isnull(d.aux, convert(int,
  3046.                 ascii(substring("???AAAFFFCKFOLS",
  3047.                 2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  3048.     length=isnull(d.length, convert(int,c.length)) +convert(int,
  3049.                     isnull(d.aux,
  3050.                     ascii(substring("AAA<BB<DDDHJSPP",
  3051.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  3052.                     1))-64)),
  3053.     scale = d.numeric_scale +convert(smallint,
  3054.                     isnull(d.aux,
  3055.                     ascii(substring("<<<<<<<<<<<<<<?",
  3056.                     2*(d.ss_dtype%35+1)+2-8/c.length,
  3057.                     1))-60))
  3058.       FROM
  3059.         systypes t,syscolumns c,  master.dbo.spt_datatype_info d
  3060.       WHERE
  3061.       c.id=@table_id
  3062.       AND c.type = d.ss_dtype
  3063.       AND c.usertype = 80
  3064.       AND t.usertype = 80
  3065.     RETURN
  3066.   END
  3067.   /* now find the id of the 'best' index for this table */
  3068.  
  3069.   SELECT @indid = (
  3070.     SELECT MIN(indid)
  3071.       FROM sysindexes
  3072.       WHERE  status&2 = 2
  3073.       AND id = @table_id
  3074.       AND indid > 0)
  3075.  
  3076.    SELECT
  3077.       scope = convert(smallint, 0),
  3078.       column_name = INDEX_COL(@full_table_name,indid,c.colid),
  3079.       data_type=d.data_type+convert(smallint,
  3080.                     isnull(d.aux,
  3081.                     ascii(substring("666AAA@@@CB??GG",
  3082.                     2*(d.ss_dtype%35+1)+2-8/c2.length,1))
  3083.                     -60)),
  3084.       type_name=rtrim(substring(d.type_name,
  3085.                     1+isnull(d.aux,
  3086.                     ascii(substring("III<<<MMMI<<A<A",
  3087.                     2*(d.ss_dtype%35+1)+2-8/c2.length,
  3088.                     1))-60), 13)),
  3089.       precision=isnull(d.data_precision, convert(int,c2.length))
  3090.                 +isnull(d.aux, convert(int,
  3091.                 ascii(substring("???AAAFFFCKFOLS",
  3092.                 2*(d.ss_dtype%35+1)+2-8/c2.length,1))-60)),
  3093.       length=isnull(d.length, convert(int,c2.length)) +convert(int,
  3094.                     isnull(d.aux,
  3095.                     ascii(substring("AAA<BB<DDDHJSPP",
  3096.                     2*(d.ss_dtype%35+1)+2-8/c2.length,
  3097.                     1))-64)),
  3098.       scale = d.numeric_scale +convert(smallint,
  3099.                     isnull(d.aux,
  3100.                     ascii(substring("<<<<<<<<<<<<<<?",
  3101.                     2*(d.ss_dtype%35+1)+2-8/c2.length,
  3102.                     1))-60))
  3103.     FROM sysindexes x,syscolumns c, master.dbo.spt_datatype_info d, systypes t, syscolumns c2
  3104.     WHERE x.id=@table_id
  3105.     AND c2.name = INDEX_COL(@full_table_name,@indid,c.colid)
  3106.     AND c2.id =x.id
  3107.     AND c.id = x.id
  3108.     AND c.colid<keycnt+ (x.status&16)/16
  3109.     AND x.indid=@indid
  3110.       AND c2.type = d.ss_dtype
  3111.      AND c2.usertype *= t.usertype
  3112.  
  3113. go
  3114. grant execute on sp_special_columns to public
  3115. go
  3116.  
  3117. print "creating sp_databases"
  3118. if (exists (select * from sysobjects where name = 'sp_databases'))
  3119. begin
  3120.     drop proc sp_databases
  3121. end
  3122. go
  3123.  
  3124. create procedure sp_databases
  3125.     as
  3126.     create table #databases ( database_name varchar(32),
  3127.                   size int)
  3128.     insert into #databases
  3129.     select  name,
  3130.         (select sum(size) from master.dbo.sysusages
  3131.       where dbid = d.dbid )
  3132.     from master.dbo.sysdatabases d
  3133.     select database_name, database_size = size*2,
  3134.          remarks = convert(varchar(254),null)       from #databases
  3135. go
  3136. grant execute on sp_databases to public
  3137. go
  3138.  
  3139. dump tran master with truncate_only
  3140. go
  3141.  
  3142.  
  3143.  
  3144.  
  3145.  
  3146.  
  3147. /*******************************************************************************/
  3148. /* This portion returns everything back to normal                   */
  3149. /*******************************************************************************/
  3150. use master
  3151. go
  3152.  
  3153. if exists (select * from sysobjects
  3154.        where name = 'sp_configure' and sysstat & 7 = 4)
  3155. begin
  3156.     execute sp_configure 'update',0
  3157. end
  3158. reconfigure with override
  3159. go
  3160. checkpoint
  3161. go
  3162.