home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1999 March / CDW0399.iso / Demos / FileM_4.1 / FMPROCD.Z / DATA1.CAB / Sybase_driver / Ivcsp492.sql next >
Encoding:
Text File  |  1998-09-02  |  77.8 KB  |  2,919 lines

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