home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 4.img / SCRIPTS / INSTMSTR.SQL < prev    next >
Encoding:
Text File  |  1992-05-17  |  380.8 KB  |  15,249 lines

  1. /* static char    Sccsid[] = "@(#)instmast.sql    1.25    4/9/92"; */
  2. use master
  3. go
  4. declare @size int
  5.     select @size = (2048 - sum(size)) / 512
  6.     from sysusages where dbid = 1
  7. if (@size = 1)
  8. begin
  9. /* If a 3 MB master, alter by 2 to make it 5 */
  10.     alter database master on master = 2
  11. end
  12. else if (@size = 2)
  13. begin
  14. /* If a 2MB master, alter by 3 to make it 5 */
  15.     alter database master on master = 3
  16. end
  17. go
  18. print "Warning: if Msg 2812 immediately follows, please disregard it."
  19. go
  20. if exists (select *
  21.     from sysobjects
  22.         where name = 'sp_configure'
  23.             and sysstat & 7 = 4)
  24. begin
  25.     execute sp_configure 'update', 1
  26. end
  27. reconfigure with override
  28. go
  29. /*
  30. ** a_aversion 49.1 9/10/91
  31. **
  32. ** Copyright Sybase, Inc. 1986, 1987, 1988, 1989
  33. **    All Rights Reserved.
  34. ** Use, duplication, or disclosure by the United States Government
  35. ** is subject to restrictions as set forth in subdivision (c) (1) (ii)
  36. ** of the Rights in Technical Data and Computer Software clause
  37. ** at CFR 252.227-7013. Sybase, Inc. 6475 Christie Avenue, Emeryville,
  38. ** CA 94608.
  39. **
  40. ** Stored procedures for Data Dictionary Operations
  41. */
  42. go
  43. dump transaction master with truncate_only
  44. go
  45. /* a_permitboth 49.1 9/10/91 */
  46. grant select on sysalternates to public
  47. grant select on syscolumns to public
  48. grant select on syscomments to public
  49. grant select on sysdepends to public
  50. grant select on sysindexes to public
  51. grant select on syskeys to public
  52. grant select on sysobjects to public grant select on sysprocedures to public
  53. grant select on sysprotects to public
  54. grant select on syssegments to public
  55. grant select on systypes to public
  56. grant select on sysusers to public
  57. grant select on syslogs to public
  58. go
  59. /* a_permitmaster 49.1 9/10/91 */
  60. grant select on sysdatabases to public
  61. grant select on sysdevices to public
  62. grant select on syslocks to public
  63. grant select on syslogins to public
  64. revoke select on syslogins(password) to public
  65. grant select on sysmessages to public
  66. grant select on sysprocesses to public
  67. grant select on sysusages to public
  68. grant select on sysconfigures to public
  69. grant select on syscurconfigs to public
  70. grant select on sysservers to public
  71. grant select on sysremotelogins to public
  72. grant select on syslanguages to public
  73. grant select on syscharsets to public
  74. go
  75. /* a_values 49.1 9/10/91 */
  76. set nocount on
  77. go
  78. use master
  79. go
  80. if exists (select *
  81.     from sysobjects
  82.         where name = "spt_values")
  83. begin
  84.     drop table spt_values
  85. end
  86. go
  87. if exists (select *
  88.         from sysobjects
  89.             where name = "spt_monitor")
  90. begin
  91.     drop table spt_monitor
  92. end
  93. go
  94. if exists (select *
  95.     from sysobjects
  96.         where sysstat & 7 = 4
  97.             and name = 'sp_validname')
  98. begin
  99.     drop procedure sp_validname
  100. end
  101. go
  102. if exists (select *
  103.     from sysobjects
  104.         where sysstat & 7 = 4
  105.             and name = 'sp_validlang')
  106. begin
  107.     drop procedure sp_validlang
  108. end
  109. go
  110. if exists (select *
  111.     from sysobjects
  112.         where sysstat & 7 = 4
  113.             and name = 'sp_validaltlang')
  114. begin
  115.     drop procedure sp_validaltlang
  116. end
  117. go
  118. if exists (select *
  119.     from sysobjects
  120.         where sysstat & 7 = 4
  121.             and name = 'sp_chklangparam')
  122. begin
  123.     drop procedure sp_chklangparam
  124. end
  125. go
  126. if exists (select *
  127.     from sysobjects
  128.         where sysstat & 7 = 4
  129.             and name = 'sp_namecrack')
  130. begin
  131.     drop procedure sp_namecrack
  132. end
  133. go
  134. if exists (select *
  135.     from sysobjects
  136.         where sysstat & 7 = 4
  137.             and name = 'sp_addalias')
  138. begin
  139.     drop procedure sp_addalias
  140. end
  141. go
  142. if exists (select *
  143.     from sysobjects
  144.         where sysstat & 7 = 4
  145.             and name = 'sp_addgroup')
  146. begin
  147.     drop procedure sp_addgroup
  148. end
  149. go
  150. if exists (select *
  151.     from sysobjects
  152.         where sysstat & 7 = 4
  153.             and name = 'sp_addlogin')
  154. begin
  155.     drop procedure sp_addlogin
  156. end
  157. go
  158. dump transaction master with truncate_only
  159. go
  160. if exists (select *
  161.     from sysobjects
  162.         where sysstat & 7 = 4
  163.             and name = 'sp_addtype')
  164. begin
  165.     drop procedure sp_addtype
  166. end
  167. go
  168. if exists (select *
  169.     from sysobjects
  170.         where sysstat & 7 = 4
  171.             and name = 'sp_addumpdevice')
  172. begin
  173.     drop procedure sp_addumpdevice
  174. end
  175. go
  176. if exists (select *
  177.     from sysobjects
  178.         where sysstat & 7 = 4
  179.             and name = 'sp_adduser')
  180. begin
  181.     drop procedure sp_adduser
  182. end
  183. go
  184. if exists (select *
  185.     from sysobjects
  186.         where sysstat & 7 = 4
  187.             and name = 'sp_addremotelogin')
  188. begin
  189.     drop procedure sp_addremotelogin
  190. end
  191. go
  192. if exists (select *
  193.     from sysobjects
  194.         where sysstat & 7 = 4
  195.             and name = 'sp_addsegment')
  196. begin
  197.     drop procedure sp_addsegment
  198. end
  199. go
  200. if exists (select *
  201.     from sysobjects
  202.         where sysstat & 7 = 4
  203.             and name = 'sp_addserver')
  204. begin
  205.     drop procedure sp_addserver
  206. end
  207. go
  208. if exists (select *
  209.     from sysobjects
  210.         where sysstat & 7 = 4
  211.             and name = 'sp_addlanguage')
  212. begin
  213.     drop procedure sp_addlanguage
  214. end
  215. go
  216. if exists (select *
  217.     from sysobjects
  218.         where sysstat & 7 = 4
  219.             and name = 'sp_defaultlanguage')
  220. begin
  221.     drop procedure sp_defaultlanguage
  222. end
  223. go
  224. if exists (select *
  225.     from sysobjects
  226.         where sysstat & 7 = 4
  227.             and name = 'sp_helplanguage')
  228. begin
  229.     drop procedure sp_helplanguage
  230. end
  231. go
  232. if exists (select *
  233.     from sysobjects
  234.         where sysstat & 7 = 4
  235.             and name = 'sp_droplanguage')
  236. begin
  237.     drop procedure sp_droplanguage
  238. end
  239. go
  240. dump transaction master with truncate_only
  241. go
  242. if exists (select *
  243.     from sysobjects
  244.         where sysstat & 7 = 4
  245.             and name = 'sp_setlangalias')
  246. begin
  247.     drop procedure sp_setlangalias
  248. end
  249. go
  250. if exists (select *
  251.     from sysobjects
  252.         where sysstat & 7 = 4
  253.             and name = 'sp_bindefault')
  254. begin
  255.     drop procedure sp_bindefault
  256. end
  257. go
  258. if exists (select *
  259.     from sysobjects
  260.         where sysstat & 7 = 4
  261.             and name = 'sp_bindrule')
  262. begin
  263.     drop procedure sp_bindrule
  264. end
  265. go
  266. if exists (select *
  267.     from sysobjects
  268.         where sysstat & 7 = 4
  269.             and name = 'sp_changedbowner')
  270. begin
  271.     drop procedure sp_changedbowner
  272. end
  273. go
  274. if exists (select *
  275.     from sysobjects
  276.         where sysstat & 7 = 4
  277.             and name = 'sp_changegroup')
  278. begin
  279.     drop procedure sp_changegroup
  280. end
  281. go
  282. if exists (select *
  283.     from sysobjects
  284.         where sysstat & 7 = 4
  285.             and name = 'sp_checknames')
  286. begin
  287.     drop procedure sp_checknames
  288. end
  289. go
  290. if exists (select *
  291.     from sysobjects
  292.         where sysstat & 7 = 4
  293.             and name = 'sp_start_xact')
  294. begin
  295.     drop procedure sp_start_xact
  296. end
  297. go
  298. if exists (select *
  299.     from sysobjects
  300.         where sysstat & 7 = 4
  301.             and name = 'sp_commit_xact')
  302. begin
  303.     drop procedure sp_commit_xact
  304. end
  305. go
  306. if exists (select *
  307.     from sysobjects
  308.         where sysstat & 7 = 4
  309.             and name = 'sp_abort_xact')
  310. begin
  311.     drop procedure sp_abort_xact
  312. end
  313. go
  314. if exists (select *
  315.     from sysobjects
  316.         where sysstat & 7 = 4
  317.             and name = 'sp_remove_xact')
  318. begin
  319.     drop procedure sp_remove_xact
  320. end
  321. go
  322. dump transaction master with truncate_only
  323. go
  324. if exists (select *
  325.     from sysobjects
  326.         where sysstat & 7 = 4
  327.             and name = 'sp_stat_xact')
  328. begin
  329.     drop procedure sp_stat_xact
  330. end
  331. go
  332. if exists (select *
  333.     from sysobjects
  334.         where sysstat & 7 = 4
  335.             and name = 'sp_probe_xact')
  336. begin
  337.     drop procedure sp_probe_xact
  338. end
  339. go
  340. if exists (select *
  341.     from sysobjects
  342.         where sysstat & 7 = 4
  343.             and name = 'sp_scan_xact')
  344. begin
  345.     drop procedure sp_scan_xact
  346. end
  347. go
  348. /* commit 27.1 3/5/90 */
  349. if exists (select *
  350.         from sysobjects
  351.             where name = "spt_committab"
  352.                 and sysstat & 7 = 3)
  353. begin
  354.     drop table spt_committab
  355. end
  356. go
  357. if exists (select *
  358.     from sysobjects
  359.         where sysstat & 7 = 4
  360.             and name = 'sp_commonkey')
  361. begin
  362.     drop procedure sp_commonkey
  363. end
  364. go
  365. if exists (select *
  366.     from sysobjects
  367.         where sysstat & 7 = 4
  368.             and name = 'sp_configure')
  369. begin
  370.     drop procedure sp_configure
  371. end
  372. go
  373. if exists (select *
  374.     from sysobjects
  375.         where sysstat & 7 = 4
  376.             and name = 'sp_dboption')
  377. begin
  378.     drop procedure sp_dboption
  379. end
  380. go
  381. if exists (select *
  382.     from sysobjects
  383.         where sysstat & 7 = 4
  384.             and name = 'sp_defaultdb')
  385. begin
  386.     drop procedure sp_defaultdb
  387. end
  388. go
  389. if exists (select *
  390.     from sysobjects
  391.         where sysstat & 7 = 4
  392.             and name = 'sp_depends')
  393. begin
  394.     drop procedure sp_depends
  395. end
  396. go
  397. if exists (select *
  398.     from sysobjects
  399.         where sysstat & 7 = 4
  400.             and name = 'sp_diskdefault')
  401. begin
  402.     drop procedure sp_diskdefault
  403. end
  404. go
  405. dump transaction master with truncate_only
  406. go
  407. if exists (select *
  408.     from sysobjects
  409.         where sysstat & 7 = 4
  410.             and name = 'sp_dropalias')
  411. begin
  412.     drop procedure sp_dropalias
  413. end
  414. go
  415. if exists (select *
  416.     from sysobjects
  417.         where sysstat & 7 = 4
  418.             and name = 'sp_dropdevice')
  419. begin
  420.     drop procedure sp_dropdevice
  421. end
  422. go
  423. if exists (select *
  424.     from sysobjects
  425.         where sysstat & 7 = 4
  426.             and name = 'sp_dropdumpdevice')
  427. begin
  428.     drop procedure sp_dropdumpdevice
  429. end
  430. go
  431. if exists (select *
  432.     from sysobjects
  433.         where sysstat & 7 = 4
  434.             and name = 'sp_dropgroup')
  435. begin
  436.     drop procedure sp_dropgroup
  437. end
  438. go
  439. if exists (select *
  440.     from sysobjects
  441.         where sysstat & 7 = 4
  442.             and name = 'sp_dropkey')
  443. begin
  444.     drop procedure sp_dropkey
  445. end
  446. go
  447. if exists (select *
  448.     from sysobjects
  449.         where sysstat & 7 = 4
  450.             and name = 'sp_droplogin')
  451. begin
  452.     drop procedure sp_droplogin
  453. end
  454. go
  455. if exists (select *
  456.     from sysobjects
  457.         where sysstat & 7 = 4
  458.             and name = 'sp_droptype')
  459. begin
  460.     drop procedure sp_droptype
  461. end
  462. go
  463. if exists (select *
  464.     from sysobjects
  465.         where sysstat & 7 = 4
  466.             and name = 'sp_dropuser')
  467. begin
  468.     drop procedure sp_dropuser
  469. end
  470. go
  471. if exists (select *
  472.     from sysobjects
  473.         where sysstat & 7 = 4
  474.             and name = 'sp_dropremotelogin')
  475. begin
  476.     drop procedure sp_dropremotelogin
  477. end
  478. go
  479. if exists (select *
  480.     from sysobjects
  481.         where sysstat & 7 = 4
  482.             and name = 'sp_dropsegment')
  483. begin
  484.     drop procedure sp_dropsegment
  485. end
  486. go
  487. dump transaction master with truncate_only
  488. go
  489. if exists (select *
  490.     from sysobjects
  491.         where sysstat & 7 = 4
  492.             and name = 'sp_dropserver')
  493. begin
  494.     drop procedure sp_dropserver
  495. end
  496. go
  497. if exists (select *
  498.     from sysobjects
  499.         where sysstat & 7 = 4
  500.             and name = 'sp_extendsegment')
  501. begin
  502.     drop procedure sp_extendsegment
  503. end
  504. go
  505. if exists (select *
  506.     from sysobjects
  507.         where sysstat & 7 = 4
  508.             and name = 'sp_fixindex')
  509. begin
  510.     drop procedure sp_fixindex
  511. end
  512. go
  513. if exists (select *
  514.     from sysobjects
  515.         where sysstat & 7 = 4
  516.             and name = 'sp_foreignkey')
  517. begin
  518.     drop procedure sp_foreignkey
  519. end
  520. go
  521. if exists (select *
  522.     from sysobjects
  523.         where sysstat & 7 = 4
  524.             and name = 'sp_helpdb')
  525. begin
  526.     drop procedure sp_helpdb
  527. end
  528. go
  529. if exists (select *
  530.     from sysobjects
  531.         where sysstat & 7 = 4
  532.             and name = 'sp_helpdevice')
  533. begin
  534.     drop procedure sp_helpdevice
  535. end
  536. go
  537. if exists (select *
  538.     from sysobjects
  539.         where sysstat & 7 = 4
  540.             and name = 'sp_helpgroup')
  541. begin
  542.     drop procedure sp_helpgroup
  543. end
  544. go
  545. if exists (select *
  546.     from sysobjects
  547.         where sysstat & 7 = 4
  548.             and name = 'sp_helplog')
  549. begin
  550.     drop procedure sp_helplog
  551. end
  552. go
  553. if exists (select *
  554.     from sysobjects
  555.         where sysstat & 7 = 4
  556.             and name = 'sp_helpindex')
  557. begin
  558.     drop procedure sp_helpindex
  559. end
  560. go
  561. if exists (select *
  562.     from sysobjects
  563.         where sysstat & 7 = 4
  564.             and name = 'sp_helpjoins')
  565. begin
  566.     drop procedure sp_helpjoins
  567. end
  568. go
  569. dump transaction master with truncate_only
  570. go
  571. if exists (select *
  572.     from sysobjects
  573.         where sysstat & 7 = 4
  574.             and name = 'sp_helpkey')
  575. begin
  576.     drop procedure sp_helpkey
  577. end
  578. go
  579. if exists (select *
  580.     from sysobjects
  581.         where sysstat & 7 = 4
  582.             and name = 'sp_objectsegment')
  583. begin
  584.     drop procedure sp_objectsegment
  585. end
  586. go
  587. if exists (select *
  588.     from sysobjects
  589.         where sysstat & 7 = 4
  590.             and name = 'sp_help')
  591. begin
  592.     drop procedure sp_help
  593. end
  594. go
  595. if exists (select *
  596.     from sysobjects
  597.         where sysstat & 7 = 4
  598.             and name = 'sp_helprotect')
  599. begin
  600.     drop procedure sp_helprotect
  601. end
  602. go
  603. if exists (select *
  604.     from sysobjects
  605.         where sysstat & 7 = 4
  606.             and name = 'sp_helptext')
  607. begin
  608.     drop procedure sp_helptext
  609. end
  610. go
  611. if exists (select *
  612.     from sysobjects
  613.         where sysstat & 7 = 4
  614.             and name = 'sp_helpuser')
  615. begin
  616.     drop procedure sp_helpuser
  617. end
  618. go
  619. if exists (select *
  620.     from sysobjects
  621.         where sysstat & 7 = 4
  622.             and name = 'sp_lock')
  623. begin
  624.     drop procedure sp_lock
  625. end
  626. go
  627. if exists (select *
  628.     from sysobjects
  629.         where sysstat & 7 = 4
  630.             and name = 'sp_logdevice')
  631. begin
  632.     drop procedure sp_logdevice
  633. end
  634. go
  635. if exists (select *
  636.     from sysobjects
  637.         where sysstat & 7 = 4
  638.             and name = 'sp_lookup')
  639. begin
  640.     drop procedure sp_lookup
  641. end
  642. go
  643. if exists (select *
  644.     from sysobjects
  645.         where sysstat & 7 = 4
  646.             and name = 'sp_helpremotelogin')
  647. begin
  648.     drop procedure sp_helpremotelogin
  649. end
  650. go
  651. dump transaction master with truncate_only
  652. go
  653. if exists (select *
  654.     from sysobjects
  655.         where sysstat & 7 = 4
  656.             and name = 'sp_helpsegment')
  657. begin
  658.     drop procedure sp_helpsegment
  659. end
  660. go
  661. if exists (select *
  662.     from sysobjects
  663.         where sysstat & 7 = 4
  664.             and name = 'sp_helpsort')
  665. begin
  666.     drop procedure sp_helpsort
  667. end
  668. go
  669. if exists (select *
  670.     from sysobjects
  671.         where sysstat & 7 = 4
  672.             and name = 'sp_helpserver')
  673. begin
  674.     drop procedure sp_helpserver
  675. end
  676. go
  677. if exists (select *
  678.     from sysobjects
  679.         where sysstat & 7 = 4
  680.             and name = 'sp_markreport')
  681. begin
  682.     drop procedure sp_markreport
  683. end
  684. go
  685. if exists (select *
  686.     from sysobjects
  687.         where sysstat & 7 = 4
  688.             and name = 'sp_monitor')
  689. begin
  690.     drop procedure sp_monitor
  691. end
  692. go
  693. if exists (select *
  694.     from sysobjects
  695.         where sysstat & 7 = 4
  696.             and name = 'sp_password')
  697. begin
  698.     drop procedure sp_password
  699. end
  700. go
  701. if exists (select *
  702.     from sysobjects
  703.         where sysstat & 7 = 4
  704.             and name = 'sp_placeobject')
  705. begin
  706.     drop procedure sp_placeobject
  707. end
  708. go
  709. if exists (select *
  710.     from sysobjects
  711.         where sysstat & 7 = 4
  712.             and name = 'sp_primarykey')
  713. begin drop procedure sp_primarykey
  714. end
  715. go
  716. if exists (select *
  717.     from sysobjects
  718.         where sysstat & 7 = 4
  719.             and name = 'sp_recompile')
  720. begin
  721.     drop procedure sp_recompile
  722. end
  723. go
  724. if exists (select *
  725.     from sysobjects
  726.         where sysstat & 7 = 4
  727.             and name = 'sp_remoteoption')
  728. begin
  729.     drop procedure sp_remoteoption
  730. end
  731. go
  732. dump transaction master with truncate_only
  733. go
  734. if exists (select *
  735.     from sysobjects
  736.         where sysstat & 7 = 4
  737.             and name = 'sp_rename')
  738. begin
  739.     drop procedure sp_rename
  740. end
  741. go
  742. if exists (select *
  743.     from sysobjects
  744.         where sysstat & 7 = 4
  745.             and name = 'sp_renamedb')
  746. begin
  747.     drop procedure sp_renamedb
  748. end
  749. go
  750. if exists (select *
  751.     from sysobjects
  752.         where sysstat & 7 = 4
  753.             and name = 'sp_serveroption')
  754. begin
  755.     drop procedure sp_serveroption
  756. end
  757. go
  758. if exists (select *
  759.     from sysobjects
  760.         where sysstat & 7 = 4
  761.             and name = 'sp_spaceused')
  762. begin
  763.     drop procedure sp_spaceused
  764. end
  765. go
  766. if exists (select *
  767.     from sysobjects
  768.         where sysstat & 7 = 4
  769.             and name = 'sp_unbindefault')
  770. begin
  771.     drop procedure sp_unbindefault
  772. end
  773. go
  774. if exists (select *
  775.     from sysobjects
  776.         where sysstat & 7 = 4
  777.             and name = 'sp_unbindrule')
  778. begin
  779.     drop procedure sp_unbindrule
  780. end
  781. go
  782. if exists (select *
  783.     from sysobjects
  784.         where sysstat & 7 = 4
  785.             and name = 'sp_who')
  786. begin
  787.     drop procedure sp_who
  788. end
  789. go
  790. dump transaction master with truncate_only
  791. go
  792.  
  793. create table spt_values (
  794. name    varchar(20) NULL,
  795. number    int,
  796. type    char(1),
  797. low    int NULL,
  798. high    int NULL)
  799. go
  800. create clustered index spt_valuesclust on spt_values(number, type)
  801. go
  802. insert spt_values(low, high, number, type) values (1, 1, 0, 'P')
  803. go
  804. set nocount on
  805. /*
  806. **insert spt_values(name, number, type)
  807. **    values ("BIT POSITIONS", -1, 'P')
  808. **go
  809. */
  810. insert spt_values(low, high, number, type) values (1, 2, 1, 'P')
  811. go
  812. insert spt_values(low, high, number, type) values (1, 4, 2, 'P')
  813. go
  814. insert spt_values(low, high, number, type) values (1, 8, 3, 'P')
  815. go
  816. insert spt_values(low, high, number, type) values (1, 16, 4, 'P')
  817. go
  818. insert spt_values(low, high, number, type) values (1, 32, 5, 'P')
  819. go
  820. insert spt_values(low, high, number, type) values (1, 64, 6, 'P')
  821. go
  822. insert spt_values(low, high, number, type) values (1, 128, 7, 'P')
  823. go
  824. /* 8 - 15 */
  825. insert spt_values(low, high, number, type)
  826. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P' 
  827. from spt_values
  828. where type = 'P' and number < 8 and number >= 0
  829. go
  830. /* 16 - 31 */
  831. insert spt_values(low, high, number, type)
  832. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P' 
  833. from spt_values
  834. where type = 'P' and number < 16 and number >= 0
  835. go
  836. /* 32 - 63 */
  837. insert spt_values(low, high, number, type)
  838. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P' 
  839. from spt_values
  840. where type = 'P' and number < 32 and number >= 0
  841. go
  842. /* 64 - 127 */
  843. insert spt_values(low, high, number, type)
  844. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P' 
  845. from spt_values
  846. where type = 'P' and number < 64 and number >= 0
  847. go
  848. /* 128 - 255 */
  849. insert spt_values(low, high, number, type)
  850. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P' 
  851. from spt_values
  852. where type = 'P' and number < 128 and number >= 0
  853. go
  854.  
  855. /*
  856. **  Configuration options.
  857. */
  858. insert spt_values(name, number, type)
  859.   values ("CONFIGURATION OPTIONS", -1, 'C')
  860. go
  861. insert spt_values(name, number, type, low, high)
  862.   values ("recovery interval", 101, 'C', 1, 32767)
  863. go
  864. insert spt_values(name, number, type, low, high)
  865.   values ("allow updates", 102, 'C', 0, 1)
  866. go
  867. insert spt_values(name, number, type, low, high)
  868.   values ("user connections", 103, 'C', 5, @@max_connections)
  869. go
  870. insert spt_values(name, number, type, low, high)
  871.   values ("memory", 104, 'C', 1000, 32000)
  872. go
  873. insert spt_values(name, number, type, low, high)
  874.   values ("open databases", 105, 'C', 5, 100)
  875. go
  876. insert spt_values(name, number, type, low, high)
  877.   values ("locks", 106, 'C', 5000, 500000)
  878. go
  879. insert spt_values(name, number, type, low, high)
  880.   values ("open objects", 107, 'C', 100, 10000)
  881. go
  882. insert spt_values(name, number, type, low, high)
  883.   values ("procedure cache", 108, 'C', 1, 99)
  884. go
  885. insert spt_values(name, number, type, low, high)
  886.   values ("fill factor", 109, 'C', 0, 100)
  887. go
  888. insert spt_values(name, number, type, low, high)
  889.   values ("time slice", 110, 'C', 50, 1000)
  890. go
  891. insert spt_values(name, number, type, low, high)
  892.   values ("database size", 111, 'C', 2, 10000)
  893. go
  894. insert spt_values(name, number, type, low, high)
  895.   values ("tape retention", 112, 'C', 0, 365)
  896. go
  897. insert spt_values(name, number, type, low, high)
  898.   values ("recovery flags", 113, 'C', 0, 1)
  899. go
  900. insert spt_values(name, number, type, low, high)
  901.   values ("serial number", 114, 'C', 1, 999999)
  902. go
  903. insert spt_values(name, number, type, low, high)
  904.   values ("nested triggers", 115, 'C', 0, 1)
  905. go
  906. insert spt_values(name, number, type, low, high)
  907.   values ("devices", 116, 'C', 4, 256)
  908. go
  909. insert spt_values(name, number, type, low, high)
  910.   values ("remote access", 117, 'C', 0, 1)
  911. go
  912. insert spt_values(name, number, type, low, high)
  913.   values ("remote logins", 118, 'C', 0, 4096)
  914. go
  915. insert spt_values(name, number, type, low, high)
  916.   values ("remote sites", 119, 'C', 0, 256)
  917. go
  918. insert spt_values(name, number, type, low, high)
  919.   values ("remote connections", 120, 'C', 0, 4096)
  920. go
  921. insert spt_values(name, number, type, low, high)
  922.   values ("pre-read packets", 121, 'C', 0, 256)
  923. go
  924. insert spt_values(name, number, type, low, high)
  925.   values ("upgrade version", 122, 'C', 0, 2147483647)
  926. go
  927. insert spt_values(name, number, type, low, high)
  928.   values ("default sortorder id", 123, 'C', 0, 255)
  929. go
  930. insert spt_values(name, number, type, low, high)
  931.   values ("default language", 124, 'C', 0, 9999)
  932. go
  933. insert spt_values(name, number, type, low, high)
  934.   values ("language in cache", 125, 'C', 3, 100)
  935. go
  936.  
  937. /* Token values */
  938. insert spt_values(name, number, type)
  939.   values ("TOKEN VALUES", -1, 'T')
  940. go
  941. insert spt_values(name, number, type)
  942.   values ("Update", 197, 'T')
  943. go
  944. insert spt_values(name, number, type)
  945.   values ("Delete", 196, 'T')
  946. go
  947. insert spt_values(name, number, type)
  948.   values ("Select", 193, 'T')
  949. go
  950. insert spt_values(name, number, type)
  951.   values ("Insert", 195, 'T')
  952. go
  953. insert spt_values(name, number, type)
  954.   values ("Execute", 224, 'T')
  955. go
  956. insert spt_values(name, number, type)
  957.   values ("Grant", 205, 'T')
  958. go
  959. insert spt_values(name, number, type)
  960.   values ("Revoke", 206, 'T')
  961. go
  962. insert spt_values(name, number, type)
  963.   values ("Create Database", 203, 'T')
  964. go
  965. insert spt_values(name, number, type)
  966.   values ("Create Default", 233, 'T')
  967. go
  968. insert spt_values(name, number, type)
  969.   values ("Create Procedure", 222, 'T')
  970. go
  971. insert spt_values(name, number, type)
  972.   values ("Create Rule", 236, 'T')
  973. go
  974. insert spt_values(name, number, type)
  975.   values ("Create Table", 198, 'T')
  976. go
  977. insert spt_values(name, number, type)
  978.   values ("Create View", 207, 'T')
  979. go
  980. insert spt_values(name, number, type)
  981.   values ("Dump Database", 228, 'T')
  982. go
  983. insert spt_values(name, number, type)
  984.   values ("Dump Transaction", 235, 'T')
  985. go
  986.  
  987. /* Values for syslocks.type */
  988. insert spt_values(name, number, type)
  989.   values ("LOCK TYPES", -1, 'L')
  990. go
  991. insert spt_values(name, number, type)
  992.   values ("Ex_table", 1, 'L')
  993. go
  994. insert spt_values(name, number, type)
  995.   values ("Sh_table", 2, 'L')
  996. go
  997. insert spt_values(name, number, type)
  998.   values ("Ex_intent", 3, 'L')
  999. go
  1000. insert spt_values(name, number, type)
  1001.   values ("Sh_intent", 4, 'L')
  1002. go
  1003. insert spt_values(name, number, type)
  1004.   values ("Ex_page", 5, 'L')
  1005. go
  1006. insert spt_values(name, number, type)
  1007.   values ("Sh_page", 6, 'L')
  1008. go
  1009. insert spt_values(name, number, type)
  1010.   values ("Update_page", 7, 'L')
  1011. go
  1012. insert spt_values(name, number, type)
  1013.   values ("Ex_extent", 8, 'L')
  1014. go
  1015. insert spt_values(name, number, type)
  1016.   values ("Sh_extent", 9, 'L')
  1017. go
  1018. insert spt_values(name, number, type)
  1019.   values ("Ex_table-blk", 256+1, 'L')
  1020. go
  1021. insert spt_values(name, number, type)
  1022.   values ("Sh_table-blk", 256+2, 'L')
  1023. go
  1024. insert spt_values(name, number, type)
  1025.   values ("Ex_intent-blk", 256+3, 'L')
  1026. go
  1027. insert spt_values(name, number, type)
  1028.   values ("Sh_intent-blk", 256+4, 'L')
  1029. go
  1030. insert spt_values(name, number, type)
  1031.   values ("Ex_page-blk", 256+5, 'L')
  1032. go
  1033. insert spt_values(name, number, type)
  1034.   values ("Sh_page-blk", 256+6, 'L')
  1035. go
  1036. insert spt_values(name, number, type)
  1037.   values ("Update_page-blk", 256+7, 'L')
  1038. go
  1039. insert spt_values(name, number, type)
  1040.   values ("Ex_extent-blk", 256+8, 'L')
  1041. go
  1042. insert spt_values(name, number, type)
  1043.   values ("Sh_extent-blk", 256+9, 'L')
  1044. go
  1045. insert spt_values(name, number, type)
  1046.   values ("Ex_table-demand", 512+1, 'L')
  1047. go
  1048. insert spt_values(name, number, type)
  1049.   values ("Sh_table-demand", 512+2, 'L')
  1050. go
  1051. insert spt_values(name, number, type)
  1052.   values ("Ex_intent-demand", 512+3, 'L')
  1053. go
  1054. insert spt_values(name, number, type)
  1055.   values ("Sh_intent-demand", 512+4, 'L')
  1056. go
  1057. insert spt_values(name, number, type)
  1058.   values ("Ex_page-demand", 512+5, 'L')
  1059. go
  1060. insert spt_values(name, number, type)
  1061.   values ("Sh_page-demand", 512+6, 'L')
  1062. go
  1063. insert spt_values(name, number, type)
  1064.   values ("Update_page-demand", 512+7, 'L')
  1065. go
  1066. insert spt_values(name, number, type)
  1067.   values ("Ex_extent-demand", 512+8, 'L')
  1068. go
  1069. insert spt_values(name, number, type)
  1070.   values ("Sh_extent-demand", 512+9, 'L')
  1071. go
  1072.  
  1073. /*
  1074. **  These values define the status bits in sysindexes.
  1075. */
  1076. insert spt_values (name, number, type)
  1077.     values ("INDEX TYPES", -1, "I")
  1078. go
  1079. insert spt_values (name, number, type)
  1080.     values ("nonclustered", 0, "I")
  1081. go
  1082. insert spt_values (name, number, type)
  1083.     values ("ignore duplicate keys", 1, "I")
  1084. go
  1085. insert spt_values (name, number, type)
  1086.     values ("unique", 2, "I")
  1087. go
  1088. insert spt_values (name, number, type)
  1089.     values ("ignore duplicate rows", 4, "I")
  1090. go
  1091. insert spt_values (name, number, type)
  1092.     values ("clustered", 16, "I")
  1093. go
  1094. insert spt_values (name, number, type)
  1095.     values ("allow duplicate rows", 64, "I")
  1096. go
  1097.  
  1098. /*
  1099. **  These values define the object type.  The number made from the low
  1100. **  3 bits in sysobjects.sysstats indicate the type of object.
  1101. */
  1102. insert spt_values (name, number, type)
  1103.     values ("OBJECT TYPES", -1, "O")
  1104. go
  1105. insert spt_values (name, number, type)
  1106.     values ("trigger", 0, "O")
  1107. go
  1108. insert spt_values (name, number, type)
  1109.     values ("system table", 1, "O")
  1110. go
  1111. insert spt_values (name, number, type)
  1112.     values ("view", 2, "O")
  1113. go
  1114. insert spt_values (name, number, type)
  1115.     values ("user table", 3, "O")
  1116. go
  1117. insert spt_values (name, number, type)
  1118.     values ("stored procedure",4, "O")
  1119. go
  1120. /* no number 5 */
  1121. insert spt_values (name, number, type)
  1122.     values ("default", 6, "O")
  1123. go
  1124. insert spt_values (name, number, type)
  1125.     values ("rule", 7, "O")
  1126. go
  1127.  
  1128. /*
  1129. **  These values translate the object type's userstat bits.  If the high
  1130. **  bit is set for a sproc, then it's a report.
  1131. */
  1132. insert spt_values (name, number, type)
  1133.     values ("REPORT TYPES", -1, "R")
  1134. go
  1135. insert spt_values (name, number, type)
  1136.     values ("", 0, "R")
  1137. go
  1138. insert spt_values (name, number, type)
  1139.     values (" (rpt)", -32768, "R")
  1140. go
  1141.  
  1142. /*
  1143. **  Database status bits.
  1144. **  NB -- If you add a bit here make sure you add the value to the value
  1145. **    of the ALL SETTABLE option.
  1146. */
  1147. insert spt_values (name, number, type)
  1148.     values ("DATABASE STATUS", -1, "D")
  1149. go
  1150. insert spt_values (name, number, type)
  1151.     values ("select into/bulkcopy", 4, "D")
  1152. go
  1153. insert spt_values (name, number, type)
  1154.     values ("no chkpt on recovery", 16, "D")
  1155. go
  1156. insert spt_values (name, number, type)
  1157.     values ("don't recover", 32, "D")
  1158. go
  1159. insert spt_values (name, number, type)
  1160.     values ("not recovered", 256, "D")
  1161. go
  1162. /*
  1163. **  NOT IMPLEMENTED BY DATASERVER
  1164. **insert spt_values (name, number, type)
  1165. **    values ("load only", 512, "D")
  1166. **go
  1167. */
  1168. insert spt_values (name, number, type)
  1169.     values ("read only", 1024, "D")
  1170. go
  1171. insert spt_values (name, number, type)
  1172.     values ("dbo use only", 2048, "D")
  1173. go
  1174. insert spt_values (name, number, type)
  1175.     values ("single user", 4096, "D")
  1176. go
  1177. insert spt_values (name, number, type)
  1178.     values ("dbname has changed", 16384, "D")
  1179. go
  1180. insert spt_values (name, number, type)
  1181.     values ("trunc. log on chkpt.", 8, "D")
  1182. go
  1183. insert spt_values (name, number, type)
  1184.     values ("ALL SETTABLE OPTIONS", 7196, "D")
  1185. go
  1186.  
  1187. /*
  1188. **  Yes or no.
  1189. */
  1190. insert spt_values (name, number, type)
  1191.     values ("YES OR NO", -1, "B")
  1192. go
  1193. insert spt_values (name, number, type)
  1194.     values ("no", 0, "B")
  1195. go
  1196. insert spt_values (name, number, type)
  1197.     values ("yes", 1, "B")
  1198. go
  1199.  
  1200. /*
  1201. **  Status bits in sysservers
  1202. */
  1203. insert spt_values (name, number, type)
  1204.     values ("SYSSERVERS TYPES", -1, "A")
  1205. go
  1206. insert spt_values (name, number, type)
  1207.     values ("timeouts", 0, "A")
  1208. go
  1209. insert spt_values (name, number, type)
  1210.     values ("no timeouts", 1, "A")
  1211. go
  1212.  
  1213. /*
  1214. **  Status bits in sysremotelogins
  1215. */
  1216. insert spt_values (name, number, type)
  1217.     values ("SYSREMOTELOGINS TYPES", -1, "F")
  1218. go
  1219. insert spt_values (name, number, type)
  1220.     values ("", 0, "F")
  1221. go
  1222. insert spt_values (name, number, type)
  1223.     values ("trusted", 1, "F")
  1224. go
  1225.  
  1226. /*
  1227. **  Translate syskeys.type to english.
  1228. */
  1229. insert spt_values (name, number, type)
  1230.     values ("SYSKEYS TYPES", -1, "K")
  1231. go
  1232. insert spt_values (name, number, type)
  1233.     values ("primary", 1, "K")
  1234. go
  1235. insert spt_values (name, number, type)
  1236.     values ("foreign", 2, "K")
  1237. go
  1238. insert spt_values (name, number, type)
  1239.     values ("common", 3, "K")
  1240. go
  1241.  
  1242. /*
  1243. **  Translate sysdevices.status to english.
  1244. */
  1245. insert spt_values (name, number, type)
  1246.     values ("SYSDEVICES STATUS", -1, "V")
  1247. go
  1248. insert spt_values (name, number, type)
  1249.     values ("default disk", 1, "V")
  1250. go
  1251. insert spt_values (name, number, type)
  1252.     values ("physical disk", 2, "V")
  1253. go
  1254. insert spt_values (name, number, type)
  1255.     values ("logical disk", 4, "V")
  1256. go
  1257. insert spt_values (name, number, type)
  1258.     values ("skip header", 8, "V")
  1259. go
  1260. insert spt_values (name, number, type)
  1261.     values ("dump device", 16, "V")
  1262. go
  1263. insert spt_values (name, number, type)
  1264.     values ("serial writes", 32, "V")
  1265. go
  1266. insert spt_values (name, number, type)
  1267.     values ("device mirrored", 64, "V")
  1268. go
  1269. insert spt_values (name, number, type)
  1270.     values ("reads mirrored", 128, "V")
  1271. go
  1272. insert spt_values (name, number, type)
  1273.     values ("half-mirror only", 256, "V")
  1274. go
  1275. insert spt_values (name, number, type)
  1276.     values ("mirror enabled", 512, "V")
  1277. go
  1278.  
  1279. /*
  1280. **  Translate @@options bit to english
  1281. */
  1282. insert spt_values (name, number, type)
  1283.     values ("OPTIONS BITS", -1, "N")
  1284. go
  1285. insert spt_values (name, number, type)
  1286.     values ("showplan", 4, "N")
  1287. go
  1288. insert spt_values (name, number, type)
  1289.     values ("noexec", 8, "N")
  1290. go
  1291. insert spt_values (name, number, type)
  1292.     values ("arithignore", 16, "N")
  1293. go
  1294. insert spt_values (name, number, type)
  1295.     values ("arithabort", 32, "N")
  1296. go
  1297. insert spt_values (name, number, type)
  1298.     values ("background", 64, "N")
  1299. go
  1300. insert spt_values (name, number, type)
  1301.     values ("control", 1024, "N")
  1302. go
  1303. insert spt_values (name, number, type)
  1304.     values ("offsets", 2048, "N")
  1305. go
  1306. insert spt_values (name, number, type)
  1307.     values ("statistics io and time", 4096, "N")
  1308. go
  1309. insert spt_values (name, number, type)
  1310.     values ("parseonly", 8192, "N")
  1311. go
  1312. insert spt_values (name, number, type)
  1313.     values ("procid", 32768, "N")
  1314. go
  1315. insert spt_values (name, number, type)
  1316.     values ("rowcount", 131072, "N")
  1317. go
  1318. insert spt_values (name, number, type)
  1319.     values ("nocount", 524288, "N")
  1320. go
  1321.  
  1322. /*
  1323. **  Set the DataServer machine type
  1324. **  spt_values.low is the number of bytes in a DataServer page for the
  1325. **  particular DataServer machine.
  1326. */
  1327. insert spt_values (name, number, type, low)
  1328.     values ("DATASERVER HOST TYPE", -1, "E", 0)
  1329. go
  1330.  
  1331. /*
  1332. **  List the physical types that are compatible.
  1333. */
  1334. insert spt_values (name, number, type)
  1335.     values ("COMPATIBLE TYPES", -1, "J")
  1336. go
  1337. insert spt_values (name, number, low, type)
  1338.     values ("binary", 1, 45, "J")
  1339. go
  1340. insert spt_values (name, number, low, type)
  1341.     values ("varbinary", 1, 37, "J")
  1342. go
  1343. insert spt_values (name, number, low, type)
  1344.     values ("bit", 2, 50, "J")
  1345. go
  1346. insert spt_values (name, number, low, type)
  1347.     values ("char", 3, 47, "J")
  1348. go
  1349. insert spt_values (name, number, low, type)
  1350.     values ("varchar", 3, 39, "J")
  1351. go
  1352. insert spt_values (name, number, low, type)
  1353.     values ("datetime", 4, 61, "J")
  1354. go
  1355. insert spt_values (name, number, low, type)
  1356.     values ("datetimn", 4, 111, "J")
  1357. go
  1358. insert spt_values (name, number, low, type)
  1359.     values ("smalldatetime", 4, 58, "J")
  1360. go
  1361. insert spt_values (name, number, low, type)
  1362.     values ("float", 5, 62, "J")
  1363. go
  1364. insert spt_values (name, number, low, type)
  1365.     values ("floatn", 5, 109, "J")
  1366. go
  1367. insert spt_values (name, number, low, type)
  1368.     values ("real", 5, 59, "J")
  1369. go
  1370. insert spt_values (name, number, low, type)
  1371.     values ("int", 6, 56, "J")
  1372. go
  1373. insert spt_values (name, number, low, type)
  1374.     values ("intn", 6, 38, "J")
  1375. go
  1376. insert spt_values (name, number, low, type)
  1377.     values ("smallint", 6, 52, "J")
  1378. go
  1379. insert spt_values (name, number, low, type)
  1380.     values ("tinyint", 6, 48, "J")
  1381. go
  1382. insert spt_values (name, number, low, type)
  1383.     values ("money", 7, 60, "J")
  1384. go
  1385. insert spt_values (name, number, low, type)
  1386.     values ("moneyn", 7, 110, "J")
  1387. go
  1388. insert spt_values (name, number, low, type)
  1389.     values ("smallmoney", 7, 122, "J")
  1390. go
  1391.  
  1392. /*
  1393. **  Add some text for sysusages.segmap.
  1394. */
  1395. insert into spt_values (name, type, number)
  1396.     values ('SYSUSAGES SEGMAP', 'S', -1)
  1397. insert into spt_values (name, type, number)
  1398.     values ('data only', 'S', 0)
  1399. insert into spt_values (name, type, number)
  1400.     values ('data only', 'S', 1)
  1401. insert into spt_values (name, type, number)
  1402.     values ('data only', 'S', 2)
  1403. insert into spt_values (name, type, number)
  1404.     values ('data only', 'S', 3)
  1405. insert into spt_values (name, type, number)
  1406.     values ('log only', 'S', 4)
  1407. insert into spt_values (name, type, number)
  1408.     values ('data only', 'S', 5)
  1409. insert into spt_values (name, type, number)
  1410.     values ('data only', 'S', 6)
  1411. insert into spt_values (name, type, number)
  1412.     values ('data and log', 'S', 7)
  1413. go
  1414.  
  1415. /*
  1416. ** Features supported on some platforms and not others.  low != 0 means
  1417. ** supported.  Initialize to 0 here.  Each of these may be reset by platform.
  1418. */
  1419. insert into spt_values (name, type, number, low)
  1420.     values ('Channel Devices', 'G', 0, 0)
  1421. go
  1422.  
  1423. /*
  1424. **  Create and initialize the table used by the sp_monitor procedure.
  1425. */
  1426. create table spt_monitor
  1427. (
  1428.     lastrun     datetime,
  1429.     cpu_busy     int,
  1430.     io_busy     int,
  1431.     idle         int,
  1432.     pack_received     int,
  1433.     pack_sent     int,
  1434.     connections     int,
  1435.     pack_errors    int,
  1436.     total_read     int,
  1437.     total_write     int,
  1438.     total_errors     int
  1439. )
  1440. go
  1441.  
  1442. grant select on spt_monitor to public
  1443. go
  1444.  
  1445. insert into spt_monitor
  1446.     select
  1447.     lastrun = getdate(),
  1448.     cpu_busy = @@cpu_busy,
  1449.     io_busy = @@io_busy,
  1450.     idle = @@idle,
  1451.     pack_received = @@pack_received,
  1452.     pack_sent = @@pack_sent,
  1453.     connections = @@connections,
  1454.     pack_errors = @@packet_errors,
  1455.     total_read = @@total_read,
  1456.     total_write = @@total_write,
  1457.     total_errors = @@total_errors
  1458. go
  1459.  
  1460. set nocount off
  1461. go
  1462. grant select on spt_values to public
  1463. go
  1464. dump transaction master with truncate_only
  1465. go
  1466. /* server_nw386        49.1    9/10/91 */
  1467. /*
  1468. **  This file contains the necessary modifications to be made for
  1469. **  various DataServer host machines.
  1470. **  It sets the DataServer host environment to be a sun 386i.
  1471. **  spt_values.low is the number of bytes in a DataServer page.
  1472. */
  1473. use master
  1474. go
  1475.  
  1476. delete spt_values
  1477.     where type = "E" and number > 0
  1478. go
  1479. /* data page size for Novell 386 */
  1480. insert into spt_values (name, number, type, low)
  1481.     values ("Novell ", 1, "E", 2048)
  1482.  
  1483. /* Value to set and clear the high bit for int datatypes for novel 386.
  1484. ** would like to enter -2,147,483,648 to avoid byte order issues, but 
  1485. ** the server won't take it, even in exponential notation.
  1486. */
  1487. insert into spt_values (name, number, type, low)
  1488.     values ("int high bit", 2, "E", 0x80000000)
  1489.  
  1490. /* Value which gives the byte position of the high byte for int datatypes for 
  1491. ** novell 386.  This value was changed from 4 (the usual Intel 80x86 order) to 1
  1492. ** when binary convert routines were changed to reverse the byte order.  So 
  1493. ** this value is accurate ONLY when ints are converted to binary datatype.
  1494. */
  1495. insert into spt_values (name, number, type, low)
  1496.     values ("int4 high byte", 3, "E", 1)
  1497. go
  1498.  
  1499. /*
  1500. **  Set the standard device entries to be NetWare compatible
  1501. */
  1502. update sysdevices
  1503.     set  phyname = "nul"
  1504.     where   name = "diskdump"
  1505.  
  1506. delete from sysdevices
  1507.     where   name like "tape%"
  1508.  
  1509. insert into sysdevices (low, high, status, cntrltype, name, phyname)
  1510.     values (0, 32, 16, 3, "floppydump", "A:DUMPDATA.DAT")
  1511.  
  1512. insert into sysdevices (low, high, status, cntrltype, name, phyname)
  1513.     values (0, 32, 16, 3, "floppydumpb", "B:DUMPDATA.DAT")
  1514.  
  1515. insert into sysdevices (low, high, status, cntrltype, name, phyname)
  1516.     values (0, 0, 16, 2, "sysdump", "SYS:\DUMPDATA.DAT")
  1517.  
  1518. insert into sysdevices (low, high, status, cntrltype, name, phyname)
  1519.     values (0, 1280, 24, 4, "tapedump", "WANGTEK")
  1520.  
  1521. insert into sysdevices (low, high, status, cntrltype, name, phyname)
  1522.     values (0, 1280, 24, 5, "scsitape", "NOVADIBI")
  1523. go
  1524.  
  1525. /* command to fix up freeoff on page 24 in all databases */
  1526. dbcc dbrepair (master, fixsysindex)
  1527.  
  1528. go
  1529.  
  1530.  
  1531. /*
  1532. ** Update sysconfigures number of disks to be a few more than are actually
  1533. ** in use.  This prevents useing too much memory for disk service process
  1534. ** stacks.  This is especially important for the upgrade from 4.0 to 4.0.1
  1535. ** where 4.0 had a default of 50 disks configured.  The new default is 10.
  1536. */
  1537. declare @ndisks int
  1538.  
  1539. /* Count the number of disks in use */
  1540. select @ndisks = count(*) from sysdevices
  1541.     where (status & 2) = 2
  1542.  
  1543. /* Add a few, but make sure there are at least 10 */
  1544. select @ndisks = 4 + @ndisks
  1545. if @ndisks < 10
  1546.     select @ndisks = 10
  1547.  
  1548. /* Update configured number of disks if it is too high */
  1549.  
  1550. if (select value from sysconfigures where config = 116) > @ndisks
  1551. begin
  1552.     update sysconfigures
  1553.         set value = @ndisks
  1554.         where config = 116
  1555.     print "Configured number of disks was much larger than the number of"
  1556.     print "disks actually in use.  Configured valued adjusted downward."
  1557. end
  1558. go
  1559.  
  1560. /* Force recalculation of disks, processes, msgs etc */
  1561. reconfigure with override
  1562. go
  1563.  
  1564. dump transaction master to diskdump with truncate_only
  1565. go
  1566. dump transaction master with truncate_only
  1567. go
  1568. /* validname    49.1    9/10/91 */
  1569. create procedure sp_validname
  1570. @name    varchar(30)
  1571. as
  1572.  
  1573. /*
  1574. **  Check to see that the new name is a legal identifier.
  1575. */
  1576.  
  1577. declare @current    int
  1578. declare @last        int
  1579. declare @curchar    tinyint
  1580. declare @underscore    tinyint
  1581. declare @poundsign    tinyint
  1582. declare @dollarsign    tinyint
  1583. declare @big_A        tinyint
  1584. declare @small_a    tinyint
  1585. declare @big_Z        tinyint
  1586. declare @small_z    tinyint
  1587. declare @digit_0    tinyint
  1588. declare @digit_9    tinyint
  1589.  
  1590. declare @csid            tinyint
  1591. declare @alpha_2_start        tinyint
  1592. declare @alpha_2_end        tinyint
  1593. declare @alpha_3_start        tinyint
  1594. declare @alpha_3_end        tinyint
  1595. declare @alpha_4_start        tinyint
  1596. declare @alpha_4_end        tinyint
  1597. declare @alpha_5_start        tinyint
  1598. declare @alpha_5_end        tinyint
  1599. declare @alpha_6_start        tinyint
  1600. declare @alpha_6_end        tinyint
  1601. declare @alpha_7_start        tinyint
  1602. declare @alpha_7_end        tinyint
  1603. declare @alpha_s_1        tinyint
  1604. declare @alpha_s_2        tinyint
  1605. declare @alpha_s_3        tinyint
  1606.  
  1607. /*
  1608. **  Ensure that the name specified is non-null.
  1609. */
  1610. if (@name is null)
  1611. begin
  1612.     return (1)
  1613. end
  1614.  
  1615.  
  1616. /*
  1617. **  Set up the codepoints that we will need.
  1618. */
  1619. select @underscore = 0x5F,
  1620.         @poundsign = 0x23,
  1621.        @dollarsign = 0x24,
  1622.             @big_A = 0x41,
  1623.           @small_a = 0x61,
  1624.             @big_Z = 0x5A,
  1625.           @small_z = 0x7A,
  1626.           @digit_0 = 0x30,
  1627.           @digit_9 = 0x39
  1628.  
  1629. /* Get the Character Set id (csid) */
  1630. select @csid = csid from master..syscharsets
  1631.     where id = (select value from master..sysconfigures
  1632.         where config = 123)
  1633.  
  1634.     if @csid = 1
  1635.         /* Set up the alphabetic codepoint ranges for iso_1. */
  1636.         select @alpha_2_start = 0xC0, @alpha_2_end = 0xD6,
  1637.            @alpha_3_start = 0xD8, @alpha_3_end = 0xF6,
  1638.                @alpha_4_start = 0xF8, @alpha_4_end = 0xFF,
  1639.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1640.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1641.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1642.            @alpha_s_1 = 0x41, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41
  1643.  
  1644.     if @csid = 2
  1645.         /* Set up the alphabetic codepoint ranges for cp850. */
  1646.         select @alpha_2_start = 0x80, @alpha_2_end = 0x9D,
  1647.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1648.                @alpha_4_start = 0xB5, @alpha_4_end = 0xB7,
  1649.                @alpha_5_start = 0xC6, @alpha_5_end = 0xC7,
  1650.                @alpha_6_start = 0xD0, @alpha_6_end = 0xD8,
  1651.                @alpha_7_start = 0xE0, @alpha_7_end = 0xED,
  1652.            @alpha_s_1 = 0xDE, @alpha_s_2 = 0xF4, @alpha_s_3 = 0xF5
  1653.  
  1654.     if @csid = 3
  1655.         /* Set up the alphabetic codepoint ranges for cp437. */
  1656.         select @alpha_2_start = 0x80, @alpha_2_end = 0x9A,
  1657.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1658.                @alpha_4_start = 0xE0, @alpha_4_end = 0xEE,
  1659.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1660.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1661.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1662.            @alpha_s_1 = 0x9E, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41
  1663.  
  1664. /*
  1665. **  Check the 1st character.
  1666. **  It can be an ASCII letter, #, _, or any character from 0x80-0xFF.
  1667. */
  1668. select @curchar = convert(binary(1), substring(@name, 1, 1))
  1669.  
  1670. if (not (   (@curchar >= @big_A    and  @curchar <= @big_Z)
  1671.      or (@curchar >= @small_a  and  @curchar <= @small_z)
  1672.      or (@curchar between @alpha_2_start and @alpha_2_end)
  1673.      or (@curchar between @alpha_3_start and @alpha_3_end)
  1674.      or (@curchar between @alpha_4_start and @alpha_4_end)
  1675.      or (@curchar between @alpha_5_start and @alpha_5_end)
  1676.      or (@curchar between @alpha_6_start and @alpha_6_end)
  1677.      or (@curchar between @alpha_7_start and @alpha_7_end)
  1678.      or @curchar = @alpha_s_1
  1679.      or @curchar = @alpha_s_2
  1680.      or @curchar = @alpha_s_3
  1681.      or @curchar = @poundsign
  1682.      or @curchar = @underscore
  1683.     )
  1684.    )
  1685. begin
  1686.     return (1)
  1687. end
  1688.  
  1689. /*
  1690. **  Figure out how long the string is.
  1691. **  N.B. - replace this once we have strlen()
  1692. */
  1693. if (charindex("!", @name) != 0)
  1694. begin
  1695.     return (1)
  1696. end
  1697. select @current = 2,
  1698.        @last = charindex("!", @name + "!") - 1
  1699.  
  1700. /*
  1701. **  Now check the rest of the characters.
  1702. **  They can be ASCII letters, #, _, $, or any character from 0x80-0xFF.
  1703. */
  1704. while @current <= @last
  1705. begin
  1706.     select @curchar = convert(binary(1), substring(@name, @current, 1))
  1707.     if (not (   (@curchar >= @big_A    and  @curchar <= @big_Z)
  1708.          or (@curchar >= @small_a  and  @curchar <= @small_z)
  1709.          or (@curchar >= @digit_0  and  @curchar <= @digit_9)
  1710.           or (@curchar between @alpha_2_start and @alpha_2_end)
  1711.           or (@curchar between @alpha_3_start and @alpha_3_end)
  1712.           or (@curchar between @alpha_4_start and @alpha_4_end)
  1713.           or (@curchar between @alpha_5_start and @alpha_5_end)
  1714.           or (@curchar between @alpha_6_start and @alpha_6_end)
  1715.           or (@curchar between @alpha_7_start and @alpha_7_end)
  1716.           or @curchar = @alpha_s_1
  1717.           or @curchar = @alpha_s_2
  1718.           or @curchar = @alpha_s_3
  1719.          or @curchar = @poundsign
  1720.          or @curchar = @dollarsign
  1721.          or @curchar = @underscore
  1722.         )
  1723.        )
  1724.     begin
  1725.         return (1)
  1726.     end
  1727.  
  1728.     select @current = @current + 1
  1729. end
  1730.  
  1731. return (0)
  1732. go
  1733. grant execute on sp_validname to public
  1734. go
  1735. /* validlang 49.1 9/10/91 */
  1736. create procedure sp_validlang
  1737. @name    varchar(30)
  1738. as
  1739.  
  1740. /* Check to see if this language is in Syslanguages. */
  1741. if exists (select *
  1742.         from master.dbo.syslanguages
  1743.         where name = @name)
  1744. begin
  1745.     return 0
  1746. end
  1747. return 1
  1748. go
  1749. grant execute on sp_validlang to public
  1750. go
  1751. dump transaction master with truncate_only
  1752. go
  1753. /* validaltlang 49.1 9/10/91 */
  1754. create procedure sp_validaltlang
  1755. @name    varchar(30)
  1756. as
  1757.  
  1758. /* Check to see if this alias is in Syslanguages. */
  1759. if exists (select *
  1760.         from master.dbo.syslanguages
  1761.         where alias = @name)
  1762. begin
  1763.     return 0
  1764. end
  1765. return 1
  1766. go
  1767. grant execute on sp_validaltlang to public
  1768. go
  1769. /* chklangparam 49.1 9/10/91 */
  1770. /*
  1771. ** sp_checklangparam is called by sp_addlanguage to check the lists of
  1772. ** full month names, short month names, and day names.  It returns a status
  1773. ** indicating whether any errors were found:
  1774. **    0 - name list is valid
  1775. **    1 - spaces were found, which are not allowed
  1776. **    2 - not enough names in the list (must be exactly @numnames of them)
  1777. **    3 - too many names in the list (must be exactly @numnames of them)
  1778. **    4 - some name(s) are too long
  1779. **    5 - some name(s) have non-aphabetic characters
  1780. */
  1781. create procedure sp_chklangparam
  1782. @namelist varchar(251),        /* month or day name list */
  1783. @msgparam varchar(30),        /* name of list, for error messages */
  1784. @numnames int,            /* correct number of names in the list */
  1785. @maxnamelen int            /* maximum length of any name in list */
  1786. as
  1787.  
  1788.     declare @totallen        int
  1789.     declare @mthlen            int
  1790.     declare @mthname        varchar(251)
  1791.     declare @msg            varchar(255)
  1792.     declare @csid            tinyint
  1793.     declare @csname            varchar(30)
  1794.     declare @alpha_0_start        tinyint
  1795.     declare @alpha_0_end        tinyint
  1796.     declare @alpha_1_start        tinyint
  1797.     declare @alpha_1_end        tinyint
  1798.     declare @alpha_2_start        tinyint
  1799.     declare @alpha_2_end        tinyint
  1800.     declare @alpha_3_start        tinyint
  1801.     declare @alpha_3_end        tinyint
  1802.     declare @alpha_4_start        tinyint
  1803.     declare @alpha_4_end        tinyint
  1804.     declare @alpha_5_start        tinyint
  1805.     declare @alpha_5_end        tinyint
  1806.     declare @alpha_6_start        tinyint
  1807.     declare @alpha_6_end        tinyint
  1808.     declare @alpha_7_start        tinyint
  1809.     declare @alpha_7_end        tinyint
  1810.     declare @alpha_s_1        tinyint
  1811.     declare @alpha_s_2        tinyint
  1812.     declare @alpha_s_3        tinyint
  1813.     declare @alpha_s_4        tinyint
  1814.     declare @curchar        tinyint
  1815.     declare @next            int
  1816.  
  1817.     /* Get the Character Set id (csid) */
  1818.     select @csid = csid from master..syscharsets
  1819.         where id = (select value from master..sysconfigures
  1820.             where config = 123)
  1821.  
  1822.     /* Get the Character Set Name (csname) */
  1823.     select @csname = name from master..syscharsets
  1824.         where id = @csid and type < 2000
  1825.  
  1826.     /* Make sure the list has no blanks. */
  1827.     if ( charindex( " ", @namelist ) != 0 )
  1828.     begin
  1829.         select @msg = "List of " + @msgparam
  1830.             + " names contains spaces, which are not allowed."
  1831.         print @msg
  1832.         return 1
  1833.     end
  1834.  
  1835.     if @csid = 1
  1836.         /* Set up the alphabetic codepoint ranges for iso_1. */
  1837.         select @alpha_0_start = 0x41, @alpha_0_end = 0x5A,
  1838.            @alpha_1_start = 0x61, @alpha_1_end = 0x7A,
  1839.            @alpha_2_start = 0xC0, @alpha_2_end = 0xD6,
  1840.            @alpha_3_start = 0xD8, @alpha_3_end = 0xF6,
  1841.                @alpha_4_start = 0xF8, @alpha_4_end = 0xFF,
  1842.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1843.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1844.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1845.            @alpha_s_1 = 0x41, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41,
  1846.            @alpha_s_4 = 0x2D
  1847.  
  1848.     if @csid = 2
  1849.         /* Set up the alphabetic codepoint ranges for cp850. */
  1850.         select @alpha_0_start = 0x41, @alpha_0_end = 0x5A,
  1851.            @alpha_1_start = 0x61, @alpha_1_end = 0x7A,
  1852.            @alpha_2_start = 0x80, @alpha_2_end = 0x9D,
  1853.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1854.                @alpha_4_start = 0xB5, @alpha_4_end = 0xB7,
  1855.                @alpha_5_start = 0xC6, @alpha_5_end = 0xC7,
  1856.                @alpha_6_start = 0xD0, @alpha_6_end = 0xD8,
  1857.                @alpha_7_start = 0xE0, @alpha_7_end = 0xED,
  1858.            @alpha_s_1 = 0xDE, @alpha_s_2 = 0xF4, @alpha_s_3 = 0xF5,
  1859.            @alpha_s_4 = 0x2D
  1860.  
  1861.     if @csid = 3
  1862.         /* Set up the alphabetic codepoint ranges for cp437. */
  1863.         select @alpha_0_start = 0x41, @alpha_0_end = 0x5A,
  1864.            @alpha_1_start = 0x61, @alpha_1_end = 0x7A,
  1865.            @alpha_2_start = 0x80, @alpha_2_end = 0x9A,
  1866.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1867.                @alpha_4_start = 0xE0, @alpha_4_end = 0xEE,
  1868.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1869.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1870.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1871.            @alpha_s_1 = 0x9E, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41,
  1872.            @alpha_s_4 = 0x2D
  1873.  
  1874.     select @totallen = datalength( @namelist )
  1875.     while ( @totallen > 0 )
  1876.     begin
  1877.         /* Move to the next name in the list. */
  1878.         select @namelist = right( @namelist, @totallen )
  1879.  
  1880.         /* If this is not the last name in the list then there must
  1881.         ** be another comma.
  1882.         */
  1883.         if ( @numnames > 1 )
  1884.         begin
  1885.             select @mthlen = charindex( ",", @namelist ) - 1
  1886.             if ( @mthlen < 0 )
  1887.             begin
  1888.                 select @msg = "List of " + @msgparam
  1889.                     + " names has too few names."
  1890.                 print @msg
  1891.                 return 2
  1892.             end
  1893.         end
  1894.  
  1895.         /* This is the last name in the list, so there cannot be
  1896.         ** another comma.  This name is all remaining characters
  1897.         ** in the list.
  1898.         */
  1899.         else
  1900.         begin
  1901.             if ( charindex( ",", @namelist ) != 0 )
  1902.             begin
  1903.                 select @msg = "List of " + @msgparam
  1904.                     + " names has too many names."
  1905.                 print @msg
  1906.                 return 3
  1907.             end
  1908.             select @mthlen = @totallen
  1909.         end
  1910.  
  1911.         /* Check the name length. */
  1912.         if ( @mthlen > @maxnamelen )
  1913.         begin
  1914.             select @msg = "List of " + @msgparam
  1915.                 + " names has name(s) which are too long."
  1916.             print @msg
  1917.             return 4
  1918.         end
  1919.  
  1920.         /*
  1921.         ** Check to see that the names contain only true
  1922.         ** ALPHA-betic characters; not even DIGITs are allowed.
  1923.         */
  1924.         select @next = 1
  1925.         while ( @next <= @mthlen )
  1926.         begin
  1927.             select @curchar = convert(binary(1),
  1928.                         substring(@namelist, @next, 1))
  1929.             if (not
  1930.             (   
  1931.                (@curchar between @alpha_0_start and @alpha_0_end)
  1932.             or (@curchar between @alpha_1_start and @alpha_1_end)
  1933.             or (@curchar between @alpha_2_start and @alpha_2_end)
  1934.             or (@curchar between @alpha_3_start and @alpha_3_end)
  1935.             or (@curchar between @alpha_4_start and @alpha_4_end)
  1936.                     or (@curchar between @alpha_5_start and @alpha_5_end)
  1937.             or (@curchar between @alpha_6_start and @alpha_6_end)
  1938.             or (@curchar between @alpha_7_start and @alpha_7_end)
  1939.             or @curchar = @alpha_s_1
  1940.             or @curchar = @alpha_s_2
  1941.             or @curchar = @alpha_s_3
  1942.             or @curchar = @alpha_s_4
  1943.             )
  1944.             )
  1945.             begin
  1946.             select @msg = 'List of ' + @msgparam
  1947.                 + ' names contains name(s) which have "'
  1948.                 + @csname + '" non-alphabetic characters.'
  1949.             print @msg
  1950.             return 5
  1951.             end
  1952.  
  1953.             select @next = @next + 1
  1954.         end
  1955.  
  1956.         /* Move to the next name in the list. */
  1957.         select @numnames = @numnames - 1
  1958.         select @totallen = @totallen - @mthlen - 1
  1959.     end
  1960.  
  1961.     /* No problems were found with the name list. */
  1962.     return 0
  1963. go
  1964. dump transaction master with truncate_only
  1965. go
  1966. /* namecrack 49.1 9/10/91 */
  1967. create procedure sp_namecrack
  1968. @name    varchar(123),
  1969. @site    varchar(30) = null output,
  1970. @db    varchar(30) = null output,
  1971. @owner    varchar(30) = null output,
  1972. @object    varchar(30) = null output
  1973. as
  1974.  
  1975. select @site = null, @db = null, @owner = null, @object = null
  1976.  
  1977. /*
  1978. **  Is there a site name?
  1979. */
  1980. if @name like "%.%.%.%"
  1981. begin
  1982.     select @site = substring(@name, 1, charindex(".", @name) - 1)
  1983.     select @name = substring(@name, charindex(".", @name) + 1, 125)
  1984. end
  1985.  
  1986. /*
  1987. **  Is there a database name?
  1988. */
  1989. if @name like "%.%.%"
  1990. begin
  1991.     select @db = substring(@name, 1, charindex(".", @name) - 1)
  1992.     select @name = substring(@name, charindex(".", @name) + 1, 125)
  1993. end
  1994.  
  1995. /*
  1996. **  Is there a owner?
  1997. */
  1998. if @name like "%.%"
  1999. begin
  2000.     select @owner = substring(@name, 1, charindex(".", @name) - 1)
  2001.     select @name = substring(@name, charindex(".", @name) + 1, 125)
  2002. end
  2003.  
  2004. select @object = @name
  2005.  
  2006. return (0)
  2007. go
  2008. grant execute on sp_namecrack to public
  2009. go
  2010. /* addalias 49.1 9/10/91 */
  2011. create procedure sp_addalias
  2012. @loginame varchar(30),        /* the name of the pretender */
  2013. @name_in_db varchar(30)        /* who the pretender wants to pretend to be */
  2014. as
  2015.  
  2016. declare @suid int        /* the suid of the pretender */
  2017. declare @asuid int        /* the suid of the person to impersonate */
  2018. declare    @msg    varchar(250)
  2019.  
  2020. /*
  2021. **  You must be SA or the dbo to execute this sproc.
  2022. */
  2023. if suser_id() != 1 and user_id() != 1
  2024. begin
  2025.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  2026.     return (1)
  2027. end
  2028.  
  2029. /*
  2030. **  Make sure that the pretender has an account.
  2031. */
  2032. select @suid = suid
  2033.     from master.dbo.syslogins
  2034. where name = @loginame
  2035.  
  2036. if @suid is NULL
  2037. begin
  2038.     print "No login exists with the specified login name."
  2039.     return (1)
  2040. end
  2041.  
  2042. /*
  2043. **  Get the suid of the person we want to pretend to be.
  2044. **  All user ids are <= 16383 and group ids are > 16383.
  2045. */
  2046. select @asuid = suid
  2047.     from sysusers
  2048. where name = @name_in_db
  2049.     and uid <= 16383
  2050.  
  2051. /*
  2052. **  Does the user to be impersonated exist in the current database?
  2053. */
  2054. if @asuid is NULL
  2055. begin
  2056.     print "No user with the specified name exists in the current database."
  2057.     return (1)
  2058. end
  2059.  
  2060. /*
  2061. **  Does the login to do the impersonating already a user in the current db?
  2062. */
  2063. if exists (select *
  2064.         from sysusers
  2065.             where suid = @suid)
  2066. begin
  2067.     select @msg = "'" + name
  2068.         from sysusers
  2069.             where suid = @suid
  2070.     select @msg = @msg + "' is already a user in the current database."
  2071.     print @msg
  2072.     return (1)
  2073. end
  2074.  
  2075. /*
  2076. **  Is the person already aliased to a user?
  2077. */
  2078. if exists (select *
  2079.         from sysalternates
  2080.     where suid = @suid)
  2081. begin
  2082.     print "The specified user name is already aliased."
  2083.     return (1)
  2084. end
  2085.  
  2086. /*
  2087. **  Add the alias.
  2088. */
  2089. insert into sysalternates (suid, altsuid)
  2090.     values (@suid, @asuid)
  2091.  
  2092. print "Alias user added."
  2093.  
  2094. return (0)
  2095. go
  2096. grant execute on sp_addalias to public
  2097. go
  2098. dump transaction master with truncate_only
  2099. go
  2100. /* addgroup 49.1 9/10/91 */
  2101. create procedure sp_addgroup
  2102. @grpname varchar(30)            /* new group name */
  2103. as
  2104.  
  2105. declare @gid int            /* group id */
  2106. declare @msg varchar(250)
  2107.  
  2108. /*
  2109. **  You must be SA or the dbo to execute this sproc.
  2110. */
  2111. if suser_id() != 1 and user_id() != 1
  2112. begin
  2113.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  2114.     return (1)
  2115. end
  2116.  
  2117. /*
  2118. **  Check to see that the @grpname is valid.
  2119. */
  2120. declare @returncode    int
  2121. execute @returncode = sp_validname @grpname
  2122. if @returncode != 0
  2123. begin
  2124.     select @msg = "'" + @grpname + "' is not a valid name."
  2125.     print @msg
  2126.     return @returncode
  2127. end
  2128.  
  2129. /*
  2130. **  See if the new group name is already being used as a user or group name.
  2131. */
  2132. select @gid = uid
  2133.     from sysusers
  2134. where name = @grpname
  2135.  
  2136. /*
  2137. **  User ids are >0 and  <= 16383 and group ids are > 16383.
  2138. */
  2139. if @gid is not null
  2140. begin
  2141.     if @gid > 0 and @gid <= 16383
  2142.         print "A user with the specified group name already exists."
  2143.     else
  2144.         print "A group with the specified name already exists."
  2145.     return (1)
  2146. end
  2147.  
  2148. /*
  2149. **  Now get the group id for the new group.  It is the current maximum group
  2150. **  number + 1.  If this is the first group use the lowest possible group id
  2151. **  -- 16384.
  2152. */
  2153. select @gid = max(uid)+1
  2154.     from sysusers
  2155.  
  2156. /*
  2157. **  This is the first group.
  2158. */
  2159. if @gid < 16384 or @gid = NULL
  2160.     select @gid = 16384
  2161.  
  2162. /*
  2163. **  Create the group.
  2164. */
  2165. insert into sysusers (uid, suid, gid, name, environ)
  2166. values (@gid, -@gid, @gid, @grpname, "")
  2167.  
  2168. print "New group added."
  2169.  
  2170. return (0)
  2171. go
  2172. grant execute on sp_addgroup to public
  2173. go
  2174. /* addlogin 49.1 9/10/91 */
  2175. create procedure sp_addlogin
  2176. @loginame varchar(30),            /* login name of the new user */
  2177. @passwd varchar(30) = NULL,        /* password of the new user */
  2178. @defdb varchar(30) = "master",        /* default db for the new user */
  2179. @deflanguage varchar(30) = NULL        /* default language for the new user */
  2180. as
  2181.  
  2182. declare @msg varchar(250)
  2183.  
  2184. /*
  2185. **  If we're in a transaction, disallow this since it might make recovery
  2186. **  impossible.
  2187. */
  2188. if @@trancount > 0
  2189. begin
  2190.     print "Can't run sp_addlogin from within a transaction."
  2191.     return (1)
  2192. end
  2193.  
  2194. /*
  2195. **  Only the system administrator (sa) can run this command.
  2196. **  Check to make sure the executor is the sa.
  2197. */
  2198. if suser_id() != 1
  2199. begin
  2200.     print "Only the System Administrator (SA) may execute this procedure."
  2201.     return (1)
  2202. end
  2203.  
  2204. /*
  2205. **  Check to see that the @loginame is valid.
  2206. */
  2207. declare @returncode    int
  2208. execute @returncode = sp_validname @loginame
  2209. if @returncode != 0
  2210. begin
  2211.     select @msg = "'" + @loginame + "' is not a valid name."
  2212.     print @msg
  2213.     return @returncode
  2214. end
  2215.  
  2216. /* 
  2217. **  Check to see that the @deflanguage is valid.
  2218. */
  2219. if @deflanguage is not null
  2220. begin
  2221.     select @returncode = 0
  2222.     execute @returncode = sp_validlang @deflanguage
  2223.     if @returncode != 0
  2224.     begin
  2225.         /* Us_english is always valid */
  2226.         if @deflanguage != "us_english"
  2227.         begin
  2228.             select @msg = "'" + @deflanguage + "' is not a valid official language name."
  2229.             print @msg
  2230.             return @returncode
  2231.         end
  2232.     end
  2233. end
  2234.  
  2235. /*
  2236. **  Make sure the login doesn't already exist.
  2237. */
  2238. if exists (select *
  2239.           from syslogins
  2240.           where name = @loginame)
  2241. begin
  2242.     print "A user with the specified login name already exists."
  2243.     return (1)
  2244. end
  2245.  
  2246. /*
  2247. **  Check that the database name is valid.
  2248. */
  2249. if not exists (select *
  2250.             from sysdatabases
  2251.         where name = @defdb)
  2252. begin
  2253.     print "Database name not valid -- login not added."
  2254.     return (1)
  2255. end
  2256.  
  2257. /*
  2258. **  Create the login.
  2259. */
  2260. insert into syslogins(suid, status, accdate, totcpu, totio,
  2261.     spacelimit, timelimit, resultlimit, dbname, name, password, language)
  2262. select max(suid)+1, 0, getdate(), 0, 0,
  2263.     0, 0, 0, @defdb, @loginame, @passwd, @deflanguage
  2264. from syslogins
  2265.  
  2266. print "New login created."
  2267.  
  2268. return (0)
  2269. go
  2270. dump transaction master with truncate_only
  2271. go
  2272. /* addtype 49.1 9/10/91 */
  2273. create procedure sp_addtype
  2274. @typename varchar(30),        /* name of user-defined type */
  2275. @phystype varchar(30),        /* physical system type of user-defined type */
  2276. @nulltype varchar(8) = "1"    /* default is NO NULLs */
  2277. as
  2278.  
  2279. declare @len int        /* length of user type */
  2280. declare @type tinyint        /* typeid of physical type */
  2281. declare @tlen tinyint        /* length of physical type */
  2282. declare @typeid smallint    /* user typeid of physical type */
  2283. declare @var bit        /* is physical type variable length? */
  2284. declare @nonull bit        /* default is to allow NO NULLs */
  2285. declare @nullegal bit        /* does physical type allow NULLs? */
  2286. declare @msg varchar(250)
  2287.  
  2288.  
  2289. /*
  2290. **  Does the user type allow NULLs?  The default is to NOT allow NULLs.
  2291. **  this is the same default the create table statement uses.
  2292. */
  2293. if @nulltype = "null" or @nulltype = "NULL" or @nulltype = null
  2294.     select @nonull = 0
  2295. else if @nulltype = "not null" or @nulltype = "NOT NULL" or @nulltype = "nonull"
  2296.     or @nulltype = "NONULL" or @nulltype = "1"
  2297.     select @nonull = 1
  2298. else
  2299. begin
  2300.     print "Usage: sp_addtype name, 'datatype' [,null | nonull]"
  2301.     return (1)
  2302. end
  2303.  
  2304. /*
  2305. **  Check to see that the @typename is valid.
  2306. */
  2307. declare @returncode    int
  2308. execute @returncode = sp_validname @typename
  2309. if @returncode != 0
  2310. begin
  2311.     select @msg = "'" + @typename + "' is not a valid type name."
  2312.     print @msg
  2313.     return @returncode
  2314. end
  2315.  
  2316. /*
  2317. **  Check to see if the type already exists.
  2318. */
  2319. if exists (select *
  2320.         from systypes
  2321.        where name = @typename)
  2322. begin
  2323.     print "A type with the specified name already exists."
  2324.     return (1)
  2325. end
  2326.  
  2327. /*
  2328. **  If a length was given with the user datatype, extract it.
  2329. */
  2330. if @phystype like "_%(%)"
  2331. begin
  2332.     select @len = convert(int, substring(@phystype,
  2333.         charindex("(",@phystype) + 1,
  2334.         charindex(")",@phystype) - 1 - charindex("(",@phystype)))
  2335.  
  2336.     /*
  2337.     ** Extract the physical type name
  2338.     */
  2339.     select @phystype = substring(@phystype, 1,
  2340.            charindex("(", @phystype) - 1)
  2341. end
  2342.  
  2343. /*
  2344. **  Make sure that the physical type exists and get its characteristics.
  2345. **  DataServer physical types have a usertype < 100 and are owned by the
  2346. **  dbo (userid = 1).
  2347. */
  2348. select @type = type, @tlen = length, @typeid = usertype,
  2349.     @var = variable, @nullegal = allownulls
  2350. from systypes
  2351.     where usertype < 100 and name = @phystype and uid = 1
  2352.  
  2353. if @type = NULL
  2354. begin
  2355.     print "Physical datatype does not exist."
  2356.     return (1)
  2357. end
  2358.  
  2359. /*
  2360. **  Disallow user-defined datatypes on timestamps.  This is done because
  2361. **  a timestamp is not a basic type but is really a binary.  There is,
  2362. **  therefore, no way to tell if a user-defined datatype is mapped to
  2363. **  a binary or a timestamp.  Timestamps can't have rules or defaults.
  2364. */
  2365. if @phystype = "timestamp"
  2366. begin
  2367.     print "User-defined datatypes based on the 'timestamp' datatype are not allowed."
  2368.     return (1)
  2369. end
  2370.  
  2371. /*
  2372. **  Check if the NULL status of the user type is consistent with the NULL status
  2373. **  of the physical type.  Here are the possible cases.
  2374. **
  2375. **           physical type
  2376. **          NULLs      NONULLs
  2377. **            -----------------
  2378. ** user    NULLs    |  ok    |  no
  2379. ** type NONULLs    |  ok    |  ok
  2380. */
  2381. if @nonull = 0 and @nullegal = 0
  2382. begin
  2383.     print "Physical datatype does not allow nulls."
  2384.     return (1)
  2385. end
  2386.  
  2387. /*
  2388. **  We'll use the variable @nullegal when we update the systypes table
  2389. **  so we need to set it to reflect if NULLs are allowed (@nonull = 0)
  2390. **  or NO NULLs are allowed (@nonull = 1).
  2391. */
  2392. if @nonull = 0
  2393. begin
  2394.     select @nullegal = 1
  2395. end
  2396. else
  2397. begin
  2398.     select @nullegal = 0
  2399. end
  2400.  
  2401. /*
  2402. **  Typeids 1 (char), 2 (varchar), 3 (binary) and 4 (varbinary) are the
  2403. **  only ones which allow a length to be specified.
  2404. */
  2405. if @typeid > 4
  2406. begin
  2407.     /*
  2408.     **  We can't use a length and we got one.
  2409.     */
  2410.     if @len > 0
  2411.     begin
  2412.        print "Physical type is fixed length. You cannot specify the length."
  2413.        return (1)
  2414.     end
  2415.  
  2416.     /*
  2417.     **  Use the fixed length of the physical type.
  2418.     */
  2419.     select @len = @tlen
  2420. end
  2421. else
  2422. begin
  2423.     /*
  2424.     **  We need a length and we didn't get one.
  2425.     */
  2426.     if @len = NULL
  2427.     begin
  2428.         print "You must specify a length with this physical type."
  2429.         return (1)
  2430.     end
  2431.  
  2432.     if @len <= 0 or @len > 255
  2433.     begin
  2434.         print "Illegal length specified -- must be between 1 and 255."
  2435.         return (1)
  2436.     end
  2437.  
  2438. end
  2439.  
  2440.  
  2441. /*
  2442. **  Finally, get the maximum existing user type so we use it + 1 for this
  2443. **  new type.
  2444. */
  2445. select @typeid = max(usertype)
  2446.     from systypes
  2447.  
  2448. /*
  2449. **  There are no user defined types yet so use the first number (101).
  2450. */
  2451. if @typeid < 100
  2452.     select @typeid = 100
  2453.  
  2454. insert systypes (uid, variable, type, length, tdefault,
  2455.     domain, name, usertype, allownulls)
  2456. select user_id(), @var, @type, @len, 0, 0, @typename, @typeid + 1, @nullegal
  2457.  
  2458. print "Type added."
  2459.  
  2460. return (0)
  2461. go
  2462. grant execute on sp_addtype to public
  2463. go
  2464. /* addumpdevice 49.1 9/10/91 */
  2465. create procedure sp_addumpdevice
  2466. @devtype    varchar(10),        /* disk, tape, or special */
  2467. @logicalname    varchar(30),        /* logical name of the device */
  2468. @physicalname    varchar(127),        /* physical name of the device */
  2469. @cntrltype    smallint,        /* controller number */
  2470. @devstatus    varchar(40) = "noskip",    /* device characteristics */
  2471. @tapesize    int = null        /* capacity of tape in MB */
  2472. as
  2473.  
  2474. declare @status smallint        /* status bits for device */
  2475. declare @msg varchar(250)
  2476. declare @tapeblocksize int
  2477. declare @channels_supported     int     /* 1 means channels supported, 0 not */
  2478.  
  2479. /*
  2480. **  If we're in a transaction, disallow this since it might make recovery
  2481. **  impossible.
  2482. */
  2483. if @@trancount > 0
  2484. begin
  2485.     print "Can't run sp_addumpdevice from within a transaction."
  2486.     return (1)
  2487. end
  2488.  
  2489. /*
  2490. **  You must be SA to execute this sproc.
  2491. */
  2492. if suser_id() != 1 
  2493. begin
  2494.     print "You must be the System Administrator (SA) to execute this procedure."
  2495.     return (1)
  2496. end
  2497.  
  2498. /*
  2499. **  Check to see that the @logicalname is valid.
  2500. */
  2501. declare @returncode    int
  2502. execute @returncode = sp_validname @logicalname
  2503. if @returncode != 0
  2504. begin
  2505.     select @msg = "'" + @logicalname + "' is not a valid name."
  2506.     print @msg
  2507.     return @returncode
  2508. end
  2509.  
  2510. /*
  2511. **  The rules for updating sysdevices are as follows:
  2512. **    devtype - may be disk, tape, or channel.  If disk then
  2513. **                    cntrltype will be 2,
  2514. **        if tape then 3-8 depending on
  2515. **                    whether there have already been tapes added.
  2516. **            if channel then 9-14 depending on
  2517. **                    whether there have already been channel devices added
  2518. **        NOTE:  These are completely host dependent.  They probably
  2519. **        need to be changed for each DataServer host type.
  2520. *******************
  2521. **  NETWARE386:
  2522. **        For disk devices:
  2523. **            devtype should be 'disk'
  2524. **            cntrltype should be 2
  2525. **        For floppy diskette devices:
  2526. **            devtype should be 'disk'
  2527. **            cntrltype should be 3
  2528. **        For tape devices:
  2529. **            devtype should be 'tape'
  2530. **            cntrltype should be between 4 and 8
  2531. **            physical name should be the device driver name
  2532. **            For example, for the non-SCSI Wangtek DIBI-2 driver
  2533. **            the tape device physical name should be 'wangtek'
  2534. *******************
  2535. **    logicalname - may be any identifier but must be unique
  2536. **    physicalname - may be anything and doesn't have to be unique
  2537. **        If it contains illegal id characters, e.g., '/', then
  2538. **        it must be in quotes.
  2539. **    status - for a tape device it may be 'skip'.  The default is 'noskip'.
  2540. **            For disk and channel devices it is ignored.
  2541. **    tapesize - for a tape device, this is the tape capacity in MB.  It
  2542. **        is ignored on VMS.
  2543. */
  2544.  
  2545. /*
  2546. ** Determine whether channel devices are supported.
  2547. */
  2548. if exists (select * from spt_values where type = "G" and number = 0 and low =1)
  2549.       select @channels_supported = 1
  2550. else
  2551.       select @channels_supported = 0
  2552.  
  2553. /*
  2554. **  Check out the @devtype.
  2555. */
  2556. if @devtype not in ("disk", "tape", "channel")
  2557. begin
  2558.     if (@channels_supported = 0)
  2559.         print "Unknown device type.  Use 'disk' or 'tape'."
  2560.     else
  2561.         print "Unknown device type.  Use 'disk', 'tape', or 'channel'."
  2562.     return (1)
  2563. end
  2564.  
  2565. /*
  2566. **  Check the args are not NULL.
  2567. */
  2568. if @logicalname = null
  2569. begin
  2570.     print "@logicalname may not be NULL."
  2571.     return
  2572. end
  2573. if @physicalname = null
  2574. begin
  2575.     print "@physicalname may not be NULL."
  2576.     return
  2577. end
  2578. if @cntrltype = null
  2579. begin
  2580.     print "@cntrltype may not be NULL."
  2581.     return
  2582. end
  2583.  
  2584. /*
  2585. **  Make sure that a device with @logicalname doesn't already exist.
  2586. */
  2587. if exists (select *
  2588.         from master.dbo.sysdevices
  2589.             where name = @logicalname)
  2590. begin
  2591.     print "Device with same logical name already exists."
  2592.     return (1)
  2593. end
  2594.  
  2595. /*
  2596. **  Check @cntrltype.  On UNIX, it must be 2 if @devtype = disk,
  2597. **  between 3 and 8 for @devtype = tape, and between 9 and 14 for
  2598. **  @devtype = channel.  For other hosts, it will undoubtedly be different.
  2599. **  NETWARE386: For 'disk' devtype the cntrltype should be 2 or 3.
  2600. **        For 'tape' devtype the cntrltype should be between 4 and 8.
  2601. */
  2602. if @devtype = "disk" and (@cntrltype != 2 and @cntrltype != 3)
  2603. begin
  2604.     print "For disk devices, controller number parameter must be 2 or 3."
  2605.     return (1)
  2606. end
  2607.  
  2608. /*  @devtype = "tape" */
  2609. else if @devtype = "tape" and @cntrltype not between 4 and 8
  2610. begin
  2611.     print "For tape devices, controller number parameter must be between 4 and 8."
  2612.     return (1)
  2613. end
  2614. /*  @devtype = "channel" */
  2615. else if @devtype = "channel" and @cntrltype not between 9 and 14
  2616. begin
  2617.       print "For channel devices, controller number parameter must be between9 and 14."
  2618.       return (1)
  2619. end
  2620.  
  2621. /*
  2622. **  Always turn on the dump status bit.
  2623. */
  2624. select @status = 16
  2625.  
  2626. /*
  2627. **  If @devtype is a tape then check to see if devstatus is 'skip'.
  2628. */
  2629. if @devtype = "tape"
  2630. begin
  2631.     /*
  2632.     **  If this is not VMS, then @tapesize is required.
  2633.     */
  2634.     if not exists (select *
  2635.             from master.dbo.spt_values
  2636.                 where type = "E"
  2637.                     and name = "vms")
  2638.     begin
  2639.         if @tapesize = null or @tapesize < 1
  2640.         begin
  2641.             print "Please specify media capacity in megabytes (1 MB minimum)."
  2642.             return (1)
  2643.         end
  2644.  
  2645.         /*
  2646.         **  @tapesize is given in MB but sysdevices expects number
  2647.                 **  of tape blocks so do the conversion.
  2648.                 **
  2649.                 **  But first get the size of a tape block.  Default to 32k.
  2650.         */
  2651.                 select @tapeblocksize = low
  2652.                         from master.dbo.spt_values
  2653.                                 where type = "E"
  2654.                                         and number = 4
  2655.                 if @tapeblocksize = NULL
  2656.                 begin
  2657.                         select @tapeblocksize = 32768
  2658.                 end
  2659.  
  2660.                 select @tapesize =
  2661.                         convert(int, (@tapesize * 1048576.) / @tapeblocksize)
  2662.     end
  2663.     else
  2664.     begin
  2665.         select @tapesize = 0
  2666.     end
  2667.  
  2668.  
  2669.     if @devstatus != "noskip" and @devstatus != "skip"
  2670.     begin
  2671.         print "The only legal option for a tape device is 'skip' or 'noskip'."
  2672.         return (1)
  2673.     end
  2674.     if @devstatus = "skip"
  2675.         select @status = @status | 8
  2676. end
  2677. else select @tapesize = 0
  2678. /* Tell the server about the device by inserting into sysdevices */
  2679. insert into sysdevices (low, high, status, cntrltype, name, phyname)
  2680.       values (0, @tapesize, @status, @cntrltype, @logicalname, @physicalname)
  2681.  
  2682. if @devtype = "disk"
  2683.     print "'Disk' device added."
  2684. else if @devtype = "tape"
  2685.     print "'Tape' device added."
  2686. else
  2687.     /* Must be a channel device */
  2688.     print "'Channel' device added."
  2689.  
  2690. /*
  2691. **  Warn about duplicate physical names.
  2692. */
  2693. if (select count(*)
  2694.     from master.dbo.sysdevices
  2695.         where phyname = @physicalname) > 1
  2696. begin
  2697.     select @msg = "WARNING: physical device name '" + @physicalname +
  2698.         "' is not unique."
  2699.     print @msg
  2700. end
  2701.  
  2702. return (0)
  2703. go
  2704. dump transaction master with truncate_only
  2705. go
  2706. /* adduser 49.1 9/10/91 */
  2707. create procedure sp_adduser
  2708. @loginame varchar(30),            /* user's login name in syslogins */
  2709. @name_in_db varchar(30) = NULL,        /* user's name to add to current db */ 
  2710. @grpname varchar(30) = NULL        /* group to put new user in */
  2711. as
  2712.  
  2713. declare @suid int            /* user's system id */
  2714. declare @grpid int            /* group id of group to put user in */
  2715. declare @uid int            /* new user's id */
  2716. declare @msg varchar(250)
  2717.  
  2718. /*
  2719. **  You must be SA or the dbo to execute this sproc.
  2720. */
  2721. if suser_id() != 1 and user_id() != 1
  2722. begin
  2723.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  2724.     return (1)
  2725. end
  2726.  
  2727. /*
  2728. **  If no new user name is given, use the login name.
  2729. */
  2730. if @name_in_db = NULL
  2731.     select @name_in_db = @loginame
  2732.  
  2733. /*
  2734. **  Check to see that the @name_in_db is valid.
  2735. */
  2736. declare @returncode    int
  2737. execute @returncode = sp_validname @name_in_db
  2738. if @returncode != 0
  2739. begin
  2740.     select @msg = "'" + @name_in_db + "' is not a valid name."
  2741.     print @msg
  2742.     return @returncode
  2743. end
  2744.  
  2745. /*
  2746. **  The name guest is a special case.  If it doesn't have a login it
  2747. **  can still be a valid user.  We'll catch it here and special case it.
  2748. */
  2749. if @loginame = "guest" and not exists
  2750.     (select name
  2751.         from master.dbo.syslogins
  2752.             where name = @loginame)
  2753. begin
  2754.     if exists (select *
  2755.             from sysusers
  2756.            where name = @loginame)
  2757.     begin
  2758.         print "A user with the same name already exists in the database."
  2759.         return (1)
  2760.     end
  2761.  
  2762.     /*
  2763.     **  Add the guest user and return.
  2764.     */
  2765.     insert into sysusers (uid, suid, gid, name)
  2766.         values (2, -1, 0, "guest")
  2767.  
  2768.     return (0)
  2769. end
  2770.  
  2771. /*
  2772. **  Check to see that the user has a login name.
  2773. **  We'll also initialize @grpid to 0 while we're here.
  2774. */
  2775. select @suid = suid, @grpid = 0
  2776.     from master.dbo.syslogins
  2777. where name = @loginame
  2778.  
  2779. if @suid = NULL
  2780. begin
  2781.     print "No login with the specified name exists."
  2782.     return (1)
  2783. end
  2784.  
  2785. /*
  2786. **  Now check to see if the user already exists in the database.
  2787. */
  2788. if exists (select *
  2789.            from sysusers
  2790.        where name = @name_in_db)
  2791. begin
  2792.     print "A user with the same name already exists in the database."
  2793.     return (1)
  2794. end
  2795.  
  2796. /*
  2797. **  See if the user already has an account under a different name.
  2798. **  That is, is the user's suid already in the sysusers table.
  2799. */
  2800. if exists (select *
  2801.        from sysusers
  2802.        where suid = @suid)
  2803. begin
  2804.     print "User already has a login under a different name."
  2805.     return (1)
  2806. end
  2807.  
  2808. /*
  2809. **  See if the user is known in the database already with an alias.
  2810. **  That is, does the user's suid appear in the sysalternates table.
  2811. */
  2812. if exists (select *
  2813.        from sysalternates
  2814.        where suid = @suid)
  2815. begin
  2816.     print "User already has alias access to the database."
  2817.     return (1)
  2818. end
  2819.  
  2820. /*
  2821. **  If a group name is given, check to see that it is valid.
  2822. **  User ids are > 0 and <= 16383 and group ids are > 16383.
  2823. */
  2824. if @grpname != NULL
  2825. begin
  2826.     select @grpid = -1
  2827.     select @grpid = gid
  2828.         from sysusers
  2829.     where name = @grpname and (uid > 16383 or uid = 0)
  2830.  
  2831.     if @grpid = -1
  2832.     begin
  2833.         print "No group with the specified name exists."
  2834.         return (1)
  2835.     end
  2836. end
  2837.  
  2838. /*
  2839. **  Add the user to the sysusers table.
  2840. **  Check to see if the special user 'guest' (uid = 2) has already
  2841. **  been added.  If not then the uid to use is 3, otherwise max(uid) + 1.
  2842. */
  2843. select @uid = max(uid) + 1
  2844.     from sysusers
  2845.         where uid < 16384 and uid > 2
  2846.  
  2847. /* check if there's any more user id's available */
  2848. if @uid >= 16384
  2849. begin
  2850.     print "All user ids have been assigned."
  2851.     return (1)
  2852. end
  2853.  
  2854. /*
  2855. **  This is the first regular user to be added so give them the first
  2856. **  number.
  2857. */
  2858. if @uid = NULL
  2859.     select @uid = 3
  2860.  
  2861. insert into sysusers (uid, suid, gid, name)
  2862.     values (@uid, @suid, @grpid, @name_in_db)
  2863.  
  2864. print "New user added."
  2865.  
  2866. return (0)
  2867. go
  2868. grant execute on sp_adduser to public
  2869. go
  2870. /* addremotelogin 49.1 9/10/91 */
  2871. create procedure sp_addremotelogin
  2872. @remoteserver    varchar(30),        /* name of remote server */
  2873. @loginame varchar(30) = NULL,        /* user's remote name */
  2874. @remotename varchar(30) = NULL        /* user's local user name */
  2875. as
  2876.  
  2877. declare    @msg    varchar(200)
  2878.  
  2879. /*
  2880. **  If we're in a transaction, disallow this since it might make recovery
  2881. **  impossible.
  2882. */
  2883. if @@trancount > 0
  2884. begin
  2885.     print "Can't run sp_addremotelogin from within a transaction."
  2886.     return (1)
  2887. end
  2888.  
  2889. /*
  2890. **  Check that the server name is valid.
  2891. */
  2892. if not exists (select *
  2893.             from master.dbo.sysservers
  2894.         where srvname = @remoteserver)
  2895. begin
  2896.     select @msg = "There is not a server named '" + @remoteserver + "'."
  2897.     print @msg
  2898.     return (1)
  2899. end
  2900.  
  2901. /*
  2902. **  If it's the local server don't bother.
  2903. */
  2904. if exists (select *
  2905.         from master.dbo.sysservers
  2906.             where srvname = @remoteserver
  2907.                 and srvid = 0)
  2908. begin
  2909.     select @msg = "'" + @remoteserver + "' is the local server - remote login not applicable."
  2910.     print @msg
  2911.     return (1)
  2912. end
  2913.  
  2914. /*
  2915. **  There are three cases to handle.
  2916. **
  2917. **    1) if only @remoteserver is given then a entry is made in
  2918. **        sysremotelogins that means anyone that doesn't have
  2919. **        an exact of mapped match in sysremotelogins will use
  2920. **        their remotename as their local name and it will be looked
  2921. **        up in syslogins.
  2922. **
  2923. **    2) if @remotename is omitted then it means that anyone from the
  2924. **        remote server logging in that doesn't have a complete
  2925. **        match in sysremotelogins will be mapped to @loginame.
  2926. **
  2927. **    3) if @remotename and @loginame are given then it is a straight
  2928. **        remote login for sysremotelogins.
  2929. */
  2930.  
  2931. /*
  2932. **  Case 1:  Only @remoteserver given.
  2933. */
  2934. if @loginame = null and @remotename = null
  2935. begin
  2936.     /*
  2937.     **  Check that there is not already an entry for local mapping.
  2938.     */
  2939.     if exists (select *
  2940.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  2941.             where l.remoteserverid = s.srvid
  2942.                 and s.srvname = @remoteserver
  2943.                 and l.remoteusername = null)
  2944.     begin
  2945.         select @msg = "There is already a default-name mapping of a remote login from remote server '"
  2946.             + @remoteserver + "'."
  2947.         print @msg
  2948.         return (1)
  2949.     end
  2950.  
  2951.     /*
  2952.     **  Add the entry.
  2953.     */
  2954.     insert into master.dbo.sysremotelogins 
  2955.         (remoteserverid, remoteusername, suid, status)
  2956.     select srvid, null, -1, 0
  2957.         from master.dbo.sysservers
  2958.             where srvname = @remoteserver
  2959.     print "New remote login created."
  2960.     return (0)
  2961.  
  2962. end
  2963.  
  2964. /*
  2965. **  Check that the @loginame is valid.  These is needed for both
  2966. **  case 2 and 3.
  2967. */
  2968. if not exists (select *
  2969.         from master.dbo.syslogins
  2970.             where name = @loginame)
  2971. begin
  2972.     select @msg = "'" + @loginame + "' isn't a local user -- remote login denied."
  2973.     print @msg
  2974.     return (1)
  2975. end
  2976.  
  2977. /*
  2978. **  Check to make sure that there is not already a @remotename for 
  2979. **  the @remoteserver.
  2980. */
  2981. if exists (select *
  2982.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  2983.             where l.remoteserverid = s.srvid
  2984.                 and s.srvname = @remoteserver
  2985.                 and l.remoteusername = @remotename)
  2986. begin
  2987.     select @msg = "There is already a remote user named '" 
  2988.         + isnull(@remotename, "NULL") + "' for remote server '" 
  2989.         + @remoteserver + "'."
  2990.     print @msg
  2991.     return (1)
  2992. end
  2993.     
  2994.  
  2995. /*
  2996. **  Case 2: We want to make an entry into sysremotelogins that will map
  2997. **    any non-exact matches to a particular local user.
  2998. */
  2999. if @remotename = null
  3000. begin
  3001.     /*
  3002.     **  Check that there is not already an entry for local mapping.
  3003.     */
  3004.     if exists (select *
  3005.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  3006.             where l.remoteserverid = s.srvid
  3007.                 and s.srvname = @remoteserver
  3008.                 and l.remoteusername = null)
  3009.     begin
  3010.         select @msg = "There is already a default-name mapping of a remote login from remote server '"
  3011.             + @remoteserver + "'."
  3012.         print @msg
  3013.         return (1)
  3014.     end
  3015.  
  3016.     /*
  3017.     **  Go ahead and make the entry.
  3018.     */
  3019.     insert into master.dbo.sysremotelogins
  3020.         (remoteserverid, remoteusername, suid, status)
  3021.     select srvid, null, suser_id(@loginame), 0
  3022.         from master.dbo.sysservers
  3023.                 where srvname = @remoteserver
  3024.  
  3025.     print "New remote login created."
  3026.     return (0)
  3027. end
  3028.  
  3029. /*
  3030. **  Case 3:  All the parameters have been supplied.  All we need to check
  3031. **    is that the entry isn't already in sysremotelogins.
  3032. **    We've verified the @remoteserver and @loginame above.
  3033. */
  3034. if @loginame != null and @remotename != null
  3035. begin
  3036.     /*
  3037.     **  Make sure that the @loginame and @remotename are a
  3038.     **  unique combination.
  3039.     */
  3040.     if exists (select *
  3041.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  3042.             where l.remoteusername = @remotename
  3043.                 and l.remoteserverid = s.srvid
  3044.                 and s.srvname = @remoteserver
  3045.                 and l.suid = suser_id(@loginame))
  3046.     begin
  3047.         select @msg = "There is already a remote user named '" 
  3048.             + @remotename + "' for remote server '" 
  3049.             + @remoteserver + "'."
  3050.         print @msg
  3051.         return (1)
  3052.     end
  3053.  
  3054.     /*
  3055.     **  Go ahead the do the insert.
  3056.     */
  3057.     insert into master.dbo.sysremotelogins
  3058.         (remoteserverid, remoteusername, suid, status)
  3059.     select srvid, @remotename, suser_id(@loginame), 0
  3060.         from master.dbo.sysservers
  3061.             where srvname = @remoteserver
  3062.     print "New remote login created."
  3063.     return (0)
  3064. end
  3065.  
  3066. /*
  3067. **  We got here because the syntax was incorrect.
  3068. */
  3069. print "Usage: sp_addremotelogin remoteserver [, loginame [,remotename]]"
  3070. return (1)
  3071. go
  3072. dump transaction master with truncate_only
  3073. go
  3074. /* addsegment    49.1    9/10/91 */
  3075. create procedure sp_addsegment
  3076. @segname    varchar(30),    /* segment name */
  3077. @devname    varchar(30)    /* device name to put segment on */
  3078. as
  3079.  
  3080. declare @dbuid    smallint    /* id of the owner of the database */
  3081. declare @segbit    int        /* this is the bit to turn on in sysusages */
  3082. declare    @msg    char(100)
  3083. declare @retcode    int
  3084.  
  3085. /*
  3086. **  If we're in a transaction, disallow this since it might make recovery
  3087. **  impossible.
  3088. */
  3089. if @@trancount > 0
  3090. begin
  3091.     print "Can't run sp_addsegment from within a transaction."
  3092.     return (1)
  3093. end
  3094.  
  3095. /*
  3096. **  You must be SA or the dbo to execute this sproc.
  3097. */
  3098. if suser_id() != 1 and user_id() != 1
  3099. begin
  3100.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  3101.     return (1)
  3102. end
  3103.  
  3104. /*
  3105. **  See if the device exists.
  3106. */
  3107. if not exists (select *
  3108.         from master.dbo.sysdevices
  3109.             where name like @devname)
  3110. begin
  3111.     print "No such device exists -- run sp_helpdb to list the devices for the current database."
  3112.     return (1)
  3113. end
  3114.  
  3115. /*
  3116. **    Check that the device is a database device.
  3117. */
  3118.  
  3119. if not exists (select *
  3120.         from master.dbo.sysdevices
  3121.             where name like @devname and cntrltype = 0)
  3122. begin
  3123.     print "The specified device is not a database device."
  3124.     return (1)
  3125. end
  3126.  
  3127. /*
  3128. **  Now see if the @dbname uses the @devname
  3129. */
  3130. if not exists (select *
  3131.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3132.             where d.name = @devname
  3133.                 and u.vstart between d.low and d.high
  3134.                 and u.dbid = db_id())
  3135. begin
  3136.     print "The specified device is not used by the database."
  3137.     return (1)
  3138. end
  3139.  
  3140. /*
  3141. **  Check to see if the device is marked as a log device.
  3142. **  If so, print error.
  3143. */
  3144. if exists (select *
  3145.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3146.             where d.name = @devname
  3147.                 and u.vstart between d.low and d.high
  3148.                 and u.dbid = db_id()
  3149.                 and segmap = 4)
  3150. begin
  3151.     select @msg = "'" + @devname + "' is reserved exclusively as a log device."
  3152.     print @msg
  3153.     return (1)
  3154. end
  3155.  
  3156. /*
  3157. ** Check for valid identifier.
  3158. */
  3159. execute @retcode = sp_validname @segname
  3160. if (@retcode != 0)
  3161. begin
  3162.     select @msg = "'" + @segname + "' is not a valid identifier."
  3163.     print @msg
  3164.     return (1)
  3165. end
  3166.  
  3167. /*
  3168. **  Now go ahead and define the new segment and add it to the segmap
  3169. **  of sysusages.
  3170. **  NOTE: Don't update master..sysusages and syssegments as a xact since
  3171. **      it could cause problems for recovery.
  3172. */
  3173.  
  3174. /*
  3175. **  Check that @segname doesn't already exist.
  3176. */
  3177. if exists (select *
  3178.         from syssegments holdlock
  3179.             where name = @segname)
  3180. begin
  3181.     select @msg = "There is already a segment named '"
  3182.         + @segname + "'."
  3183.     print @msg
  3184.     return (1)
  3185. end
  3186.  
  3187. /*
  3188. **  Figure out the next segment number to use.
  3189. **  Segment number may be 0-31.
  3190. */
  3191. select @segbit = 3
  3192. while @segbit < 32
  3193. begin
  3194.     /*
  3195.     **  Did we find one?
  3196.     */
  3197.     if exists (select *
  3198.             from syssegments
  3199.                 where segment = @segbit)
  3200.     begin
  3201.         select @segbit = @segbit + 1
  3202.     end
  3203.  
  3204.     /*
  3205.     **  We found an opening so break out.
  3206.     */
  3207.     else break
  3208.  
  3209. end
  3210.  
  3211. if @segbit >= 32
  3212. begin
  3213.     print "The maximum number of segments for the current database are already defined."
  3214.     return (1)
  3215. end
  3216.  
  3217. /*
  3218. **  Add the new segment.
  3219. */
  3220. insert into syssegments (segment, name, status)
  3221.     values (@segbit, @segname, 0)
  3222.  
  3223. /*
  3224. **  Now set the segments on @devname sysusages.
  3225. */
  3226. if (@segbit < 31)
  3227.     select @segbit = power(2, @segbit)
  3228. else
  3229.     /*
  3230.     **  Since this is segment 31, power(2, 31) will overflow
  3231.     **  since segmap is an int.  We'll grab the machine-dependent
  3232.     **  bit mask from spt_values to set the right bit.
  3233.     */
  3234.     select @segbit = low
  3235.         from master.dbo.spt_values
  3236.             where type = "E"
  3237.                 and number = 2
  3238. update master.dbo.sysusages
  3239.     set segmap = segmap | @segbit
  3240.         from master.dbo.sysusages u,
  3241.             master.dbo.sysdevices d
  3242.         where d.name = @devname
  3243.             and u.vstart between d.low and d.high
  3244.             and u.dbid = db_id()
  3245.  
  3246. /*
  3247. **  Now we need to activate the new segment map.
  3248. */
  3249. declare @dbname    varchar(30)
  3250. select @dbname = db_name()
  3251. dbcc dbrepair (@dbname, remap)
  3252. print "Segment created."
  3253.  
  3254. return (0)
  3255. go
  3256. grant execute on sp_addsegment to public
  3257. go
  3258. /* addserver    49.1    9/10/91 */
  3259. create procedure sp_addserver
  3260. @server    varchar(30),        /* server name */
  3261. @local    varchar(10) = NULL    /* local server */
  3262. as
  3263.  
  3264. declare    @msg    varchar(100)
  3265.  
  3266. /*
  3267. **  If we're in a transaction, disallow this since it might make recovery
  3268. **  impossible.
  3269. */
  3270. if @@trancount > 0
  3271. begin
  3272.     print "Can't run sp_addserver from within a transaction."
  3273.     return (1)
  3274. end
  3275.  
  3276. /*
  3277. **  Server names must be unique so check.
  3278. */
  3279. if exists (select *
  3280.         from master.dbo.sysservers
  3281.             where srvname = @server)
  3282. begin
  3283.     select @msg = "There is already a server named '" + @server + "'."
  3284.     print @msg
  3285.     return (1)
  3286. end
  3287.  
  3288. /*
  3289. **  Check to see that the @server is valid.
  3290. */
  3291. declare @returncode    int
  3292. execute @returncode = sp_validname @server
  3293. if @returncode != 0
  3294. begin
  3295.     select @msg = "'" + @server + "' is not a valid name."
  3296.     print @msg
  3297.     return @returncode
  3298. end
  3299.  
  3300. /*
  3301. **  If this is the not the local server, then its srvid = max(srvid) + 1.
  3302. */
  3303. if @local = null
  3304. begin    
  3305.     insert into master.dbo.sysservers (srvid, srvstatus, srvname, srvnetname)
  3306.             select isnull(max(srvid), 0) + 1, 0, @server, @server
  3307.                 from master.dbo.sysservers
  3308. end
  3309.  
  3310. /*
  3311. **  If @local = "local" then this is the local server and it's
  3312. **  srvid = 0.
  3313. */
  3314. else
  3315. begin
  3316.     if @local != "local" and @local != "LOCAL"
  3317.     begin
  3318.         print "Usage: sp_addserver servername [, 'local']"
  3319.         return (1)
  3320.     end
  3321.  
  3322.     if exists (select *
  3323.             from master.dbo.sysservers
  3324.                 where srvid = 0)
  3325.     begin
  3326.         print "There is already a local server."
  3327.         return (1)
  3328.     end
  3329.  
  3330.     insert into master.dbo.sysservers (srvid, srvstatus, srvname, srvnetname)
  3331.             values (0, 0, @server, @server)
  3332. end
  3333.  
  3334. select @msg = "Server added."
  3335. print @msg
  3336. return (0)
  3337. go
  3338. dump transaction master with truncate_only
  3339. go
  3340. /* addlanguage 49.1 9/10/91 */
  3341. create procedure sp_addlanguage 
  3342. @language varchar(30),
  3343. @alias varchar(30) = NULL,
  3344. @months varchar(251),
  3345. @shortmons varchar(119),
  3346. @days varchar(216),
  3347. @datefmt char(3),
  3348. @datefirst tinyint
  3349. as
  3350.  
  3351. declare @msg varchar(250)
  3352. declare @nextlangid smallint
  3353. declare @returncode    int 
  3354.  
  3355. /*  Check to see if the language exists. */
  3356. select @returncode = 0
  3357. execute @returncode = sp_validlang @language
  3358. if @returncode = 0
  3359. begin
  3360.     select @msg = "'" + @language + "' already exists in Syslanguages."
  3361.     print @msg
  3362.     return 1
  3363. end
  3364.  
  3365. /* Check to see that the list of full month names is valid. */
  3366. execute @returncode = sp_chklangparam @months, "full month", 12, 20
  3367. if @returncode != 0
  3368.     return 1
  3369.  
  3370. /* Check to see that the list of short month names is valid. */
  3371. execute @returncode = sp_chklangparam @shortmons, "short month", 12, 9
  3372. if @returncode != 0
  3373.     return 1
  3374.  
  3375. /* Check to see that the list of day names is valid. */
  3376. execute @returncode = sp_chklangparam @days, "day", 7, 30
  3377. if @returncode != 0
  3378.     return 1
  3379.  
  3380. /* Check to see that the @datefmt is valid. */
  3381. if (@datefmt != 'mdy' and @datefmt != 'dmy' and @datefmt != 'ymd' and @datefmt != 'ydm'
  3382.     and @datefmt != 'myd' and @datefmt != 'dym')
  3383. begin
  3384.     select @msg = "'" + @datefmt + "' is not a valid date order."
  3385.     print @msg
  3386.     return 1
  3387. end
  3388.  
  3389. /*
  3390. **  Check to see that the @datefirst is valid.
  3391. */
  3392. if (@datefirst < 1 or @datefirst > 7)
  3393. begin
  3394.     select @msg = "'" + convert(char(3), @datefirst) + "' is not a valid first day." 
  3395.     print @msg
  3396.     return 1
  3397. end
  3398.  
  3399. /*
  3400. **  If the user didn't specify the alias name, the alias name is same as 
  3401. **  the official name.
  3402. */
  3403. if @alias is null
  3404. begin
  3405.     select @alias =  @language
  3406. end
  3407.  
  3408. /*  Check to see if the alias exists. */
  3409. select @returncode = 0
  3410. execute @returncode = sp_validaltlang @alias
  3411. if @returncode = 0
  3412. begin
  3413.     select @msg = "'" + @alias + "' alias already exists in Syslanguages."
  3414.     print @msg
  3415.     return 1
  3416. end
  3417.  
  3418. if @language = 'us_english'
  3419. begin
  3420.     /*  The language id of us_english is 0. */
  3421.     insert syslanguages (langid, dateformat, datefirst, upgrade, 
  3422.         name, alias, months, shortmonths, days)
  3423.     select 0, @datefmt, @datefirst, 0, 
  3424.         @language, @alias, @months, @shortmons, @days
  3425. end
  3426. else
  3427. begin
  3428.  
  3429.     if (select max(langid) from syslanguages) is null
  3430.     begin
  3431.         select @nextlangid = 1
  3432.     end
  3433.     else
  3434.     begin
  3435.         select @nextlangid = (select max(langid)+1 
  3436.         from syslanguages)
  3437.     end
  3438.  
  3439.     insert syslanguages (langid, dateformat, datefirst, upgrade, 
  3440.         name, alias, months, shortmonths, days)
  3441.     select @nextlangid, @datefmt, @datefirst, 0, 
  3442.         @language, @alias, @months, @shortmons, @days
  3443. end
  3444.  
  3445.  
  3446. /* If the insert failed, say so. */
  3447. if @@error != 0
  3448. begin
  3449.     print "Language not inserted."
  3450.     return
  3451. end
  3452.  
  3453. print "New language inserted."
  3454. go
  3455. /* defaultlanguage 49.1 9/10/91 */
  3456. create procedure sp_defaultlanguage
  3457. @loginame varchar(30),            /* login name of the user */
  3458. @language varchar(30) = NULL        /* default language for the new user */
  3459. as
  3460.  
  3461. declare @msg varchar(250)
  3462. declare @returncode int
  3463.  
  3464. /*
  3465. **  Only the system administrator (sa) can run this command.
  3466. **  Check to make sure the executor is the sa.
  3467. */
  3468. if (suser_id() != 1 and suser_id() != suser_id(@loginame))
  3469. begin
  3470.     select @msg = "Only the System Administrator (SA) or '" + @loginame + "' can change " + @loginame + "'s default language."
  3471.     print @msg
  3472.     return 1
  3473. end
  3474.  
  3475. /* 
  3476. **  Check to see that the @language is valid.
  3477. */
  3478. if @language is not null
  3479. begin
  3480.     select @returncode = 0
  3481.     execute @returncode = sp_validlang @language
  3482.     if @returncode != 0
  3483.     begin
  3484.         if @language != "us_english"
  3485.         begin
  3486.             select @msg = "'" + @language + "' is not a valid official language name."
  3487.             print @msg
  3488.             return @returncode
  3489.         end
  3490.     end
  3491. end
  3492.  
  3493. /*
  3494. **  Make sure the login already exist.
  3495. */
  3496. if not exists (select *
  3497.           from syslogins
  3498.           where name = @loginame)
  3499. begin
  3500.     print "This user does not exist.  Run sp_addlogin to add this user in."
  3501.     return 1
  3502. end
  3503.  
  3504. /*
  3505. **  Set the default language for this user.
  3506. */
  3507. update syslogins 
  3508.     set language = @language
  3509.          where name = @loginame
  3510.  
  3511. select @msg =  @loginame + "'s default language is changed to " + @language
  3512. print @msg
  3513.  
  3514. return 0
  3515. go
  3516. grant execute on sp_defaultlanguage to public
  3517. go
  3518. dump transaction master with truncate_only
  3519. go
  3520. /* helplanguage 49.1 9/10/91 */
  3521. create procedure sp_helplanguage 
  3522. @language varchar(30) = NULL
  3523. as
  3524.  
  3525. declare @msg varchar(250)
  3526.  
  3527. /* Print all languages if the user didn't give the language name. */
  3528. if @language is null
  3529. begin
  3530.     if exists (select * from syslanguages)
  3531.         select * from syslanguages
  3532.     else
  3533.         print "No alternate languages are available."
  3534.  
  3535.     /* Find out whether us_english is there or not. */
  3536.     if not exists (select * from syslanguages   
  3537.             where name = 'us_english') 
  3538.         print "us_english is always available, even though it is not in master.dbo.syslanguages."
  3539.  
  3540.     return 0
  3541. end
  3542.  
  3543. /*  Report information on this language. */
  3544. if exists (select * from syslanguages   
  3545.                 where name = @language) 
  3546. begin
  3547.         select * from syslanguages 
  3548.                 where name = @language
  3549.         return 0
  3550. end 
  3551.  
  3552. if exists (select * from syslanguages
  3553.         where alias = @language)
  3554. begin
  3555.     select * from syslanguages 
  3556.         where alias = @language
  3557.     return 0
  3558. end
  3559.  
  3560. /* Couldn't find this language. */
  3561. if @language = 'us_english'
  3562. begin
  3563.     print "us_english is always available, even though it is not in master.dbo.syslanguages."
  3564.     return 0
  3565. end
  3566. else
  3567. begin
  3568.     select @msg = "'" + @language + "' is not in Syslanguages."
  3569.     print @msg
  3570.     return 1
  3571. end
  3572. go
  3573. grant execute on sp_helplanguage to public
  3574. go
  3575. /* droplanguage 49.1 9/10/91 */
  3576. create procedure sp_droplanguage 
  3577. @language varchar(30),
  3578. @dropmessages varchar(30) = NULL
  3579. as
  3580.  
  3581. declare @msg varchar(250)
  3582. declare @langid smallint
  3583. declare @dropmsgs int
  3584.  
  3585.  
  3586. /*  Check to see if the language exists. */
  3587. declare @returncode    int 
  3588. select @returncode = 0
  3589. execute @returncode = sp_validlang @language
  3590. if @returncode != 0
  3591. begin
  3592.     select @msg = "'" + @language + "' is not in Syslanguages."
  3593.     print @msg
  3594.     return 1
  3595. end
  3596.  
  3597. /* Get language id from syslanguages. */
  3598. select @langid = (select langid 
  3599.             from syslanguages 
  3600.                 where name = @language)
  3601.  
  3602. /*  Check to see if "dropmessages" is requested. */
  3603. select @dropmsgs = 0
  3604. if @dropmessages is null
  3605. begin
  3606.     if exists (select * from sysmessages 
  3607.             where langid = @langid)
  3608.     begin
  3609.         /* 
  3610.         **  Cannot drop a language if the language has associated
  3611.         **  entries in the master.dbo.sysmessages.
  3612.         */
  3613.         select @msg = "Can't drop '" + @language + "' because there are associated entries in master.dbo.sysmessages.  Run sp_droplanguage with dropmessages flag."
  3614.         print @msg 
  3615.         return 1
  3616.     end
  3617. end
  3618. else
  3619. begin
  3620.     if @dropmessages != "dropmessages"
  3621.     begin
  3622.         print "The only legal value for @dropmessages is 'dropmessages'."
  3623.         return 1
  3624.     end
  3625.  
  3626.     select @dropmsgs = 1
  3627. end
  3628.  
  3629. /* 
  3630. **  Drop the language and delete messages from Sysmessages if there is any.
  3631. */
  3632. if @dropmsgs = 1
  3633. begin
  3634.     delete sysmessages
  3635.         where langid = @langid
  3636. end
  3637.  
  3638. delete syslanguages 
  3639.     where langid = @langid
  3640.  
  3641. print "Language deleted."
  3642.  
  3643. go
  3644. dump transaction master with truncate_only
  3645. go
  3646. /* setlangalias 49.1 9/10/91 */
  3647. create procedure sp_setlangalias 
  3648. @language varchar(30),
  3649. @alias varchar(30)
  3650. as
  3651.  
  3652. declare @msg varchar(250)
  3653.  
  3654. /*  Check to see if the language exists. */
  3655. declare @returncode    int 
  3656. select @returncode = 0
  3657. execute @returncode = sp_validlang @language
  3658. if @returncode != 0
  3659. begin
  3660.     select @msg = "'" + @language + "' is not a valid language."
  3661.     print @msg
  3662.     return 1
  3663. end
  3664.  
  3665. /*  Check to see if the alias exists. */
  3666. select @returncode = 0
  3667. execute @returncode = sp_validaltlang @alias
  3668. if @returncode = 0
  3669. begin
  3670.     select @msg = "'" + @alias + "' alias already exists in Syslanguages."
  3671.     print @msg
  3672.     return 1
  3673. end
  3674.  
  3675. /* Reset the alternate language name. */
  3676. update syslanguages 
  3677.     set alias = @alias
  3678.         where name = @language
  3679.  
  3680. /* If the update failed, say so. */
  3681. if @@error != 0
  3682. begin
  3683.     print "Language alias not changed."
  3684.     return
  3685. end
  3686.  
  3687. print "Language alias reset."
  3688. go
  3689. /* bindefault 49.1 9/10/91 */
  3690. create procedure sp_bindefault
  3691. @defname varchar(92),            /* name of the default */
  3692. @objname varchar(61),            /* table or usertype name */
  3693. @futureonly varchar(15) = NULL        /* flag to indicate extent of binding */
  3694. as
  3695.  
  3696. declare @defid int            /* id of the default to bind */
  3697. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  3698.                     ** the binding to be limited */
  3699.  
  3700. set nocount on
  3701.  
  3702. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  3703.  
  3704. /*
  3705. **  When a default or rule is bound to a user-defined datatype, it is also
  3706. **  bound, by default, to any columns of the user datatype that are currently
  3707. **  using the existing default or rule as their default or rule.  This default
  3708. **  action may be overridden by setting @futureonly = @futurevalue when the
  3709. **  procedure is invoked.  In this case existing columns with the user
  3710. **  datatype won't have their existing default or rule changed.
  3711. */
  3712.  
  3713. /*
  3714. **  Check to see that the object names are local to the current database.
  3715. */
  3716. if (@objname like "%.%.%")
  3717.    or
  3718.    (@defname like "%.%.%" and
  3719.     substring(@defname, 1, charindex(".", @defname) - 1) != db_name())
  3720. begin
  3721.     print "Default and table or usertype must be in 'current' database."
  3722.     return (1)
  3723. end
  3724.  
  3725. /*
  3726. **  Check that the @futureonly argument, if supplied, is correct.
  3727. */
  3728. if (@futureonly != null)
  3729. begin
  3730.     if (@futureonly != @futurevalue)
  3731.     begin
  3732.         print "Usage: bindefault defaultname, objectname [, 'futureonly']"
  3733.         return (1)
  3734.     end
  3735. end
  3736.  
  3737. /*
  3738. **  Check to see that the default exists and get it's id.
  3739. */
  3740. select @defid = id
  3741.     from sysobjects
  3742. where id = object_id(@defname)
  3743.     and sysstat & 7 = 6        /* default object */
  3744.  
  3745. if @defid is NULL
  3746. begin
  3747.     print "No such default exists.  You must create the default first."
  3748.     return (1)
  3749. end
  3750.  
  3751. /*
  3752. **  If @objname is of the form tab.col then we are binding to a column.
  3753. **  Otherwise its a datatype.  In the column case, we need to extract
  3754. **  and verify the table and column names and make sure the user owns
  3755. **  the table that is getting the default bound.
  3756. */
  3757. if @objname like "%.%"
  3758. begin
  3759.     declare @tabname varchar(30)        /* name of table */
  3760.     declare @colname varchar(30)        /* name of column */
  3761.  
  3762.     /*
  3763.     **  Get the table name out.
  3764.     */
  3765.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  3766.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  3767.  
  3768.     /*
  3769.     **  If the column type is timestamp, disallow the bind.
  3770.     **  Defaults can't be bound to timestamp columns.
  3771.     */
  3772.     if exists (select *
  3773.             from sysobjects o, syscolumns c
  3774.         where c.id = object_id(@tabname)
  3775.             and c.name = @colname
  3776.             and c.name = @colname
  3777.             and o.id = object_id(@tabname)
  3778.             and o.sysstat & 7 = 3
  3779.             and c.usertype = 80)
  3780.     begin
  3781.         print "You can't bind a default to a timestamp datatype column."
  3782.         return (1)
  3783.     end
  3784.  
  3785.     update syscolumns
  3786.         set cdefault = @defid
  3787.             from syscolumns, sysobjects
  3788.         where syscolumns.id = object_id(@tabname)
  3789.             and syscolumns.name = @colname
  3790.             and sysobjects.id = object_id(@tabname)
  3791.             and uid = user_id()
  3792.             and sysobjects.sysstat & 7 = 3      /* user table */
  3793.  
  3794.     /*
  3795.     **  Did the bind happen?
  3796.     */
  3797.     if @@rowcount != 1
  3798.     begin
  3799.         print "You do not own a column of that name."
  3800.         return (1)
  3801.     end
  3802.  
  3803.     /*
  3804.     **  Since binding a default is a schema change, update schema count
  3805.     **  for the object in the sysobjects table.
  3806.     */
  3807.     update sysobjects
  3808.         set schema = schema + 1
  3809.             from sysobjects
  3810.                 where id = object_id(@tabname)
  3811.                     and uid = user_id()
  3812.  
  3813.     print "Default bound to column."
  3814.  
  3815. end
  3816. else
  3817. begin
  3818.     /*
  3819.     **  We're binding to a user type.  In this case, the @objname
  3820.     **  is really the name of the user datatype.
  3821.     **  When we bind to a user type, any existing columns get changed
  3822.     **  to the new binding unless their current binding is not equal
  3823.     **  to the current binding for the usertype or if they set the
  3824.     **  @futureonly parameter to @futurevalue.
  3825.     */
  3826.     declare @olddefault int            /* current default for type */
  3827.  
  3828.     /*
  3829.     **  Get the current default for the datatype.
  3830.     */
  3831.     select @olddefault = tdefault
  3832.         from systypes
  3833.     where name = @objname
  3834.         and uid = user_id()
  3835.         and usertype > 100
  3836.  
  3837.     if @olddefault = null
  3838.     begin
  3839.         print "You do not own a datatype of that name."
  3840.         return (1)
  3841.     end
  3842.  
  3843.     update systypes
  3844.         set tdefault = @defid
  3845.             from systypes
  3846.         where name = @objname
  3847.             and uid = user_id()
  3848.             and usertype > 100
  3849.  
  3850.  
  3851.     print "Default bound to datatype."
  3852.  
  3853.     /*
  3854.     **  Now see if there are any columns with the usertype that
  3855.     **  need the new binding.
  3856.     */
  3857.     if isnull(@futureonly, "") != @futurevalue
  3858.         and exists (select *
  3859.             from syscolumns, systypes
  3860.                 where syscolumns.usertype = systypes.usertype
  3861.                     and systypes.name = @objname
  3862.                     and systypes.usertype > 100
  3863.                     and systypes.uid = user_id()
  3864.                     and (syscolumns.cdefault = @olddefault
  3865.                     or syscolumns.cdefault = 0 ) )
  3866.     begin
  3867.         /*
  3868.         **  Update syscolumns with new binding.
  3869.         */
  3870.         update syscolumns
  3871.             set cdefault = systypes.tdefault
  3872.                 from syscolumns, systypes
  3873.             where syscolumns.usertype = systypes.usertype
  3874.                 and systypes.name = @objname
  3875.                 and systypes.usertype > 100
  3876.                 and systypes.uid = user_id()
  3877.                 and (syscolumns.cdefault = @olddefault
  3878.                 or syscolumns.cdefault = 0 )
  3879.  
  3880.         /*
  3881.         **  Update the table schema to indicate that something
  3882.         **  has changed in the table's schema.
  3883.         */
  3884.         update sysobjects
  3885.             set schema = schema + 1
  3886.                 from sysobjects, syscolumns, systypes
  3887.             where sysobjects.id = syscolumns.id
  3888.                 and syscolumns.usertype = systypes.usertype
  3889.                 and systypes.name = @objname
  3890.                 and systypes.usertype > 100
  3891.                 and systypes.uid = user_id()
  3892.                                 and (syscolumns.cdefault = @olddefault
  3893.                                           or  syscolumns.cdefault = 0 )
  3894.  
  3895.         print "The new default has been bound to columns(s) of the specified user datatype."
  3896.     end
  3897. end
  3898.  
  3899. return (0)
  3900. go
  3901. grant execute on sp_bindefault to public
  3902. go
  3903. dump transaction master with truncate_only
  3904. go
  3905. /* bindrule 49.1 9/10/91 */
  3906. create procedure sp_bindrule
  3907. @rulename varchar(92),            /* name of the rule */
  3908. @objname varchar(61),            /* table or usertype name */
  3909. @futureonly varchar(15) = NULL        /* column name */
  3910. as
  3911.  
  3912. declare @ruleid int            /* id of the rule to bind */
  3913. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  3914.                     ** the binding to be limited */
  3915.  
  3916. set nocount on
  3917.  
  3918. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  3919.  
  3920. /*
  3921. **  When a default or rule is bound to a user-defined datatype, it is also
  3922. **  bound, by default, to any columns of the user datatype that are currently
  3923. **  using the existing default or rule as their default or rule.  This default
  3924. **  action may be overridden by setting @futureonly = @futurevalue when the
  3925. **  procedure is invoked.  In this case existing columns with the user
  3926. **  datatype won't have their existing default or rule changed.
  3927. */
  3928.  
  3929. /*
  3930. **  Check to see that the object names are local to the current database.
  3931. */
  3932. if (@objname like "%.%.%")
  3933.    or
  3934.    (@rulename like "%.%.%" and
  3935.     substring(@rulename, 1, charindex(".", @rulename) - 1) != db_name())
  3936. begin
  3937.     print "Rule and table or usertype must be in 'current' database."
  3938.     return (1)
  3939. end
  3940.  
  3941. /*
  3942. **  Check that the @futureonly argument, if supplied, is correct.
  3943. */
  3944. if (@futureonly != null)
  3945. begin
  3946.     if (@futureonly != @futurevalue)
  3947.     begin
  3948.         print "Usage: bindrule rulename, objectname [, 'futureonly']"
  3949.         return (1)
  3950.     end
  3951. end
  3952.  
  3953. /*
  3954. **  Check to see that the rule exists and get it's id.
  3955. */
  3956. select @ruleid = id
  3957.     from sysobjects
  3958. where id = object_id(@rulename)
  3959.     and sysstat & 7 = 7        /* rule object */
  3960.  
  3961. if @ruleid is NULL
  3962. begin
  3963.     print "No such rule exists.  You must create the rule first."
  3964.     return
  3965. end
  3966.  
  3967. /*
  3968. **  If @objname is of the form tab.col then we are binding to a column.
  3969. **  Otherwise its a datatype.  In the column case, we need to extract
  3970. **  and verify the table and column names and make sure the user owns
  3971. **  the table that is getting the rule bound.
  3972. */
  3973. if @objname like "%.%"
  3974. begin
  3975.     declare @tabname varchar(30)        /* name of table */
  3976.     declare @colname varchar(30)        /* name of column */
  3977.  
  3978.     /*
  3979.     **  Get the table name out.
  3980.     */
  3981.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  3982.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  3983.  
  3984.     /*
  3985.     **  If the column type is image, text, or timestamp, disallow the bind.
  3986.     **  Rules can't be bound to image, text, or timestamp columns.
  3987.     **  The qualification to check for types is a little strange because
  3988.     **  text and image are real, basic data types while timestamp is not.
  3989.     **  Timestamp is really a binary.  The types are checked in case
  3990.     **  there is a user-defined datatype that is an image or text.
  3991.     **  User-defined datatypes mapping to timestamp are not allowed
  3992.     **  by sp_addtype.
  3993.     */
  3994.     if exists (select *
  3995.             from sysobjects o, syscolumns c
  3996.         where c.id = object_id(@tabname)
  3997.             and c.name = @colname
  3998.             and c.name = @colname
  3999.             and o.id = object_id(@tabname)
  4000.             and o.sysstat & 7 = 3
  4001.             and (c.type in (35, 34) or c.usertype = 80))
  4002.     begin
  4003.         print "You can't bind a rule to a text, image, or timestamp datatype column."
  4004.         return (1)
  4005.     end
  4006.  
  4007.     update syscolumns
  4008.         set domain = @ruleid
  4009.             from syscolumns, sysobjects
  4010.         where syscolumns.id = object_id(@tabname)
  4011.             and syscolumns.name = @colname
  4012.             and sysobjects.id = object_id(@tabname)
  4013.             and uid = user_id()
  4014.             and sysobjects.sysstat & 7 = 3      /* user table */
  4015.  
  4016.     /*
  4017.     **  Did the bind happen?
  4018.     */
  4019.     if @@rowcount != 1
  4020.     begin
  4021.         print "You do not own a column of that name."
  4022.         return (1)
  4023.     end
  4024.  
  4025.     /*
  4026.     **  Since binding a rule is a schema change, update schema count
  4027.     **  for the object in the sysobjects table.
  4028.     */
  4029.     update sysobjects
  4030.         set schema = schema + 1
  4031.             from sysobjects
  4032.                 where id = object_id(@tabname)
  4033.                     and uid = user_id()
  4034.  
  4035.     print "Rule bound to table column."
  4036.  
  4037. end
  4038. else
  4039. begin
  4040.     /*
  4041.     **  We're binding to a user type.  In this case, the @objname
  4042.     **  is really the name of the user datatype.
  4043.     **  When we bind to a user type, any existing columns get changed
  4044.     **  to the new binding unless their current binding is not equal
  4045.     **  to the current binding for the usertype or if they set the
  4046.     **  @futureonly parameter to @futurevalue.
  4047.     */
  4048.     declare @oldrule int            /* current rule for type */
  4049.  
  4050.     /*
  4051.     **  If the column type is image, text, or timestamp, disallow the bind.
  4052.     **  Rules can't be bound to image or text columns.
  4053.     */
  4054.     if exists (select *
  4055.             from systypes
  4056.                 where name = @objname
  4057.                     and (type in (35, 34) or usertype = 80))
  4058.     begin
  4059.         print "You can't bind a rule to a text, image, or timestamp datatype."
  4060.         return (1)
  4061.     end
  4062.  
  4063.     /*
  4064.     **  Get the current rule for the datatype.
  4065.     */
  4066.     select @oldrule = domain
  4067.         from systypes
  4068.     where name = @objname
  4069.         and uid = user_id()
  4070.         and usertype > 100
  4071.  
  4072.     if @oldrule = null
  4073.     begin
  4074.         print "You do not own a datatype of that name."
  4075.         return (1)
  4076.     end
  4077.  
  4078.     update systypes
  4079.         set domain = @ruleid
  4080.             from systypes
  4081.         where name = @objname
  4082.             and uid = user_id()
  4083.             and usertype > 100
  4084.  
  4085.  
  4086.     print "Rule bound to datatype."
  4087.  
  4088.     /*
  4089.     **  Now see if there are any columns with the usertype that
  4090.     **  need the new binding.
  4091.     */
  4092.     if isnull(@futureonly, "") != @futurevalue
  4093.         and exists (select *
  4094.             from syscolumns, systypes
  4095.                 where syscolumns.usertype = systypes.usertype
  4096.                     and systypes.name = @objname
  4097.                     and systypes.usertype > 100
  4098.                     and systypes.uid = user_id()
  4099.                                     and (syscolumns.domain = @oldrule
  4100.                                       or  syscolumns.domain = 0 ))
  4101.     begin
  4102.         print "The new rule has been bound to column(s) of the specified user datatype."
  4103.         /*
  4104.         **  Update syscolumns with new binding.
  4105.         */
  4106.         update syscolumns
  4107.             set domain = systypes.domain
  4108.                 from syscolumns, systypes
  4109.             where syscolumns.usertype = systypes.usertype
  4110.                 and systypes.name = @objname
  4111.                 and systypes.usertype > 100
  4112.                 and systypes.uid = user_id()
  4113.                                 and (syscolumns.domain = @oldrule
  4114.                                   or  syscolumns.domain = 0 )
  4115.  
  4116.         /*
  4117.         **  Update the table schema to indicate that something
  4118.         **  has changed in the table's schema.
  4119.         */
  4120.         update sysobjects
  4121.             set schema = schema + 1
  4122.                 from sysobjects, syscolumns, systypes
  4123.             where sysobjects.id = syscolumns.id
  4124.                 and syscolumns.usertype = systypes.usertype
  4125.                 and systypes.name = @objname
  4126.                 and systypes.usertype > 100
  4127.                 and systypes.uid = user_id()
  4128.                                 and (syscolumns.domain = @oldrule
  4129.                                   or  syscolumns.domain = 0 )
  4130.     end
  4131. end
  4132.  
  4133. return (0)
  4134. go
  4135. grant execute on sp_bindrule to public
  4136. go
  4137. /* changedbowner 49.1 9/10/91 */
  4138. create procedure sp_changedbowner
  4139. @loginame varchar(30),         /* login to become dbo */
  4140. @map      varchar(10) = NULL   /* True to map aliases, else drop  */
  4141. as
  4142.  
  4143. declare @suid    smallint
  4144. declare @oldsuid smallint
  4145.  
  4146. /*
  4147. **  Only the SA can change the owner of a database.
  4148. */
  4149. if suser_id() != 1
  4150. begin
  4151.     print "Only the System Administrator (SA) can change to owner of a database."
  4152.     return(1)
  4153. end
  4154.  
  4155. /*
  4156. **  Can't change the owner of the master database.
  4157. */
  4158. if db_id() = 1
  4159. begin
  4160.     print "Can't change the owner of the master database."
  4161.     return(1)
  4162. end
  4163.  
  4164. /*
  4165. **  Make sure that @loginame exists and has a login.
  4166. */
  4167. select @suid = suid
  4168.     from master.dbo.syslogins
  4169.         where name = @loginame
  4170.  
  4171. if @suid = NULL
  4172. begin
  4173.     print "No login with the specified name exists."
  4174.     return(1)
  4175. end
  4176.  
  4177. /*
  4178. **  Make sure that @loginame isn't already a user or alias in the database.
  4179. */
  4180. if exists (select *
  4181.         from sysusers
  4182.             where suid = @suid)
  4183. begin
  4184.     print "The proposed new db owner already is a user in the database."
  4185.     return(1)
  4186. end
  4187. if exists (select *
  4188.         from sysalternates
  4189.             where suid = @suid)
  4190. begin
  4191.     print "The proposed new db owner already is aliased in the database."
  4192.     return(1)
  4193. end
  4194.  
  4195. /* 
  4196. ** find old (current) dbo's suid
  4197. */
  4198. select @oldsuid = suid
  4199.     from sysusers
  4200.         where uid = 1
  4201.  
  4202. begin transaction
  4203.  
  4204. /*
  4205. **  Now change the suid of the owner of the database to the suid of @loginame.
  4206. */
  4207. update sysusers
  4208.     set suid = @suid
  4209.         where uid = 1
  4210.  
  4211. /*
  4212. ** if the user requested that aliases be mapped to new dbo, do that.
  4213. */
  4214. if @map = "true"
  4215. begin
  4216.  
  4217.     if exists (select *
  4218.             from sysalternates
  4219.                 where altsuid = @oldsuid)
  4220.     begin
  4221.         update sysalternates
  4222.             set altsuid = @suid
  4223.                 where altsuid = @oldsuid
  4224.  
  4225.         print "The dependent aliases were mapped to the new dbo."
  4226.     end
  4227. end
  4228. /* else drop the aliases to the old dbo */
  4229. else
  4230. begin
  4231.  
  4232.     if exists (select *
  4233.             from sysalternates
  4234.                 where altsuid = @oldsuid)
  4235.     begin
  4236.         delete from sysalternates
  4237.             where altsuid = @oldsuid
  4238.  
  4239.         print "The dependent aliases were dropped."
  4240.     end
  4241. end
  4242.  
  4243.  
  4244. /*
  4245. **  Reflect the new owner of the database in master.dbo.sysdatabases.
  4246. */
  4247. update master.dbo.sysdatabases
  4248.     set suid = @suid
  4249.         where dbid = db_id()
  4250.  
  4251. commit transaction
  4252.  
  4253. /*
  4254. **  Checkpoint the database so that the in-memory data structure for the
  4255. **  database will be consistent with sysdatabases.
  4256. */
  4257. checkpoint
  4258.  
  4259. print "Database owner changed."
  4260.  
  4261. return (0)
  4262. go
  4263. dump transaction master with truncate_only
  4264. go
  4265. /* changegroup 49.1 9/10/91 */
  4266. create procedure sp_changegroup
  4267. @grpname varchar(30),            /* group name */
  4268. @username varchar(30)            /* user name to add to group */
  4269. as
  4270.  
  4271. declare @gid int            /* group id */
  4272. declare @uid int            /* user id */
  4273.  
  4274. /*
  4275. **  You must be SA or the dbo to execute this sproc.
  4276. */
  4277. if suser_id() != 1 and user_id() != 1
  4278. begin
  4279.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  4280.     return (1)
  4281. end
  4282.  
  4283. /*
  4284. **  See if the group name exists.
  4285. */
  4286. select @gid = uid
  4287.     from sysusers
  4288. where name = @grpname
  4289.     and (uid > 16383 or uid = 0)
  4290.  
  4291. /*
  4292. **  If no such group, quit.
  4293. **  User ids are <= 16383 and group ids are > 16383 or = 0.
  4294. */
  4295. if @gid = NULL
  4296. begin
  4297.     print "No such group exists."
  4298.     return (1)
  4299. end
  4300.  
  4301. /*
  4302. **  See if the user name exists.
  4303. */
  4304. select @uid = uid
  4305.     from sysusers
  4306. where name = @username
  4307.     and (uid <= 16383 and uid > 0)
  4308.  
  4309. /*
  4310. **  If no such user in the database, quit.
  4311. */
  4312. if @uid = NULL
  4313. begin
  4314.     print "No such user exists."
  4315.     return (1)
  4316. end
  4317.  
  4318. /*
  4319. **  Everything is consistent so change the group.
  4320. */
  4321. update sysusers
  4322.     set gid = @gid
  4323.         from sysusers
  4324.     where uid = @uid
  4325.  
  4326. /*
  4327. **  We need to invalidate the protection cache since objects have
  4328. **  changed ownership.  This command will invalidate the current
  4329. **  protection cache so when protections are checked the new and
  4330. **  correct protections will be used.
  4331. */
  4332. grant all to null
  4333.  
  4334. print "Group changed."
  4335.  
  4336. return (0)
  4337. go
  4338. grant execute on sp_changegroup to public
  4339. go
  4340. /* checknames 49.1 9/10/91 */
  4341. create procedure sp_checknames
  4342. @mode varchar(20) = NULL        /* mode of operation; e.g. 'silent' */
  4343. as
  4344.  
  4345. declare @msilent    int        /* set to 1 if 'silent' mode is on */
  4346. declare @ret_val    int        /* set to 1 if we find funny char */
  4347. declare @codepoint    tinyint        /* set to 1 if we find funny char */
  4348. declare @dbname        varchar(30)    /* holds database name */
  4349. declare @msg        varchar(90)    /* used for messages to user */
  4350. declare @pat        varchar(132)    /* holds the pattern to search for */
  4351.  
  4352. set nocount on
  4353.  
  4354. if (@mode like '%help%')
  4355. begin
  4356.     print 'sp_checknames is used to search for non 7-bit ASCII characters'
  4357.     print 'in several important columns of system tables.  The following'
  4358.     print 'columns are searched:'
  4359.     print ' '
  4360.     print '    In "master":'
  4361.     print '        sysdatabases.name'
  4362.     print '        sysdevices.name'
  4363.     print '        syslogins.name'
  4364.     print '        syslogins.dbname'
  4365.     print '        syslogins.password'
  4366.     print '        sysremotelogins.remoteusername'
  4367.     print '        sysservers.srvname'
  4368.     print '        sysservers.srvnetname'
  4369.     print ' '
  4370.     print '    In all databases:'
  4371.     print '        syscolumns.name'
  4372.     print '        sysindexes.name'
  4373.     print '        sysobjects.name'
  4374.     print '        syssegments.name'
  4375.     print '        systypes.name'
  4376.     print '        sysusers.name'
  4377.     print ' '
  4378.  
  4379.     return (0)
  4380. end
  4381.  
  4382. /*
  4383. **  First, initialize return value, and set up mode variables:
  4384. */
  4385. select @ret_val = 0
  4386.  
  4387. if (@mode like '%silent%')
  4388.     select @msilent = 1
  4389. else
  4390.     select @msilent = 0
  4391.  
  4392.  
  4393. /*
  4394. **  Now, initialize the pattern string we will search for:
  4395. */
  4396. select @pat = '%[', @codepoint = 127
  4397. while (@codepoint < 255)
  4398. begin
  4399.     select @codepoint = @codepoint + 1
  4400.     select @pat = @pat + char(@codepoint)
  4401. end
  4402. select @pat = @pat + ']%'
  4403.  
  4404.  
  4405. /*
  4406. **  Get the database name we are in:
  4407. */
  4408. select @dbname = db_name()
  4409.  
  4410. if (@msilent = 0)
  4411. begin
  4412.     print ' '
  4413.     select @msg = 'Looking for non 7-bit ASCII characters in the system ' +
  4414.             'tables of database:  "' + @dbname + '"'
  4415.     print @msg
  4416.     print ' '
  4417. end
  4418.  
  4419.  
  4420. /*
  4421. **  Look through these only if in the master database:
  4422. */
  4423. if (@dbname = 'master')
  4424. begin
  4425.     if exists (select name from master.dbo.sysdatabases
  4426.             where name like @pat)
  4427.     begin
  4428.     if (@msilent = 1)
  4429.         return (1)
  4430.  
  4431.     select @ret_val = 1
  4432.     print ' '
  4433.     print '==============================================================='
  4434.     print 'Table.Column name:  "sysdatabases.name"'
  4435.     print ' '
  4436.     print 'The following database names contain non 7-bit ASCII characters.'
  4437.     print 'If you wish to change their names, use "sp_renamedb":'
  4438.     print ' '
  4439.     select dbid,name from master.dbo.sysdatabases
  4440.             where name like @pat
  4441.     end
  4442.  
  4443.     if exists (select name from master.dbo.sysdevices
  4444.             where name like @pat)
  4445.     begin
  4446.     if (@msilent = 1)
  4447.         return (1)
  4448.  
  4449.     select @ret_val = 1
  4450.     print ' '
  4451.     print '==============================================================='
  4452.     print 'Table.Column name:  "sysdevices.name"'
  4453.     print ' '
  4454.     print 'The following device names contain non 7-bit ASCII characters.'
  4455.     print 'If you wish to change their names, use "UPDATE":'
  4456.         print ' '
  4457.     select name from master.dbo.sysdevices
  4458.             where name like @pat
  4459.     end
  4460.  
  4461.     if exists (select name from master.dbo.syslogins
  4462.             where name like @pat)
  4463.     begin
  4464.     if (@msilent = 1)
  4465.         return (1)
  4466.  
  4467.     select @ret_val = 1
  4468.     print ' '
  4469.     print '==============================================================='
  4470.     print 'Table.Column name:  "syslogins.name"'
  4471.         print ' '
  4472.     print 'The following login names contain non 7-bit ASCII characters.'
  4473.     print 'If you wish to change these names, use "sp_droplogin" and'
  4474.     print '"sp_addlogin":'
  4475.         print ' '
  4476.     select suid,name from master.dbo.syslogins
  4477.             where name like @pat
  4478.     end
  4479.  
  4480.     if exists (select dbname from master.dbo.syslogins
  4481.             where dbname like @pat)
  4482.     begin
  4483.     if (@msilent = 1)
  4484.         return (1)
  4485.  
  4486.     select @ret_val = 1
  4487.     print ' '
  4488.     print '==============================================================='
  4489.     print 'Table.Column name:  "syslogins.dbname"'
  4490.     print ' '
  4491.     print 'The following logins have default database names that contain'
  4492.     print 'non 7-bit ASCII characters.  If you wish to change them use'
  4493.     print '"sp_defaultdb":'
  4494.         print ' '
  4495.     select suid,name,dbname from master.dbo.syslogins
  4496.             where dbname like @pat
  4497.     end
  4498.  
  4499.     if exists (select password from master.dbo.syslogins
  4500.             where password like @pat)
  4501.     begin
  4502.     if (@msilent = 1)
  4503.         return (1)
  4504.  
  4505.     select @ret_val = 1
  4506.     print ' '
  4507.     print '==============================================================='
  4508.     print 'Table.Column name:  "syslogins.password"'
  4509.     print ' '
  4510.     print 'The following logins have passwords that contain non 7-bit'
  4511.     print 'ASCII characters.  If you wish to change them use "sp_password";'
  4512.     print 'Remember, only the sa and the login itself may examine or change'
  4513.     print 'the syslogins.password column:'
  4514.         print ' '
  4515.     select suid,name from master.dbo.syslogins
  4516.             where password like @pat
  4517.     end
  4518.  
  4519.     if exists (select remoteusername from master.dbo.sysremotelogins
  4520.             where remoteusername like @pat)
  4521.     begin
  4522.     if (@msilent = 1)
  4523.         return (1)
  4524.  
  4525.     select @ret_val = 1
  4526.     print ' '
  4527.     print '==============================================================='
  4528.     print 'Table.Column name:  "sysremotelogins.remoteusername"'
  4529.         print ' '
  4530.     print 'The following remote login names contain non 7-bit ASCII'
  4531.     print 'characters.  If you wish to change these names, use'
  4532.     print '"sp_dropremotelogin" and "sp_addremotelogin":'
  4533.         print ' '
  4534.     select remoteserverid,remoteusername from master.dbo.sysremotelogins
  4535.             where remoteusername like @pat
  4536.     end
  4537.  
  4538.     if exists (select srvname from master.dbo.sysservers
  4539.             where srvname like @pat)
  4540.     begin
  4541.     if (@msilent = 1)
  4542.         return (1)
  4543.  
  4544.     select @ret_val = 1
  4545.     print ' '
  4546.     print '==============================================================='
  4547.     print 'Table.Column name:  "sysservers.srvname"'
  4548.         print ' '
  4549.     print 'The following server names contain non 7-bit ASCII characters.'
  4550.     print 'If you wish to change their names, use "sp_dropserver" and'
  4551.     print '"sp_addserver":'
  4552.         print ' '
  4553.     select srvid,srvname from master.dbo.sysservers
  4554.             where srvname like @pat
  4555.     end
  4556.  
  4557.     if exists (select srvnetname from master.dbo.sysservers
  4558.             where srvnetname like @pat)
  4559.     begin
  4560.     if (@msilent = 1)
  4561.         return (1)
  4562.  
  4563.     select @ret_val = 1
  4564.     print ' '
  4565.     print '==============================================================='
  4566.     print 'Table.Column name:  "sysservers.srvnetname"'
  4567.         print ' '
  4568.     print 'The following servers have "interface file" names that contain'
  4569.     print 'non 7-bit ASCII characters.  If you wish to change these names,'
  4570.     print 'use "UPDATE":'
  4571.         print ' '
  4572.     select srvid,srvname,srvnetname from master.dbo.sysservers
  4573.             where srvnetname like @pat
  4574.     end
  4575.  
  4576. end
  4577.  
  4578.  
  4579. /*
  4580. **  For *ALL* databases, we want to look through these:
  4581. */
  4582. if exists (select name from dbo.syscolumns
  4583.         where name like @pat)
  4584. begin
  4585.     if (@msilent = 1)
  4586.     return (1)
  4587.  
  4588.     select @ret_val = 1
  4589.     print ' '
  4590.     print '==============================================================='
  4591.     print 'Table.Column name:  "syscolumns.name"'
  4592.     print ' '
  4593.     print 'The following column and parameter names contain non 7-bit ASCII'
  4594.     print 'characters.  If you wish to change these names, use "sp_rename":'
  4595.     print ' '
  4596.     select objname=o.name,colname=c.name from dbo.syscolumns c, dbo.sysobjects o
  4597.         where c.name like @pat and o.id = c.id
  4598. end
  4599.  
  4600. if exists (select name from dbo.sysindexes
  4601.         where name like @pat
  4602.         and indid > 0)
  4603. begin
  4604.     if (@msilent = 1)
  4605.     return (1)
  4606.  
  4607.     select @ret_val = 1
  4608.     print ' '
  4609.     print '==============================================================='
  4610.     print 'Table.Column name:  "sysindexes.name"'
  4611.     print ' '
  4612.     print 'The following index names contain non 7-bit ASCII characters.'
  4613.     print 'If you wish to change these names, use "UPDATE":'
  4614.     print ' '
  4615.     select id,indid,name from dbo.sysindexes
  4616.         where name like @pat
  4617.         and indid > 0
  4618. end
  4619.  
  4620. if exists (select name from dbo.sysobjects
  4621.         where name like @pat)
  4622. begin
  4623.     if (@msilent = 1)
  4624.  
  4625.     return (1)
  4626.  
  4627.     select @ret_val = 1
  4628.     print ' '
  4629.     print '==============================================================='
  4630.     print 'Table.Column name:  "sysobjects.name"'
  4631.     print ' '
  4632.     print 'The following object names contain non 7-bit ASCII characters.'
  4633.     print 'If you wish to change these names, use "sp_rename":'
  4634.     print ' '
  4635.     select owner=u.name,o.name from dbo.sysobjects o,dbo.sysusers u
  4636.         where o.name like @pat and o.uid=u.uid
  4637. end
  4638.  
  4639. if exists (select name from dbo.syssegments
  4640.         where name like @pat)
  4641. begin
  4642.     if (@msilent = 1)
  4643.     return (1)
  4644.  
  4645.     select @ret_val = 1
  4646.     print ' '
  4647.     print '==============================================================='
  4648.     print 'Table.Column name:  "syssegments.name"'
  4649.     print ' '
  4650.     print 'The following segment names contain non 7-bit ASCII characters.'
  4651.     print 'If you wish to change these names, use "UPDATE":'
  4652.     print ' '
  4653.     select segment,name from dbo.syssegments
  4654.         where name like @pat
  4655. end
  4656.  
  4657. if exists (select name from dbo.systypes
  4658.         where name like @pat)
  4659. begin
  4660.     if (@msilent = 1)
  4661.     return (1)
  4662.  
  4663.     select @ret_val = 1
  4664.     print ' '
  4665.     print '==============================================================='
  4666.     print 'Table.Column name:  "systypes.name"'
  4667.     print ' '
  4668.     print 'The following datatype names contain non 7-bit ASCII characters.'
  4669.     print 'If you wish to change these names, use "sp_rename":'
  4670.     print ' '
  4671.     select name from dbo.systypes
  4672.         where name like @pat
  4673. end
  4674.  
  4675. if exists (select name from dbo.sysusers
  4676.         where name like @pat)
  4677. begin
  4678.     if (@msilent = 1)
  4679.     return (1)
  4680.  
  4681.     select @ret_val = 1
  4682.     print ' '
  4683.     print '==============================================================='
  4684.     print 'Table.Column name:  "sysusers.name"'
  4685.     print ' '
  4686.     print 'The following user or group names contain non 7-bit ASCII'
  4687.     print 'characters.  If you wish to change these names, use "UPDATE":'
  4688.     print ' '
  4689.     select suid,uid,name from dbo.sysusers
  4690.         where name like @pat
  4691. end
  4692.  
  4693. if (@ret_val = 0  and  @msilent = 0)
  4694. begin
  4695.     select @msg = 'Good news!  Database "' + @dbname + '" has no obj/user/etc.'
  4696.     print @msg
  4697.     print 'names that contain non 7-bit ASCII characters.'
  4698. end
  4699.  
  4700. return (@ret_val)
  4701. go
  4702. grant execute on sp_checknames to public
  4703. go
  4704. dump transaction master with truncate_only
  4705. go
  4706. CREATE TABLE    spt_committab
  4707. (
  4708.     commid    int, /* id used to refer to transaction */
  4709.     start    datetime, /* time transaction started */
  4710.     lastchange datetime, /* last time this row was updated */
  4711.     totnum    int,         /* number of servers 
  4712.               ** initially involved in xact 
  4713.               */
  4714.     outnum    int,       /* number of servers 
  4715.               ** who still have not completed 
  4716.               */
  4717.     status    char(1), /* 'a'- abort, 'c'- commit, 'b'- begin */
  4718.     applname varchar(30), /* application name */
  4719.     xactname varchar(30), /* transaction name */
  4720.     password varchar(30) NULL /* password protection for updating */
  4721. )
  4722. go
  4723. create unique clustered index commitclust on spt_committab(commid)
  4724. go
  4725.  
  4726. create procedure sp_start_xact 
  4727.     @applname varchar(30),
  4728.     @xactname varchar(30),
  4729.     @count      int,
  4730.     @password varchar(30) = NULL
  4731. AS
  4732.  
  4733. set nocount on
  4734.  
  4735. BEGIN
  4736.     BEGIN TRANSACTION
  4737.         INSERT spt_committab VALUES 
  4738.         (
  4739.             0,
  4740.             getdate(),
  4741.             getdate(),
  4742.             @count, 
  4743.             @count, 
  4744.             'b',
  4745.             @applname, 
  4746.             @xactname,
  4747.             @password
  4748.         ) 
  4749.  
  4750.         UPDATE spt_committab
  4751.             set commid = 1 + (select max(commid) from spt_committab)
  4752.             where commid = 0
  4753.  
  4754.         SELECT max(commid) from spt_committab
  4755.     COMMIT TRANSACTION
  4756. END
  4757. go
  4758.  
  4759. create procedure sp_commit_xact
  4760.     @commid    int,
  4761.     @password varchar(30) = NULL
  4762. AS
  4763. if exists (select *
  4764.     from spt_committab
  4765.     where commid = @commid
  4766.     and password = @password)
  4767. begin
  4768.     /*
  4769.     **  If status is aborted, return a 1 to indicate a failure.
  4770.     */
  4771.     if exists (select *
  4772.             from spt_committab
  4773.                 where commid = @commid
  4774.                     and status = 'a')
  4775.     begin
  4776.         select 1
  4777.         return (1)
  4778.     end
  4779.  
  4780.     /*  Mark the xact as committed. */
  4781.     UPDATE spt_committab 
  4782.         SET status = 'c', lastchange = getdate()
  4783.         WHERE commid = @commid
  4784.     select 0
  4785.     return (0)
  4786. end
  4787. else
  4788. begin
  4789.     /* Error so return 1 */
  4790.     raiserror 30000 "Commit service xact id doesn't exist."
  4791.     select 1
  4792.     return (1)
  4793. end
  4794. go
  4795.  
  4796. CREATE PROCEDURE sp_abort_xact
  4797.     @commid    int,
  4798.     @password varchar(30) = NULL
  4799. AS
  4800. if exists (select *
  4801.     from spt_committab
  4802.     where commid = @commid
  4803.     and password = @password)
  4804. begin
  4805.     UPDATE spt_committab 
  4806.         SET status = 'a' , lastchange = getdate()
  4807.         WHERE commid = @commid
  4808. END
  4809. else
  4810. begin
  4811.     raiserror 30001 "Commit service xact id doesn't exist."
  4812. end
  4813. go
  4814.  
  4815. CREATE PROCEDURE sp_remove_xact
  4816.     @commid    int,
  4817.     @count    int,
  4818.     @password varchar(30) = NULL
  4819. AS
  4820. if exists (select *
  4821.     from spt_committab
  4822.     where commid = @commid
  4823.     and password = @password)
  4824. begin
  4825.     /*
  4826.     ** Decrement outnum by @count.
  4827.     */
  4828.     UPDATE spt_committab 
  4829.         SET outnum = outnum - @count, lastchange = GETDATE()
  4830.         WHERE commid = @commid
  4831.  
  4832.     /*
  4833.     **  Delete the row if outnum < 1
  4834.     */
  4835.     DELETE spt_committab
  4836.         WHERE commid = @commid
  4837.             and outnum < 1
  4838.  
  4839. end
  4840. else
  4841. begin
  4842.     raiserror 30002 "Commit service xact id doesn't exist."
  4843. end
  4844. go
  4845.  
  4846. create procedure sp_stat_xact
  4847.     @commid    int
  4848. AS
  4849. BEGIN
  4850.     IF EXISTS 
  4851.     (
  4852.         SELECT status 
  4853.             FROM spt_committab
  4854.             WHERE commid = @commid
  4855.     )
  4856.         SELECT status 
  4857.             FROM spt_committab
  4858.             WHERE commid = @commid
  4859.     ELSE
  4860.         SELECT status = 'u'
  4861. END
  4862. go
  4863.  
  4864. create procedure sp_probe_xact
  4865.     @commid    int
  4866. AS
  4867. BEGIN
  4868.     declare @stat    char(1)
  4869.  
  4870.     begin tran
  4871.         IF EXISTS ( SELECT status 
  4872.                 FROM spt_committab
  4873.                     WHERE commid = @commid)
  4874.         begin
  4875.             SELECT @stat = status 
  4876.                 FROM spt_committab
  4877.                     WHERE commid = @commid
  4878.             
  4879.             if (@stat != 'c')
  4880.             begin
  4881.                 update spt_committab
  4882.                     set status = 'a'
  4883.                         where commid = @commid
  4884.                 select status = 'a'
  4885.             end
  4886.             else select status = @stat
  4887.         end
  4888.         else select status = 'u'
  4889.     commit tran
  4890.  
  4891. END
  4892. go
  4893.  
  4894. create procedure sp_scan_xact 
  4895.     @commid    int = NULL
  4896. as
  4897. BEGIN
  4898.     IF    @commid = -1 or @commid is NULL
  4899.  
  4900.         SELECT commid,start,lastchange,totnum,outnum,
  4901.                status,applname,xactname
  4902.         FROM spt_committab
  4903.     ELSE
  4904.         SELECT commid,start,lastchange,totnum,outnum,
  4905.                status,applname,xactname
  4906.         FROM spt_committab
  4907.         WHERE @commid = commid
  4908. END
  4909. go
  4910. go
  4911. grant execute on sp_start_xact to public
  4912. go
  4913. grant execute on sp_commit_xact to public
  4914. go
  4915. grant execute on sp_abort_xact to public
  4916. go
  4917. grant execute on sp_remove_xact to public
  4918. go
  4919. grant execute on sp_stat_xact to public
  4920. go
  4921. grant execute on sp_probe_xact to public
  4922. go
  4923. grant execute on sp_scan_xact to public
  4924. go
  4925. /* commonkey 49.1 9/10/91 */
  4926. create procedure sp_commonkey 
  4927. @tabaname  varchar(92),        /* name of first table in the key */
  4928. @tabbname varchar(92),        /* name of second table in the key */
  4929. @col1a     varchar(30),     /* first column name of first table */
  4930. @col1b     varchar(30),     /* first column name of second table */
  4931. @col2a     varchar(30) = NULL,
  4932. @col2b     varchar(30) = NULL,
  4933. @col3a     varchar(30) = NULL,
  4934. @col3b     varchar(30) = NULL,
  4935. @col4a     varchar(30) = NULL,
  4936. @col4b     varchar(30) = NULL,
  4937. @col5a     varchar(30) = NULL,
  4938. @col5b     varchar(30) = NULL,
  4939. @col6a     varchar(30) = NULL,
  4940. @col6b     varchar(30) = NULL,
  4941. @col7a     varchar(30) = NULL,
  4942. @col7b     varchar(30) = NULL,
  4943. @col8a     varchar(30) = NULL,
  4944. @col8b     varchar(30) = NULL
  4945. as
  4946.  
  4947. declare @objida int        /* id of table we are doing */
  4948. declare @objidb int        /* id of table with primary key */
  4949. declare @uida smallint        /* owner of the first table */
  4950. declare @cnt    int        /* how many columns are in the key */
  4951.  
  4952. declare @key1a int        /* colids of the first table */
  4953. declare @key2a int
  4954. declare @key3a int
  4955. declare @key4a int
  4956. declare @key5a int
  4957. declare @key6a int
  4958. declare @key7a int
  4959. declare @key8a int
  4960.  
  4961. declare @key1b int        /* colids of the second table */
  4962. declare @key2b int
  4963. declare @key3b int
  4964. declare @key4b int
  4965. declare @key5b int
  4966. declare @key6b int
  4967. declare @key7b int
  4968. declare @key8b int
  4969.  
  4970. /*
  4971. **  Check to see that the tabnames are local.
  4972. */
  4973. if @tabaname like "%.%.%"
  4974. begin
  4975.     if substring(@tabaname, 1, charindex(".", @tabaname) - 1) != db_name()
  4976.     begin
  4977.         print "Table or view name must be in 'current' database."
  4978.         return (1)
  4979.     end
  4980. end
  4981. if @tabbname like "%.%.%"
  4982. begin
  4983.     if substring(@tabbname, 1, charindex(".", @tabbname) - 1) != db_name()
  4984.     begin
  4985.         print "Table or view name must be in 'current' database."
  4986.         return (1)
  4987.     end
  4988. end
  4989.  
  4990. /*
  4991. **  See if we can find the objects.  They must be a system table, user table,
  4992. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the 
  4993. **  object type is -- it's more reliable than using sysobjects.type which
  4994. **  could change.
  4995. */
  4996. select @objida = id, @uida = uid
  4997.     from sysobjects
  4998.         where id = object_id(@tabaname)
  4999.             and (sysstat & 7 = 1        /* system table */
  5000.                 or sysstat & 7 = 2    /* view */
  5001.                 or sysstat & 7 = 3)    /* user table */
  5002. /*
  5003. **  If either of the tables don't exist, quit.
  5004. */
  5005. if @objida is NULL
  5006. begin
  5007.     print "First table in the common key doesn't exist."
  5008.     return (1)
  5009. end 
  5010. select @objidb = id
  5011.     from sysobjects
  5012.         where id = object_id(@tabbname)
  5013.             and (sysstat & 7 = 1
  5014.                 or sysstat & 7 = 2
  5015.                 or sysstat & 7 = 3)
  5016. if @objidb is NULL
  5017. begin
  5018.     print "Second table in the common key doesn't exist."
  5019.     return (1)
  5020. end 
  5021.  
  5022. /*
  5023. **  Only the owner of the table can define it's common keys.
  5024. */
  5025. if @uida != user_id()
  5026. begin
  5027.     print "Only the table owner may define its common keys."
  5028.     return (1)
  5029. end
  5030.  
  5031. /*
  5032. **  Now check to see that the first key columns exist and have compatible types.
  5033. */
  5034. select @cnt = 1, @key1a = a.colid, @key1b = b.colid
  5035.     from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5036.         master.dbo.spt_values z
  5037.         where a.name = @col1a
  5038.             and a.id = @objida
  5039.             and b.name = @col1b
  5040.             and b.id = @objidb
  5041.             and y.type = "J"
  5042.             and a.type = y.low
  5043.             and z.type = "J"
  5044.             and b.type = z.low
  5045.             and y.number = z.number
  5046. if @key1a = NULL
  5047. begin
  5048.     print "The tables have no such first column or the columns are of different types."
  5049.     return (1)
  5050. end
  5051.  
  5052. if @col2a != NULL
  5053. begin
  5054.     select @cnt = @cnt + 1, @key2a = a.colid, @key2b = b.colid
  5055.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5056.             master.dbo.spt_values z
  5057.             where a.name = @col2a
  5058.                 and a.id = @objida
  5059.                 and b.name = @col2b
  5060.                 and b.id = @objidb
  5061.                 and y.type = "J"
  5062.                 and a.type = y.low
  5063.                 and z.type = "J"
  5064.                 and b.type = z.low
  5065.                 and y.number = z.number
  5066.     if @key2a = NULL
  5067.     begin
  5068.         print "The tables have no such second column or the columns are of different types."
  5069.         return (1)
  5070.     end
  5071. end
  5072. else goto keys_ok
  5073.  
  5074. if @col3a != NULL
  5075. begin
  5076.     select @cnt = @cnt + 1, @key3a = a.colid, @key3b = b.colid
  5077.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5078.             master.dbo.spt_values z
  5079.             where a.name = @col3a
  5080.                 and a.id = @objida
  5081.                 and b.name = @col3b
  5082.                 and b.id = @objidb
  5083.                 and y.type = "J"
  5084.                 and a.type = y.low
  5085.                 and z.type = "J"
  5086.                 and b.type = z.low
  5087.                 and y.number = z.number
  5088.     if @key3a = NULL
  5089.     begin
  5090.         print "The tables have no such third column or the columns are of different types."
  5091.         return (1)
  5092.     end
  5093. end
  5094. else goto keys_ok
  5095.  
  5096. if @col4a != NULL
  5097. begin
  5098.     select @cnt = @cnt + 1, @key4a = a.colid, @key4b = b.colid
  5099.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5100.             master.dbo.spt_values z
  5101.             where a.name = @col4a
  5102.                 and a.id = @objida
  5103.                 and b.name = @col4b
  5104.                 and b.id = @objidb
  5105.                 and y.type = "J"
  5106.                 and a.type = y.low
  5107.                 and z.type = "J"
  5108.                 and b.type = z.low
  5109.                 and y.number = z.number
  5110.     if @key4a = NULL
  5111.     begin
  5112.         print "The tables have no such fourth column or the columns are of different types."
  5113.         return (1)
  5114.     end
  5115. end
  5116. else goto keys_ok
  5117.  
  5118. if @col5a != NULL
  5119. begin
  5120.     select @cnt = @cnt + 1, @key5a = a.colid, @key5b = b.colid
  5121.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5122.             master.dbo.spt_values z
  5123.             where a.name = @col5a
  5124.                 and a.id = @objida
  5125.                 and b.name = @col5b
  5126.                 and b.id = @objidb
  5127.                 and y.type = "J"
  5128.                 and a.type = y.low
  5129.                 and z.type = "J"
  5130.                 and b.type = z.low
  5131.                 and y.number = z.number
  5132.     if @key5a = NULL
  5133.     begin
  5134.         print "The tables have no such fifth column or the columns are of different types."
  5135.         return (1)
  5136.     end
  5137. end
  5138. else goto keys_ok
  5139.  
  5140. if @col6a != NULL
  5141. begin
  5142.     select @cnt = @cnt + 1, @key6a = a.colid, @key6b = b.colid
  5143.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5144.             master.dbo.spt_values z
  5145.             where a.name = @col6a
  5146.                 and a.id = @objida
  5147.                 and b.name = @col6b
  5148.                 and b.id = @objidb
  5149.                 and y.type = "J"
  5150.                 and a.type = y.low
  5151.                 and z.type = "J"
  5152.                 and b.type = z.low
  5153.                 and y.number = z.number
  5154.     if @key6a = NULL
  5155.     begin
  5156.         print "The tables have no such sixth column or the columns are of different types."
  5157.         return (1)
  5158.     end
  5159. end
  5160. else goto keys_ok
  5161.  
  5162. if @col7a != NULL
  5163. begin
  5164.     select @cnt = @cnt + 1, @key7a = a.colid, @key7b = b.colid
  5165.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5166.             master.dbo.spt_values z
  5167.             where a.name = @col7a
  5168.                 and a.id = @objida
  5169.                 and b.name = @col7b
  5170.                 and b.id = @objidb
  5171.                 and y.type = "J"
  5172.                 and a.type = y.low
  5173.                 and z.type = "J"
  5174.                 and b.type = z.low
  5175.                 and y.number = z.number
  5176.     if @key7a = NULL
  5177.     begin
  5178.         print "The tables have no such seventh column or the columns are of different types."
  5179.         return (1)
  5180.     end
  5181. end
  5182. else goto keys_ok
  5183.  
  5184. if @col8a != NULL
  5185. begin
  5186.     select @cnt = @cnt + 1, @key8a = a.colid, @key8b = b.colid
  5187.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5188.             master.dbo.spt_values z
  5189.             where a.name = @col8a
  5190.                 and a.id = @objida
  5191.                 and b.name = @col8b
  5192.                 and b.id = @objidb
  5193.                 and y.type = "J"
  5194.                 and a.type = y.low
  5195.                 and z.type = "J"
  5196.                 and b.type = z.low
  5197.                 and y.number = z.number
  5198.     if @key8a = NULL
  5199.     begin
  5200.         print "The tables have no such eighth column or the columns are of different types."
  5201.         return (1)
  5202.     end
  5203. end
  5204.  
  5205. /*
  5206. **  If we made it this far then all the columns for the common key are ok.
  5207. **  Everything is consistent so add the common key to syskeys.
  5208. */
  5209. keys_ok:
  5210.  
  5211. insert syskeys (id, type, depid, keycnt, size, key1, key2, key3, key4, key5,
  5212.     key6, key7, key8, depkey1, depkey2, depkey3, depkey4, depkey5,
  5213.     depkey6, depkey7, depkey8)
  5214. values (@objida, 3, @objidb, @cnt, 0, @key1a, @key2a, @key3a, @key4a, @key5a,
  5215.     @key6a, @key7a, @key8a, @key1b, @key2b, @key3b, @key4b, @key5b,
  5216.     @key6b, @key7b, @key8b)   
  5217.  
  5218. print "New common key added."
  5219.  
  5220. return (0)
  5221. go
  5222. grant execute on sp_commonkey to public
  5223. go
  5224. dump transaction master with truncate_only
  5225. go
  5226. /* configure 49.1 9/10/91 */
  5227. create procedure sp_configure
  5228. @configname varchar(20) = NULL,        /* option name to configure */
  5229. @configvalue int = NULL            /* configure value */
  5230. as
  5231.  
  5232. declare @confignum int            /* number of option to be configured */
  5233. declare @configcount int        /* number of options like @configname */
  5234.  
  5235. /*
  5236. **  If no option name is given, the procedure will just print out all the
  5237. **  options and their values.
  5238. */
  5239. if @configname is NULL
  5240. begin
  5241.     select name, minimum = low, maximum = high,
  5242.         config_value = sysconfigures.value,
  5243.         run_value = syscurconfigs.value
  5244.     from spt_values, sysconfigures, syscurconfigs
  5245.         where type = "C"
  5246.             and number *= sysconfigures.config
  5247.             and number *= syscurconfigs.config
  5248.             and number >= 0
  5249.  
  5250.     return (0)
  5251. end
  5252.  
  5253. set nocount on
  5254.  
  5255. /*
  5256. **  Use @configname and try to find the right option.
  5257. **  If there isn't just one, print appropriate diagnostics and return.
  5258. */
  5259. select @configcount = count(*)
  5260.         from spt_values
  5261.         where name like "%" + @configname + "%" and type = "C"
  5262.             and number >= 0
  5263.  
  5264. /*
  5265. **  If no option, show the user what the options are.
  5266. */
  5267. if @configcount = 0
  5268. begin
  5269.     print "Configuration option doesn't exist."
  5270.  
  5271.     /*
  5272.     ** Show the user what the options are.
  5273.     */
  5274.     select name, minimum = low, maximum = high,
  5275.         config_value = sysconfigures.value,
  5276.         run_value = syscurconfigs.value
  5277.     from spt_values, sysconfigures, syscurconfigs
  5278.         where type = "C"
  5279.             and number *= sysconfigures.config
  5280.             and number *= syscurconfigs.config
  5281.             and number >= 0
  5282.     return (0)
  5283. end
  5284.  
  5285. /*
  5286. **  If more than one option like @configname, show the duplicates and return.
  5287. */
  5288. if @configcount > 1
  5289. begin
  5290.     print "Configuration option is not unique."
  5291.  
  5292.     select duplicate_options = name
  5293.         from spt_values
  5294.             where name like "%" + @configname + "%"
  5295.                 and type = "C"
  5296.                 and number >= 0
  5297.  
  5298.     return (1)
  5299. end
  5300.  
  5301. /*
  5302. ** If @configvalue is NULL, just show the current state of the option.
  5303. */
  5304. if @configvalue = NULL
  5305. begin
  5306.     select name, minimum = low, maximum = high,
  5307.         config_value = sysconfigures.value,
  5308.         run_value = syscurconfigs.value
  5309.     from spt_values, sysconfigures, syscurconfigs
  5310.         where type = "C"
  5311.             and number *= sysconfigures.config
  5312.             and number *= syscurconfigs.config
  5313.             and name like "%" + @configname + "%"
  5314.             and number >= 0
  5315.     return (0)
  5316. end
  5317.  
  5318. /*
  5319. **  Only the SA can execute the update part of this procedure so check.
  5320. */
  5321. if suser_id() != 1
  5322. begin
  5323.     print "Only the System Administrator (SA) may change configuration parameters."
  5324.     return (1)
  5325. end
  5326.  
  5327. /*
  5328. **  Now get the configuration number.
  5329. */
  5330. select @confignum = number
  5331.     from spt_values
  5332.         where type = "C" and (@configvalue between low and high
  5333.             or @configvalue = 0)
  5334.             and name like "%" + @configname + "%"
  5335.             and number >= 0
  5336.  
  5337.  
  5338. /*
  5339. **  If this is the number of devices configuration parameter,
  5340. **  we want to make sure that it's not being set to lower than the
  5341. **  number of devices in sysdevices.
  5342. */
  5343. if @confignum = 116
  5344. begin
  5345.     /*
  5346.     **  Get the max vdevno.
  5347.     */
  5348.     declare @maxvdevno    int
  5349.     select @maxvdevno = max(convert(tinyint, substring(convert(binary(4),
  5350.         d.low), v.low, 1)))
  5351.     from master.dbo.sysdevices d, master.dbo.spt_values v
  5352.         where v.type = "E"
  5353.         and v.number = 3
  5354.  
  5355.     /*
  5356.     **  If @configvalue == 0, then set it to the current number of devices.
  5357.     */
  5358.     if @configvalue = 0
  5359.     select @configvalue = @maxvdevno + 1
  5360.     if (select low
  5361.         from master.dbo.spt_values
  5362.             where type = "C"
  5363.                 and number = 116) > @configvalue
  5364.     begin
  5365.         select @configvalue = low
  5366.             from master.dbo.spt_values
  5367.                 where type = "C"
  5368.                     and number = 116
  5369.     end
  5370.  
  5371.     if ((@maxvdevno + 1) > @configvalue)
  5372.     begin
  5373.         print "You can't set the number of devices to be smaller than the highest vdevno defined in sysdevices."
  5374.         return (1)
  5375.     end
  5376. end
  5377.  
  5378. /* 
  5379. **  If this is the number of default language, we want to make sure
  5380. **  that the new value is a valid language id in Syslanguages.
  5381. */ 
  5382. if @confignum = 124
  5383. begin
  5384.     if not exists (select *
  5385.         from syslanguages
  5386.             where langid = @configvalue)   
  5387.     begin
  5388.         /* 0 is Sybase default language, us_english */
  5389.         if @configvalue != 0
  5390.         begin
  5391.             print "You can't set the default language to a language ID that is not defined in Syslanguages."
  5392.             return (1)
  5393.         end
  5394.     end
  5395. end
  5396.  
  5397. /*
  5398. **  Although the @configname is good, @configvalue wasn't in range.
  5399. */
  5400. if @confignum is NULL
  5401. begin
  5402.     print "Configuration option value is not legal."
  5403.     return (1)
  5404. end
  5405.  
  5406. /*
  5407. **  If we're in a transaction, disallow this since it might make recovery
  5408. **  impossible.
  5409. */
  5410. if @@trancount > 0
  5411. begin
  5412.     print "Can't run sp_configure from within a transaction."
  5413.     return (1)
  5414. end
  5415.  
  5416. /*
  5417. **  Now update sysconfigure.
  5418. */
  5419. update sysconfigures
  5420.     set value = @configvalue
  5421.         where config = @confignum
  5422.  
  5423. print "Configuration option changed. Run the RECONFIGURE command to install."
  5424.  
  5425. return (0)
  5426. go
  5427. grant execute on sp_configure to public
  5428. go
  5429. /* dboption 49.1 9/10/91 */
  5430. create procedure sp_dboption
  5431. @dbname varchar(30) = NULL,        /* database name to change */
  5432. @optname varchar(20) = NULL,        /* option name to turn on/off */
  5433. @optvalue varchar(10) = NULL        /* true or false */
  5434. as
  5435.  
  5436. declare @dbid int            /* dbid of the database */
  5437. declare @dbuid int            /* id of the owner of the database */
  5438. declare @statvalue smallint        /* number of option */
  5439. declare @optcount int            /* number of options like @optname */
  5440.  
  5441. /*
  5442. **  If no @dbname given, just list the possible dboptions.
  5443. **  Only certain status bits may be set or cleared.  
  5444. **       settable                        not settable
  5445. **      ------------------------------  --------------------------
  5446. **    allow select into/bulkcopy (4)    load only (512)
  5447. **    read only (1024)        not recovered (256)
  5448. **    dbo use only (2048)        dbname has changed (16384)
  5449. **    single user (4096)        don't recover (32)
  5450. **    truncate log on checkpoint (8)
  5451. **    no checkpoint on recovery (16)
  5452. **      ALL SETTABLE OPTIONS (7196)
  5453. */
  5454. if @dbname = null
  5455. begin
  5456.     print "Settable database options."
  5457.     select database_options = name
  5458.         from spt_values
  5459.             where type = "D"
  5460.                 and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5461.                 and number > 0
  5462.         order by name
  5463.     return (0)
  5464. end
  5465.  
  5466. /*
  5467. **  Verify the database name and get the @dbid and @dbuid
  5468. */
  5469. select @dbid = dbid, @dbuid = suid
  5470.     from sysdatabases
  5471.         where name = @dbname
  5472.  
  5473. /*
  5474. **  If @dbname not found, say so and list the databases.
  5475. */
  5476. if @dbid = NULL
  5477. begin
  5478.     print "No such database -- run sp_helpdb to list databases."
  5479.     return (1)
  5480. end
  5481.  
  5482. /*
  5483. **  You can not change any of the options in master.  If the user tries to
  5484. **  do so tell them they can't.
  5485. */
  5486. if @dbid = 1
  5487. begin
  5488.     print "The 'master' database's options can not be changed."
  5489.     return (1)
  5490. end
  5491.  
  5492. /*
  5493. **  Check remaining parameters.
  5494. */
  5495. if @optname is NULL or @optvalue not in ("true", "false") or @optvalue = null
  5496. begin
  5497.     print "usage: sp_dboption [dbname, optname, {true | false}]"
  5498.     return (1)
  5499. end
  5500.  
  5501. /*
  5502. **  Use @optname and try to find the right option.
  5503. **  If there isn't just one, print appropriate diagnostics and return.
  5504. */
  5505. select @optcount = count(*)
  5506.         from spt_values
  5507.         where name like "%" + @optname + "%" and type = "D"
  5508.             and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5509.  
  5510. /*
  5511. **  If no option, show the user what the options are.
  5512. */
  5513. if @optcount = 0
  5514. begin
  5515.     print "Database option doesn't exist or can't be set by user."
  5516.     print "Run sp_dboption with no parameters to see options."
  5517.     return (0)
  5518. end
  5519.  
  5520. /*
  5521. **  If more than one option like @optname, show the duplicates and return.
  5522. */
  5523. if @optcount > 1
  5524. begin
  5525.     print "Database option is not unique."
  5526.  
  5527.     select duplicate_options = name
  5528.         from spt_values
  5529.             where name like "%" + @optname + "%"
  5530.                 and type = "D"
  5531.                 and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5532.                 and number > 0
  5533.  
  5534.     return (1)
  5535. end
  5536.  
  5537. /*
  5538. **  Only the SA or the dbo of @dbnbame can execute the update part
  5539. **  of this procedure so check.  Also the sproc must be executed in the master
  5540. **  database.
  5541. */
  5542. if suser_id() != 1 and suser_id() != @dbuid
  5543. begin
  5544.     print "Only the System Administrator (SA) or the owner of the database may set db options."
  5545.     return (1)
  5546. end
  5547. if db_name() != "master"
  5548. begin
  5549.     print "You must be in the 'master' database in order to change database options."
  5550.     return (1)
  5551. end
  5552.  
  5553. /*
  5554. **  If we're in a transaction, disallow this since it might make recovery
  5555. **  impossible.
  5556. */
  5557. if @@trancount > 0
  5558. begin
  5559.     print "Can't run sp_dboption from within a transaction."
  5560.     return (1)
  5561. end
  5562.  
  5563. /*
  5564. **  Get the number which is the bit value to set
  5565. */
  5566. select @statvalue = number
  5567.         from spt_values
  5568.         where name like "%" + @optname + "%" and type = "D"
  5569.             and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5570.             and number > 0
  5571.  
  5572.  
  5573. /*
  5574. **  Now update sysdatabases.
  5575. */
  5576. if @optvalue = "true"
  5577. begin
  5578.     /*
  5579.     **  If this the option to make the database read only,
  5580.     **  we need to do some checking first.
  5581.     **  Unless it's the master db, no one can be using it.
  5582.     **  If it's the master db, only the SA may be using it.
  5583.     */
  5584.     if (@statvalue = 1024) and (select count(*)
  5585.                     from master.dbo.sysprocesses
  5586.                         where dbid = @dbid) > 0
  5587.     begin
  5588.         print "The database is currently in use -- 'read only' option disallowed."
  5589.         return (1)
  5590.     end
  5591.  
  5592.     update sysdatabases
  5593.         set status = status | @statvalue
  5594.             where dbid = @dbid
  5595. end
  5596.  
  5597. /*
  5598. **  We want to turn it off.
  5599. */
  5600. else
  5601. begin
  5602.     update sysdatabases
  5603.         set status = status & ~@statvalue
  5604.             where dbid = @dbid
  5605. end
  5606.  
  5607. /*
  5608. **  Advise the user to run the CHECKPOINT command in the database that
  5609. **  was changed.
  5610. */
  5611. print "Run the CHECKPOINT command in the database that was changed."
  5612.  
  5613. return (0)
  5614. go
  5615. grant execute on sp_dboption to public
  5616. go
  5617. dump transaction master with truncate_only
  5618. go
  5619. /* defaultdb 49.1 9/10/91 */
  5620. create procedure sp_defaultdb
  5621. @loginame varchar(30),            /* login name of the user */
  5622. @defdb varchar(30)             /* default db for the user */
  5623. as
  5624.  
  5625. declare @suid int        /* the suid to use to qualify the change */
  5626.  
  5627. /*
  5628. **  If we're in a transaction, disallow this since it might make recovery
  5629. **  impossible.
  5630. */
  5631. if @@trancount > 0
  5632. begin
  5633.     print "Can't run sp_defaultdb from within a transaction."
  5634.     return (1)
  5635. end
  5636.  
  5637. /*
  5638. **  Check that the account exists.
  5639. */
  5640. if not exists (select *
  5641.             from syslogins
  5642.         where name = @loginame)
  5643. begin
  5644.     print "No such login exists."
  5645.     return (1)
  5646. end
  5647.  
  5648. /*
  5649. **  Check that the database name is valid.
  5650. */
  5651. if not exists (select *
  5652.             from sysdatabases
  5653.         where name = @defdb)
  5654. begin
  5655.     print "Database name not valid -- default not changed."
  5656.     return (1)
  5657. end
  5658.  
  5659. /*
  5660. **  Both the owner of the account (@loginame) and the SA can run this
  5661. **  command.  If the SA is running it, set @suid to the suid of @loginame.
  5662. */
  5663. select @suid = suser_id()
  5664. if @suid = 1
  5665. begin
  5666.     /*
  5667.     **  Get the suid for @loginame.  We know it exists because we
  5668.     **  checked above.
  5669.     */
  5670.     select @suid = suid
  5671.         from syslogins 
  5672.             where name = @loginame
  5673. end
  5674.             
  5675. /*
  5676. **  Change the database
  5677. */
  5678. update syslogins
  5679.     set dbname = @defdb
  5680.         where suid = @suid
  5681.             and name = @loginame
  5682.  
  5683. /*
  5684. **  If nothing got updated then it means that the the user tried to change
  5685. **  someone else's default.
  5686. */
  5687. if @@rowcount < 1
  5688. begin
  5689.     print "You can't change someone else's default database."
  5690.     return (1)
  5691. end
  5692.  
  5693. print "Default database changed."
  5694.  
  5695. return (0)
  5696. go
  5697. grant execute on sp_defaultdb to public
  5698. go
  5699. /* depends 49.1 9/10/91 */
  5700. create procedure sp_depends
  5701. @objname varchar(92)        /* the object we want to check */
  5702. as
  5703.  
  5704. declare @objid int            /* the id of the object we want */
  5705. declare @found_some bit            /* flag for dependencies found */
  5706.  
  5707. /*
  5708. **  Make sure the @objname is local to the current database.
  5709. */
  5710. if @objname like "%.%.%" and
  5711.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  5712. begin
  5713.     print "Object must be in your current database."
  5714.     return (1)
  5715. end
  5716.  
  5717. /*
  5718. **  See if @objname exists.
  5719. */
  5720. select @objid = object_id(@objname)
  5721. if @objid = null
  5722. begin
  5723.     print "No such object in the current database."
  5724.     return (1)
  5725. end
  5726.  
  5727. /*
  5728. **  Initialize @found_some to indicate that we haven't seen any dependencies.
  5729. */
  5730. select @found_some = 0
  5731.  
  5732. set nocount on
  5733.  
  5734. /*
  5735. **  Print out the particulars about the local dependencies.
  5736. */
  5737. if exists (select *
  5738.         from sysdepends
  5739.             where id = @objid)
  5740. begin
  5741.     print "Things the object references in the current database."
  5742.     select object = substring((s.name + "." + o.name), 1, 40),
  5743.         type = substring(v.name, 1, 16),
  5744.         updated = substring(u.name, 1, 7),
  5745.         selected = substring(w.name, 1, 8)
  5746.             from sysobjects o, master.dbo.spt_values v, sysdepends d,
  5747.                 master.dbo.spt_values u, master.dbo.spt_values w,
  5748.                 sysusers s
  5749.             where o.id = d.depid
  5750.                 and o.sysstat & 7 = v.number and v.type = 'O'
  5751.                 and u.type = 'B' and u.number = d.resultobj
  5752.                 and w.type = 'B' and w.number = d.readobj
  5753.                 and d.id = @objid
  5754.                 and o.uid = s.uid
  5755.     select @found_some = 1
  5756. end
  5757.  
  5758. /*
  5759. **  Now check for things that the objects depends upon
  5760. */
  5761. if exists (select *
  5762.         from sysdepends
  5763.             where depid = @objid)
  5764. begin
  5765.     print "Things inside the current database that reference the object."
  5766.     select distinct object = substring((s.name + "." + o.name), 1, 40),
  5767.         type = substring(v.name, 1, 16)
  5768.             from sysobjects o, master.dbo.spt_values v, sysdepends d,
  5769.                 sysusers s
  5770.             where o.id = d.id
  5771.                 and o.sysstat & 7 = v.number and v.type = 'O'
  5772.                 and d.depid = @objid
  5773.                 and o.uid = s.uid
  5774.     select @found_some = 1
  5775. end
  5776.  
  5777. /*
  5778. **  Did we find anything in sysdepends?
  5779. */
  5780. if @found_some = 0
  5781. begin
  5782.     print "Object doesn't reference any object and no objects reference it."
  5783. end
  5784.  
  5785. set nocount off
  5786.  
  5787. return (0)
  5788. go
  5789. grant execute on sp_depends to public
  5790. go
  5791. dump transaction master with truncate_only
  5792. go
  5793. /* diskdefault 49.1 9/10/91 */
  5794. create procedure sp_diskdefault
  5795. @logicalname    varchar(30),        /* logical name of the device */
  5796. @defstatus    varchar(15)        /* turn on or off */
  5797. as
  5798.  
  5799. /*
  5800. **  If we're in a transaction, disallow this since it might make recovery
  5801. **  impossible.
  5802. */
  5803. if @@trancount > 0
  5804. begin
  5805.     print "Can't run sp_diskdefault from within a transaction."
  5806.     return (1)
  5807. end
  5808.  
  5809. /*
  5810. **  Only the SA can run this sproc.
  5811. */
  5812. if suser_id() != 1
  5813. begin
  5814.     print "Only the System Administrator (SA) may execute this procedure."
  5815.     return (1)
  5816. end
  5817.  
  5818. /*
  5819. **  Make sure that a device with @logicalname exists.
  5820. */
  5821. if not exists (select *
  5822.         from master.dbo.sysdevices
  5823.             where name = @logicalname)
  5824. begin
  5825.     print "No such device exists."
  5826.     return (1)
  5827. end
  5828.  
  5829. /*
  5830. **  Make sure that is a database disk and not a dump device.
  5831. */
  5832. if exists (select *
  5833.         from master.dbo.sysdevices
  5834.             where name = @logicalname 
  5835.                 and status & 16 = 16)
  5836. begin
  5837.     print "The device name supplied is not a database disk."
  5838.     return (1)
  5839. end
  5840.  
  5841. if @defstatus = "defaulton"
  5842. begin
  5843.     update master.dbo.sysdevices
  5844.         set status = status | 1
  5845.             where name = @logicalname
  5846.     return (0)
  5847. end
  5848.  
  5849. if @defstatus = "defaultoff"
  5850. begin
  5851.     update master.dbo.sysdevices
  5852.         set status = status & ~1
  5853.             where name = @logicalname
  5854.     return (0)
  5855. end
  5856.  
  5857. /*
  5858. **  @defstatus must be "defaulton" or "defaultoff"
  5859. */
  5860. print "Usage: sp_diskdefault logicalname {defaulton | defaultoff}."
  5861.  
  5862. return (1)
  5863. go
  5864. /* dropalias 49.1 9/10/91 */
  5865. create procedure sp_dropalias
  5866. @loginame varchar(30)        /* account name of the user with the alias */
  5867. as
  5868.  
  5869. /*
  5870. **  You must be SA or the dbo to execute this sproc.
  5871. */
  5872. if suser_id() != 1 and user_id() != 1
  5873. begin
  5874.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  5875.     return (1)
  5876. end
  5877.  
  5878. /*
  5879. **  Check to make sure that the @loginame has an account.
  5880. */
  5881. if (select suser_id(@loginame)) = NULL
  5882. begin
  5883.     print "No login with the specified name exists."
  5884.     return (1)
  5885. end
  5886.  
  5887. /*
  5888. **  Delete the alias, if any, from sysalternates.
  5889. */
  5890. delete from sysalternates
  5891.     where suid = suser_id(@loginame)
  5892.  
  5893. /*
  5894. **  If nothing happened (rowcount = 0), there was no alias.
  5895. */
  5896. if @@rowcount > 0
  5897. begin
  5898.     print "Alias user dropped."
  5899.     return (0)
  5900. end
  5901. else
  5902. begin
  5903.     print "No alias for specified user exists."
  5904.     return (1)
  5905. end
  5906.  
  5907. go
  5908. grant execute on sp_dropalias to public
  5909. go
  5910. dump transaction master with truncate_only
  5911. go
  5912. /* dropdevice    49.1    9/10/91 */
  5913. create procedure sp_dropdevice
  5914. @logicalname    varchar(30)        /* logical name of the device */
  5915. as
  5916.  
  5917. /*
  5918. **  If we're in a transaction, disallow this since it might make recovery
  5919. **  impossible.
  5920. */
  5921. if @@trancount > 0
  5922. begin
  5923.     print "Can't run sp_dropdevice from within a transaction."
  5924.     return (1)
  5925. end
  5926.  
  5927. /*
  5928. **  Only the system administrator (SA) can run this command.
  5929. **  Check to make sure the executor is the sa.
  5930. */
  5931. if suser_id() != 1
  5932. begin
  5933.     print "Only the System Administrator (SA) may execute this procedure."
  5934.     return (1)
  5935. end
  5936.  
  5937. /*
  5938. **  Check and make sure that the device actually exists.
  5939. */
  5940. if not exists (select * 
  5941.         from master.dbo.sysdevices
  5942.             where name = @logicalname)
  5943. begin
  5944.     print "No such device exists -- use sp_helpdevice."
  5945.     return (1)
  5946. end
  5947.  
  5948. /*
  5949. **  Check and make sure that no database is using the device.
  5950. **  If so, print out who is using it and exit.
  5951. */
  5952. if exists (select *
  5953.     from sysdatabases, sysusages, sysdevices, spt_values
  5954.         where sysdatabases.dbid = sysusages.dbid
  5955.             and segmap >= 0
  5956.             and sysdevices.low <= size + vstart
  5957.             and sysdevices.high >= size + vstart - 1
  5958.             and sysdevices.status & 2 = 2 
  5959.             and type = "S"
  5960.             and sysdevices.name = @logicalname)
  5961. begin
  5962.     print "Device is being used by a database. You can't drop it."
  5963.  
  5964.     select database_name = substring(sysdatabases.name, 1, 23),
  5965.         device = substring(sysdevices.name, 1, 21),
  5966.         size = convert(varchar(8), size / 500) + " MB",
  5967.         usage = spt_values.name
  5968.     from sysdatabases, sysusages, sysdevices, spt_values
  5969.         where sysdatabases.dbid = sysusages.dbid
  5970.             and segmap >= 0
  5971.             and sysdevices.low <= size + vstart
  5972.             and sysdevices.high >= size + vstart - 1
  5973.             and sysdevices.status & 2 = 2 
  5974.             and type = "S"
  5975.             and sysdevices.name = @logicalname
  5976.     order by sysdatabases.name, sysdevices.name, spt_values.name,
  5977.         size
  5978.  
  5979.     return (1)
  5980. end
  5981.  
  5982. /*
  5983. **  Everything's ok so drop the device.
  5984. */
  5985. delete sysdevices
  5986.     where name = @logicalname
  5987.  
  5988. print "Device dropped."
  5989.  
  5990. return (0)
  5991. go
  5992. /* dropdumpdevice 49.1 9/10/91 */
  5993. create procedure sp_dropdumpdevice
  5994. @logicalname varchar(30)        /* name of device to drop */
  5995. as
  5996.  
  5997. /*
  5998. **  N.B.  This sproc is redundant due to the addition of sp_dropdevice.
  5999. **        However, it is still included in installmaster for backwards
  6000. **      compatibility with existing programs.
  6001. */
  6002.  
  6003. /*
  6004. **  If we're in a transaction, disallow this since it might make recovery
  6005. **  impossible.
  6006. */
  6007. if @@trancount > 0
  6008. begin
  6009.     print "Can't run sp_dropdumpdevice from within a transaction."
  6010.     return (1)
  6011. end
  6012.  
  6013. /*
  6014. **  Only the system administrator (SA) can run this command.
  6015. **  Check to make sure the executor is the sa.
  6016. */
  6017. if suser_id() != 1
  6018. begin
  6019.     print "Only the System Administrator (SA) may execute this procedure."
  6020.     return (1)
  6021. end
  6022.  
  6023. /*
  6024. **  Delete the device.  Only dump devices may be dropped.
  6025. */
  6026. delete from master.dbo.sysdevices
  6027.     where name = @logicalname
  6028.         and status & 16 = 16
  6029.  
  6030. /*
  6031. ** Check @@rowcount when it works
  6032. */
  6033. if @@rowcount > 0
  6034. begin
  6035.     print "Device dropped."
  6036.     return (0)
  6037. end
  6038. else
  6039. begin
  6040.     /*
  6041.     **  Nothing happened because the device doesn't exist or isn't
  6042.     **  a dump device.
  6043.     */
  6044.     if not exists (select *
  6045.             from sysdevices
  6046.                 where name = @logicalname)
  6047.     begin
  6048.         print "No device with specified logical name exists."
  6049.     end
  6050.     else print "Device is not a dump device and may not be dropped."
  6051.  
  6052.     return (1)
  6053. end
  6054. go
  6055. dump transaction master with truncate_only
  6056. go
  6057. /* dropgroup 49.1 9/10/91 */
  6058. create procedure sp_dropgroup
  6059. @grpname varchar(30)        /* group to be dropped */
  6060. as
  6061.  
  6062. declare @gid int        /* group id of the group to be dropped */
  6063.  
  6064. /*
  6065. **  You must be SA or the dbo to execute this sproc.
  6066. */
  6067. if suser_id() != 1 and user_id() != 1
  6068. begin
  6069.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  6070.     return (1)
  6071. end
  6072.  
  6073. /*
  6074. **  See if the group exists.
  6075. **  User ids are <= 16383 and group ids are > 16383.
  6076. */
  6077. select @gid = uid
  6078.     from sysusers
  6079.         where name = @grpname
  6080.             and (uid > 16383 or uid = 0)
  6081.  
  6082. if @gid = NULL
  6083. begin
  6084.     print "No group with the specified name exists."
  6085.     return (1)
  6086. end
  6087.  
  6088. /*
  6089. **  Can't drop the group public.
  6090. */
  6091. if @gid = 0
  6092. begin
  6093.     print "Can't drop the group 'public'."
  6094.     return (1)
  6095. end
  6096.  
  6097. /*
  6098. **  Check to see if the group owns anything.  If so, return.
  6099. */
  6100. if exists (select *
  6101.            from sysobjects
  6102.                where uid = @gid)
  6103. begin
  6104.     print "You cannot drop group because it owns objects in database."
  6105.  
  6106.     /*
  6107.     **  Show what is owned by the group.
  6108.     */
  6109.     select s.name, s.type, owner = u.name
  6110.         from sysobjects s, sysusers u
  6111.             where s.id = @gid
  6112.                 and u.uid = @gid
  6113.     return (1)
  6114. end
  6115.  
  6116.  
  6117. /*
  6118. **  Check to see that nobody is in the group.  If so, return.
  6119. */
  6120. if (select count(*)
  6121.     from sysusers
  6122.         where gid = @gid
  6123.             and uid <= 16383) != 0
  6124. begin
  6125.     print "Group has members.  It must be empty before it can be dropped."
  6126.  
  6127.     /*
  6128.     **  Show who is in the group.
  6129.     */
  6130.     select name
  6131.         from sysusers
  6132.             where gid = @gid
  6133.                 and uid <= 16383
  6134.     
  6135.     return (1)
  6136. end
  6137.  
  6138. /*
  6139. **  Drop the group.
  6140. **  Also drop any references to the group in the sysprotects table.
  6141. */
  6142. begin transaction
  6143.  
  6144.     delete from sysusers
  6145.         where uid = @gid
  6146.  
  6147.     delete from sysprotects
  6148.         where uid = @gid
  6149.  
  6150. commit transaction
  6151.  
  6152. print "Group has been dropped."
  6153.  
  6154. return (0)
  6155. go
  6156. grant execute on sp_dropgroup to public
  6157. go
  6158. /* dropkey 49.1 9/10/91 */
  6159. create procedure sp_dropkey
  6160. @keytype varchar(10),             /* type of key to drop */
  6161. @tabname varchar(92),            /* table with the key */
  6162. @deptabname varchar(92) = null        /* dependent table */
  6163. as
  6164.  
  6165. declare @id int                /* id of @tabname */
  6166. declare @uid smallint            /* owner of @tabname */
  6167. declare @depid int            /* id of related table */
  6168. declare @depuid smallint        /* owner of @related table */
  6169.  
  6170. /*
  6171. **  First make sure that the key type is ok.
  6172. */
  6173. if @keytype not in ("primary", "foreign", "common")
  6174. begin
  6175.     print "Usage: sp_dropkey {primary | foreign | common}, tabname [,deptabname]."
  6176.     print "Type must be 'primary', 'foreign', or 'common'."
  6177.     return (1)
  6178. end
  6179.  
  6180. /*
  6181. **  Check to see that the tabname is local.
  6182. */
  6183. if @tabname like "%.%.%"
  6184. begin
  6185.     if substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  6186.     begin
  6187.         print "Table or view name must be in 'current' database."
  6188.         return (1)
  6189.     end
  6190. end
  6191. if @deptabname like "%.%.%"
  6192. begin
  6193.     if substring(@deptabname, 1, charindex(".", @deptabname) - 1) != db_name()
  6194.     begin
  6195.         print "Table or view name must be in 'current' database."
  6196.         return (1)
  6197.     end
  6198. end
  6199.  
  6200. /*
  6201. **  Get the ids of the @tabname and @deptabname.
  6202. */
  6203. select @id = id, @depid = object_id(@deptabname), @uid = uid
  6204.         from sysobjects
  6205.             where id = object_id(@tabname)
  6206. if @id = null
  6207. begin
  6208.     print "The table or view named doesn't exist in the current database."
  6209.     return (1)
  6210. end
  6211.  
  6212. if @uid != user_id()
  6213. begin
  6214.     print "You must be the owner of the table or view to drop its key."
  6215.     return (1)
  6216. end
  6217.  
  6218. /*
  6219. **  If primary key, just drop it.
  6220. */
  6221. if @keytype = "primary"
  6222. begin
  6223.     delete from syskeys
  6224.         where id = @id
  6225.             and type = 1
  6226.  
  6227.     if @@rowcount = 0
  6228.     begin
  6229.         print "No primary key for the table or view exists."
  6230.         return (1)
  6231.     end
  6232.  
  6233.     else
  6234.     begin
  6235.         print "Primary key for the table or view dropped."
  6236.     end
  6237.  
  6238.     /*
  6239.     **  Check to see if there are any foreign keys dependent on the
  6240.     **  primary key.  If so -- drop them.
  6241.     */
  6242.     delete from syskeys
  6243.         where depid = @id
  6244.             and type = 2
  6245.     
  6246.     if @@rowcount != 0
  6247.     begin
  6248.         print "Dependent foreign keys were also dropped."
  6249.     end
  6250.  
  6251.     return (0)
  6252. end
  6253.  
  6254. /*
  6255. **  It's either a foreign or common key so we need to verify the
  6256. **  existence of the @deptabname.
  6257. */
  6258. if @depid = null
  6259. begin
  6260.     /*
  6261.     **  Was the @deptabname supplied?
  6262.     */
  6263.     if @deptabname = null
  6264.     begin
  6265.         print "You need to supply the dependent table or view as the third parameter."
  6266.         return (1)
  6267.     end
  6268.  
  6269.     /*
  6270.     **  It was supplied but it doesn't exist.
  6271.     */
  6272.     print "The dependent table or view doesn't exist in the current database."
  6273.     return (1)
  6274. end
  6275.  
  6276.  
  6277. /*
  6278. **  If foreign key, get rid of it.
  6279. */
  6280. if @keytype = "foreign"
  6281. begin
  6282.     /*
  6283.     **  Get rid of the foreign key entry.
  6284.     */
  6285.     delete from syskeys
  6286.         where type = 2
  6287.             and id = @id
  6288.             and depid = @depid
  6289.     if @@rowcount = 0
  6290.     begin
  6291.         print "No foreign key for the table or view exists."
  6292.         return (1)
  6293.     end
  6294.  
  6295.     print "Foreign key dropped."
  6296.  
  6297.     return (0)
  6298. end
  6299.  
  6300. /*
  6301. **  Key type must be common so just get rid of the common keys
  6302. **  with the right ids and depids.  Since whenever a common key is defined
  6303. **  it is added to both of the tables involved, we'll get rid of both of
  6304. **  those entries.
  6305. */
  6306. delete from syskeys
  6307.     where type = 3
  6308.         and id = @id
  6309.         and depid = @depid
  6310.  
  6311. if @@rowcount = 0
  6312. begin
  6313.     print "No common keys exist between the two tables or views supplied."
  6314.     return (1)
  6315. end
  6316.  
  6317. /*
  6318. **  Now get rid of the inverse common key entries.
  6319. */
  6320. delete from syskeys
  6321.     where type = 3
  6322.         and id = @depid
  6323.         and depid = @id
  6324.  
  6325. print "Common keys dropped."
  6326.  
  6327. return (0)
  6328. go
  6329. grant execute on sp_dropkey to public
  6330. go
  6331. dump transaction master with truncate_only
  6332. go
  6333. /* droplogin 49.1 9/10/91 */
  6334. create procedure sp_droplogin
  6335. @loginame varchar(30)            /* name of login to drop */
  6336. as
  6337.  
  6338. /*
  6339. **  This procedure makes a weak attempt to check for any dependencies.
  6340. **  It looks in the current database to see if the suid is used in
  6341. **  sysusers or sysalternates.  If so, a diagnostic is issued and the
  6342. **  login is not dropped.
  6343. **
  6344. **  Ideally, this procedure should check each database to see if the login
  6345. **  is a user.  However, this is not yet possible since procedures do not
  6346. **  allow parameters to be used for database or table names.
  6347. */
  6348.  
  6349. /*
  6350. **  If we're in a transaction, disallow this since it might make recovery
  6351. **  impossible.
  6352. */
  6353. if @@trancount > 0
  6354. begin
  6355.     print "Can't run sp_droplogin from within a transaction."
  6356.     return (1)
  6357. end
  6358.  
  6359. /*
  6360. **  Only the system administrator (SA) can run this command.
  6361. **  Check to make sure the executor is the sa.
  6362. */
  6363. if suser_id() != 1
  6364. begin
  6365.     print "Only the System Administrator (sa) may execute this procedure."
  6366.     return (1)
  6367. end
  6368.  
  6369. /*
  6370. **  Check sysusers.
  6371. */
  6372. if exists (select *
  6373.         from sysusers
  6374.             where suid = suser_id(@loginame))
  6375. begin
  6376.     print "User exists in current database.  Drop user before dropping login."
  6377.     select database_name = db_name(), login_name = @loginame,
  6378.         name_in_database = name
  6379.             from sysusers where suser_id(@loginame) = suid
  6380.     return (1)
  6381. end
  6382.  
  6383. /*
  6384. **  Check sysalternates.
  6385. */
  6386. if exists (select *
  6387.         from sysalternates
  6388.             where suid = suser_id(@loginame))
  6389. begin
  6390.     print "User exists as an alias in current database.  Drop alias before dropping login."
  6391.     select database_name = db_name(), login_name = @loginame,
  6392.         name_in_database = u.name
  6393.             from sysusers u, sysalternates a
  6394.                 where suser_id(@loginame) = a.suid
  6395.                     and a.altsuid = u.suid
  6396.     return (1)
  6397. end
  6398.  
  6399. /*
  6400. **  Delete the login.
  6401. */
  6402. delete from syslogins
  6403.     where name = @loginame
  6404.  
  6405. /*
  6406. ** Check @@rowcount when it works
  6407. */
  6408. if @@rowcount > 0
  6409. begin
  6410.     print "Login dropped."
  6411.     return (0)
  6412. end
  6413. else
  6414. begin
  6415.     print "No login exists with supplied name."
  6416.     return (1)
  6417. end
  6418. go
  6419. /* droptype 49.1 9/10/91 */
  6420. create procedure sp_droptype
  6421. @typename varchar(30)            /* the user type to drop */
  6422. as
  6423.  
  6424. declare @typeid smallint        /* the typeid of the usertype to drop */
  6425.  
  6426. /*
  6427. **  Initialize @typeid so we can tell if we can't find it.
  6428. */
  6429. select @typeid = 0
  6430.  
  6431. /*
  6432. **  Find the user type with @typename.  It must be a user type (usertype > 99)
  6433. **  and it must be owned by the person (or dbo) running the procedure.
  6434. */
  6435. select @typeid = usertype
  6436.     from systypes
  6437.         where (uid = user_id() or user_id() = 1)
  6438.             and name = @typename
  6439.             and usertype > 99
  6440.  
  6441. if @typeid = 0
  6442. begin
  6443.     print "The type doesn't exist or you don't own it."
  6444.     return (1)
  6445. end
  6446.  
  6447. /*
  6448. **  Check to see if the type is being used.  If it is, it can't be dropped.
  6449. */
  6450. if exists (select *
  6451.            from syscolumns
  6452.                where usertype = @typeid)
  6453. begin
  6454.     print "Type is being used. You cannot drop it."
  6455.  
  6456.     /*
  6457.     **  Show where it's being used.
  6458.     */
  6459.     select object = o.name, type = o.type, owner = u.name, column = c.name,
  6460.         datatype = t.name
  6461.     from syscolumns c, systypes t, sysusers u, sysobjects o
  6462.         where c.usertype = @typeid
  6463.             and t.usertype = @typeid
  6464.             and o.uid = u.uid
  6465.             and c.id = o.id
  6466.     order by object, column
  6467.  
  6468.     return (1)
  6469. end
  6470.  
  6471. /*
  6472. **  Everything is consistent so drop the type.
  6473. */
  6474. delete from systypes
  6475.     where usertype = @typeid
  6476.  
  6477. print "Type has been dropped."
  6478.  
  6479. return (0)
  6480. go
  6481. grant execute on sp_droptype to public
  6482. go
  6483. dump transaction master with truncate_only
  6484. go
  6485. /* dropuser 49.1 9/10/91 */
  6486. create procedure sp_dropuser
  6487. @name_in_db varchar(30)            /* user name to drop */
  6488. as
  6489.  
  6490. declare @suid int            /* suid of the user */
  6491. declare @uid int            /* uid of the user */
  6492. declare @objectcount int        /* count of objects user owns */
  6493. declare @typecount int            /* count of types user owns */
  6494. declare    @userdropped int        /* flag to indicate user was dropped */
  6495.  
  6496. /*
  6497. **  You must be SA or the dbo to execute this sproc.
  6498. */
  6499. if suser_id() != 1 and user_id() != 1
  6500. begin
  6501.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  6502.     return (1)
  6503. end
  6504.  
  6505. /*
  6506. **  See if the user exists in the database.
  6507. **  User ids are <= 16383 and group ids are > 16383.
  6508. */
  6509. select @uid = uid, @suid = suid
  6510.     from sysusers
  6511.         where name = @name_in_db
  6512.             and uid <= 16383 and uid > 0
  6513.  
  6514. /*
  6515. **  No such user so return.
  6516. */
  6517. if @uid = NULL
  6518. begin
  6519.     print "No user exists with the supplied name."
  6520.     return (1)
  6521. end
  6522.  
  6523. /*
  6524. **  Big trouble if dbo is dropped so check. Only check if uid = 1 is not enough,
  6525. **  since if SA drops a user in master database, uid is not equal to 1, but the
  6526. **  user could be the dbo of the other database(s). So check to see whether suid
  6527. **  in sysdatabases.
  6528. */
  6529. if exists (select * from sysdatabases 
  6530.         where suid = @suid) 
  6531. begin
  6532.     print "You cannot drop the 'database owner'."
  6533.     return (1)
  6534. end
  6535.  
  6536. /*
  6537. **  Trouble if guest gets dropped from master or tempdb, so check.
  6538. */
  6539. if (@name_in_db = "guest" and (db_id() = 1 or db_id() = 2))
  6540. begin
  6541.     print "You cannot drop the 'guest' user from master or tempdb."
  6542.     return(1)
  6543. end
  6544.  
  6545. /*
  6546. **  If the user owns any objects or datatypes and we're not
  6547. **  forcing the drop, return without doing anything.
  6548. */
  6549. select @objectcount = count(*)
  6550.        from sysobjects
  6551.            where uid = @uid
  6552. select @typecount = count(*)
  6553.     from systypes
  6554.         where uid = @uid
  6555.  
  6556. if @objectcount > 0 or @typecount > 0
  6557. begin
  6558.     if @objectcount > 0
  6559.     begin
  6560.         print "You cannot drop user because he or she owns objects in database."
  6561.         select name, type
  6562.             from sysobjects
  6563.                 where uid = @uid
  6564.     end
  6565.  
  6566.     if @typecount > 0
  6567.     begin
  6568.         print "You cannot drop user because he or she owns types in database."
  6569.         select user_type = a.name, physical_type = b.name
  6570.             from systypes a, systypes b
  6571.                 where a.uid = @uid
  6572.                     and a.type = b.type
  6573.                     and b.usertype < 100
  6574.     end
  6575.  
  6576.     return (1)
  6577. end
  6578.  
  6579.  
  6580. /*
  6581. **  Drop the user.
  6582. **  Also drop any references to the user in the sysprotects table.
  6583. **  If anyone is aliased to the user, drop them also.
  6584. */
  6585. begin transaction
  6586.  
  6587.     delete from sysusers
  6588.         where suid = @suid
  6589.     select @userdropped = @@rowcount
  6590.  
  6591.     delete from sysprotects
  6592.         where uid = @uid
  6593.  
  6594.     /*
  6595.     **  Drop any dependent aliases.
  6596.     */
  6597.     if exists (select *
  6598.             from sysalternates
  6599.                 where altsuid = @suid)
  6600.     begin
  6601.         delete from sysalternates
  6602.             where altsuid = @suid
  6603.  
  6604.         print "The dependent aliases were also dropped."
  6605.     end
  6606.  
  6607. if @userdropped = 1
  6608.     commit transaction
  6609. else rollback transaction
  6610.  
  6611. print "User has been dropped from current database."
  6612.  
  6613. return (0)
  6614. go
  6615. grant execute on sp_dropuser to public
  6616. go
  6617. /* dropremotelogin 49.1 9/10/91 */
  6618. create procedure sp_dropremotelogin
  6619. @remoteserver    varchar(30),        /* name of remote server */
  6620. @loginame varchar(30) = NULL,        /* user's remote name */
  6621. @remotename varchar(30) = NULL        /* user's local user name */
  6622. as
  6623.  
  6624. declare    @msg    varchar(250)
  6625. declare @suid    smallint
  6626.  
  6627. /*
  6628. **  If we're in a transaction, disallow this since it might make recovery
  6629. **  impossible.
  6630. */
  6631. if @@trancount > 0
  6632. begin
  6633.     print "Can't run sp_dropremotelogin from within a transaction."
  6634.     return (1)
  6635. end
  6636.  
  6637. /*
  6638. **  If @loginame is NULL then we want to set @suid = -1. Otherwise get
  6639. **  it real value.
  6640. */
  6641. if @loginame = null
  6642.     select @suid = -1
  6643. else select @suid = suser_id(@loginame)
  6644.  
  6645. /*
  6646. **  Delete the remote login.
  6647. */
  6648. delete master.dbo.sysremotelogins
  6649.     from master.dbo.sysremotelogins l, master.dbo.sysservers s
  6650.     where l.remoteserverid = s.srvid
  6651.         and s.srvname = @remoteserver
  6652.         and l.remoteusername = @remotename
  6653.         and l.suid = @suid
  6654.  
  6655. /*
  6656. ** Check @@rowcount when it works
  6657. */
  6658. if @@rowcount > 0
  6659. begin
  6660.     print "Remote login dropped."
  6661.     return (0)
  6662. end
  6663. else
  6664. begin
  6665.     select @msg = "There is no remote user '" + isnull(@remotename, "NULL")
  6666.         + "' mapped to local user '" + isnull(@loginame, "NULL")
  6667.         + "' from the remote server '" + @remoteserver + "'."
  6668.     print @msg
  6669.     return (1)
  6670. end
  6671. go
  6672. dump transaction master with truncate_only
  6673. go
  6674. /* dropsegment    49.1    9/10/91 */
  6675. create procedure sp_dropsegment
  6676. @segname    varchar(30),        /* segment name */
  6677. @device        varchar(30) = null    /* device name */
  6678. as
  6679.  
  6680. declare @dbuid    smallint    /* id of the owner of the database */
  6681. declare @segbit    int        /* this is the bit to turn on in sysusages */
  6682. declare    @msg    varchar(200)
  6683.  
  6684. /*
  6685. **  If we're in a transaction, disallow this since it might make recovery
  6686. **  impossible.
  6687. */
  6688. if @@trancount > 0
  6689. begin
  6690.     print "Can't run sp_dropsegment from within a transaction."
  6691.     return (1)
  6692. end
  6693.  
  6694. /*
  6695. **  You must be SA or the dbo to execute this sproc.
  6696. */
  6697. if suser_id() != 1 and user_id() != 1
  6698. begin
  6699.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  6700.     return (1)
  6701. end
  6702.  
  6703. /*
  6704. **  There are two drop cases:
  6705. **    1) dropping the segment
  6706. **    2) dropping a segment reference to a device
  6707. **  The sproc does things differently depending on which we're doing.
  6708. */
  6709.  
  6710. /*
  6711. **  Check to see if the segment exists.
  6712. */
  6713. if not exists (select *
  6714.         from syssegments
  6715.             where name = @segname)
  6716. begin
  6717.     select @msg = "There is no such segment as '" + @segname + "'."
  6718.     print @msg
  6719.     return (1)
  6720. end
  6721.  
  6722. /*
  6723. **  Get the segment number of @segname.
  6724. */
  6725. select @segbit = segment
  6726.     from syssegments 
  6727.         where name = @segname
  6728.  
  6729. /*
  6730. **  Now convert the segment number to the right bit for segmap.
  6731. */
  6732. if (@segbit < 31)
  6733.     select @segbit = power(2, @segbit)
  6734. else
  6735.     /*
  6736.     **  Since this is segment 31, power(2, 31) will overflow
  6737.     **  since segmap is an int.  We'll grab the machine-dependent
  6738.     **  bit mask from spt_values to clear the right bit.
  6739.     */
  6740.     select @segbit = low
  6741.         from master.dbo.spt_values
  6742.             where type = "E"
  6743.                 and number = 2
  6744.  
  6745. /*
  6746. **  Don't allow the segments 'default', 'system', or 'logsegment' to
  6747. **  be dropped completely.
  6748. */
  6749. if @segname in ("system", "default", "logsegment")
  6750. begin
  6751.     if @device = null
  6752.     begin
  6753.         select @msg = "Can't drop the '" + @segname
  6754.             + "' segment completely."
  6755.         print @msg
  6756.         return (1)
  6757.     end
  6758.  
  6759.     /*
  6760.     **  For the case where we are dropping the logsegment from a 
  6761.     **  device, the logic below works OK.  If this is the last
  6762.     **  device for the the segment, it won't let the segment be
  6763.     **  dropped.
  6764.     */
  6765. end
  6766.  
  6767.  
  6768. /*
  6769. **  If we are dropping the segment, check to see that the segment isn't
  6770. **  being used by anyone.
  6771. */
  6772. if @device = null
  6773. begin
  6774.  
  6775.     /*
  6776.     **  Check to see if the segment is being used.
  6777.     */
  6778.     if exists (select *
  6779.             from sysindexes i, syssegments s
  6780.                 where s.name = @segname
  6781.                     and i.segment = s.segment)
  6782.     begin
  6783.         select @msg = "The segment '" + @segname + "' is being used."
  6784.         print @msg
  6785.         return (1)
  6786.     end
  6787. end
  6788.  
  6789. /*
  6790. **  If we are dropping the segment reference, check to see that arguments are
  6791. **  reasonable.
  6792. */
  6793. if @device != null
  6794. begin
  6795.     /*
  6796.     **  Now see if the database even uses the @device
  6797.     */
  6798.     if not exists (select *
  6799.             from sysusages u, sysdevices d
  6800.                 where d.name = @device
  6801.                     and u.vstart between d.low and d.high
  6802.                     and u.dbid = db_id())
  6803.     begin
  6804.         select @msg = "The device '" + @device
  6805.             + "' is not used by the database."
  6806.         print @msg
  6807.         return (1)
  6808.     end
  6809.  
  6810.     /*
  6811.     **  Now check that the segment references the device.
  6812.     */
  6813.     if not exists (select *
  6814.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6815.                 where u.segmap & @segbit = @segbit
  6816.                 and d.name = @device
  6817.                 and d.low <= u.size + u.vstart
  6818.                 and d.high >= u.size + u.vstart - 1 
  6819.                 and u.dbid = db_id()
  6820.                 and d.status & 2 = 2)
  6821.     begin
  6822.         select @msg = "Segment '" + @segname 
  6823.             + "' does not reference device '" + @device + "'."
  6824.         print @msg
  6825.         return (1)
  6826.     end
  6827.     /*
  6828.     **  Now check to see if this is the last unique device reference for the
  6829.     **  segment.  If it is then we don't want to drop the reference.
  6830.     */
  6831.     select distinct d.name into #temptable
  6832.         from master.dbo.sysusages u, master.dbo.sysdevices d
  6833.             where u.segmap & @segbit = @segbit
  6834.                 and d.low <= u.size + u.vstart
  6835.                 and d.high >= u.size + u.vstart - 1
  6836.                 and u.dbid = db_id()
  6837.                 and d.status & 2 = 2
  6838.  
  6839.     if (select count(*) from #temptable) <= 1
  6840.     begin
  6841.         select @msg = "There is only one unique device mapping for the segment '"
  6842.             + @segname + "' -- use sp_dropsegment with no device argument."
  6843.         print @msg
  6844.         return (1)
  6845.     end
  6846.  
  6847.     drop table #temptable
  6848. end
  6849.  
  6850. /*
  6851. **  If we're dropping the segment, go ahead drop the segment and
  6852. **  clear its bit in sysusages.
  6853. */
  6854. if @device = null
  6855. begin
  6856.     /*
  6857.     **  NOTE: Don't update master..sysusages and syssegments as a xact since
  6858.     **      it could cause problems for recovery.
  6859.     */
  6860.  
  6861.     delete syssegments
  6862.         where name = @segname
  6863.  
  6864.     /*
  6865.     **  Now clear the segment from device in sysusages.
  6866.     */
  6867.     update master.dbo.sysusages
  6868.         set segmap = segmap & (~@segbit)
  6869.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6870.                 where d.low <= u.size + u.vstart
  6871.                     and d.high >= u.size + u.vstart - 1
  6872.                     and u.dbid = db_id()
  6873.                     and d.status & 2 = 2
  6874. end
  6875.  
  6876. /*
  6877. **  We want to just clear the segment reference from the device.
  6878. */
  6879. else
  6880. begin
  6881.     update master.dbo.sysusages
  6882.         set segmap = segmap & (~@segbit)
  6883.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6884.                 where d.low <= u.size + u.vstart
  6885.                     and d.high >= u.size + u.vstart - 1
  6886.                     and u.dbid = db_id()
  6887.                     and d.status & 2 = 2
  6888.                     and d.name = @device
  6889.  
  6890. end
  6891.  
  6892. /*
  6893. **  Now we need to activate the new segment map.
  6894. */
  6895. declare @dbname    varchar(30)
  6896. select @dbname = db_name()
  6897. dbcc dbrepair (@dbname, remap)
  6898.  
  6899. if @device = null
  6900.     print "Segment dropped."
  6901. else print "Segment reference to device dropped."
  6902.  
  6903. /*
  6904. **  If there are no more segment references to the device,
  6905. **  print a warning.
  6906. */
  6907. if exists (select *
  6908.         from master.dbo.sysusages
  6909.             where segmap = 0
  6910.                 and dbid = db_id())
  6911. begin
  6912.     declare @nosegs    varchar(255),    /* list of unreferenced devices */
  6913.         @curdevice    varchar(30),
  6914.         @i    int
  6915.     
  6916.     select @curdevice = min(d.name)
  6917.         from master.dbo.sysusages u, master.dbo.sysdevices d
  6918.             where d.low <= u.size + u.vstart
  6919.                 and d.high >= u.size + u.vstart - 1
  6920.                 and u.dbid = db_id()
  6921.                 and d.status & 2 = 2
  6922.                 and segmap = 0
  6923.     select @nosegs = @curdevice, @i = 1
  6924.     while (@curdevice != null)
  6925.     begin
  6926.         select @curdevice = min(d.name)
  6927.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6928.                 where d.low <= u.size + u.vstart
  6929.                     and d.high >= u.size + u.vstart - 1
  6930.                     and u.dbid = db_id()
  6931.                     and d.status & 2 = 2
  6932.                     and segmap = 0
  6933.                     and d.name > @curdevice
  6934.         if @curdevice != null
  6935.         begin
  6936.             select @nosegs = @nosegs + ", " + @curdevice
  6937.             select @i = @i + 1
  6938.         end
  6939.     end
  6940.  
  6941.     /*
  6942.     **  Get the device names.
  6943.     */
  6944.     select @msg =  "WARNING: There are no longer any segments referencing "
  6945.     if @i > 1
  6946.         select @msg = @msg + "devices '"
  6947.     else select @msg = @msg + "device '"
  6948.     select @msg = @msg + @nosegs + "'. "
  6949.     if @i > 1
  6950.         select @msg = @msg + "These devices "
  6951.     else select @msg = @msg + "This device "
  6952.     select @msg = @msg + "will no longer be used for space allocation."
  6953.     print @msg
  6954. end
  6955. return (0)
  6956. go
  6957. grant execute on sp_dropsegment to public
  6958. go
  6959. /* dropserver    49.1    9/10/91 */
  6960. create procedure sp_dropserver
  6961. @server    varchar(30),            /* server name */
  6962. @droplogins    char(10) = NULL        /* drop all related logins? */
  6963. as
  6964.  
  6965. declare    @msg    varchar(200)
  6966.  
  6967. /*
  6968. **  If we're in a transaction, disallow this since it might make recovery
  6969. **  impossible.
  6970. */
  6971. if @@trancount > 0
  6972. begin
  6973.     print "Can't run sp_dropserver from within a transaction."
  6974.     return (1)
  6975. end
  6976.  
  6977. /*
  6978. **  Check to see if the server exists.
  6979. */
  6980. if not exists (select *
  6981.         from master.dbo.sysservers
  6982.             where srvname = @server)
  6983. begin
  6984.     select @msg = "There is no such server as '" + @server + "'."
  6985.     print @msg
  6986.     return (1)
  6987. end
  6988.  
  6989. /*
  6990. **  Check to see if there are any related logins in sysremotelogins.
  6991. */
  6992. if @droplogins = NULL
  6993. begin
  6994.     if exists (select *
  6995.             from master.dbo.sysremotelogins l,
  6996.                 master.dbo.sysservers s
  6997.             where s.srvid = l.remoteserverid
  6998.                 and s.srvname = @server)
  6999.     begin
  7000.         select @msg = "There are still remote logins for the server '"
  7001.             + @server + "'."
  7002.         print @msg
  7003.         return (1)
  7004.     end
  7005. end
  7006.  
  7007. /*
  7008. **  If @droplogins is true then drop any associated logins
  7009. */
  7010. else if @droplogins = "droplogins"
  7011. begin
  7012.     begin transaction dropserver
  7013.  
  7014.         delete master.dbo.sysremotelogins
  7015.             from master.dbo.sysremotelogins l,
  7016.                 master.dbo.sysservers s
  7017.             where s.srvid = l.remoteserverid
  7018.                 and s.srvname = @server
  7019.         if @@rowcount > 0
  7020.         begin
  7021.             select @msg = "Remote logins for remote server '"
  7022.                 + @server + "' have been dropped."
  7023.             print @msg
  7024.         end
  7025.  
  7026.         delete master.dbo.sysservers
  7027.             where srvname = @server
  7028.  
  7029.         print "Server dropped."
  7030.  
  7031.     commit transaction dropserver
  7032.  
  7033.     return (0)
  7034. end
  7035.  
  7036. /*
  7037. **  Bad argument to @droplogins.
  7038. */
  7039. else
  7040. begin
  7041.     print "Usage: sp_dropserver server [, droplogins]"
  7042.     return (1)
  7043. end
  7044.         
  7045. /*
  7046. **  Drop the server.
  7047. */
  7048. delete master.dbo.sysservers
  7049.     where srvname = @server
  7050.  
  7051. print "Server dropped."
  7052.  
  7053. return (0)
  7054. go
  7055. dump transaction master with truncate_only
  7056. go
  7057. /* extendsegment    49.1    9/10/91 */
  7058. create procedure sp_extendsegment
  7059. @segname    varchar(30),    /* segment name */
  7060. @devname    varchar(30)    /* device name to put segment on */
  7061. as
  7062.  
  7063. declare @dbuid    smallint    /* id of the owner of the database */
  7064. declare @segbit    int        /* this is the bit to turn on in sysusages */
  7065. declare    @msg    char(100)
  7066. declare @retcode    int
  7067.  
  7068. /*
  7069. **  If we're in a transaction, disallow this since it might make recovery
  7070. **  impossible.
  7071. */
  7072. if @@trancount > 0
  7073. begin
  7074.     print "Can't run sp_extendsegment from within a transaction."
  7075.     return (1)
  7076. end
  7077.  
  7078. /*
  7079. **  You must be SA or the dbo to execute this sproc.
  7080. */
  7081. if suser_id() != 1 and user_id() != 1
  7082. begin
  7083.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  7084.     return (1)
  7085. end
  7086.  
  7087. /*
  7088. **  Check to see if the segment exists.
  7089. */
  7090. if not exists (select *
  7091.         from syssegments
  7092.             where name = @segname)
  7093. begin
  7094.     select @msg = "There is no such segment as '" + @segname + "'."
  7095.     print @msg
  7096.     return (1)
  7097. end
  7098.  
  7099. /*
  7100. **  See if the device exists.
  7101. */
  7102. if not exists (select *
  7103.         from master.dbo.sysdevices
  7104.             where name like @devname)
  7105. begin
  7106.     print "No such device exists -- run sp_helpdb to list the devices for the current database."
  7107.     return (1)
  7108. end
  7109.  
  7110. /*
  7111. **  Now see if the @dbname uses the @devname
  7112. */
  7113. if not exists (select *
  7114.         from master.dbo.sysusages u, master.dbo.sysdevices d
  7115.             where d.name = @devname
  7116.                 and u.vstart between d.low and d.high
  7117.                 and u.dbid = db_id())
  7118. begin
  7119.     print "The specified device is not used by the database."
  7120.     return (1)
  7121. end
  7122.  
  7123. /*
  7124. **  Check to see if the device is marked as a log device.
  7125. **  If so, print error.
  7126. */
  7127. if exists (select *
  7128.         from master.dbo.sysusages u, master.dbo.sysdevices d
  7129.             where d.name = @devname
  7130.                 and u.vstart between d.low and d.high
  7131.                 and u.dbid = db_id()
  7132.                 and segmap = 4)
  7133. begin
  7134.     select @msg = "'" + @devname + "' is reserved exclusively as a log device."
  7135.     print @msg
  7136.     return (1)
  7137. end
  7138.  
  7139. /*
  7140. ** Check for valid identifier.
  7141. */
  7142. execute @retcode = sp_validname @segname
  7143. if (@retcode != 0)
  7144. begin
  7145.     select @msg = "'" + @segname + "' is not a valid identifier."
  7146.     print @msg
  7147.     return (1)
  7148. end
  7149.  
  7150. /*
  7151. **  Now go ahead and define the new segment and add it to the segmap
  7152. **  of sysusages.
  7153. **  NOTE: Don't update master..sysusages and syssegments as a xact since
  7154. **      it could cause problems for recovery.
  7155. */
  7156.  
  7157. /*
  7158. **  Get the segment number for @segname.
  7159. */
  7160. select @segbit = segment
  7161.     from syssegments
  7162.         where name = @segname
  7163.  
  7164. /*
  7165. **  If @segbit is 2 then it's the logsegment and the logsegment
  7166. **  doesn't share devices with other segments.  In this case,
  7167. **  don't OR the @segbit in but just set segmap to 4.
  7168. */
  7169. if @segbit = 2
  7170. begin
  7171.     update master.dbo.sysusages
  7172.         set segmap = 4
  7173.             from master.dbo.sysusages u,
  7174.                 master.dbo.sysdevices d
  7175.             where d.name = @devname
  7176.                 and u.vstart between d.low and d.high
  7177.                 and u.dbid = db_id()
  7178. end
  7179.  
  7180. else
  7181. begin
  7182.     if (@segbit < 31)
  7183.         select @segbit = power(2, @segbit)
  7184.     else
  7185.         /*
  7186.         **  Since this is segment 31, power(2, 31) will
  7187.         **  overflow since segmap is an int.  We'll grab the
  7188.         **  machine-dependent bit mask from spt_values to set
  7189.         **  the right bit.
  7190.         */
  7191.         select @segbit = low
  7192.             from master.dbo.spt_values
  7193.                 where type = "E"
  7194.                     and number = 2
  7195.     /*
  7196.     **  Add the segment to @devname in sysusages.
  7197.     */
  7198.     update master.dbo.sysusages
  7199.         set segmap = segmap | @segbit
  7200.             from master.dbo.sysusages u,
  7201.                 master.dbo.sysdevices d
  7202.             where d.name = @devname
  7203.                 and u.vstart between d.low and d.high
  7204.                 and u.dbid = db_id()
  7205. end
  7206.  
  7207. /*
  7208. **  Now we need to activate the new segment map.
  7209. */
  7210. declare @dbname    varchar(30)
  7211. select @dbname = db_name()
  7212. dbcc dbrepair (@dbname, remap)
  7213. print "Segment extended."
  7214.  
  7215. /*
  7216. **  Get the segment number for @segname.
  7217. */
  7218. select @segbit = segment
  7219.     from syssegments
  7220.         where name = @segname
  7221. /*
  7222. **  If we are extending the logsegment, print out a message about what it
  7223. **  means to do so.
  7224. */
  7225. if @segbit = 2
  7226. begin
  7227.     select @msg = "Device '" + @devname + "' is now exclusively used by '"
  7228.         + @segname + "'."
  7229.     print @msg
  7230. end
  7231.  
  7232. return (0)
  7233. go
  7234. grant execute on sp_extendsegment to public
  7235. go
  7236. /* fixindex 49.1 9/10/91 */
  7237. create procedure sp_fixindex
  7238.         @dbname        varchar(30),
  7239.         @tabname    varchar(30),          /* system table name */
  7240.         @indid        int            /* index id value    */
  7241. as
  7242.     /*
  7243.     **    Description:    allow the SA to force a drop and then a
  7244.     **            create index on system catalogs.
  7245.     **
  7246.     **    Usage:        sp_fixindex  database, systemcatalog, ind_id
  7247.     **
  7248.     **    Note:        before using this procedure the database has to
  7249.     **            be in single user mode. The sp_dboption has to
  7250.     **            be used for user databases, and update of
  7251.     **            sysdatabases for master.
  7252.     **    
  7253.     **    History:    3/28/88        (robbie)
  7254.     */
  7255.  
  7256.     declare    @ramboix smallint            /* mask for rambo indx*/
  7257.     declare @badlist  varchar(255)   /* name list of all tables that should
  7258.                           not have any indexes on them */
  7259.     /*
  7260.     **    Initialize name lists. These will be used in catching
  7261.     **    any attempts to fix indexes on fake tables or tables
  7262.     **    that have no indexes on them.
  7263.     */
  7264.     select @badlist = "syslocks, sysprocesses, syscurconfig, 
  7265.                syssegments, syslogckp"
  7266.  
  7267.     /*
  7268.     **    Make sure we are "fixing" a system catalog.
  7269.     */
  7270.     if @tabname NOT LIKE  "sys%"
  7271.     begin
  7272.         print "Procedure should be used on system tables only."
  7273.         return (1)
  7274.     end
  7275.  
  7276.     /*
  7277.     **    Make sure that we are doing this on somenthing that 
  7278.     **    has indexes (or real tables).
  7279.     */
  7280.     if @badlist LIKE  "%" + @tabname + "%"
  7281.     begin
  7282.         print "Cannot re-create index on this table."
  7283.         return (1)
  7284.     end
  7285.  
  7286.     /*
  7287.     **    At this point we are sure that the table has an index
  7288.     **    of requested type, so all there is to be done is to
  7289.     **    set the ramboix bit for this object unless we try to
  7290.     **    fix Sysobjects.
  7291.     */
  7292.     if @tabname != "sysobjects"
  7293.     begin
  7294.         /* Use decimal integer equivalent of hex 1000 to avoid
  7295.         ** byte order issues.
  7296.         */
  7297.         select @ramboix = 4096
  7298.         update sysobjects set sysstat = sysstat | @ramboix
  7299.         where name = @tabname
  7300.  
  7301.         checkpoint
  7302.     end
  7303.  
  7304.     /*
  7305.     **    Call dbcc to do the job.
  7306.     */
  7307.     dbcc dbrepair(@dbname, REPAIRINDEX, @tabname, @indid)
  7308.  
  7309.     /*
  7310.     **    Processing successfully done, so turn off the ramboix
  7311.     **    bit if not sysobjects, and tell the user that all is fine.
  7312.     */
  7313.     if @tabname != "sysobjects"
  7314.     begin
  7315.         update sysobjects set sysstat = sysstat & ~@ramboix
  7316.         where name = @tabname
  7317.      
  7318.         checkpoint
  7319.     end
  7320.  
  7321.     return (0)
  7322. go
  7323. dump transaction master with truncate_only
  7324. go
  7325. /* foreignkey    49.1    9/10/91 */
  7326. create procedure sp_foreignkey 
  7327. @tabname  varchar(92),        /* name of table that we are doing */
  7328. @pktabname varchar(92),        /* name of table with primary key */
  7329. @col1     varchar(30),         /* column name of table we are doing */
  7330. @col2     varchar(30) = NULL,
  7331. @col3     varchar(30) = NULL,
  7332. @col4     varchar(30) = NULL,
  7333. @col5     varchar(30) = NULL,
  7334. @col6     varchar(30) = NULL,
  7335. @col7     varchar(30) = NULL,
  7336. @col8     varchar(30) = NULL
  7337. as
  7338.  
  7339. declare @objida int        /* id of table we are doing */
  7340. declare @objidb int        /* id of table with primary key */
  7341. declare @uid smallint        /* id of owner of the talbe */
  7342. declare @cnt    int        /* how many columns are in foreign key */
  7343.  
  7344. declare @pkey1 int        /* colids of the foreign key */
  7345. declare @pkey2 int
  7346. declare @pkey3 int
  7347. declare @pkey4 int
  7348. declare @pkey5 int
  7349. declare @pkey6 int
  7350. declare @pkey7 int
  7351. declare @pkey8 int
  7352.  
  7353. declare @fkey1 int        /* colids of the primary key */
  7354. declare @fkey2 int
  7355. declare @fkey3 int
  7356. declare @fkey4 int
  7357. declare @fkey5 int
  7358. declare @fkey6 int
  7359. declare @fkey7 int
  7360. declare @fkey8 int
  7361.  
  7362. /*
  7363. **  Check to see that the tabname is local.
  7364. */
  7365. if @tabname like "%.%.%"
  7366. begin
  7367.     if substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  7368.     begin
  7369.         print "Table or view name must be in 'current' database."
  7370.         return (1)
  7371.     end
  7372. end
  7373. if @pktabname like "%.%.%"
  7374. begin
  7375.     if substring(@pktabname, 1, charindex(".", @pktabname) - 1) != db_name()
  7376.     begin
  7377.         print "Table or view name must be in 'current' database."
  7378.         return (1)
  7379.     end
  7380. end
  7381.  
  7382. /*
  7383. **  See if we can find the objects.  They must be a system table, user table,
  7384. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the 
  7385. **  object type is -- it's more reliable than using sysobjects.type which
  7386. **  could change.
  7387. */
  7388. select @objida = id, @uid = uid
  7389.     from sysobjects
  7390.         where id = object_id(@tabname)
  7391.             and (sysstat & 7 = 1        /* system table */
  7392.                 or sysstat & 7 = 2    /* view */
  7393.                 or sysstat & 7 = 3)    /* user table */
  7394. /*
  7395. **  If either of the tables don't exist, quit.
  7396. */
  7397. if @objida is NULL
  7398. begin
  7399.     print "Foreign key table doesn't exist."
  7400.     return (1)
  7401. end 
  7402. select @objidb = id
  7403.     from sysobjects
  7404.         where id = object_id(@pktabname)
  7405.             and (sysstat & 7 = 1
  7406.                 or sysstat & 7 = 2
  7407.                 or sysstat & 7 = 3)
  7408. if @objidb is NULL
  7409. begin
  7410.     print "Primary key table doesn't exist."
  7411.     return (1)
  7412. end 
  7413.  
  7414. /*
  7415. **  Only the owner of the table can define it's foreign keys.
  7416. */
  7417. if @uid != user_id()
  7418. begin
  7419.     print "Only the owner of the table may define a foreign key."
  7420.     return (1)
  7421. end
  7422.  
  7423. /*
  7424. **  Now check to see that the foreign key columns exist.
  7425. */
  7426. select @cnt = 1, @fkey1 = colid
  7427.     from syscolumns
  7428.         where name = @col1 
  7429.             and id = @objida
  7430. if @fkey1 = NULL
  7431. begin
  7432.     print "The table has no such first column."
  7433.     return (1)
  7434. end
  7435.  
  7436. if @col2 != NULL
  7437. begin
  7438.     select @cnt = @cnt + 1, @fkey2 = colid
  7439.         from syscolumns
  7440.             where name = @col2 
  7441.                 and id = @objida
  7442.     if @fkey2 is NULL
  7443.     begin
  7444.         print "The table has no such second column."
  7445.         return (1)
  7446.     end
  7447. end
  7448. else goto foreign_ok
  7449.  
  7450. if @col3 != NULL
  7451. begin
  7452.     select @cnt = @cnt + 1, @fkey3 = colid
  7453.         from syscolumns
  7454.             where name = @col3 
  7455.                 and id = @objida
  7456.     if @fkey3 is NULL
  7457.     begin
  7458.         print "The table has no such third column."
  7459.         return (1)
  7460.     end
  7461. end
  7462. else goto foreign_ok
  7463.  
  7464. if @col4 != NULL
  7465. begin
  7466.     select @cnt = @cnt + 1, @fkey4 = colid
  7467.         from syscolumns
  7468.             where name = @col4 
  7469.                 and id = @objida
  7470.     if @fkey4 is NULL
  7471.     begin
  7472.         print "The table has no such fourth column."
  7473.         return (1)
  7474.     end
  7475. end
  7476. else goto foreign_ok
  7477.  
  7478. if @col5 != NULL
  7479. begin
  7480.     select @cnt = @cnt + 1, @fkey5 = colid
  7481.         from syscolumns
  7482.             where name = @col5 
  7483.                 and id = @objida
  7484.     if @fkey5 is NULL
  7485.     begin
  7486.         print "The table has no such fifth column."
  7487.         return (1)
  7488.     end
  7489. end
  7490. else goto foreign_ok
  7491.  
  7492. if @col6 != NULL
  7493. begin
  7494.     select @cnt = @cnt + 1, @fkey6 = colid
  7495.         from syscolumns
  7496.             where name = @col6 
  7497.                 and id = @objida
  7498.     if @fkey6 is NULL
  7499.     begin
  7500.         print "The table has no such sixth column."
  7501.         return (1)
  7502.     end
  7503. end
  7504. else goto foreign_ok
  7505.  
  7506. if @col7 != NULL
  7507. begin
  7508.     select @cnt = @cnt + 1, @fkey7 = colid
  7509.         from syscolumns
  7510.             where name = @col7 
  7511.                 and id = @objida
  7512.     if @fkey7 is NULL
  7513.     begin
  7514.         print "The table has no such seventh column."
  7515.         return (1)
  7516.     end
  7517. end
  7518. else goto foreign_ok
  7519.  
  7520. if @col8 != NULL
  7521. begin
  7522.     select @cnt = @cnt + 1, @fkey8 = colid
  7523.         from syscolumns
  7524.             where name = @col8 
  7525.                 and id = @objida
  7526.     if @fkey8 is NULL
  7527.     begin
  7528.         print "The table has no such eighth column."
  7529.         return (1)
  7530.     end
  7531. end
  7532.  
  7533.  
  7534. /*
  7535. **  If we made it this far then all the columns for the foreign key are ok.
  7536. */
  7537. foreign_ok:
  7538.  
  7539. /*
  7540. **  Now let's check out the primary key that the foreign key is on.
  7541. **  There must be the same number of columns in the key and the
  7542. **  base types of the columns must agree.
  7543. */
  7544. select @pkey1 = key1, @pkey2 = key2, @pkey3 = key3, @pkey4 = key4,
  7545.     @pkey5 = key5, @pkey6 = key6, @pkey7 = key7, @pkey8 = key8
  7546.         from syskeys
  7547.     where id = @objidb
  7548.         and type = 1
  7549.         and keycnt = @cnt
  7550.  
  7551. /*
  7552. **  If @pkey1 is null then there is no such primary key or the number of
  7553. **  columns in the primary key are not the same as the number of columns
  7554. **  in the foreign key.
  7555. */
  7556. if @pkey1 = NULL
  7557. begin
  7558.     if exists (select *
  7559.             from syskeys
  7560.         where id = @objidb
  7561.             and type = 1)
  7562.     begin
  7563.         print "Primary key does not exist with the same number of columns as the foreign key."
  7564.     end
  7565.     else
  7566.     begin
  7567.         print "Primary key does not exist."
  7568.     end
  7569.     return (1)
  7570. end
  7571.  
  7572. /*
  7573. **  Since we have the right number of columns in the foreign and primary keys,
  7574. **  check that there base datatypes agree.
  7575. */
  7576. if not exists (select *
  7577.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7578.             master.dbo.spt_values z
  7579.         where a.colid = @fkey1
  7580.             and a.id = @objida
  7581.             and b.colid = @pkey1
  7582.             and b.id = @objidb
  7583.             and y.type = "J"
  7584.             and a.type = y.low
  7585.             and z.type = "J"
  7586.             and b.type = z.low
  7587.             and y.number = z.number)
  7588. begin
  7589.     print "Datatypes of the first column in the keys are different."
  7590.     return (1)
  7591. end
  7592.  
  7593. if @fkey2 != NULL and not exists (select *
  7594.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7595.             master.dbo.spt_values z
  7596.         where a.colid = @fkey2
  7597.             and a.id = @objida
  7598.             and b.colid = @pkey2
  7599.             and b.id = @objidb
  7600.             and y.type = "J"
  7601.             and a.type = y.low
  7602.             and z.type = "J"
  7603.             and b.type = z.low
  7604.             and y.number = z.number)
  7605. begin
  7606.     print "Datatypes of the second column in the keys are different."
  7607.     return (1)
  7608. end
  7609. else goto doinsert
  7610.  
  7611. if @fkey3 != NULL and not exists (select *
  7612.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7613.             master.dbo.spt_values z
  7614.         where a.colid = @fkey3
  7615.             and a.id = @objida
  7616.             and b.colid = @pkey3
  7617.             and b.id = @objidb
  7618.             and y.type = "J"
  7619.             and a.type = y.low
  7620.             and z.type = "J"
  7621.             and b.type = z.low
  7622.             and y.number = z.number)
  7623. begin
  7624.     print "Datatypes of the third column in the keys are different."
  7625.     return (1)
  7626. end
  7627. else goto doinsert
  7628.  
  7629. if @fkey4 != NULL and not exists (select *
  7630.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7631.             master.dbo.spt_values z
  7632.         where a.colid = @fkey4
  7633.             and a.id = @objida
  7634.             and b.colid = @pkey4
  7635.             and b.id = @objidb
  7636.             and y.type = "J"
  7637.             and a.type = y.low
  7638.             and z.type = "J"
  7639.             and b.type = z.low
  7640.             and y.number = z.number)
  7641. begin
  7642.     print "Datatypes of the fourth column in the keys are different."
  7643.     return (1)
  7644. end
  7645.  
  7646. if @fkey5 != NULL and not exists (select *
  7647.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7648.             master.dbo.spt_values z
  7649.         where a.colid = @fkey5
  7650.             and a.id = @objida
  7651.             and b.colid = @pkey5
  7652.             and b.id = @objidb
  7653.             and y.type = "J"
  7654.             and a.type = y.low
  7655.             and z.type = "J"
  7656.             and b.type = z.low
  7657.             and y.number = z.number)
  7658. begin
  7659.     print "Datatypes of the fifth column in the keys are different."
  7660.     return (1)
  7661. end
  7662. else goto doinsert
  7663.  
  7664. if @fkey6 != NULL and not exists (select *
  7665.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7666.             master.dbo.spt_values z
  7667.         where a.colid = @fkey6
  7668.             and a.id = @objida
  7669.             and b.colid = @pkey6
  7670.             and b.id = @objidb
  7671.             and y.type = "J"
  7672.             and a.type = y.low
  7673.             and z.type = "J"
  7674.             and b.type = z.low
  7675.             and y.number = z.number)
  7676. begin
  7677.     print "Datatypes of the sixth column in the keys are different."
  7678.     return (1)
  7679. end
  7680. else goto doinsert
  7681.  
  7682. if @fkey7 != NULL and not exists (select *
  7683.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7684.             master.dbo.spt_values z
  7685.         where a.colid = @fkey7
  7686.             and a.id = @objida
  7687.             and b.colid = @pkey7
  7688.             and b.id = @objidb
  7689.             and y.type = "J"
  7690.             and a.type = y.low
  7691.             and z.type = "J"
  7692.             and b.type = z.low
  7693.             and y.number = z.number)
  7694. begin
  7695.     print "Datatypes of the seventh column in the keys are different."
  7696.     return (1)
  7697. end
  7698. else goto doinsert
  7699.  
  7700. if @fkey8 != NULL and not exists (select *
  7701.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7702.             master.dbo.spt_values z
  7703.         where a.colid = @fkey8
  7704.             and a.id = @objida
  7705.             and b.colid = @pkey8
  7706.             and b.id = @objidb
  7707.             and y.type = "J"
  7708.             and a.type = y.low
  7709.             and z.type = "J"
  7710.             and b.type = z.low
  7711.             and y.number = z.number)
  7712. begin
  7713.     print "Datatypes of the eighth column in the keys are different."
  7714.     return (1)
  7715. end
  7716.  
  7717. /*
  7718. **  Everything is consistent so add the foreign key to syskeys.
  7719. */
  7720. doinsert:
  7721.  
  7722. insert syskeys (id, type, depid, keycnt, size, key1, key2, key3, key4, key5,
  7723.     key6, key7, key8, depkey1, depkey2, depkey3, depkey4, depkey5,
  7724.     depkey6, depkey7, depkey8)
  7725. values (@objida, 2, @objidb, @cnt, 0, @fkey1, @fkey2, @fkey3, @fkey4, @fkey5,
  7726.     @fkey6, @fkey7, @fkey8, @pkey1, @pkey2, @pkey3, @pkey4, @pkey5,
  7727.     @pkey6, @pkey7, @pkey8)   
  7728.  
  7729. print "New foreign key added."
  7730.  
  7731. return (0)
  7732. go
  7733. grant execute on sp_foreignkey to public
  7734. go
  7735. if exists (select *
  7736.     from sysobjects
  7737.         where sysstat & 7 = 4
  7738.             and name = 'sp_helpdb')
  7739. begin
  7740.     drop procedure sp_helpdb
  7741. end
  7742. go
  7743. /* helpdb 49.1 9/10/91 */
  7744. create procedure sp_helpdb
  7745. @dbname varchar(30) = NULL            /* database name to change */
  7746. as
  7747.  
  7748. declare @showdev    bit
  7749. declare @allopts    int
  7750.  
  7751. set nocount on
  7752.  
  7753. /*
  7754. **  If no database name given, get 'em all.
  7755. */
  7756. if @dbname = null
  7757.     select @dbname = "%", @showdev = 0
  7758. else select @showdev = 1
  7759.  
  7760. /*
  7761. **  Sure the database exists
  7762. */
  7763. if not exists (select *
  7764.     from sysdatabases
  7765.         where name like @dbname)
  7766. begin
  7767.     print "No such database exists."
  7768.     return (1)
  7769. end
  7770.  
  7771. /*
  7772. **  Set allopts to be the sum of all possible user-settable database status
  7773. **  bits.
  7774. */
  7775. select @allopts = 7196
  7776.  
  7777. /*
  7778. **  Since we examine the status bits in sysdatabase and turn them
  7779. **  into english, we need a temporary table to build the descriptions.
  7780. */
  7781. create table #spdbdesc
  7782. (
  7783.     dbid    smallint null,
  7784.     dbdesc    varchar(102) null
  7785. )
  7786.  
  7787. /*
  7788. **  Initialize #spdbdesc from sysdatabases
  7789. */
  7790. insert into #spdbdesc (dbid)
  7791.         select dbid 
  7792.             from sysdatabases
  7793.                 where name like @dbname
  7794.  
  7795. /*
  7796. **  Now for each dbid in #spdbdesc, build the database status
  7797. **  description.
  7798. */
  7799. declare @curdbid smallint        /* the one we're currently working on */
  7800. declare @dbdesc varchar(102)        /* the total description for the db */
  7801. declare @bitdesc varchar(30)        /* the bit description for the db */
  7802.  
  7803. /*
  7804. **  Set @curdbid to the first dbid.
  7805. */
  7806. select @curdbid = min(dbid)
  7807.     from #spdbdesc
  7808. while @curdbid != NULL
  7809. begin
  7810.     /*
  7811.     **  Initialize @dbdesc.
  7812.     */
  7813.     select @dbdesc = ""
  7814.  
  7815.     /*
  7816.     **  Check select into/bulk copy bit (4)
  7817.     */
  7818.     select @bitdesc = null
  7819.     select @bitdesc = v.name
  7820.         from spt_values v, sysdatabases d
  7821.             where d.dbid = @curdbid
  7822.                 and v.type = "D"
  7823.                 and d.status & v.number = 4
  7824.                 and v.number != @allopts       /* all options */
  7825.     if @bitdesc != null
  7826.     begin
  7827.         if @dbdesc != ""
  7828.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7829.         else select @dbdesc = @bitdesc
  7830.     end
  7831.  
  7832.     /*
  7833.     **  Check no checkpoint on recovery bit (16)
  7834.     */
  7835.     select @bitdesc = null
  7836.     select @bitdesc = v.name
  7837.         from spt_values v, sysdatabases d
  7838.             where d.dbid = @curdbid
  7839.                 and v.type = "D"
  7840.                 and d.status & v.number = 16
  7841.                 and v.number != @allopts /* all options */
  7842.     if @bitdesc != null
  7843.     begin
  7844.         if @dbdesc != ""
  7845.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7846.         else select @dbdesc = @bitdesc
  7847.     end
  7848.  
  7849.     /*
  7850.     **  Check single user bit (4096)
  7851.     */
  7852.     select @bitdesc = null
  7853.     select @bitdesc = v.name
  7854.         from spt_values v, sysdatabases d
  7855.             where d.dbid = @curdbid
  7856.                 and v.type = "D"
  7857.                 and d.status & v.number = 4096
  7858.                 and v.number != @allopts       /* all options */
  7859.     if @bitdesc != null
  7860.     begin
  7861.         if @dbdesc != ""
  7862.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7863.         else select @dbdesc = @bitdesc
  7864.     end
  7865.  
  7866.     /*
  7867.     **  Check dbo only bit (2048)
  7868.     */
  7869.     select @bitdesc = null
  7870.     select @bitdesc = v.name
  7871.         from spt_values v, sysdatabases d
  7872.             where d.dbid = @curdbid
  7873.                 and v.type = "D"
  7874.                 and d.status & v.number = 2048
  7875.                 and v.number != @allopts       /* all options */
  7876.     if @bitdesc != null
  7877.     begin
  7878.         if @dbdesc != ""
  7879.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7880.         else select @dbdesc = @bitdesc
  7881.     end
  7882.  
  7883.     /*
  7884.     **  Check read only bit (1024)
  7885.     */
  7886.     select @bitdesc = null
  7887.     select @bitdesc = v.name
  7888.         from spt_values v, sysdatabases d
  7889.             where d.dbid = @curdbid
  7890.                 and v.type = "D"
  7891.                 and d.status & v.number = 1024
  7892.                 and v.number != @allopts       /* all options */
  7893.     if @bitdesc != null
  7894.     begin
  7895.         if @dbdesc != ""
  7896.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7897.         else select @dbdesc = @bitdesc
  7898.     end
  7899.  
  7900.     /*
  7901.     **  Check load only bit (512)
  7902.     */
  7903.     select @bitdesc = null
  7904.     select @bitdesc = v.name
  7905.         from spt_values v, sysdatabases d
  7906.             where d.dbid = @curdbid
  7907.                 and v.type = "D"
  7908.                 and d.status & v.number = 512
  7909.                 and v.number != @allopts       /* all options */
  7910.     if @bitdesc != null
  7911.     begin
  7912.         if @dbdesc != ""
  7913.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7914.         else select @dbdesc = @bitdesc
  7915.     end
  7916.  
  7917.     /*
  7918.     **  Check not recovered only bit (256)
  7919.     */
  7920.     select @bitdesc = null
  7921.     select @bitdesc = v.name
  7922.         from spt_values v, sysdatabases d
  7923.             where d.dbid = @curdbid
  7924.                 and v.type = "D"
  7925.                 and d.status & v.number = 256
  7926.                 and v.number != @allopts       /* all options */
  7927.     if @bitdesc != null
  7928.     begin
  7929.         if @dbdesc != ""
  7930.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7931.         else select @dbdesc = @bitdesc
  7932.     end
  7933.  
  7934.     /*
  7935.     **  Check don't recover bit (32)
  7936.     */
  7937.     select @bitdesc = null
  7938.     select @bitdesc = v.name
  7939.         from spt_values v, sysdatabases d
  7940.             where d.dbid = @curdbid
  7941.                 and v.type = "D"
  7942.                 and d.status & v.number = 32
  7943.                 and v.number != @allopts       /* all options */
  7944.     if @bitdesc != null
  7945.     begin
  7946.         if @dbdesc != ""
  7947.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7948.         else select @dbdesc = @bitdesc
  7949.     end
  7950.  
  7951.     /*
  7952.     **  Check truncate log on checkpoint bit (8)
  7953.     */
  7954.     select @bitdesc = null
  7955.     select @bitdesc = v.name
  7956.         from spt_values v, sysdatabases d
  7957.             where d.dbid = @curdbid
  7958.                 and v.type = "D"
  7959.                 and d.status & v.number = 8
  7960.                 and v.number != @allopts       /* all options */
  7961.     if @bitdesc != null
  7962.     begin
  7963.         if @dbdesc != ""
  7964.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7965.         else select @dbdesc = @bitdesc
  7966.     end
  7967.  
  7968.     /*
  7969.     **  If not flags are set, say so.
  7970.     */
  7971.     if @dbdesc = ""
  7972.     begin
  7973.         select @dbdesc = "no options set"
  7974.     end
  7975.  
  7976.     /*
  7977.     **  Save the description.
  7978.     */
  7979.     update #spdbdesc
  7980.         set dbdesc = @dbdesc
  7981.             from #spdbdesc
  7982.                 where dbid = @curdbid
  7983.  
  7984.     /*
  7985.     **  Now get the next, if any dbid.
  7986.     */
  7987.     select @curdbid = min(dbid)
  7988.         from #spdbdesc
  7989.             where dbid > @curdbid
  7990. end 
  7991.  
  7992. /*
  7993. **  Now #spdbdesc is complete so we can print out the db info
  7994. */
  7995. select distinct name = substring(sysdatabases.name, 1, 24),
  7996.         db_size = substring(convert(varchar(10),
  7997.             round(
  7998.             (spt_values.low * convert(float, sum(sysusages.size)))
  7999.             / 1048576, 0))
  8000.             + " " + "MB", 1, 8),
  8001.         owner = substring(syslogins.name, 1, 24),
  8002.         dbid = sysdatabases.dbid,
  8003.         created = convert(char(11), sysdatabases.crdate),
  8004.         status = #spdbdesc.dbdesc
  8005.             from sysdatabases, syslogins, sysusages, #spdbdesc,
  8006.                 spt_values
  8007.         where sysdatabases.dbid = #spdbdesc.dbid
  8008.             and sysdatabases.suid = syslogins.suid
  8009.             and #spdbdesc.dbid = sysusages.dbid
  8010.             and spt_values.type = "E"
  8011.             and spt_values.number = 1
  8012.         group by #spdbdesc.dbid
  8013.         having sysdatabases.dbid = #spdbdesc.dbid
  8014.             and sysdatabases.suid = syslogins.suid
  8015.             and #spdbdesc.dbid = sysusages.dbid
  8016.             and spt_values.type = "E"
  8017.             and spt_values.number = 1
  8018.         order by sysdatabases.name
  8019.  
  8020. /*
  8021. **  If we are looking at one database, show it's device allocation.
  8022. */
  8023. if @showdev = 1
  8024. begin
  8025.     select device_fragments = sysdevices.name, size =
  8026.         convert(varchar(10),
  8027.             round(
  8028.             (spt_values.low * convert(float, size))
  8029.             / 1048576, 0)) + " " + "MB",
  8030.         usage = b.name
  8031.     from sysdatabases, sysusages, sysdevices, spt_values a, spt_values b
  8032.         where sysdatabases.dbid = sysusages.dbid
  8033.             and sysdevices.low <= size + vstart
  8034.             and sysdevices.high >= size + vstart - 1
  8035.             and sysdevices.status & 2 = 2 
  8036.             and sysdatabases.name = @dbname
  8037.             and a.type = "E"
  8038.             and a.number = 1
  8039.             and b.type = "S"
  8040.             and sysusages.segmap & 7 = b.number
  8041.     order by 1
  8042.  
  8043.     /*
  8044.     **  If there is only one database and we are in it, show the
  8045.     **  segments.
  8046.     */
  8047.     if exists (select *
  8048.             from #spdbdesc
  8049.                 where db_id() = dbid)
  8050.     begin
  8051.         declare @curdevice    varchar(30),
  8052.             @curseg        smallint,
  8053.             @segbit        int
  8054.  
  8055.         delete #spdbdesc
  8056.  
  8057.         select @curdevice = min(d.name)
  8058.             from  master.dbo.sysusages u, master.dbo.sysdevices d
  8059.                 where u.dbid = db_id()
  8060.                     and d.low <= size + vstart
  8061.                     and d.high >= size + vstart - 1
  8062.                     and d.status & 2 = 2 
  8063.         while (@curdevice != null)
  8064.         begin
  8065.             /*
  8066.             ** We need an inner loop here to go through
  8067.             **  all the possible segment.
  8068.             */
  8069.             select @curseg = min(segment)
  8070.                     from syssegments
  8071.             while (@curseg != null)
  8072.             begin
  8073.                 if (@curseg < 31)
  8074.                     select @segbit = power(2, @curseg)
  8075.                 else select @segbit = low
  8076.                     from master.dbo.spt_values
  8077.                         where type = "E"
  8078.                             and number = 2
  8079.                 insert into #spdbdesc
  8080.                     select @curseg, @curdevice
  8081.                         from master.dbo.sysusages u,
  8082.                             master.dbo.sysdevices d,
  8083.                             master.dbo.spt_values v
  8084.                     where u.segmap & @segbit = @segbit
  8085.                         and d.low <= u.size + u.vstart
  8086.                         and d.high >= u.size + u.vstart - 1
  8087.                         and u.dbid = db_id()
  8088.                         and d.status & 2 = 2 
  8089.                         and v.number = 1
  8090.                         and v.type = "E"
  8091.                         and d.name = @curdevice
  8092.                 select @curseg = min(segment)
  8093.                         from syssegments
  8094.                             where segment > @curseg
  8095.             end
  8096.  
  8097.             select @curdevice = min(d.name)
  8098.                 from  master.dbo.sysusages u,
  8099.                     master.dbo.sysdevices d
  8100.                 where u.dbid = db_id()
  8101.                     and d.low <= size + vstart
  8102.                     and d.high >= size + vstart - 1
  8103.                     and d.status & 2 = 2 
  8104.                     and d.name > @curdevice
  8105.         end
  8106.  
  8107.         /*
  8108.         **  One last check for any devices that have no segments.
  8109.         */
  8110.         insert into #spdbdesc
  8111.             select null, d.name
  8112.                 from master.dbo.sysusages u,
  8113.                     master.dbo.sysdevices d
  8114.             where u.segmap = 0
  8115.                 and d.low <= u.size + u.vstart
  8116.                 and d.high >= u.size + u.vstart - 1
  8117.                 and u.dbid = db_id()
  8118.                 and d.status & 2 = 2 
  8119.  
  8120.         select distinct device = substring(dbdesc, 1, 30),
  8121.             segment = isnull(name, " -- unused by any segments --")
  8122.         from #spdbdesc, syssegments
  8123.             where dbid *= segment
  8124.         order by 1, 2
  8125.     end
  8126.  
  8127. end
  8128.  
  8129. drop table #spdbdesc
  8130. return (0)
  8131. go
  8132. grant execute on sp_helpdb to public
  8133. go
  8134. dump transaction master with truncate_only
  8135. go
  8136. /* helpdevice 49.1 9/10/91 */
  8137. create procedure sp_helpdevice
  8138. @devname varchar(30) = "%"        /* device to check out */
  8139. as
  8140.  
  8141. declare @tapeblocksize int
  8142.  
  8143. /*
  8144. **  See if the device exists.
  8145. */
  8146. if not exists (select *
  8147.             from master.dbo.sysdevices
  8148.                 where name like @devname)
  8149. begin
  8150.     print "No such i/o device exists."
  8151.     return (1)
  8152. end
  8153.  
  8154. /*
  8155. **  Create a temporary table where we can build up a translation of
  8156. **  the device status bits.
  8157. */
  8158. create table #spdevtab
  8159. (
  8160.     name varchar(30),
  8161.     statusdesc varchar(250) null
  8162. )
  8163.  
  8164. set nocount on
  8165.  
  8166. /*
  8167. **  Initialize the temporary table with the names of the devices.
  8168. */
  8169. insert into #spdevtab (name)
  8170.     select name
  8171.         from master.dbo.sysdevices
  8172.             where name like @devname
  8173.  
  8174.  
  8175. /*
  8176. **  Now figure out what kind of controller type it is.  The type are
  8177. **  COMPLETELY machine dependent and are for UNIX.
  8178. **  cntrltype = 0    special
  8179. **            2    disk
  8180. **          3-8    tape
  8181. **          9-14    channel
  8182. */
  8183. update #spdevtab
  8184.     set statusdesc = "special"
  8185.         from master.dbo.sysdevices d, #spdevtab
  8186.             where d.cntrltype = 0
  8187.                 and #spdevtab.name = d.name
  8188. update #spdevtab
  8189.     set statusdesc = "disk"
  8190.         from master.dbo.sysdevices d, #spdevtab
  8191.             where d.cntrltype = 2
  8192.                 and #spdevtab.name = d.name
  8193. update #spdevtab
  8194.     set statusdesc = "tape"
  8195.         from master.dbo.sysdevices d, #spdevtab
  8196.             where d.cntrltype >= 3 and d.cntrltype <= 8
  8197.                 and #spdevtab.name = d.name
  8198. update #spdevtab
  8199.     set statusdesc = "channel"
  8200.         from master.dbo.sysdevices d, #spdevtab
  8201.             where d.cntrltype >= 9 and d.cntrltype <= 14
  8202.                 and #spdevtab.name = d.name
  8203.  
  8204. /*
  8205. **  If a tape device, also figure out the tape capacity which is listed
  8206. **  in sysdevices.high in number of 32k blocks.
  8207. */
  8208. if exists (select *
  8209.         from master.dbo.sysdevices d, #spdevtab
  8210.             where d.cntrltype >= 3 and d.cntrltype <= 8
  8211.                 and d.high > 0
  8212.                 and #spdevtab.name = d.name)
  8213. begin
  8214.         /*
  8215.         ** Find size of tape block.  Default to 32k.
  8216.         */
  8217.         select @tapeblocksize = low
  8218.                 from master.dbo.spt_values
  8219.                         where type = "E"
  8220.                                 and number = 4
  8221.         if @tapeblocksize = NULL
  8222.         begin
  8223.                 select @tapeblocksize = 32768
  8224.         end
  8225.  
  8226.     update #spdevtab
  8227.         set statusdesc = statusdesc + ", " + convert(varchar(10),
  8228.             round((d.high *
  8229.                                 convert(float, @tapeblocksize)) / 1048576, 0))
  8230.             + " MB"
  8231.         from master.dbo.sysdevices d, #spdevtab
  8232.             where d.cntrltype >= 3 and d.cntrltype <= 8
  8233.                 and d.high > 0
  8234.                 and #spdevtab.name = d.name
  8235. end
  8236.  
  8237. update #spdevtab
  8238.     set statusdesc = "UNKNOWN DEVICE"
  8239.         from master.dbo.sysdevices d, #spdevtab
  8240.             where d.cntrltype > 14
  8241.                 and #spdevtab.name = d.name
  8242.  
  8243. /*
  8244. **  Now check out the status bits and turn them into english.
  8245. **  The mirror status bits are >= 32.
  8246. */
  8247. if exists (select *
  8248.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8249.         where v.type = "V" and v.number > -1
  8250.             and d.status >= 32
  8251.             and #spdevtab.name = d.name)
  8252. begin
  8253.     /*
  8254.     **  Check to see if the mirror is enabled. 0x200 (512) bit in status.
  8255.     */
  8256.     if exists (select *
  8257.         from master.dbo.sysdevices d, #spdevtab
  8258.             where d.status & 512 = 512
  8259.                 and #spdevtab.name = d.name)
  8260.     begin
  8261.         update #spdevtab
  8262.             set statusdesc = statusdesc + ", MIRROR ENABLED"
  8263.         from master.dbo.sysdevices d, #spdevtab
  8264.             where d.status & 512 = 512
  8265.                 and #spdevtab.name = d.name
  8266.     end
  8267.  
  8268.     if exists (select *
  8269.         from master.dbo.sysdevices d, #spdevtab
  8270.             where d.status & 512 != 512
  8271.                 and d.status >= 32
  8272.                 and #spdevtab.name = d.name)
  8273.     begin
  8274.         update #spdevtab
  8275.             set statusdesc = statusdesc + ", MIRROR DISABLED"
  8276.         from master.dbo.sysdevices d, #spdevtab
  8277.             where d.status & 512 != 512
  8278.                 and d.status >= 32
  8279.                 and #spdevtab.name = d.name
  8280.     end
  8281.  
  8282.     update #spdevtab
  8283.         set statusdesc = statusdesc + ", " + v.name
  8284.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8285.             where v.type = "V" and v.number > -1
  8286.                 and d.status >= 32
  8287.                 and #spdevtab.name = d.name
  8288.     /* add mirror name */
  8289.     update #spdevtab
  8290.         set statusdesc = statusdesc + ", mirrored on '" + d.mirrorname
  8291.             + "'"
  8292.         from master.dbo.sysdevices d, #spdevtab
  8293.             where #spdevtab.name = d.name
  8294.                 and d.status >= 32
  8295. end
  8296.  
  8297. /*
  8298. **  Check to see if there is a mirrorname entry but mirroring not enabled.
  8299. **  If so, then one side of the mirror is off-line.
  8300. */
  8301. else if exists (select *
  8302.     from master.dbo.sysdevices d, #spdevtab
  8303.         where #spdevtab.name = d.name
  8304.         and d.mirrorname != null)
  8305. begin
  8306.     /*
  8307.     **  Figure out which side of the mirror is disabled.
  8308.     **  If 0x100 is on then phyname is disabled and mirrorname is enabled.
  8309.     */
  8310.     if exists (select *
  8311.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8312.             where v.type = "V" and v.number > -1
  8313.                 and d.status & v.number = 256
  8314.                 and #spdevtab.name = d.name)
  8315.     begin
  8316.         update #spdevtab
  8317.             set statusdesc = "only device '" + d.mirrorname 
  8318.                 + "' of mirror is enabled -- device '"
  8319.                 + d.phyname + "' is disabled"
  8320.             from master.dbo.sysdevices d, master.dbo.spt_values v,
  8321.                 #spdevtab
  8322.             where v.type = "V" and v.number > -1
  8323.                 and d.status & v.number = 256
  8324.                 and #spdevtab.name = d.name
  8325.     end
  8326.     else if exists (select *
  8327.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8328.             where v.type = "V" and v.number > -1
  8329.                 and d.status & v.number = 64
  8330.                 and #spdevtab.name = d.name)
  8331.     begin
  8332.         update #spdevtab
  8333.             set statusdesc = "only device '" + d.phyname 
  8334.                 + "' of mirror is enabled -- device '"
  8335.                 + d.mirrorname + "' is disabled"
  8336.             from master.dbo.sysdevices d, master.dbo.sysdevices e,
  8337.                 master.dbo.spt_values v, #spdevtab
  8338.             where v.type = "V" and v.number > -1
  8339.                 and d.status & v.number = 64
  8340.                 and #spdevtab.name = d.name
  8341.                 and e.status & 256 != 256
  8342.                 and #spdevtab.name = e.name
  8343.     end
  8344. end
  8345.  
  8346. /*
  8347. **  Status of 0x20 is "serial writes" for mirrored disks.
  8348. */
  8349. if exists (select *
  8350.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8351.         where v.type = "V" and v.number > -1
  8352.             and d.status & v.number = 32
  8353.             and #spdevtab.name = d.name)
  8354. begin
  8355.     update #spdevtab
  8356.         set statusdesc = statusdesc + ", " + v.name
  8357.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8358.             where v.type = "V" and v.number > -1
  8359.                 and d.status & v.number = 32
  8360.                 and #spdevtab.name = d.name
  8361. end
  8362.  
  8363.     update #spdevtab
  8364.         set statusdesc = statusdesc + ", nonserial writes"
  8365.         from master.dbo.sysdevices d, #spdevtab
  8366.             where   d.status > 32
  8367.                 and d.status & 32 != 32
  8368.                 and #spdevtab.name = d.name
  8369.  
  8370. /*
  8371. **  Status of 0x80 is "reads mirrored" for mirrored disks.
  8372. */
  8373. if exists (select *
  8374.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8375.         where v.type = "V" and v.number > -1
  8376.             and d.status & v.number = 128
  8377.             and #spdevtab.name = d.name)
  8378. begin
  8379.     update #spdevtab
  8380.         set statusdesc = statusdesc + ", " + v.name
  8381.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8382.             where v.type = "V" and v.number > -1
  8383.                 and d.status & v.number = 128
  8384.                 and #spdevtab.name = d.name
  8385. end
  8386.  
  8387. /*
  8388. **  Now check out the status bits and turn them into english.
  8389. **  Status of 0x10 is a dump device.
  8390. */
  8391. if exists (select *
  8392.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8393.         where v.type = "V" and v.number > -1
  8394.             and d.status & v.number = 16
  8395.             and #spdevtab.name = d.name)
  8396. begin
  8397.     update #spdevtab
  8398.         set statusdesc = statusdesc + ", " + v.name
  8399.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8400.             where v.type = "V" and v.number > -1
  8401.                 and d.status & v.number = 16
  8402.                 and #spdevtab.name = d.name
  8403. end
  8404.  
  8405. /*
  8406. **  Now check out the status bits and turn them into english.
  8407. **  Status of 0x01 is a default disk.
  8408. */
  8409. if exists (select *
  8410.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8411.         where v.type = "V" and v.number > -1
  8412.             and d.status & v.number = 1
  8413.             and #spdevtab.name = d.name)
  8414. begin
  8415.     update #spdevtab
  8416.         set statusdesc = statusdesc + ", " + v.name
  8417.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8418.             where v.type = "V" and v.number > -1
  8419.                 and d.status & v.number = 1
  8420.                 and #spdevtab.name = d.name
  8421. end
  8422.  
  8423. /*
  8424. **  Now check out the status bits and turn them into english.
  8425. **  Status of 0x02 is a physical disk.
  8426. */
  8427. if exists (select *
  8428.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8429.         where v.type = "V" and v.number > -1
  8430.             and d.status & v.number = 2
  8431.             and #spdevtab.name = d.name)
  8432. begin
  8433.     update #spdevtab
  8434.         set statusdesc = substring(statusdesc, 1, 225) + ", " + v.name
  8435.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8436.             where v.type = "V" and v.number > -1
  8437.                 and d.status & v.number = 2
  8438.                 and #spdevtab.name = d.name
  8439.     /*
  8440.     **  Add in it's size in MB.
  8441.     */
  8442.     update #spdevtab
  8443.         set statusdesc = statusdesc + ", " + convert(varchar(10),
  8444.             round(((d.high - d.low)
  8445.                 * convert(float, v.low)) / 1048576, 0)) + " MB"
  8446.         from master.dbo.sysdevices d, #spdevtab, master.dbo.spt_values v
  8447.             where d.status & 2 = 2
  8448.                 and #spdevtab.name = d.name
  8449.                 and v.number = 1
  8450.                 and v.type = "E"
  8451. end
  8452.  
  8453. /*
  8454. **  Now check out the status bits and turn them into english.
  8455. **  Status of 0x04 is a logical disk.
  8456. */
  8457. if exists (select *
  8458.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8459.         where v.type = "V" and v.number > -1
  8460.             and d.status & v.number = 4
  8461.             and #spdevtab.name = d.name)
  8462. begin
  8463.     update #spdevtab
  8464.         set statusdesc = substring(statusdesc, 1, 225) + ", " + v.name
  8465.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8466.             where v.type = "V" and v.number > -1
  8467.                 and d.status & v.number = 4
  8468.                 and #spdevtab.name = d.name
  8469. end
  8470.  
  8471. /*
  8472. **  Now check out the status bits and turn them into english.
  8473. **  Status of 0x08 is a skip tape header.
  8474. */
  8475. if exists (select *
  8476.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8477.         where v.type = "V" and v.number > -1
  8478.             and d.status & v.number = 8
  8479.             and #spdevtab.name = d.name)
  8480. begin
  8481.     update #spdevtab
  8482.         set statusdesc = substring(statusdesc, 1, 225) + ", " + v.name
  8483.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8484.             where v.type = "V" and v.number > -1
  8485.                 and d.status & v.number = 8
  8486.                 and #spdevtab.name = d.name
  8487. end
  8488.  
  8489. set nocount off
  8490.  
  8491. /*
  8492. **  The device number is in the high byte of sysdevices.low so
  8493. **  spt_values tells us which byte to pick out.
  8494. */
  8495. select device_name = d.name, physical_name = substring(d.phyname, 1, 46),
  8496.     description = #spdevtab.statusdesc,
  8497.     d.status, d.cntrltype,
  8498.     device_number = convert(tinyint, substring(convert(binary(4), d.low),
  8499.         v.low, 1)),
  8500.     d.low, d.high
  8501.         from master.dbo.sysdevices d, #spdevtab, master.dbo.spt_values v
  8502.     where d.name = #spdevtab.name
  8503.         and v.type = "E"
  8504.         and v.number = 3
  8505.  
  8506. return (0)
  8507. go
  8508. grant execute on sp_helpdevice to public
  8509. go
  8510. /* helpgroup 49.1 9/10/91 */
  8511. create procedure sp_helpgroup
  8512. @grpname varchar(30) = NULL        /* group name of interest */
  8513. as
  8514.  
  8515. /*
  8516. **  If no group name given, list all the groups.
  8517. **  User ids are <= 16383 and group ids are > 16383 or = 0.
  8518. */
  8519. if @grpname = NULL
  8520. begin
  8521.     select Group_name = name, Group_id = gid
  8522.         from sysusers
  8523.             where (uid > 16383 or uid = 0)
  8524.         order by name
  8525.  
  8526.     return (0)
  8527. end
  8528.  
  8529. /*
  8530. **  Check to see if group exists.
  8531. */
  8532. if not exists (select *
  8533.             from sysusers
  8534.         where name = @grpname
  8535.             and (uid > 16383 or uid = 0))
  8536. begin
  8537.     print "No such group exists in the current database."
  8538.     return (1)
  8539. end
  8540.  
  8541. /*
  8542. **  List the particulars for the group.
  8543. */
  8544. select Group_name = substring(g.name, 1, 25), Group_id = g.gid,
  8545.     Users_in_group = substring(u.name, 1, 25), Userid = u.uid
  8546.         from sysusers u, sysusers g
  8547.             where g.name like @grpname
  8548.             and g.uid = u.gid
  8549.             and (g.uid > 16383 or g.uid = 0)
  8550.             and u.uid != u.gid
  8551.         order by g.name, u.name
  8552.  
  8553. return (0)
  8554. go
  8555. grant execute on sp_helpgroup to public
  8556. go
  8557. dump transaction master with truncate_only
  8558. go
  8559. /*  helplog 49.1 9/10/91 */
  8560. create procedure sp_helplog
  8561. as
  8562. declare @firstpage int,
  8563.     @devname varchar(255),
  8564.     @msg varchar(255)
  8565.  
  8566.  
  8567. /* Get the first page of the log from sysindexes */
  8568. select @firstpage = first
  8569.     from sysindexes
  8570.         where id = 8
  8571.  
  8572. /*
  8573. **  Get the name of the device which contains the first page of the log.
  8574. **  The device number is in the high byte of d.low and u.vstart and which is the
  8575. **  high byte is defined in spt_values.
  8576. */
  8577. select @devname = d.name 
  8578.     from master.dbo.sysdevices d, master.dbo.sysusages u,
  8579.         master.dbo.spt_values v
  8580.     where (d.status & 2 = 2 or d.status & 4 = 4) 
  8581.         and @firstpage >= u.lstart
  8582.         and @firstpage < u.lstart + u.size - 1
  8583.         and convert(tinyint, substring(convert(binary(4), d.low),
  8584.             v.low, 1))
  8585.           = convert(tinyint, substring(convert(binary(4), u.vstart),
  8586.                v.low, 1))
  8587.         and v.type = "E"
  8588.         and v.number = 3
  8589.  
  8590. /* Print out the name of the device which contains the first page of the log */
  8591. select @msg = "In database """ + (select db_name())
  8592.     + """, the log starts on device """ + @devname + """."
  8593.  
  8594. print @msg
  8595.  
  8596. return (0)
  8597. go
  8598. grant execute on sp_helplog to public
  8599. go
  8600. /* helpindex 49.1 9/10/91 */
  8601. create procedure sp_helpindex
  8602. @objname varchar(92)            /* the table to check for indexes */
  8603. as
  8604.  
  8605. declare @objid int            /* the object id of the table */
  8606. declare @indid int            /* the index id of an index */
  8607. declare @keys varchar(200)        /* string to build up index key in */
  8608. declare @inddesc varchar(68)        /* string to build up index desc in */
  8609.  
  8610.  
  8611. /*
  8612. **  Check to see that the object names are local to the current database.
  8613. */
  8614. if @objname like "%.%.%" and
  8615.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  8616. begin
  8617.     print "Object name must be in 'current' database."
  8618.     return (1)
  8619. end
  8620.  
  8621. /*
  8622. **  Check to see the the table exists and initialize @objid.
  8623. */
  8624. select @objid = object_id(@objname)
  8625.  
  8626. /*
  8627. **  Table doesn't exist so return.
  8628. */
  8629. if @objid is NULL
  8630. begin
  8631.     print "Object does not exist."
  8632.     return (1)
  8633. end
  8634.  
  8635. /*
  8636. **  See if the object has any indexes.
  8637. **  Since there may be more than one entry in sysindexes for the object,
  8638. **  this select will set @indid to the index id of the first index.
  8639. */
  8640. select @indid = min(indid)
  8641.     from sysindexes
  8642.         where id = @objid
  8643.             and indid > 0
  8644.             and indid < 255
  8645.  
  8646. /*
  8647. **  If no indexes, return.
  8648. */
  8649. if @indid is NULL
  8650. begin
  8651.     print "Object does not have any indexes."
  8652.     return (1)
  8653. end
  8654.  
  8655. /*
  8656. **  Now check out each index, figure out it's type and keys and
  8657. **  save the info in a temporary table that we'll print out at the end.
  8658. */
  8659. create table #spindtab
  8660. (
  8661.     index_name    varchar(30),
  8662.     index_keys    varchar(255),
  8663.     index_description    varchar(68)
  8664. )
  8665.  
  8666. while @indid != NULL
  8667. begin
  8668.     
  8669.     /*
  8670.     **  First we'll figure out what the keys are.
  8671.     */
  8672.     declare @i int
  8673.     declare @thiskey varchar(30)
  8674.     declare @lastindid int
  8675.  
  8676.     select @keys = "", @i = 1
  8677.  
  8678.     set nocount on
  8679.  
  8680.     while @i <= 16
  8681.     begin
  8682.         select @thiskey = index_col(@objname, @indid, @i)
  8683.  
  8684.         if @thiskey = NULL
  8685.         begin
  8686.             goto keysdone
  8687.         end
  8688.  
  8689.         if @i > 1
  8690.         begin
  8691.             select @keys = @keys + ", "
  8692.         end
  8693.  
  8694.         select @keys = @keys + index_col(@objname, @indid, @i)
  8695.  
  8696.         /*
  8697.         **  Increment @i so it will check for the next key.
  8698.         */
  8699.         select @i = @i + 1
  8700.  
  8701.     end
  8702.  
  8703.  
  8704.     /*
  8705.     **  When we get here we now have all the keys.
  8706.     */
  8707.     keysdone:
  8708.         set nocount off
  8709.  
  8710.     /*
  8711.     **  Initialize the index description by figuring out if it's a
  8712.     **  clustered or nonclustered index.
  8713.     */
  8714.     if @indid = 1
  8715.     begin
  8716.         select @inddesc = "clustered"
  8717.     end
  8718.     if @indid > 1
  8719.     begin
  8720.         select @inddesc = "nonclustered"
  8721.     end
  8722.  
  8723.     /*
  8724.     **  Now we'll check out the status bits for this index and
  8725.     **  build an english description from them.
  8726.     */
  8727.  
  8728.     /*
  8729.     **  See if the index is unique (0x02).
  8730.     */
  8731.     if exists (select *
  8732.         from master.dbo.spt_values v, sysindexes i
  8733.             where i.status & v.number = v.number
  8734.                 and v.type = "I"
  8735.                 and v.number = 2
  8736.                 and i.id = @objid
  8737.                 and i.indid = @indid)
  8738.     begin
  8739.     select @inddesc = @inddesc + ", " + v.name
  8740.         from master.dbo.spt_values v, sysindexes i
  8741.             where i.status & v.number = v.number
  8742.                 and v.type = "I"
  8743.                 and v.number = 2
  8744.                 and i.id = @objid
  8745.                 and i.indid = @indid
  8746.     end
  8747.  
  8748.     /*
  8749.     **  See if the index is ignore_dupkey (0x01).
  8750.     */
  8751.     if exists (select *
  8752.         from master.dbo.spt_values v, sysindexes i
  8753.             where i.status & v.number = v.number
  8754.                 and v.type = "I"
  8755.                 and v.number = 1
  8756.                 and i.id = @objid
  8757.                 and i.indid = @indid)
  8758.     begin
  8759.     select @inddesc = @inddesc + ", " + v.name
  8760.         from master.dbo.spt_values v, sysindexes i
  8761.             where i.status & v.number = v.number
  8762.                 and v.type = "I"
  8763.                 and v.number = 1
  8764.                 and i.id = @objid
  8765.                 and i.indid = @indid
  8766.     end
  8767.  
  8768.     /*
  8769.     **  See if the index is ignore_dup_row (0x04).
  8770.     */
  8771.     if exists (select *
  8772.         from master.dbo.spt_values v, sysindexes i
  8773.             where i.status & v.number = v.number
  8774.                 and v.type = "I"
  8775.                 and v.number = 4
  8776.                 and i.id = @objid
  8777.                 and i.indid = @indid)
  8778.     begin
  8779.     select @inddesc = @inddesc + ", " + v.name
  8780.         from master.dbo.spt_values v, sysindexes i
  8781.             where i.status & v.number = v.number
  8782.                 and v.type = "I"
  8783.                 and v.number = 4
  8784.                 and i.id = @objid
  8785.                 and i.indid = @indid
  8786.     end
  8787.  
  8788.     /*
  8789.     **  See if the index is allow_dup_row (0x40).
  8790.     */
  8791.     if exists (select *
  8792.         from master.dbo.spt_values v, sysindexes i
  8793.             where i.status & v.number = v.number
  8794.                 and v.type = "I"
  8795.                 and v.number = 64
  8796.                 and i.id = @objid
  8797.                 and i.indid = @indid)
  8798.     begin
  8799.     select @inddesc = @inddesc + ", " + v.name   
  8800.         from master.dbo.spt_values v, sysindexes i
  8801.             where i.status & v.number = v.number
  8802.                 and v.type = "I"
  8803.                 and v.number = 64
  8804.                 and i.id = @objid
  8805.                 and i.indid = @indid
  8806.     end
  8807.  
  8808.     /*
  8809.     **  Add the location of the data.
  8810.     */
  8811.     select @inddesc = @inddesc + " located on " + s.name
  8812.         from syssegments s, sysindexes i
  8813.             where s.segment = i.segment
  8814.                 and i.id = @objid
  8815.                 and i.indid = @indid
  8816.  
  8817.     /*
  8818.     **  Now we have the whole description for the index so we'll add
  8819.     **  the goods to the temporary table.
  8820.     */
  8821.     insert into #spindtab
  8822.         select name, @keys, @inddesc
  8823.             from sysindexes
  8824.                 where id = @objid
  8825.                     and indid = @indid
  8826.     /*
  8827.     **  Now move @indid to the next index.
  8828.     */
  8829.     select @lastindid = @indid
  8830.     select @indid = NULL
  8831.     select @indid = min(indid)
  8832.         from sysindexes
  8833.             where id = @objid
  8834.                 and indid > @lastindid
  8835.                 and indid < 255
  8836. end
  8837.  
  8838. /*
  8839. **  Now print out the contents of the temporary index table.
  8840. */
  8841. select index_name = substring(index_name, 1, 20),
  8842.     index_description = substring(index_description, 1, 56),
  8843.     index_keys = index_keys from #spindtab
  8844.  
  8845. drop table #spindtab
  8846.  
  8847. return (0)
  8848. go
  8849. grant execute on sp_helpindex to public
  8850. go
  8851. dump transaction master with truncate_only
  8852. go
  8853. /* helpjoins 49.1 9/10/91 */
  8854. create procedure sp_helpjoins
  8855. @lefttab varchar(92),            /* name of first table to join */
  8856. @righttab varchar(92)            /* name of first table to join */
  8857. as
  8858.  
  8859. declare @leftid int            /* id of first table */
  8860. declare @rightid int            /* id of second table */
  8861. declare @foundit bit            /* flag to indicate joins found */
  8862.  
  8863. /*
  8864. **  This procedure returns any foreign or common joins.
  8865. **  If none exists, then it returns any joins that have the same usertype.
  8866. **  If none exists, it returns any joins that have the same physical type.
  8867. */
  8868.  
  8869. /*
  8870. **  Make sure the @objname is local to the current database.
  8871. */
  8872. if @lefttab like "%.%.%" and
  8873.     substring(@lefttab, 1, charindex(".", @lefttab) - 1) != db_name()
  8874. begin
  8875.     print "Object must be in your current database."
  8876.     return (1)
  8877. end
  8878. if @righttab like "%.%.%" and
  8879.     substring(@righttab, 1, charindex(".", @righttab) - 1) != db_name()
  8880. begin
  8881.     print "Object must be in your current database."
  8882.     return (1)
  8883. end
  8884.  
  8885. /*
  8886. **  See if we can find the objects.  They must be a system table, user table,
  8887. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the 
  8888. **  object type is -- it's more reliable than using sysobjects.type which
  8889. **  could change.
  8890. */
  8891. select @leftid = id
  8892.     from sysobjects
  8893.         where id = object_id(@lefttab)
  8894.             and (sysstat & 7 = 1        /* system table */
  8895.                 or sysstat & 7 = 2    /* view */
  8896.                 or sysstat & 7 = 3)    /* user table */
  8897. select @rightid = id
  8898.     from sysobjects
  8899.         where id = object_id(@righttab)
  8900.             and (sysstat & 7 = 1        /* system table */
  8901.                 or sysstat & 7 = 2    /* view */
  8902.                 or sysstat & 7 = 3)    /* user table */
  8903.  
  8904. /*
  8905. **  If either of the tables don't exist, quit.
  8906. */
  8907. if @leftid is NULL
  8908. begin
  8909.     print "First table doesn't exist."
  8910.     return (1)
  8911. end 
  8912. if @rightid is NULL
  8913. begin
  8914.     print "Second table doesn't exist."
  8915.     return (1)
  8916. end 
  8917.  
  8918. create table #hjtab
  8919. (
  8920.     a1    varchar(30),
  8921.     a2    varchar(30),
  8922.     b1    varchar(30) null,
  8923.     b2    varchar(30) null,
  8924.     c1    varchar(30) null,
  8925.     c2    varchar(30) null,
  8926.     d1    varchar(30) null,
  8927.     d2    varchar(30) null,
  8928.     e1    varchar(30) null,
  8929.     e2    varchar(30) null,
  8930.     f1    varchar(30) null,
  8931.     f2    varchar(30) null,
  8932.     g1    varchar(30) null,
  8933.     g2    varchar(30) null,
  8934.     h1    varchar(30) null,
  8935.     h2    varchar(30) null
  8936. )
  8937.  
  8938. /*
  8939. **  Look for foreign key joins.
  8940. */
  8941. select @foundit = 0
  8942. if exists (select *
  8943.         from syskeys
  8944.             where type = 2
  8945.                 and id = @leftid
  8946.                 and depid = @rightid)
  8947. begin
  8948.     insert into #hjtab
  8949.     select distinct first_pair = col_name(id, key1), col_name(depid, depkey1),
  8950.         second_pair = col_name(id, key2), col_name(depid, depkey2),
  8951.         third_pair = col_name(id, key3), col_name(depid, depkey3),
  8952.         fourth_pair = col_name(id, key4), col_name(depid, depkey4),
  8953.         fifth_pair = col_name(id, key5), col_name(depid, depkey5),
  8954.         sixth_pair = col_name(id, key6), col_name(depid, depkey6),
  8955.         seventh_pair = col_name(id, key7), col_name(depid, depkey7),
  8956.         eighth_pair = col_name(id, key8), col_name(depid, depkey8)
  8957.     from syskeys
  8958.         where type = 2
  8959.             and id = @leftid
  8960.             and depid = @rightid
  8961.  
  8962.     select @foundit = 1
  8963. end
  8964.  
  8965. if exists (select *
  8966.         from syskeys
  8967.             where type = 2
  8968.                 and id = @rightid
  8969.                 and depid = @leftid)
  8970. begin
  8971.     insert into #hjtab
  8972.     select distinct first_pair = col_name(depid, depkey1), col_name(id, key1),
  8973.         second_pair = col_name(depid, depkey2), col_name(id, key2),
  8974.         third_pair = col_name(depid, depkey3), col_name(id, key3),
  8975.         fourth_pair = col_name(depid, depkey4), col_name(id, key4),
  8976.         fifth_pair = col_name(depid, depkey5), col_name(id, key5),
  8977.         sixth_pair = col_name(depid, depkey6), col_name(id, key6),
  8978.         seventh_pair = col_name(depid, depkey7), col_name(id, key7),
  8979.         eighth_pair = col_name(depid, depkey8), col_name(id, key8)
  8980.     from syskeys
  8981.         where type = 2
  8982.             and id = @rightid
  8983.             and depid = @leftid
  8984.  
  8985.     select @foundit = 1
  8986. end
  8987.  
  8988. /*
  8989. **  Look for common key joins.
  8990. */
  8991. if exists (select *
  8992.         from syskeys
  8993.             where type = 3
  8994.                 and id = @leftid
  8995.                 and depid = @rightid)
  8996. begin
  8997.     insert into #hjtab
  8998.     select distinct first_pair = col_name(id, key1), col_name(depid, depkey1),
  8999.         second_pair = col_name(id, key2), col_name(depid, depkey2),
  9000.         third_pair = col_name(id, key3), col_name(depid, depkey3),
  9001.         fourth_pair = col_name(id, key4), col_name(depid, depkey4),
  9002.         fifth_pair = col_name(id, key5), col_name(depid, depkey5),
  9003.         sixth_pair = col_name(id, key6), col_name(depid, depkey6),
  9004.         seventh_pair = col_name(id, key7), col_name(depid, depkey7),
  9005.         eighth_pair = col_name(id, key8), col_name(depid, depkey8)
  9006.     from syskeys
  9007.         where type = 3
  9008.             and id = @leftid
  9009.             and depid = @rightid
  9010.  
  9011.     select @foundit = 1
  9012. end
  9013.  
  9014. if exists (select *
  9015.         from syskeys
  9016.             where type = 3
  9017.                 and id = @rightid
  9018.                 and depid = @leftid)
  9019. begin
  9020.     insert into #hjtab
  9021.     select distinct first_pair = col_name(depid, depkey1), col_name(id, key1),
  9022.         second_pair = col_name(depid, depkey2), col_name(id, key2),
  9023.         third_pair = col_name(depid, depkey3), col_name(id, key3),
  9024.         fourth_pair = col_name(depid, depkey4), col_name(id, key4),
  9025.         fifth_pair = col_name(depid, depkey5), col_name(id, key5),
  9026.         sixth_pair = col_name(depid, depkey6), col_name(id, key6),
  9027.         seventh_pair = col_name(depid, depkey7), col_name(id, key7),
  9028.         eighth_pair = col_name(depid, depkey8), col_name(id, key8)
  9029.     from syskeys
  9030.         where type = 3
  9031.             and id = @rightid
  9032.             and depid = @leftid
  9033.  
  9034.     select @foundit = 1
  9035. end
  9036.  
  9037. /*
  9038. **  We got a foreignkey and we didn't get a common key.
  9039. */
  9040. if @foundit = 1
  9041. begin
  9042.     select distinct *
  9043.         from #hjtab
  9044.  
  9045.     drop table #hjtab
  9046.  
  9047.     return (0)
  9048. end
  9049.  
  9050. /*
  9051. **  We didn't find any defined joins so we'll look for common user types.
  9052. */
  9053. if exists (select *
  9054.         from syscolumns a, syscolumns b
  9055.             where a.id = @leftid
  9056.                 and b.id = @rightid
  9057.                 and a.usertype = b.usertype
  9058.                 and a.usertype > 99)
  9059. begin
  9060.     select distinct first_pair = col_name(a.id, a.colid),
  9061.         col_name(b.id, b.colid)
  9062.     from syscolumns a, syscolumns b
  9063.         where a.id = @leftid
  9064.             and b.id = @rightid
  9065.             and a.usertype = b.usertype
  9066.             and a.usertype > 99
  9067.     order by first_pair
  9068.  
  9069.     /*
  9070.     **  If we found something, we can stop.
  9071.     */
  9072.     if @@rowcount > 0
  9073.         return (0)
  9074. end
  9075.  
  9076. /*
  9077. **  We couldn't find anything so far so we'll see if we have anything
  9078. **  that just shares the same physical type.
  9079. */
  9080. if exists (select *
  9081.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  9082.             master.dbo.spt_values z
  9083.         where a.id = @leftid
  9084.             and b.id = @rightid
  9085.             and a.id != b.id
  9086.             and a.name = b.name
  9087.             and y.type = "J"
  9088.             and a.type = y.low
  9089.             and z.type = "J"
  9090.             and b.type = z.low
  9091.             and y.number = z.number)
  9092. begin
  9093.     select distinct first_pair = col_name(a.id, a.colid),
  9094.         col_name(b.id, b.colid)
  9095.     from syscolumns a, syscolumns b, master.dbo.spt_values y,
  9096.         master.dbo.spt_values z
  9097.     where a.id = @leftid
  9098.         and b.id = @rightid
  9099.         and a.id != b.id
  9100.         and a.name = b.name
  9101.         and y.type = "J"
  9102.         and a.type = y.low
  9103.         and z.type = "J"
  9104.         and b.type = z.low
  9105.         and y.number = z.number
  9106.     order by first_pair
  9107.  
  9108. end
  9109.  
  9110. return (0)
  9111. go
  9112. grant execute on sp_helpjoins to public
  9113. go
  9114. /* helpkey 49.1 9/10/91 */
  9115. create procedure sp_helpkey
  9116. @tabname varchar(92) = null    /* table or view we want to check */
  9117. as
  9118.  
  9119. declare    @tabid    int        /* id of table or view we want to check */
  9120.  
  9121. /*
  9122. **  Check to see that the object names are local to the current database.
  9123. */
  9124. if @tabname like "%.%.%" and
  9125.     substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  9126. begin
  9127.     print "Table or view name must be in 'current' database."
  9128.     return (1)
  9129. end
  9130.  
  9131. /*
  9132. **  If @tabname is null, show all the keys.
  9133. */
  9134. if @tabname = null
  9135. begin
  9136.     select keytype = substring(v.name, 1, 10), object = object_name(k.id),
  9137.         related_object = isnull(object_name(k.depid), " -- none --"),
  9138.         object_keys = substring(col_name(k.id, key1) + ", "
  9139.             + isnull(col_name(k.id, key2), "*") + ", "
  9140.             + isnull(col_name(k.id, key3), "*") + ", "
  9141.             + isnull(col_name(k.id, key4), "*") + ", "
  9142.             + isnull(col_name(k.id, key5), "*") + ", "
  9143.             + isnull(col_name(k.id, key6), "*") + ", "
  9144.             + isnull(col_name(k.id, key7), "*") + ", "
  9145.             + isnull(col_name(k.id, key8), "*"), 1, 70),
  9146.         related_keys = substring(isnull(col_name(k.depid, depkey1), "*")
  9147.             + ", "
  9148.             + isnull(col_name(k.depid, depkey2), "*") + ", "
  9149.             + isnull(col_name(k.depid, depkey3), "*") + ", "
  9150.             + isnull(col_name(k.depid, depkey4), "*") + ", "
  9151.             + isnull(col_name(k.depid, depkey5), "*") + ", "
  9152.             + isnull(col_name(k.depid, depkey6), "*") + ", "
  9153.             + isnull(col_name(k.depid, depkey7), "*") + ", "
  9154.             + isnull(col_name(k.depid, depkey8), "*"), 1, 70)
  9155.     from syskeys k, master.dbo.spt_values v
  9156.         where k.type = v.number and v.type = 'K'
  9157.     order by object, keytype, related_object
  9158.  
  9159.     return (0)
  9160. end
  9161.  
  9162. /*
  9163. **  We're looking for a particular table or view.
  9164. */
  9165. else
  9166. begin
  9167.     /*
  9168.     **  Make sure the table or view exists.
  9169.     */
  9170.     select @tabid = object_id(@tabname)
  9171.  
  9172.     if @tabid = null
  9173.     begin
  9174.         print "No such table or view in the current database."
  9175.         return (1)
  9176.     end
  9177.  
  9178.     /*
  9179.     **  See if any keys exist.
  9180.     */
  9181.     if not exists (select *
  9182.             from syskeys
  9183.             where id = @tabid or depid = @tabid)
  9184.     begin
  9185.         print "No defined keys for this object."
  9186.         return (1)
  9187.     end
  9188.  
  9189.     select keytype = substring(v.name, 1, 10), object = object_name(k.id),
  9190.         related_object = isnull(object_name(k.depid), " -- none --"),
  9191.         object_keys = substring(col_name(k.id, key1) + ", "
  9192.             + isnull(col_name(k.id, key2), "*") + ", "
  9193.             + isnull(col_name(k.id, key3), "*") + ", "
  9194.             + isnull(col_name(k.id, key4), "*") + ", "
  9195.             + isnull(col_name(k.id, key5), "*") + ", "
  9196.             + isnull(col_name(k.id, key6), "*") + ", "
  9197.             + isnull(col_name(k.id, key7), "*") + ", "
  9198.             + isnull(col_name(k.id, key8), "*"), 1, 70),
  9199.         related_keys = substring(isnull(col_name(k.depid, depkey1), "*")
  9200.             + ", "
  9201.             + isnull(col_name(k.depid, depkey2), "*") + ", "
  9202.             + isnull(col_name(k.depid, depkey3), "*") + ", "
  9203.             + isnull(col_name(k.depid, depkey4), "*") + ", "
  9204.             + isnull(col_name(k.depid, depkey5), "*") + ", "
  9205.             + isnull(col_name(k.depid, depkey6), "*") + ", "
  9206.             + isnull(col_name(k.depid, depkey7), "*") + ", "
  9207.             + isnull(col_name(k.depid, depkey8), "*"), 1, 70)
  9208.     from syskeys k, master.dbo.spt_values v
  9209.         where k.type = v.number and v.type = 'K'
  9210.             and (k.id = @tabid or k.depid = @tabid)
  9211.     order by object, keytype, related_object
  9212.  
  9213.     return (0)
  9214. end
  9215. go
  9216. grant execute on sp_helpkey to public
  9217. go
  9218. dump transaction master with truncate_only
  9219. go
  9220. /* objectsegment 49.1 9/10/91 */
  9221. create procedure sp_objectsegment
  9222. @objid    int
  9223. as
  9224.     /* 
  9225.     ** Print out the create date for the object and it's
  9226.     ** data segment, if applicable.
  9227.     */
  9228.  
  9229.     /*
  9230.     **  Objects have the following value for sysstat & 7:
  9231.     **    0 - trigger
  9232.     **    1 - system table
  9233.     **    2 - view
  9234.     **    3 - user table
  9235.     **    4 - sproc
  9236.     **    5 - NOT UNUSED
  9237.     **    6 - default
  9238.     **    7 - rule
  9239.     */
  9240.     if exists (select *
  9241.             from sysobjects
  9242.                 where id = @objid
  9243.                     and sysstat & 7 in (1, 3))
  9244.     begin
  9245.         select Data_located_on_segment = s.name, When_created = crdate
  9246.             from sysobjects o, syssegments s, sysindexes i
  9247.                 where o.id = @objid
  9248.                     and i.id = o.id
  9249.                     and i.indid < 2
  9250.                     and i.segment = s.segment
  9251.     end
  9252.  
  9253.     /*
  9254.     **  It's not a table so segment is not applicable.
  9255.     */
  9256.     else
  9257.     begin
  9258.         select Data_located_on_segment = "not applicable",
  9259.             When_created = crdate
  9260.         from sysobjects
  9261.             where id = @objid
  9262.     end
  9263.  
  9264. return (0)
  9265.  
  9266. go
  9267. grant execute on sp_objectsegment to public
  9268. go
  9269. /* help 49.1 9/10/91 */
  9270. create procedure sp_help
  9271. @objname varchar(92) = NULL            /* object name we're after */
  9272. as
  9273.  
  9274. declare @objid int                /* id of the object */
  9275. declare @sysstat smallint            /* the type of the object */
  9276.  
  9277. set nocount on
  9278.  
  9279. /*
  9280. **  If no @objname given, give a little info about all objects.
  9281. */
  9282. if @objname = NULL
  9283. begin
  9284.     select Name = substring(o.name, 1, 15),
  9285.         Owner = substring(user_name(uid), 1, 15),
  9286.         Object_type = substring(v.name + x.name, 1, 22)
  9287.     from sysobjects o, master.dbo.spt_values v, master.dbo.spt_values x
  9288.         where o.sysstat & 7 = v.number
  9289.             and v.type = "O"
  9290.             and x.type = "R"
  9291.             and o.userstat & -32768 = x.number
  9292.     order by Object_type desc, Name asc
  9293.  
  9294.     select User_type = substring(s.name, 1, 15),
  9295.            Storage_type = substring(st.name, 1, 15),
  9296.            Length = s.length,
  9297.            Nulls = s.allownulls,
  9298.            Default_name=substring(object_name(s.tdefault), 1, 15),
  9299.            Rule_name= substring(object_name(s.domain), 1, 15)
  9300.     from systypes s, systypes st
  9301.         where s.type = st.type
  9302.             and s.usertype > 99
  9303.             and st.usertype != 18 /* ignore sysname */
  9304.             and st.usertype < 100
  9305.     order by s.name
  9306.  
  9307.     return (0)
  9308. end
  9309.  
  9310. /*
  9311. **  Make sure the @objname is local to the current database.
  9312. */
  9313. if @objname like "%.%.%" and
  9314.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  9315. begin
  9316.     print "Object must be in your current database."
  9317.     return  (1)
  9318. end
  9319.  
  9320. /*
  9321. **  Now check to see if the @objname is in sysobjects.  It has to be either
  9322. **  in sysobjects or systypes.
  9323. */
  9324. select @objid = id, @sysstat = sysstat
  9325.     from sysobjects
  9326.         where id = object_id(@objname)
  9327.  
  9328. /*
  9329. **  It wasn't in sysobjects so we'll check in systypes.
  9330. */
  9331. if @objid = NULL
  9332. begin
  9333.     select @objid = usertype
  9334.         from systypes
  9335.             where name = @objname
  9336.  
  9337.     /*
  9338.     **  Time to give up -- @objname is not in sysobjects or systypes.
  9339.     */
  9340.     if @objid = NULL
  9341.     begin
  9342.         print "Object does not exist in this database."
  9343.         return  (1)
  9344.     end
  9345.  
  9346.     /*
  9347.     ** Print help about a data type
  9348.     */
  9349.     select Type_name = substring(s.name, 1, 15),
  9350.            Storage_type = substring(st.name, 1, 15),
  9351.            Length = s.length,
  9352.            Nulls = s.allownulls,
  9353.            Default_name=substring(object_name(s.tdefault), 1, 15),
  9354.            Rule_name= substring(object_name(s.domain), 1, 15)
  9355.     from systypes s, systypes st
  9356.         where s.usertype = @objid
  9357.             and s.type = st.type
  9358.             and st.usertype != 18 /* ignore sysname */
  9359.             and st.usertype < 100
  9360.  
  9361.     return (0)
  9362. end
  9363.  
  9364. /*
  9365. **  It's in sysobjects so print out the information.
  9366. */
  9367. select Name = o.name, Owner = user_name(uid), 
  9368.     Type = substring(v.name + x.name, 1, 16)
  9369.     from sysobjects o, master.dbo.spt_values v,
  9370.         master.dbo.spt_values x
  9371.     where o.id = @objid
  9372.         and o.sysstat & 7 = v.number
  9373.         and v.type = "O"
  9374.         and x.type = "R"
  9375.         and o.userstat & -32768 = x.number
  9376.  
  9377. execute sp_objectsegment @objid
  9378.  
  9379. /*
  9380. **  Objects have the following value for sysstat & 7:
  9381. **    0 - trigger
  9382. **    1 - system table
  9383. **    2 - view
  9384. **    3 - user table
  9385. **    4 - sproc
  9386. **    5 - NOT UNUSED
  9387. **    6 - default
  9388. **    7 - rule
  9389. **
  9390. **  If the object is a system table, view, or user table, we want to check
  9391. **  out the objects columns here. Since the moneyn, intn and floatn are 
  9392. **  internal representations, created a temporary table to save the values
  9393. **  and update those types to money, int and float when printing out.
  9394. */
  9395. if @sysstat & 7 in (1, 2, 3)
  9396. begin
  9397.  
  9398.     create table #sphelptab
  9399.     (
  9400.         col_name    char (30),
  9401.         col_type    char (30),    
  9402.         col_len        tinyint,    
  9403.         col_status    tinyint,
  9404.         col_def        char (30)  null,
  9405.         col_dom        char (30)  null
  9406.     )
  9407.  
  9408.     insert into #sphelptab
  9409.         select c.name, t.name, c.length, c.status, 
  9410.             object_name(c.cdefault),object_name(c.domain)
  9411.         from syscolumns c, systypes t
  9412.         where c.id = @objid
  9413.             and c.usertype *= t.usertype
  9414.  
  9415.     update #sphelptab
  9416.         set col_type = "money"
  9417.         where col_type = "moneyn"
  9418.  
  9419.     update #sphelptab
  9420.         set col_type = "float"
  9421.         where col_type = "floatn"
  9422.  
  9423.     update #sphelptab
  9424.         set col_type = "int"
  9425.         where col_type = "intn"
  9426.  
  9427.     update #sphelptab
  9428.         set col_type = "datetime"
  9429.         where col_type = "datetimn"
  9430.  
  9431.     select Column_name = substring(col_name, 1, 15),
  9432.            Type = substring(col_type, 1, 15),
  9433.            Length = col_len,
  9434.            Nulls = convert(bit, (col_status & 8)),
  9435.            Default_name=substring(col_def, 1, 15),
  9436.            Rule_name = substring(col_dom, 1, 15)
  9437.     from #sphelptab
  9438. end
  9439.  
  9440. /*
  9441. **  For procedures, the parameters of the procedures are stored in syscolumns.
  9442. */
  9443. if @sysstat & 7 = 4
  9444. begin
  9445.     /*
  9446.     **  Does the sproc have any parameters?
  9447.     */
  9448.     if exists (select *
  9449.             from syscolumns c, systypes t
  9450.                 where c.id = @objid
  9451.                     and c.usertype *= t.usertype)
  9452.     begin
  9453.         select Parameter_name = substring(c.name,1,15),
  9454.                Type = substring(t.name,1,15),
  9455.                Length = c.length,
  9456.                Param_order=c.colid
  9457.         from syscolumns c, systypes t
  9458.             where c.id = @objid
  9459.                 and c.usertype *= t.usertype
  9460.     end
  9461. end
  9462.  
  9463. /*
  9464. **  If the object is a table, check out the indexes.
  9465. */
  9466. if @sysstat & 7 in (1, 3)
  9467.     execute dbo.sp_helpindex @objname
  9468.  
  9469. /*
  9470. **  If the object is a table or view, check out the keys.
  9471. */
  9472. if @sysstat & 7 in (1, 2, 3)
  9473.     execute dbo.sp_helpkey @objname
  9474.  
  9475. /*
  9476. ** Drop the table if we created it
  9477. */
  9478. if @sysstat & 7 in (1, 2, 3)
  9479.     drop table #sphelptab
  9480.  
  9481. return (0)
  9482. go
  9483. grant execute on sp_help to public
  9484. go
  9485. dump transaction master with truncate_only
  9486. go
  9487. /* helprotect 49.1 9/10/91 */
  9488. create procedure sp_helprotect
  9489. @name varchar(92),            /* name of object or user to check */
  9490. @username varchar(30) = NULL        /* name of user to restrict check */
  9491. as
  9492.  
  9493. declare @low int            /* range of userids to check */
  9494. declare @high int
  9495. declare @objid int            /* id of @name if object */
  9496.  
  9497. /*
  9498. **  Check to see that the object names are local to the current database.
  9499. */
  9500. if @name like "%.%.%" and
  9501.     substring(@name, 1, charindex(".", @name) - 1) != db_name()
  9502. begin
  9503.     print "Name must be in 'current' database."
  9504.     return (1)
  9505. end
  9506.  
  9507. /*
  9508. **  Initialize the range of user ids to check.  We will either check all
  9509. **  users (the initial values) or just one in particular (@low = @high)
  9510. */
  9511. select @low = 0, @high = 32767
  9512.  
  9513. if @username is not NULL
  9514. begin
  9515.     /*
  9516.     **  Set @low and @high so the search will be restricted to
  9517.     **  a particular user.
  9518.     */
  9519.     select @low = uid, @high = uid
  9520.         from sysusers
  9521.             where name = @username
  9522.  
  9523.     /*
  9524.     **  @username isn't valid.
  9525.     */
  9526.     if @@rowcount = 0
  9527.     begin
  9528.         print "No such user exists in the database."
  9529.         return (1)
  9530.     end
  9531. end
  9532.  
  9533. /*
  9534. **  There are two cases handled by this procedure.  If the first parameter
  9535. **  is an object (table, view, procedure) then @name is taken as an object
  9536. **  name and the procedure will figure out permissions for the object.
  9537. **
  9538. **  If the first parameter is not one of the objects mentioned it will be
  9539. **  taken as user name and all the permissions for the user or group name
  9540. **  will be shown.
  9541. */
  9542.  
  9543. /*
  9544. **  Check to see if it's an object.
  9545. */
  9546. if exists (select *
  9547.         from sysobjects
  9548.             where id = object_id(@name)
  9549.                 and (sysstat & 7 = 1    /* system table */
  9550.                 or sysstat & 7 = 2    /* view */
  9551.                 or sysstat & 7 = 3    /* user table */
  9552.                 or sysstat & 7 = 4))    /* procedure */
  9553. begin
  9554.     /*
  9555.     **  This is the case where we will show the various permissions for
  9556.     **  an object, possibly restricted by a particular user.
  9557.     */
  9558.     select type = substring(b.name, 1, 6), action = a.name,
  9559.         user = substring(user_name(p.uid), 1, 15),
  9560.         column = substring(isnull(col_name(id, c.number), "All"), 1, 10)
  9561.             from sysprotects p, master.dbo.spt_values c,
  9562.                 master.dbo.spt_values a, master.dbo.spt_values b
  9563.             where convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  9564.                 & c.high != 0
  9565.                 and c.number <=
  9566.                     (select count(*)
  9567.                         from syscolumns
  9568.                         where id = object_id(@name))
  9569.                 and a.type = "T"
  9570.                 and a.number = p.action
  9571.                 and b.type = "T"
  9572.                 and b.number = p.protecttype
  9573.                 and p.id = object_id(@name)
  9574.                 and p.uid between @low and @high
  9575.             order by type, action, user, column
  9576.     return (0)
  9577. end
  9578.  
  9579. /*
  9580. **  Since @name is not an object let's try it as a user.
  9581. */
  9582. select @low = uid, @high = uid
  9583.     from sysusers
  9584.         where name = @name
  9585.  
  9586. /*
  9587. **  @name isn't a user either so return.
  9588. */
  9589. if @@rowcount = 0
  9590. begin
  9591.     print "No such object or user exists in the database."
  9592.     return (1)
  9593. end
  9594.  
  9595. /*
  9596. **  Now we have the user so run the same protection query as before but
  9597. **  restrict on user and not on object.
  9598. */
  9599. select distinct type = substring(b.name, 1, 6), action = a.name,
  9600.     object = substring(isnull(object_name(p.id), ""), 1, 15),
  9601.     column = substring(isnull(col_name(id, c.number), "All"), 1, 10)
  9602.         from sysprotects p, master.dbo.spt_values c,
  9603.             master.dbo.spt_values a, master.dbo.spt_values b
  9604.         where convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  9605.             & c.high != 0
  9606.             and c.number <= 255
  9607.             and a.type = "T"
  9608.             and a.number = p.action
  9609.             and b.type = "T"
  9610.             and b.number = p.protecttype
  9611.             and p.uid = @low
  9612.         order by type, object, column, action
  9613.  
  9614. return (0)
  9615. go
  9616. grant execute on sp_helprotect to public
  9617. go
  9618. /* helptext 49.1 9/10/91 */
  9619. create procedure sp_helptext
  9620. @objname varchar(92)
  9621. as
  9622.  
  9623. /*
  9624. **  Make sure the @objname is local to the current database.
  9625. */
  9626. if (@objname like "%.%.%" and
  9627.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name() )
  9628. begin
  9629.     print "Object must be in your current database."
  9630.     return (1)
  9631. end
  9632.  
  9633. /*
  9634. ** See if @objname exists.
  9635. */
  9636. if (object_id(@objname) = NULL)
  9637. begin
  9638.     print "No such object in the current database."
  9639.     return (1)
  9640. end
  9641.  
  9642. /*
  9643. **  Find out howmany lines of text are coming back,
  9644. **    and return if there are none.
  9645. */
  9646. select count(*)
  9647.     from syscomments
  9648. where id = object_id(@objname)
  9649.  
  9650. /*
  9651. **  Now get the text.
  9652. */
  9653. select text
  9654.     from syscomments 
  9655. where id = object_id(@objname)
  9656.  
  9657. return (0)
  9658. go
  9659. grant execute on sp_helptext to public
  9660. go
  9661. dump transaction master with truncate_only
  9662. go
  9663. /* helpuser 49.1 9/10/91 */
  9664. create procedure sp_helpuser
  9665. @name_in_db varchar(30) = NULL
  9666. as
  9667.  
  9668. set nocount on
  9669.  
  9670. /*
  9671. **  If no @name_in_db, list all the users.
  9672. **  User ids are <= 16383 and group ids are > 16383.
  9673. */
  9674. if @name_in_db = NULL
  9675. begin
  9676.     select Users_name = substring(u.name, 1, 17), ID_in_db = u.uid,
  9677.         Group_name = substring(g.name, 1, 17),
  9678.         Login_name = substring(m.name, 1, 17),
  9679.         Default_db = substring(m.dbname, 1, 15)
  9680.             from sysusers u, sysusers g,
  9681.                 master.dbo.syslogins m
  9682.             where u.suid *= m.suid
  9683.                 and u.gid *= g.uid
  9684.                 and u.uid <= 16383 and u.uid > 0
  9685.             order by Users_name
  9686.     return (0)
  9687. end
  9688.  
  9689. /*
  9690. **  See if the @name_in_db is a user.  If so, print out the info for
  9691. **  the user, including users pretending to be the user (sysaliases).
  9692. */
  9693. if exists (select *
  9694.         from sysusers
  9695.             where name = @name_in_db
  9696.                 and uid <= 16383 and uid > 0)
  9697. begin
  9698.     select Users_name = substring(u.name, 1, 17), ID_in_db = u.uid,
  9699.         Group_name = substring(g.name, 1, 17),
  9700.         Login_name = substring(m.name, 1, 17),
  9701.         Default_db = substring(m.dbname, 1, 15)
  9702.             from sysusers u, sysusers g,
  9703.                 master.dbo.syslogins m
  9704.             where u.suid *= m.suid
  9705.                 and u.gid *= g.uid
  9706.                 and u.name = @name_in_db
  9707.                 and u.uid <= 16383 and u.uid > 0
  9708.  
  9709.     declare @suid    smallint
  9710.  
  9711.     select @suid = suid
  9712.         from sysusers 
  9713.             where name = @name_in_db
  9714.  
  9715.     /*
  9716.     ** If anyone is using @name_in_db as an alias print it out.
  9717.     */
  9718.     if exists (select *
  9719.             from sysalternates
  9720.         where altsuid = @suid)
  9721.     begin
  9722.         print "Users aliased to user."
  9723.         select Login_name = suser_name(suid)
  9724.             from sysalternates
  9725.                 where altsuid = @suid
  9726.             order by Login_name
  9727.     end
  9728.  
  9729.     return (0)
  9730. end
  9731.  
  9732. /*
  9733. **  Maybe the @name_in_db is a group.  Is so print out the info.
  9734. */
  9735. if exists (select *
  9736.         from sysusers
  9737.             where name = @name_in_db
  9738.                 and (uid > 16383 or uid = 0))
  9739. begin
  9740.     print "The name supplied is a group name."
  9741.     execute sp_helpgroup @name_in_db
  9742.     return (0)
  9743. end
  9744.  
  9745. /*
  9746. **  Last thing to try is that the @name_in_db might be aliased.
  9747. */
  9748. if exists (select *
  9749.         from sysalternates
  9750.             where suid = suser_id(@name_in_db))
  9751. begin
  9752.     print "The name supplied is aliased to another user."
  9753.     select Alias_name = substring(u.name, 1, 17), ID_in_db = u.uid,
  9754.         Group_name = substring(g.name, 1, 17),
  9755.         Login_name = substring(m.name, 1, 17),
  9756.         Default_db = substring(m.dbname, 1, 15)
  9757.             from sysusers u, sysusers g, sysalternates a,
  9758.                 master.dbo.syslogins m
  9759.             where u.suid *= m.suid
  9760.                 and u.gid *= g.uid
  9761.                 and u.uid <= 16383 and u.uid > 0
  9762.                 and a.suid = suser_id(@name_in_db)
  9763.                 and a.altsuid = u.suid
  9764.     return (0)
  9765. end
  9766.  
  9767. print "The name supplied is not a user, group, or aliased."
  9768.  
  9769. return (1)
  9770. go
  9771. grant execute on sp_helpuser to public
  9772. go
  9773. /* lock 49.1 9/10/91 */
  9774. create procedure sp_lock
  9775. @spid1 int = NULL,        /* server process id to check for locks */
  9776. @spid2 int = NULL        /* other process id to check for locks */
  9777. as
  9778.  
  9779. /*
  9780. **  Show the locks for both parameters.
  9781. */
  9782. if @spid1 is not NULL
  9783. begin
  9784.     select spid, locktype = name, table_id = id, page,
  9785.         dbname = substring(db_name(dbid), 1, 15)
  9786.     from syslocks, spt_values
  9787.         where syslocks.type = spt_values.number
  9788.             and spt_values.type = "L"
  9789.             and spid in (@spid1, @spid2)
  9790. end
  9791.  
  9792. /*
  9793. **  No parameters, so show all the locks.
  9794. */
  9795. else
  9796. begin
  9797.     select spid, locktype = name, table_id = id, page,
  9798.         dbname = substring(db_name(dbid), 1, 15)
  9799.     from syslocks, spt_values
  9800.         where syslocks.type = spt_values.number
  9801.             and spt_values.type = "L"
  9802.     order by spid, dbname, table_id, locktype, page
  9803. end
  9804.  
  9805. return (0)
  9806. go
  9807. grant execute on sp_lock to public
  9808. go
  9809. dump transaction master with truncate_only
  9810. go
  9811. /* logdevice    49.1    9/10/91 */
  9812. create procedure sp_logdevice
  9813. @dbname    varchar(30),        /* database name that has the syslogs */
  9814. @devname    varchar(30)    /* device name to put syslogs on */
  9815. as
  9816.  
  9817. declare @dbid    smallint    /* dbid of the database to be changed */
  9818. declare @dbuid    smallint    /* id of the owner of the database */
  9819. declare @logbit    int        /* this is the bit to turn on in sysusages */
  9820.  
  9821. select @logbit = 4        /* bit 3 is the one to turn on */
  9822.  
  9823. /*
  9824. **  Verify the database name and get the @dbid and @dbuid
  9825. */
  9826. select @dbid = dbid, @dbuid = suid
  9827.     from sysdatabases
  9828.         where name = @dbname
  9829.  
  9830. /*
  9831. **  If @dbname not found, say so and list the databases.
  9832. */
  9833. if @dbid = NULL
  9834. begin
  9835.     print "No such database -- run sp_helpdb to list databases."
  9836.     return (1)
  9837. end
  9838.  
  9839. /*
  9840. **  See if the device exists.
  9841. */
  9842. if not exists (select *
  9843.         from master.dbo.sysdevices
  9844.             where name like @devname)
  9845. begin
  9846.     print "No such device exists -- run sp_helpdevice to list the SQLServer devices."
  9847.     return (1)
  9848. end
  9849.  
  9850. /*
  9851. **  You must be SA or the dbo to execute this sproc.
  9852. */
  9853. if suser_id() != 1 and suser_id() != @dbuid
  9854. begin
  9855.     print "Only the System Administrator (SA) or the Database Owner (dbo) may move the syslogs table."
  9856.     return (1)
  9857. end
  9858.  
  9859. /*
  9860. **  Now see if the @dbname uses the @devname
  9861. */
  9862. if not exists (select *
  9863.         from sysusages u, sysdevices d
  9864.             where d.name = @devname
  9865.                 and u.vstart between d.low and d.high
  9866.                 and u.dbid = @dbid)
  9867. begin
  9868.     print "The specified device is not used by the database."
  9869.     return (1)
  9870. end
  9871.  
  9872. /*
  9873. **  Check if there are any additional segments on original device to be made
  9874. **  into log only (don't want to make the only segment not usable for data!)
  9875. */
  9876. if (select count(*)
  9877.     from master.dbo.sysusages u, master.dbo.sysdevices d
  9878.         where d.name = @devname
  9879.             and u.vstart between d.low and d.high
  9880.             and u.dbid = @dbid
  9881.             and lstart != 0) = 0
  9882. begin
  9883.     print "Can't make the only segment on original device log-only"
  9884.     return (1)
  9885. end
  9886.  
  9887. /*
  9888. **  clear the bit from any database segments that aren't already log only
  9889. */
  9890. update master.dbo.sysusages
  9891.     set segmap = segmap & ~@logbit
  9892.         from master.dbo.sysusages
  9893.             where dbid = @dbid
  9894.             and segmap != @logbit
  9895.  
  9896. /*
  9897. **  Now set the segments on @devname as log-only.
  9898. */
  9899. update master.dbo.sysusages
  9900.     set segmap = @logbit
  9901.         from master.dbo.sysusages u, master.dbo.sysdevices d
  9902.             where d.name = @devname
  9903.                 and u.vstart between d.low and d.high
  9904.                 and u.dbid = @dbid
  9905.                 and lstart != 0
  9906.  
  9907. /*
  9908. **  Now we need to activate the new segment map.
  9909. */
  9910. dbcc dbrepair (@dbname, remap)
  9911. print "Syslogs moved."
  9912.  
  9913. return (0)
  9914. go
  9915. grant execute on sp_logdevice to public
  9916. go
  9917. /* lookup 49.1 9/10/91 */
  9918. create procedure sp_lookup
  9919. @typearg int,
  9920. @owname    varchar(30) = "%", /* owner of the desired objects (except indexes) */
  9921. @obname    varchar(30) = "%", /* name of the desired objects (including indexes) */
  9922. @indtab    varchar(30) = "%", /* table associated with the desired index */
  9923. @indown varchar(30) = "%"  /* owner of the desired index */
  9924. as
  9925.     create table #ddtab
  9926.         (name    varchar(30),
  9927.          owner    varchar(30),
  9928.          type    int,
  9929.          indtab    varchar(30))
  9930.  
  9931. declare @table    int
  9932. declare @view    int
  9933. declare @index    int
  9934. declare @sproc    int
  9935. declare @trgr    int
  9936. declare @dflt    int
  9937. declare @rule    int
  9938. declare @dtype    int
  9939. declare @db    int
  9940. declare @report int
  9941.  
  9942. select @table = 1, @view = 2, @index = 4, @sproc = 8, @trgr = 16, @dflt = 32,
  9943.     @rule = 64, @dtype = 128, @db = 256, @report = 512
  9944.  
  9945. if (@typearg & @table = @table)
  9946.     insert into #ddtab
  9947.         select name, user_name(uid), @table, '' from dbo.sysobjects
  9948.             where name like @obname
  9949.                 and user_name(uid) like @owname
  9950.                 and (sysstat & 7 = 1    /* system table */
  9951.                   or sysstat & 7 = 3)    /* user table */
  9952.  
  9953.  
  9954. if (@typearg & @view = @view)
  9955.     insert into #ddtab
  9956.         select name, user_name(uid), @view, '' from dbo.sysobjects
  9957.             where name like @obname
  9958.                 and user_name(uid) like @owname
  9959.                   and sysstat & 7 = 2        /* view */
  9960.  
  9961. if (@typearg & @index = @index)
  9962.     insert into #ddtab
  9963.         select i.name, user_name(o.uid), @index, o.name
  9964.             from dbo.sysindexes i, dbo.sysobjects o
  9965.                 where i.name like @obname
  9966.                     and i.indid > 0
  9967.                     and i.id = o.id
  9968.                     and user_name(o.uid) like @indown
  9969.                     and o.name = object_name(o.id)
  9970.                     and o.name like @indtab
  9971.  
  9972. if (@typearg & @report = @report)
  9973.     insert into #ddtab
  9974.         select name, user_name(uid), @report, '' from dbo.sysobjects
  9975.             where name like @obname
  9976.                 and user_name(uid) like @owname
  9977.                 and sysstat & 7 = 4        /* sproc */
  9978.                 and userstat & -32768 = -32768    /* report */
  9979.  
  9980. if (@typearg & @sproc = @sproc)
  9981. begin
  9982.     /*
  9983.     **  If we also wanted reports, don't pick up reports again as sprocs.
  9984.     */
  9985.     if (@typearg & @report = @report)
  9986.     begin
  9987.         insert into #ddtab
  9988.             select name, user_name(uid), @sproc, '' from dbo.sysobjects
  9989.              where name like @obname
  9990.                 and user_name(uid) like @owname
  9991.                 and sysstat & 7 = 4    /* stored procedure */
  9992.                 and userstat & -32768 != -32768    /* report */
  9993.     end
  9994.     else
  9995.     begin
  9996.         insert into #ddtab
  9997.             select name, user_name(uid), @sproc, '' from dbo.sysobjects
  9998.              where name like @obname
  9999.                 and user_name(uid) like @owname
  10000.                 and sysstat & 7 = 4    /* stored procedure */
  10001.     end
  10002. end
  10003.  
  10004. if (@typearg & @trgr = @trgr)
  10005.     insert into #ddtab
  10006.         select name, user_name(uid), @trgr, '' from dbo.sysobjects
  10007.             where name like @obname
  10008.                 and user_name(uid) like @owname
  10009.                   and sysstat & 7 = 0    /* trigger */
  10010.  
  10011. if (@typearg & @dflt = @dflt)
  10012.     insert into #ddtab
  10013.         select name, user_name(uid), @dflt, '' from dbo.sysobjects
  10014.              where name like @obname
  10015.                  and user_name(uid) like @owname
  10016.                  and sysstat & 7 = 6        /* default */
  10017.  
  10018. if (@typearg & @rule = @rule)
  10019.     insert into #ddtab
  10020.         select name, user_name(uid), @rule, '' from dbo.sysobjects
  10021.              where name like @obname
  10022.                  and user_name(uid) like @owname
  10023.                  and sysstat & 7 = 7        /* rule */
  10024.  
  10025. if (@typearg & @dtype = @dtype)
  10026.     insert into #ddtab
  10027.         select name, user_name(uid), @dtype, '' from dbo.systypes
  10028.             where name like @obname
  10029.                 and user_name(uid) like @owname
  10030.  
  10031. if (@typearg & @db = @db)
  10032.     insert into #ddtab
  10033.         select name, suser_name(suid), @db, ''
  10034.             from master.dbo.sysdatabases
  10035.                 where (name like @obname)
  10036.                     and suser_name(suid) like @owname
  10037.  
  10038. select * from #ddtab order by name
  10039.  
  10040. drop table #ddtab
  10041.  
  10042. return (0)
  10043. go
  10044. grant execute on sp_lookup to public
  10045. go
  10046. dump transaction master with truncate_only
  10047. go
  10048. /* helpremotelogin 49.1 9/10/91 */
  10049. create procedure sp_helpremotelogin
  10050. @remoteserver varchar(30) = NULL,    /* remote server name */
  10051. @remotename varchar(30) = NULL        /* remote login name */
  10052. as
  10053.  
  10054. declare    @msg    varchar(100)
  10055.  
  10056. set nocount on
  10057.  
  10058. /*
  10059. **  If no server given, get 'em all.
  10060. */
  10061. if @remoteserver = null
  10062. begin
  10063.     select @remoteserver = "%"
  10064. end
  10065.  
  10066. else
  10067. begin
  10068.     if not exists (select *
  10069.             from master.dbo.sysservers s,
  10070.                 master.dbo.sysremotelogins r
  10071.             where s.srvid = r.remoteserverid
  10072.                 and s.srvname like @remoteserver
  10073.                 and s.srvid > 0)
  10074.     begin
  10075.         if @remoteserver = "%"
  10076.         begin
  10077.             select @msg = "There are no remote servers defined."
  10078.             print @msg
  10079.             return (1)
  10080.         end
  10081.  
  10082.         select @msg = "There are no remote logins for the remote server '"
  10083.             + @remoteserver + "'."
  10084.         print @msg
  10085.         return (1)
  10086.     end
  10087. end
  10088.  
  10089. /*
  10090. **  If no remotename given, get 'em all.
  10091. */
  10092. if @remotename = null
  10093. begin
  10094.     select @remotename = "%"
  10095. end
  10096.  
  10097. else
  10098. begin
  10099.     if not exists (select *
  10100.             from master.dbo.sysremotelogins
  10101.             where isnull(remoteusername, "") like @remotename)
  10102.     begin
  10103.         if @remotename = "%"
  10104.         begin
  10105.             select @msg = "There are no remotelogins defined."
  10106.             print @msg
  10107.             return (1)
  10108.         end
  10109.  
  10110.         select @msg = "There are no remote logins for '" 
  10111.             + @remotename + "'."
  10112.         print @msg
  10113.         return (1)
  10114.     end
  10115. end
  10116.  
  10117. /*
  10118. **  Check for empty results.
  10119. */
  10120. if not exists (select *
  10121.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  10122.         where isnull(r.remoteusername, "") like @remotename
  10123.             and s.srvid = r.remoteserverid
  10124.                 and s.srvname like @remoteserver)
  10125. begin
  10126.     if ((@remoteserver = '%') and (@remotename = '%'))
  10127.         select @msg = "There are no remote logins."
  10128.     else select @msg = "There are no remote logins for '" 
  10129.         + @remotename + "' on remote server '" + @remoteserver + "'."
  10130.     print @msg
  10131.     return (1)
  10132. end
  10133.  
  10134. /*
  10135. **  Select the information.
  10136. */
  10137. select server = substring(s.srvname, 1, 22),
  10138.     local_user_name = 
  10139.         substring(isnull(suser_name(r.suid), "** use local name **"), 1, 22),
  10140.     remote_user_name = 
  10141.         substring(isnull(r.remoteusername, "** mapped locally **"), 1, 22),
  10142.     options = substring(v.name, 1, 9)
  10143.         from master.dbo.sysservers s, master.dbo.sysremotelogins r,
  10144.             master.dbo.spt_values v
  10145.     where s.srvid = r.remoteserverid
  10146.         and s.srvname like @remoteserver
  10147.         and s.srvid > 0
  10148.         and isnull(r.remoteusername, "") like @remotename
  10149.         and v.type = "F"
  10150.         and v.number = r.status
  10151. order by server, remote_user_name
  10152.  
  10153. return (0)
  10154. go
  10155. grant execute on sp_helpremotelogin to public
  10156. go
  10157. /* helpsegment 49.1 9/10/91 */
  10158. create procedure sp_helpsegment
  10159. @segname varchar(30) = NULL        /* segment name */
  10160. as
  10161.  
  10162. declare    @segbit    int        /* this is the bit version of the segment # */
  10163. declare    @segment    int    /* the segment number of the segment */
  10164.  
  10165. set nocount on
  10166.  
  10167. /*
  10168. **  If no segment name given, get 'em all.
  10169. */
  10170. if @segname = null
  10171. begin
  10172.     select *
  10173.         from syssegments
  10174.     return (0)
  10175. end
  10176.  
  10177. /*
  10178. **  Make sure the segment exists
  10179. */
  10180. if not exists (select *
  10181.     from syssegments
  10182.         where name = @segname)
  10183. begin
  10184.     print "No such segment exists."
  10185.     return (1)
  10186. end
  10187.  
  10188. /*
  10189. **  Show the syssegment entry, then the fragments and size it is on,
  10190. **  then any dependent objects in the database.
  10191. */
  10192. select *
  10193.     from syssegments
  10194.         where name = @segname
  10195.  
  10196. /*
  10197. **  Set the bit position for the segment.
  10198. */
  10199. select @segment = segment
  10200.     from syssegments
  10201.         where name = @segname
  10202.  
  10203. /*
  10204. **  Now set the segments on @devname sysusages.
  10205. */
  10206. if (@segment < 31)
  10207.     select @segbit = power(2, @segment)
  10208. else
  10209.     /*
  10210.     **  Since this is segment 31, power(2, 31) will overflow
  10211.     **  since segmap is an int.  We'll grab the machine-dependent
  10212.     **  bit mask from spt_values to set the right bit.
  10213.     */
  10214.     select @segbit = low
  10215.         from master.dbo.spt_values
  10216.             where type = "E"
  10217.                 and number = 2
  10218.  
  10219. select device = d.name, size = convert(varchar(20),
  10220.         round((u.size * convert(float, v.low)) / 1048576, 0)) + "MB"
  10221.     from master.dbo.sysusages u, master.dbo.sysdevices d,
  10222.         master.dbo.spt_values v
  10223.         where u.segmap & @segbit = @segbit
  10224.             and d.low <= u.size + u.vstart
  10225.             and d.high >= u.size + u.vstart - 1
  10226.             and u.dbid = db_id()
  10227.             and d.status & 2 = 2 
  10228.             and v.number = 1
  10229.             and v.type = "E"
  10230.  
  10231. if exists (select *
  10232.         from sysindexes i, syssegments s
  10233.             where s.name = @segname
  10234.                 and s.segment = i.segment)
  10235. begin
  10236.     select table_name = object_name(i.id), index_name = i.name, i.indid
  10237.         from sysindexes i, syssegments s
  10238.             where s.name = @segname
  10239.                 and s.segment = i.segment
  10240.         order by table_name, indid
  10241. end
  10242.  
  10243. return (0)
  10244. go
  10245. grant execute on sp_helpsegment to public
  10246. go
  10247. dump transaction master with truncate_only
  10248. go
  10249. /* @(#)helpsort    49.1    9/10/91 */
  10250. create procedure sp_helpsort
  10251. AS
  10252. set nocount on
  10253.  
  10254. declare @sortid tinyint, @charid tinyint, @status tinyint
  10255. declare @image binary(1)
  10256. declare @count int, @end_flag int
  10257.     
  10258.  
  10259. declare @string varchar(66)
  10260. create table #helpsort3 (string varchar(66))
  10261.  
  10262. /*
  10263. ** Get the sortorder ID, character set ID, and status
  10264. */
  10265. select @sortid = id, @charid = csid, @image = convert(binary(1), definition)
  10266.     from syscharsets, syscurconfigs
  10267.     where id = value and config = 123
  10268.  
  10269. /*
  10270. ** Get the name and description of the character set and sort order
  10271. */
  10272. if (@sortid = NULL)
  10273.  BEGIN
  10274.     select @sortid = 0, @charid = 0, @status = 0
  10275.     insert #helpsort3 values ("Character Set = 0, ascii_8")
  10276.     insert #helpsort3 values ("    8-bit ASCII Character Set - System Default")
  10277.     insert #helpsort3 values ("Sort Order = 0, bin_ascii_8")
  10278.     insert #helpsort3 values ("    Binary Ordering - System Default")
  10279.  END
  10280. else
  10281.  BEGIN
  10282.     select @status = convert(tinyint, @image)
  10283.  
  10284.     select @string = "Character Set = " + convert(varchar(4), id) + ", " + name
  10285.         from syscharsets 
  10286.         where id = @charid and type = 1001
  10287.  
  10288.     insert into #helpsort3 values (@string)
  10289.  
  10290.     select @count = 1
  10291.     select @end_flag = 1
  10292.  
  10293.     while (@end_flag != 0)
  10294.     begin
  10295.         select @string = substring(description, @count, 62)
  10296.             from syscharsets 
  10297.             where id = @charid and type = 1001
  10298.         select @end_flag = isnull(ascii(@string), 0)
  10299.  
  10300.         if (@end_flag != 0)
  10301.         begin
  10302.             select @string = "    " + @string
  10303.             insert #helpsort3 values (@string)
  10304.         end
  10305.         select @count = @count + 62
  10306.     end
  10307.  
  10308.     select @string = "Sort Order = " + convert(varchar(4), id) + ", " + name
  10309.         from syscharsets 
  10310.         where id = @sortid and type = 2001
  10311.  
  10312.     insert into #helpsort3 values (@string)
  10313.  
  10314.     select @count = 1
  10315.     select @end_flag = 1
  10316.  
  10317.     while (@end_flag != 0)
  10318.     begin
  10319.         select @string = substring(description, @count, 62)
  10320.             from syscharsets 
  10321.             where id = @sortid and type = 2001
  10322.         select @end_flag = isnull(ascii(@string), 0)
  10323.  
  10324.         if (@end_flag != 0)
  10325.         begin
  10326.             select @string = "    " + @string
  10327.             insert #helpsort3 values (@string)
  10328.         end
  10329.  
  10330.         select @count = @count + 62
  10331.     end
  10332. END
  10333.  
  10334. select "Sort Order Description" = string from #helpsort3
  10335.  
  10336. /*
  10337. ** Build the sorted list of characters for this sort order.
  10338. ** If it's not stored in spt_values yet, then put the results in there.
  10339. */
  10340. if not exists (select * from spt_values where type = 'Z' and high = @sortid)
  10341. BEGIN
  10342.     create table #helpsort1 (char_val char(1))
  10343.      
  10344.  
  10345.  
  10346.     if @charid = 0       /*    ascii_8 has valid values from 32 to 126 */
  10347.     begin
  10348.             select @count = 32
  10349.             while @count <= 126
  10350.             begin
  10351.                 insert #helpsort1 (char_val)
  10352.                     values (char(@count))
  10353.                 select @count = @count + 1
  10354.             end
  10355.  
  10356.     end
  10357.  
  10358.     if @charid = 1      /*     iso_1 has values from 32 to 126, 160 to 255 */
  10359.     begin
  10360.             select @count = 32
  10361.             while @count <= 126
  10362.             begin
  10363.                 insert #helpsort1 (char_val)
  10364.                     values (char(@count))
  10365.                 select @count = @count + 1
  10366.             end
  10367.  
  10368.             select @count = 160
  10369.             while @count <= 255
  10370.             begin
  10371.                    insert #helpsort1 (char_val)
  10372.                         values (char(@count))
  10373.                     select @count = @count + 1
  10374.             end
  10375.     end
  10376.  
  10377.  
  10378.     if @charid = 2      /*     cp850 has valid values from 0 to 255 */
  10379.     begin
  10380.  
  10381.             select @count = 32
  10382.             while @count <= 255
  10383.             begin
  10384.                 insert #helpsort1 (char_val)
  10385.                     values (char(@count))
  10386.                 select @count = @count + 1
  10387.             end
  10388.     end
  10389.  
  10390.     if @charid = 3      /*     cp437 has valid values from 0 to 255 */
  10391.     begin
  10392.             select @count =32 
  10393.             while @count <= 255
  10394.             begin
  10395.                 insert #helpsort1 (char_val)
  10396.                     values (char(@count))
  10397.                 select @count = @count + 1
  10398.             end
  10399.     end
  10400.     
  10401.      if @charid > 3     /* User Character Set Not recognised by sp_helpsort */
  10402.     begin
  10403.         
  10404.             select "Unknown Character Set :   " + name from syscharsets 
  10405.                     where id = @sortid and type = 2001
  10406.         return (0)
  10407.     end
  10408.  
  10409.     select (char_val) into #helpsort2
  10410.         from #helpsort1
  10411.         order by char_val
  10412.     
  10413.     declare @char char(1)
  10414.     declare @char_str varchar(20)
  10415.     declare @incr int
  10416.     declare @seq_num int
  10417.     declare @rel char(1)
  10418.     declare @old_char char(1)
  10419.     
  10420.     select @char = char(0), @seq_num = 0, @count = 0, @incr = 8, @end_flag = 1
  10421.     select @rel = " ", @old_char = NULL
  10422.     
  10423.     set rowcount 1
  10424.    
  10425.     while @end_flag != 0
  10426.     begin
  10427.     
  10428.         select @char_str = ""
  10429.     
  10430.     /* If PST_NOTUNIQ is set, then possibly caseless */
  10431.     if (@status & 8 = 8)
  10432.             while (@count < @incr  and  @end_flag != 0)
  10433.             begin
  10434.                 select @char = char_val
  10435.                     from #helpsort2
  10436.                     where char_val >= @char
  10437.                 select @end_flag = @@rowcount
  10438.  
  10439.         if (@end_flag != 0)
  10440.         begin
  10441.             if (@char = @old_char)
  10442.             select @rel = "="
  10443.             else
  10444.             select @rel = " "
  10445.  
  10446.             select @old_char = @char
  10447.             select @char_str = @char_str + @rel + @char
  10448.             delete from #helpsort2 where ascii(@char) = ascii(char_val)
  10449.     
  10450.             select @count = @count + 1
  10451.         end
  10452.             end
  10453.     else
  10454.             while (@count < @incr  and  @end_flag != 0)
  10455.             begin
  10456.                 select @char = char_val 
  10457.                     from #helpsort2
  10458.                     where char_val > @char
  10459.                 select @end_flag = @@rowcount
  10460.  
  10461.         if (@end_flag != 0)
  10462.         begin
  10463.             select @char_str = @char_str + @rel + @char
  10464.             delete from #helpsort2 where ascii(@char) = ascii(char_val)
  10465.  
  10466.             select @count = @count + 1
  10467.         end
  10468.             end
  10469.  
  10470.         select @incr = @incr + 8, @seq_num = @seq_num + 1
  10471.         insert spt_values (name, number, type, low, high) 
  10472.         values (@char_str, @seq_num, 'Z', @charid, @sortid)
  10473.     end
  10474.     
  10475.     set rowcount 0
  10476.     
  10477. END
  10478.  
  10479. /*
  10480. ** Display the sorted list of characters.
  10481. */
  10482. select @count = 1
  10483. select @end_flag = 1
  10484. select @incr = 5
  10485.  
  10486. truncate table #helpsort3
  10487.  
  10488. while @end_flag != 0
  10489. BEGIN
  10490.     select @string = ""
  10491.  
  10492.     while @count < @incr
  10493.     begin
  10494.         select @string = @string + substring(name, 2, 19) 
  10495.             from spt_values
  10496.             where type = 'Z' and number = @count and high = @sortid
  10497.         select @end_flag = @@rowcount
  10498.         select @count = @count + 1
  10499.     end
  10500.  
  10501.     select @incr = @incr + 4
  10502.  
  10503.     insert into #helpsort3 values (@string)
  10504. END
  10505.  
  10506. select "Characters, in Order" = string 
  10507.     from #helpsort3
  10508.  
  10509. set nocount off
  10510.  
  10511. return(0)
  10512. go
  10513. grant execute on sp_helpsort to public
  10514. go
  10515. /* helpserver 49.1 9/10/91 */
  10516. create procedure sp_helpserver
  10517. @server varchar(30) = NULL        /* server name */
  10518. as
  10519.  
  10520. declare    @msg    varchar(100)
  10521.  
  10522. set nocount on
  10523.  
  10524. /*
  10525. **  If no server name given, get 'em all.
  10526. */
  10527. if @server = null
  10528. begin
  10529.     select @server = "%"
  10530. end
  10531.  
  10532. /*
  10533. **  Does the server exist?
  10534. */
  10535. if not exists (select *
  10536.         from master.dbo.sysservers 
  10537.             where srvname like @server
  10538.                 or srvnetname like @server)
  10539. begin
  10540.     if @server = "%"
  10541.         select @msg = "There are no servers defined."
  10542.     else select @msg = "There is no server named '" + @server + "'."
  10543.     print @msg
  10544.     return (1)
  10545. end
  10546.     
  10547. /*
  10548. **  Display server information.
  10549. **  First put it into a temp table so we can blot inappropriate status 
  10550. **  for the local server.  It's ugly but I couldn't think of another way.
  10551. */
  10552. create table #spt_server
  10553. (
  10554.     name    varchar(30),
  10555.     network    varchar(30),
  10556.     status    varchar(20),
  10557.     id    smallint
  10558. )
  10559. insert into #spt_server
  10560.     select s.srvname, s.srvnetname, status = isnull(v.name, ""), 
  10561.         id = s.srvid
  10562.     from master.dbo.sysservers s, master.dbo.spt_values v
  10563.         where s.srvstatus *= v.number
  10564.             and v.type = "A"
  10565.             and (s.srvname like @server
  10566.                 or s.srvnetname like @server)
  10567. update #spt_server
  10568.     set status = ""
  10569.         from master.dbo.sysservers s, #spt_server t
  10570.             where s.srvid = 0
  10571.                 and s.srvname = t.name
  10572.  
  10573. select name, network_name = substring(network, 1, 28), 
  10574.     status = substring(status, 1, 11), id = convert(char(4), id)
  10575.         from #spt_server
  10576.     order by name
  10577.  
  10578. return (0)
  10579. go
  10580. grant execute on sp_helpserver to public
  10581. go
  10582. dump transaction master with truncate_only
  10583. go
  10584. /* markreport 49.1 9/10/91 */
  10585. create procedure sp_markreport
  10586. @reportname varchar(92)        /* proc name we want to mark as report */
  10587. as
  10588.  
  10589. /*
  10590. **  Make sure the @reportname is local to the current database.
  10591. */
  10592. if @reportname like "%.%.%" and
  10593.     substring(@reportname, 1, charindex(".", @reportname) - 1) != db_name()
  10594. begin
  10595.     print "Report must be in your current database."
  10596.     return (1)
  10597. end
  10598.  
  10599. /*
  10600. **  Make sure that it actually exists as a sproc.
  10601. */
  10602. if not exists (select *
  10603.         from sysobjects
  10604.             where id = object_id(@reportname)
  10605.             and sysstat & 7 = 4    /* stored procedure */
  10606.             and uid = user_id())
  10607. begin
  10608.     print "No such report exists."
  10609.     return (1)
  10610. end
  10611.  
  10612. update sysobjects
  10613.     set userstat = userstat | -32768    /* high bit means report */
  10614.         from sysobjects
  10615.             where id = object_id(@reportname)
  10616.             and sysstat & 7 = 4        /* stored procedure */
  10617.             and uid = user_id()
  10618.  
  10619. return (0)
  10620. go
  10621. grant execute on sp_markreport to public
  10622. go
  10623. /* monitor 49.1 9/10/91 */
  10624. create procedure sp_monitor
  10625. as
  10626.  
  10627. /*
  10628. **  Declare variables to be used to hold current monitor values.
  10629. */
  10630. declare @now         datetime
  10631. declare @cpu_busy     int
  10632. declare @io_busy    int
  10633. declare @idle        int
  10634. declare @pack_received    int
  10635. declare @pack_sent    int
  10636. declare @pack_errors    int
  10637. declare @connections    int
  10638. declare @total_read    int
  10639. declare @total_write    int
  10640. declare @total_errors    int
  10641.  
  10642. declare @oldcpu_busy     int    /* used to see if DataServer has been rebooted */
  10643. declare @interval    int
  10644. declare @mspertick    int    /* milliseconds per tick */
  10645.  
  10646. /*
  10647. **  If we're in a transaction, disallow this since it might make recovery
  10648. **  impossible.
  10649. */
  10650. if @@trancount > 0
  10651. begin
  10652.     print "Can't run sp_monitor from within a transaction."
  10653.     return (1)
  10654. end
  10655.  
  10656. /*
  10657. **  Set @mspertick.  This is just used to make the numbers easier to handle
  10658. **  and avoid overflow.
  10659. */
  10660. select @mspertick = convert(int, @@timeticks / 1000.0)
  10661.  
  10662. /*
  10663. **  Get current monitor values.
  10664. */
  10665. select
  10666.     @now = getdate(),
  10667.     @cpu_busy = @@cpu_busy,
  10668.     @io_busy = @@io_busy,
  10669.     @idle = @@idle,
  10670.     @pack_received = @@pack_received,
  10671.     @pack_sent = @@pack_sent,
  10672.     @connections = @@connections,
  10673.     @pack_errors = @@packet_errors,
  10674.     @total_read = @@total_read,
  10675.     @total_write = @@total_write,
  10676.     @total_errors = @@total_errors
  10677.  
  10678. /*
  10679. **  Check to see if DataServer has been rebooted.  If it has then the
  10680. **  value of @@cpu_busy will be less than the value of spt_monitor.cpu_busy.
  10681. **  If it has update spt_monitor.
  10682. */
  10683. select @oldcpu_busy = cpu_busy
  10684.     from spt_monitor
  10685. if @oldcpu_busy > @cpu_busy
  10686. begin
  10687.     update spt_monitor
  10688.         set
  10689.             lastrun = @now,
  10690.             cpu_busy = @cpu_busy,
  10691.             io_busy = @io_busy,
  10692.             idle = @idle,
  10693.             pack_received = @pack_received,
  10694.             pack_sent = @pack_sent,
  10695.             connections = @connections,
  10696.             pack_errors = @pack_errors,
  10697.             total_read = @total_read,
  10698.             total_write = @total_write,
  10699.             total_errors = @total_errors
  10700. end
  10701.  
  10702. /*
  10703. **  Now print out old and new monitor values.
  10704. */
  10705. set nocount on
  10706. select @interval = datediff(ss, lastrun, @now)
  10707.     from spt_monitor
  10708.  
  10709. /* To prevent a divide by zero error when run for the first 
  10710. ** time after boot up
  10711. */
  10712. if @interval = 0
  10713.     select @interval = 1
  10714. select last_run = lastrun, current_run = @now, seconds = @interval
  10715.     from spt_monitor
  10716.  
  10717. select
  10718.     cpu_busy = substring(convert(varchar(11),
  10719.         convert(int, ((@cpu_busy * @mspertick) / 1000)))
  10720.         + "("
  10721.         + convert(varchar(11), convert(int, (((@cpu_busy - cpu_busy)
  10722.         * @mspertick) / 1000)))
  10723.         + ")"
  10724.         + "-"
  10725.         + convert(varchar(11), convert(int, ((((@cpu_busy - cpu_busy)
  10726.         * @mspertick) / 1000) * 100) / @interval))
  10727.         + "%",
  10728.         1, 25),
  10729.     io_busy = substring(convert(varchar(11),
  10730.         convert(int, ((@io_busy * @mspertick) / 1000)))
  10731.         + "("
  10732.         + convert(varchar(11), convert(int, (((@io_busy - io_busy)
  10733.         * @mspertick) / 1000)))
  10734.         + ")"
  10735.         + "-"
  10736.         + convert(varchar(11), convert(int, ((((@io_busy - io_busy)
  10737.         * @mspertick) / 1000) * 100) / @interval))
  10738.         + "%",
  10739.         1, 25),
  10740.     idle = substring(convert(varchar(11),
  10741.         convert(int, ((@idle * @mspertick) / 1000)))
  10742.         + "("
  10743.         + convert(varchar(11), convert(int, (((@idle - idle)
  10744.         * @mspertick) / 1000)))
  10745.         + ")"
  10746.         + "-"
  10747.         + convert(varchar(11), convert(int, ((((@idle - idle)
  10748.         * @mspertick) / 1000) * 100) / @interval))
  10749.         + "%",
  10750.         1, 25)
  10751. from spt_monitor
  10752.  
  10753. select
  10754.     packets_received = substring(convert(varchar(11), @pack_received) + "(" +
  10755.         convert(varchar(11), @pack_received - pack_received) + ")", 1, 25),
  10756.     packets_sent = substring(convert(varchar(11), @pack_sent) + "(" +
  10757.         convert(varchar(11), @pack_sent - pack_sent) + ")", 1, 25),
  10758.     packet_errors = substring(convert(varchar(11), @pack_errors) + "(" +
  10759.         convert(varchar(11), @pack_errors - pack_errors) + ")", 1, 25)
  10760. from spt_monitor
  10761.  
  10762. select
  10763.     total_read = substring(convert(varchar(11), @total_read) + "(" +
  10764.         convert(varchar(11), @total_read - total_read) + ")", 1, 19),
  10765.     total_write = substring(convert(varchar(11), @total_write) + "(" +
  10766.         convert(varchar(11), @total_write - total_write) + ")", 1, 19),
  10767.     total_errors = substring(convert(varchar(11), @total_errors) + "(" +
  10768.         convert(varchar(11), @total_errors - total_errors) + ")", 1, 19),
  10769.     connections = substring(convert(varchar(11), @connections) + "(" +
  10770.         convert(varchar(11), @connections - connections) + ")", 1, 18)
  10771. from spt_monitor
  10772.  
  10773. /*
  10774. **  Now update spt_monitor
  10775. */
  10776. update spt_monitor
  10777.     set
  10778.         lastrun = @now,
  10779.         cpu_busy = @cpu_busy,
  10780.         io_busy = @io_busy,
  10781.         idle = @idle,
  10782.         pack_received = @pack_received,
  10783.         pack_sent = @pack_sent,
  10784.         connections = @connections,
  10785.         pack_errors = @pack_errors,
  10786.         total_read = @total_read,
  10787.         total_write = @total_write,
  10788.         total_errors = @total_errors
  10789.  
  10790. return (0)
  10791. go
  10792. dump transaction master with truncate_only
  10793. go
  10794. /* password 49.1 9/10/91 */
  10795. create procedure sp_password
  10796. @old varchar(30) = NULL,        /* the old (current) password */
  10797. @new varchar(30),            /* the new password */
  10798. @loginame varchar(30) = NULL        /* user to change password on */
  10799. as
  10800.  
  10801. declare @suid int            /* suid of person to change pw on */
  10802.  
  10803. /*
  10804. **  If we're in a transaction, disallow this since it might make recovery
  10805. **  impossible.
  10806. */
  10807. if @@trancount > 0
  10808. begin
  10809.     print "Can't run sp_password from within a transaction."
  10810.     return (1)
  10811. end
  10812.  
  10813. /*
  10814. **  If the system administrator (sa) is running this command, the sa can
  10815. **  change anyone's password and doesn't have to know the old password.
  10816. **  We have this case is suser_id() = 1 and @loginame is NOT NULL.
  10817. **  In this case, we'll fill in the old password and set the suid.
  10818. */
  10819. if suser_id() != 1 and @loginame != NULL
  10820. begin
  10821.     /*
  10822.     **  Only the sa can use the @loginame.
  10823.     */
  10824.     print "Only System Administrator (SA) can use loginame option -- password not changed."
  10825.     return (1)
  10826. end
  10827.  
  10828. /*
  10829. **  Does the SA want to change someone's password?
  10830. */
  10831. if suser_id() = 1 and @loginame != NULL
  10832. begin
  10833.     /*  Check to see if the user exists. */
  10834.     select @suid = suid
  10835.         from syslogins
  10836.     where name = @loginame
  10837.  
  10838.     if @suid = NULL
  10839.     begin
  10840.         print "No such login -- no password changed."
  10841.         return (1)
  10842.     end
  10843.  
  10844.     /*
  10845.     **  Get the old password for the user.  If @old not given, don't
  10846.     **  bother checking against the old password.
  10847.     */
  10848.     if @old != null
  10849.     begin
  10850.         select @suid = suid
  10851.             from syslogins
  10852.         where name = @loginame and password = @old
  10853.  
  10854.         if @suid = NULL
  10855.         begin
  10856.             print "Old (current) password incorrect for user -- password not changed."
  10857.             return (1)
  10858.         end
  10859.     end
  10860. end
  10861.  
  10862. /*
  10863. **  This is just the normal case when someone wants to change their own
  10864. **  password.  Set the @suid.
  10865. */
  10866. else if @loginame = NULL
  10867. begin
  10868.     select @suid = suid
  10869.         from syslogins
  10870.     where password = @old
  10871.         and suid = suser_id()
  10872. end
  10873. if @suid = NULL
  10874. begin
  10875.     print "Old (current) password incorrect for user -- password not changed."
  10876.     return (1)
  10877. end
  10878.  
  10879. /*
  10880. **  This is the final case.  The sa is running the command to change
  10881. **  someone else's password and is supplying the old password.  Confirm
  10882. **  that the old password is correct.
  10883. */
  10884. if suser_id() = 1
  10885. begin
  10886.     select @suid = suid
  10887.         from syslogins
  10888.     where password = @old
  10889.         and suid = suser_id(@loginame)
  10890.  
  10891.     if @suid = NULL
  10892.     begin
  10893.         print "Old (current) password incorrect for user -- password not changed."
  10894.         return (1)
  10895.     end
  10896. end
  10897.  
  10898. /*
  10899. **  If @suid still hasn't been set then the user running the procedure
  10900. **  doesn't have the right current password.
  10901. */
  10902. if @suid = NULL
  10903. begin
  10904.     print "Old (current) password incorrect for user -- password not changed."
  10905.     return (1)
  10906. end
  10907.  
  10908. /*
  10909. **  Everything is consistent so change the password.
  10910. */
  10911. update syslogins
  10912.     set password = @new
  10913.         where suid = @suid
  10914.  
  10915. print 'Password changed.'
  10916.  
  10917. return (0)
  10918. go
  10919. grant execute on sp_password to public
  10920. go
  10921. /* placeobject    49.1    9/10/91 */
  10922. create procedure sp_placeobject
  10923. @segname    varchar(30),    /* segment name */
  10924. @objname    varchar(62)    /* object name */
  10925. as
  10926.  
  10927. declare    @msg    char(100)
  10928. declare @objid    int
  10929. declare @tabname    varchar(30)
  10930. declare @indexname    varchar(30)
  10931. declare    @indexid    smallint
  10932. declare    @segment    smallint
  10933.  
  10934. /*
  10935. **  Check to see if the segment exists.
  10936. */
  10937. select @segment = segment
  10938.         from syssegments
  10939.             where name = @segname
  10940. if @segment = null
  10941. begin
  10942.     select @msg = "There is no such segment as '" + @segname + "'."
  10943.     print @msg
  10944.     return (1)
  10945. end
  10946.  
  10947. /*
  10948. **  Are we dealing with a table or it's index?
  10949. */
  10950. if @objname like "%.%"
  10951. begin
  10952.  
  10953.     /*
  10954.     **  Get the table name out.
  10955.     */
  10956.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  10957.     select @indexname = substring(@objname, charindex(".", @objname) + 1, 61)
  10958. end
  10959. else
  10960. begin
  10961.     select @tabname = @objname, @indexname = null
  10962. end
  10963.  
  10964. /*
  10965. **  Can't use placeobject to move syslogs.
  10966. */
  10967. if (object_id(@tabname) = 8)
  10968. begin
  10969.     print "Use sp_logdevice to move syslogs table."
  10970.     return (1)
  10971. end
  10972.  
  10973. /*
  10974. **  Can't move system tables.
  10975. */
  10976. if (object_id(@tabname) < 100)
  10977. begin
  10978.     print "You can't move system tables."
  10979.     return (1)
  10980. end
  10981.  
  10982. /*
  10983. **  If the user is not the dbo or the SA, make sure he/she owns it.
  10984. */
  10985. if suser_id() != 1 and user_id() != 1
  10986. begin
  10987.     if not exists (select *
  10988.             from sysobjects
  10989.                 where name = @tabname
  10990.                     and uid = user_id())
  10991.     begin
  10992.         select @msg = "You do not own a table named '" + @tabname
  10993.             + "'."
  10994.         print @msg
  10995.         return (1)
  10996.     end
  10997. end
  10998. else
  10999. begin
  11000.     /*
  11001.     **  If dbo or SA, make sure the table exists.
  11002.     */
  11003.     if not exists (select *
  11004.             from sysobjects
  11005.                 where name = @tabname)
  11006.     begin
  11007.         select @msg = "There is no table named '" + @tabname + "'."
  11008.         print @msg
  11009.         return (1)
  11010.     end
  11011. end
  11012.  
  11013. /*
  11014. **  If we are dealing with an index, make sure it exists.
  11015. */
  11016. if @indexname != null and not exists (select *
  11017.                     from sysindexes
  11018.                         where id = object_id(@tabname)
  11019.                             and name = @indexname)
  11020. begin
  11021.     select @msg = "There is no index named '" + @indexname
  11022.         + "' for table '" + @tabname + "'."
  11023.     print @msg
  11024.     return (1)
  11025. end
  11026.  
  11027. /*
  11028. **  Everything's ok so change the segment for the object.
  11029. */
  11030. if @indexname = null
  11031. begin
  11032.     select @indexid = indid
  11033.         from sysindexes
  11034.             where id = object_id(@tabname)
  11035.                 and indid < 2
  11036. end
  11037. else
  11038. begin
  11039.     select @indexid = indid
  11040.         from sysindexes
  11041.             where id = object_id(@tabname)
  11042.                 and name = @indexname
  11043. end
  11044.  
  11045. update sysindexes
  11046. set segment = @segment
  11047.     from sysindexes
  11048.         where id = object_id(@tabname)
  11049.             and indid = @indexid
  11050.  
  11051. declare @dbname    varchar(30)
  11052. select @dbname = db_name()
  11053. dbcc dbrepair (@dbname, remap)
  11054. select @msg = "'" + @objname + "' is now on segment '" + @segname + "'."
  11055. print @msg
  11056.  
  11057. return (0)
  11058. go
  11059. grant execute on sp_placeobject to public
  11060. go
  11061. dump transaction master with truncate_only
  11062. go
  11063. /* primarykey 49.1 9/10/91 */
  11064. create procedure sp_primarykey
  11065. @tabname  varchar(92),            /* table name that has the key */
  11066. @col1     varchar(30),             /* columns that make up the key */
  11067. @col2     varchar(30) = NULL,
  11068. @col3     varchar(30) = NULL,
  11069. @col4     varchar(30) = NULL,
  11070. @col5     varchar(30) = NULL,
  11071. @col6     varchar(30) = NULL,
  11072. @col7     varchar(30) = NULL,
  11073. @col8     varchar(30) = NULL
  11074. as
  11075.  
  11076. declare @objid int            /* object id of the table */
  11077. declare @uid smallint            /* owner id of the object */
  11078. declare @cnt smallint            /* howmany columns in key */
  11079. declare @key1 tinyint            /* colids of the columns in the key */
  11080. declare @key2 tinyint
  11081. declare @key3 tinyint
  11082. declare @key4 tinyint
  11083. declare @key5 tinyint
  11084. declare @key6 tinyint
  11085. declare @key7 tinyint
  11086. declare @key8 tinyint
  11087.  
  11088. /*
  11089. **  Check to see that the tabname is local.
  11090. */
  11091. if @tabname like "%.%.%" and
  11092.     substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  11093. begin
  11094.     print "Table or view name must be in 'current' database."
  11095.     return (1)
  11096. end
  11097.  
  11098. /*
  11099. **  See if we can find the object.  It must be a system table, user table,
  11100. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the 
  11101. **  object type is -- it's more reliable than using sysobjects.type which
  11102. **  could change.
  11103. */
  11104. select @objid = id, @uid = uid
  11105.     from sysobjects
  11106.         where id = object_id(@tabname)
  11107.             and (sysstat & 7 = 1        /* system table */
  11108.                 or sysstat & 7 = 2    /* view */
  11109.                 or sysstat & 7 = 3)    /* user table */
  11110.  
  11111. if @objid is NULL
  11112. begin
  11113.     print "No table or view exists with the supplied name."
  11114.     return (1)
  11115. end 
  11116.  
  11117. /*
  11118. **  Check to see that object owner is adding the key.
  11119. */
  11120. if @uid != user_id()
  11121. begin
  11122.     print "Only the owner of the table may define a primary key."
  11123.     return (1)
  11124. end
  11125.  
  11126. /*
  11127. **  See if the primary key exists already.  Syskeys.type = 1 indicates
  11128. **  a primary key.
  11129. */
  11130. if exists (select *
  11131.         from syskeys
  11132.             where id = @objid
  11133.                 and type = 1)
  11134. begin
  11135.     print "Primary key already exists on table -- drop key first."
  11136.     return (1)
  11137. end
  11138.  
  11139. /*
  11140. **  Now check out each column argument to verify it's existence.
  11141. */
  11142. select @cnt = 1, @key1 = colid
  11143.     from syscolumns
  11144.         where name = @col1 
  11145.             and id = @objid
  11146. if @key1 is NULL
  11147. begin
  11148.     print "The table has no such first column."
  11149.     return (1)
  11150. end
  11151.  
  11152. if @col2 != NULL
  11153. begin
  11154.     select @cnt = @cnt + 1, @key2 = colid
  11155.         from syscolumns
  11156.             where name = @col2 
  11157.                 and id = @objid
  11158.     if @key2 is NULL
  11159.     begin
  11160.         print "The table has no such second column."
  11161.         return (1)
  11162.     end
  11163. end
  11164. else goto doinsert
  11165.  
  11166. if @col3 != NULL
  11167. begin
  11168.     select @cnt = @cnt + 1, @key3 = colid
  11169.         from syscolumns
  11170.             where name = @col3 
  11171.                 and id = @objid
  11172.     if @key3 is NULL
  11173.     begin
  11174.         print "The table has no such third column."
  11175.         return (1)
  11176.     end
  11177. end
  11178. else goto doinsert
  11179.  
  11180. if @col4 != NULL
  11181. begin
  11182.     select @cnt = @cnt + 1, @key4 = colid
  11183.         from syscolumns
  11184.             where name = @col4 
  11185.                 and id = @objid
  11186.     if @key4 is NULL
  11187.     begin
  11188.         print "The table has no such fourth column."
  11189.         return (1)
  11190.     end
  11191. end
  11192. else goto doinsert
  11193.  
  11194. if @col5 != NULL
  11195. begin
  11196.     select @cnt = @cnt + 1, @key5 = colid
  11197.         from syscolumns
  11198.             where name = @col5 
  11199.                 and id = @objid
  11200.     if @key5 is NULL
  11201.     begin
  11202.         print "The table has no such fifth column."
  11203.         return (1)
  11204.     end
  11205. end
  11206. else goto doinsert
  11207.  
  11208. if @col6 != NULL
  11209. begin
  11210.     select @cnt = @cnt + 1, @key6 = colid
  11211.         from syscolumns
  11212.             where name = @col6 
  11213.                 and id = @objid
  11214.     if @key6 is NULL
  11215.     begin
  11216.         print "The table has no such sixth column."
  11217.         return (1)
  11218.     end
  11219. end
  11220. else goto doinsert
  11221.  
  11222. if @col7 != NULL
  11223. begin
  11224.     select @cnt = @cnt + 1, @key7 = colid
  11225.         from syscolumns
  11226.             where name = @col7 
  11227.                 and id = @objid
  11228.     if @key7 is NULL
  11229.     begin
  11230.         print "The table has no such seventh column."
  11231.         return (1)
  11232.     end
  11233. end
  11234. else goto doinsert
  11235.  
  11236. if @col8 != NULL
  11237. begin
  11238.     select @cnt = @cnt + 1, @key8 = colid
  11239.         from syscolumns
  11240.             where name = @col8 
  11241.                 and id = @objid
  11242.     if @key8 is NULL
  11243.     begin
  11244.         print "The table has no such eighth column."
  11245.         return (1)
  11246.     end
  11247. end
  11248.  
  11249. /*
  11250. **  Type 1 is a primary key, 2 is a foreignkey, and 3 is a commonjoin.
  11251. */
  11252. doinsert:
  11253.  
  11254. insert into syskeys
  11255.     (id, type, depid, keycnt, size, key1, key2, key3, key4, key5,
  11256.         key6, key7, key8)
  11257. values (@objid, 1, NULL, @cnt, 0, @key1, @key2, @key3, @key4, @key5,
  11258.         @key6, @key7, @key8)   
  11259.  
  11260. print "New primary key added."
  11261.  
  11262. return (0)
  11263. go
  11264. grant execute on sp_primarykey to public
  11265. go
  11266. /* recompile 49.1 9/10/91 */
  11267. create procedure sp_recompile
  11268. @objname    varchar(92)
  11269. as
  11270.  
  11271. declare @id        int
  11272. declare @type    smallint
  11273. declare @uid        smallint
  11274. declare @errmsg    varchar(250)
  11275.  
  11276. if @objname like '%%.%%.%%'
  11277. begin
  11278.     select @errmsg = 'Table ' + @objname + ' must be in current database (' + db_name() + ')'
  11279.     print @errmsg
  11280.     return
  11281. end
  11282.  
  11283. select @id = id, @type = sysstat & 7, @uid = uid
  11284. from sysobjects
  11285. where id = object_id(@objname)
  11286.  
  11287. if @id = null
  11288. begin
  11289.     select @errmsg = 'Object ' + @objname + ' is not a table in database ' + db_name() + '.'
  11290.     print @errmsg
  11291.     return
  11292. end
  11293.  
  11294. if @type = 1
  11295. begin
  11296.     select @errmsg = @objname + ' is a system table.'
  11297.     print @errmsg
  11298.     print 'sp_recompile cannot be used on system tables.'
  11299.     return
  11300. end
  11301. if @type != 3
  11302. begin
  11303.     select @errmsg = 'Object ' + @objname + ' is not a table.'
  11304.     print @errmsg
  11305.     return
  11306. end
  11307.  
  11308. if user_id() != 1 and @uid != user_id()
  11309. begin
  11310.     select @errmsg = 'You do not own table ' + @objname + '.'
  11311.     print @errmsg
  11312.     return
  11313. end
  11314.  
  11315. set nocount on
  11316.  
  11317. update sysobjects
  11318. set schema = schema + 1
  11319. where id = @id
  11320.  
  11321. select @errmsg = 'Each stored procedure and trigger that uses table ' + @objname
  11322. print @errmsg
  11323. print 'will be recompiled the next time it is executed.'
  11324. go
  11325. grant execute on sp_recompile to public
  11326. go
  11327. dump transaction master with truncate_only
  11328. go
  11329. /* remoteoption 49.1 9/10/91 */
  11330. create procedure sp_remoteoption
  11331. @remoteserver varchar(30) = NULL,    /* server name to change */
  11332. @loginame varchar(30) = NULL,        /* user's remote name */
  11333. @remotename varchar(30) = NULL,        /* user's local user name */
  11334. @optname varchar(20) = NULL,        /* option name to turn on/off */
  11335. @optvalue varchar(10) = NULL        /* true or false */
  11336. as
  11337.  
  11338. declare @statvalue smallint        /* number of option */
  11339. declare @optcount int            /* number of options like @optname */
  11340. declare    @msg    varchar(100)
  11341. declare @suid    smallint
  11342.  
  11343. /*
  11344. **  If no @server given, just list the possible remote login options.
  11345. **  Only certain status bits may be set or cleared.  
  11346. **       settable                        not settable
  11347. **      ------------------------------  --------------------------
  11348. **    trusted (1)
  11349. */
  11350. if @remoteserver = null
  11351. begin
  11352.     print "Settable remote login options."
  11353.     select remotelogin_option = name
  11354.         from spt_values
  11355.             where type = "F"
  11356.                 and number in (1, 1)
  11357.                 and number > 0
  11358.         order by name
  11359.     return (0)
  11360. end
  11361.  
  11362. /*
  11363. **  If @loginame is NULL then we want to set @suid = -1. Otherwise get
  11364. **  it real value.
  11365. */
  11366. if @loginame = null
  11367.     select @suid = -1
  11368. else select @suid = suser_id(@loginame)
  11369.  
  11370. /*
  11371. **  Verify the server name, local, and remote names. 
  11372. */
  11373. if not exists (select *
  11374.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  11375.             where r.remoteserverid = s.srvid
  11376.                 and s.srvname = @remoteserver
  11377.                 and r.remoteusername = @remotename
  11378.                 and r.suid = @suid)
  11379. begin
  11380.     select @msg = "There is no remote user '" + isnull(@remotename, "NULL")
  11381.         + "' mapped to local user '" + isnull(@loginame, "NULL")
  11382.         + "' on remote server '" + @remoteserver + "'."
  11383.     print @msg
  11384.     return (1)
  11385. end
  11386.  
  11387. /*
  11388. **  Check remaining parameters.
  11389. */
  11390. if @optname is NULL or @optvalue not in ("true", "false") or @optvalue = null
  11391. begin
  11392.     print "Usage: sp_remoteoption [remoteserver, loginame, remotename, optname, {true | false}]"
  11393.     return (1)
  11394. end
  11395.  
  11396. /*
  11397. **  Use @optname and try to find the right option.
  11398. **  If there isn't just one, print appropriate diagnostics and return.
  11399. */
  11400. select @optcount = count(*)
  11401.         from spt_values
  11402.         where name like "%" + @optname + "%" and type = "F"
  11403.             and number in (1, 1)
  11404.             and number > 0
  11405.  
  11406. /*
  11407. **  If no option, show the user what the options are.
  11408. */
  11409. if @optcount = 0
  11410. begin
  11411.     print "Remote login option doesn't exist or can't be set by user."
  11412.     print "Run sp_remoteoption with no parameters to see options."
  11413.     return (1)
  11414. end
  11415.  
  11416. /*
  11417. **  If more than one option like @optname, show the duplicates and return.
  11418. */
  11419. if @optcount > 1
  11420. begin
  11421.     print "Remote login option is not unique."
  11422.  
  11423.     select duplicate_option = name
  11424.         from spt_values
  11425.             where name like "%" + @optname + "%"
  11426.                 and type = "F"
  11427.                 and number in (1, 1)
  11428.                 and number > 0
  11429.  
  11430.     return (1)
  11431. end
  11432.  
  11433. /*
  11434. **  Only the SA or the dbo of @dbnbame can execute the update part
  11435. **  of this procedure so check.  Also the sproc must be executed in the master
  11436. **  database.
  11437. */
  11438. if suser_id() != 1
  11439. begin
  11440.     print "Only the System Administrator (SA) may set remote login options."
  11441.     return (1)
  11442. end
  11443.  
  11444. /*
  11445. **  If we're in a transaction, disallow this since it might make recovery
  11446. **  impossible.
  11447. */
  11448. if @@trancount > 0
  11449. begin
  11450.     print "Can't run sp_remoteoption from within a transaction."
  11451.     return (1)
  11452. end
  11453.  
  11454. /*
  11455. **  Get the number which is the bit value to set
  11456. */
  11457. select @statvalue = number
  11458.         from spt_values
  11459.         where name like "%" + @optname + "%" and type = "F"
  11460.             and number in (1, 1)
  11461.             and number > 0
  11462.  
  11463.  
  11464. /*
  11465. **  Now update sysremotelogins.
  11466. */
  11467. if @optvalue = "true"
  11468. begin
  11469.     update master.dbo.sysremotelogins
  11470.         set status = status | @statvalue
  11471.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  11472.             where r.remoteserverid = s.srvid
  11473.                 and s.srvname = @remoteserver
  11474.                 and r.remoteusername = @remotename
  11475.                 and r.suid = @suid
  11476.     select @msg = "Option '" + @optname + "' turned on."
  11477.     print @msg
  11478.     return (0)
  11479.  
  11480. end
  11481.  
  11482. /*
  11483. **  We want to turn it off.
  11484. */
  11485. else
  11486. begin
  11487.     update master.dbo.sysremotelogins
  11488.         set status = status & ~@statvalue
  11489.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  11490.             where r.remoteserverid = s.srvid
  11491.                 and s.srvname = @remoteserver
  11492.                 and r.remoteusername = @remotename
  11493.                 and r.suid = @suid
  11494.     select @msg = "Option '" + @optname + "' turned off."
  11495.     print @msg
  11496.     return (0)
  11497. end
  11498.  
  11499. return (0)
  11500. go
  11501. grant execute on sp_remoteoption to public
  11502. go
  11503. /* rename 49.1 9/10/91 */
  11504. create procedure sp_rename
  11505. @objname varchar(61),            /* old (current) object name */
  11506. @newname sysname(30)            /* new name we want to call it */
  11507. as
  11508.  
  11509. declare @objid int            /* object id of the thing to rename */
  11510. declare @msg varchar(250)
  11511. declare @returncode    int
  11512.  
  11513. if @objname like "%.%.%"
  11514. begin
  11515.     print "Object must be in the current database."
  11516.     return (1)
  11517. end
  11518.  
  11519. /*
  11520. **  Check to see that the @newname is valid.
  11521. */
  11522. execute @returncode = sp_validname @newname
  11523. if @returncode != 0
  11524. begin
  11525.     select @msg = "'" + @newname + "' is not a valid name."
  11526.     print @msg
  11527.     return @returncode
  11528. end
  11529.  
  11530. /*
  11531. **  Check to see if we are renaming a table/column (or table/index)
  11532. **  or a table or usertype.
  11533. **  If @objname is of the form table.column then it's a column.
  11534. **  In the column case, we need to extract and verify the table and
  11535. **  column names and make sure the user owns the table that is getting
  11536. **  the rule bound.
  11537. */
  11538. if @objname like "%.%"
  11539. begin
  11540.     declare @tabname varchar(30)        /* name of table */
  11541.     declare @colname varchar(30)        /* name of column */
  11542.     declare @colid tinyint            /* colid of the column */
  11543.  
  11544.     /*
  11545.     **  Get the table name out.
  11546.     */
  11547.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  11548.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  11549.  
  11550.     /*
  11551.     **  Now check to see that the column exists in a table owned
  11552.     **  by the user.
  11553.     */
  11554.     select @objid = syscolumns.id, @colid = syscolumns.colid
  11555.         from sysobjects, syscolumns
  11556.     where syscolumns.id = sysobjects.id
  11557.         and sysobjects.name = @tabname
  11558.         and syscolumns.name = @colname
  11559.         and (sysobjects.sysstat & 7 = 2        /* user table */
  11560.         or sysobjects.sysstat & 7 = 3)        /* view */
  11561.         and sysobjects.uid = user_id()
  11562.  
  11563.     /*
  11564.     **  Did we find it?  If not, it might be an index name so we'll
  11565.     **  check in the next case.
  11566.     */
  11567.     if @objid != null
  11568.     begin
  11569.         /*
  11570.         **  Check to make sure that there is no already a column
  11571.         **  that has the new name.
  11572.         */
  11573.         if exists (select *
  11574.                 from syscolumns
  11575.                     where id = @objid
  11576.                         and name = @newname)
  11577.         begin
  11578.             select @msg = "There is already a column named '"
  11579.                 + @newname + "'"
  11580.                 + " in table '"
  11581.                 + @tabname
  11582.                 + "'."
  11583.             print @msg
  11584.             return (1)
  11585.         end
  11586.  
  11587.         /*
  11588.         **  Go ahead and change the column name.
  11589.         */
  11590.         update syscolumns
  11591.             set name = @newname
  11592.                 from syscolumns
  11593.             where id = @objid
  11594.                 and colid = @colid
  11595.  
  11596.         print "Column name has been changed."
  11597.         return (0)
  11598.     end
  11599.  
  11600.     /*
  11601.     **  Might be an index name.
  11602.     */
  11603.     else
  11604.     begin
  11605.         select @objid = sysindexes.id, @colid = sysindexes.indid
  11606.             from sysobjects, sysindexes
  11607.         where sysindexes.id = sysobjects.id
  11608.             and sysobjects.name = @tabname
  11609.             and sysindexes.name = @colname
  11610.             and sysindexes.indid != 0
  11611.             and sysobjects.sysstat & 7 = 3    /* user table */
  11612.             and sysobjects.uid = user_id()
  11613.  
  11614.         /*
  11615.         **  If @objid is still NULL then that means it isn't an
  11616.         **  index name.  We checked above to see if it was a column
  11617.         **  name so now it's time to give up.
  11618.         */
  11619.         if @objid = null
  11620.         begin
  11621.             print "You do not own a table or column (or index) of that name in the current database."
  11622.             return (1)
  11623.         end
  11624.  
  11625.         /*
  11626.         **  Check to make sure that there is no already an index
  11627.         **  that has the new name.
  11628.         */
  11629.         if exists (select *
  11630.                 from sysindexes
  11631.                     where id = @objid
  11632.                         and name = @newname)
  11633.         begin
  11634.             select @msg = "There is already an index named '"
  11635.                 + @newname + "'"
  11636.                 + " for table '"
  11637.                 + @tabname
  11638.                 + "'."
  11639.             print @msg
  11640.             return (1)
  11641.         end
  11642.  
  11643.         /*
  11644.         **  Go ahead and change the index name.
  11645.         */
  11646.         update sysindexes
  11647.             set name = @newname
  11648.                 from sysindexes
  11649.             where id = @objid
  11650.                 and indid = @colid
  11651.  
  11652.         print "Index name has been changed."
  11653.         return (0)
  11654.     end
  11655. end
  11656.  
  11657. /*
  11658. **  Check to see if the object exists and is owned by the user.
  11659. **  It will either be in the sysobjects table or the systypes table.
  11660. **  Check sysobjects first.
  11661. **  System objects have ids < 100 and we don't allow their names to change.
  11662. */
  11663. select @objid = id
  11664.     from sysobjects
  11665.         where id = object_id(@objname)
  11666.             and uid = user_id()
  11667.             and id > 99
  11668.  
  11669. /*
  11670. **  Don't allow the newname to begin with #.
  11671. */
  11672. if @objid != null
  11673. begin
  11674.     if substring(@newname, 1, 1) = "#"
  11675.     begin
  11676.         print "Object names beginning with '#' are not allowed."
  11677.         return (1)
  11678.     end
  11679. end
  11680.  
  11681. /*
  11682. **  It's not in sysobjects so check systypes.
  11683. */
  11684. if @objid is NULL
  11685. begin
  11686.     select @objid = usertype
  11687.         from systypes
  11688.             where name = @objname
  11689.                 and uid = user_id()
  11690.                 and usertype > 99
  11691.  
  11692.     /*
  11693.     **  It's nowhere to be found so quit.
  11694.     */
  11695.     if @objid is NULL
  11696.     begin
  11697.         print "Object name cannot be changed either because it does not exist in this database, or you don't own it, or it is a system name."
  11698.         return (1)
  11699.     end
  11700.  
  11701.     /* Check that newname doesn't already exist. */
  11702.     if exists (select *
  11703.             from systypes
  11704.                 where name = @newname)
  11705.     begin
  11706.         print "Newname already exists in systypes."
  11707.         return (1)
  11708.     end
  11709.  
  11710.     /*
  11711.     ** Change the name of a datatype
  11712.     */
  11713.     update systypes
  11714.         set name = @newname
  11715.             where usertype = @objid
  11716.  
  11717.     print "User datatype name has been changed."
  11718.     return (0)
  11719. end
  11720.  
  11721. /*
  11722. **  It might be a table so update the index entry for the table IF it has
  11723. **  no clustered index.  No clustered index means that there is an entry
  11724. **  for the table under the @objid with indid = 0.
  11725. */
  11726. begin transaction
  11727.  
  11728.     /* Check that newname doesn't already exist. */
  11729.     if exists (select *
  11730.             from sysobjects
  11731.                 where name = @newname)
  11732.     begin
  11733.         print "Newname already exists in sysobjects."
  11734.         rollback transaction
  11735.         return (1)
  11736.     end
  11737.  
  11738.     update sysobjects
  11739.         set name = @newname
  11740.             where id = @objid
  11741.  
  11742.      /*
  11743.      **  This might not do anything -- only if we are dealing with a
  11744.      **  table that has no clustered index.
  11745.      **  Also change the name of the text entry, if any.
  11746.      */
  11747.      update sysindexes
  11748.         set name = @newname
  11749.             where id = @objid
  11750.                 and indid = 0
  11751.      update sysindexes
  11752.         set name = "t" + @newname
  11753.             where id = @objid
  11754.                 and indid = 255
  11755. commit transaction
  11756.  
  11757. print "Object name has been changed."
  11758.  
  11759. return (0)
  11760. go
  11761. grant execute on sp_rename to public
  11762. go
  11763. dump transaction master with truncate_only
  11764. go
  11765. /* renamedb 49.1 9/10/91 */
  11766. create procedure sp_renamedb
  11767. @dbname sysname(30),            /* old (current) db name */
  11768. @newname sysname(30)            /* new name we want to call it */
  11769. as
  11770.  
  11771. declare @objid int            /* object id of the thing to rename */
  11772. declare @msg varchar(250)
  11773. declare @bitdesc varchar(30)        /* bit description for the db */
  11774. declare @curdbid int            /* id of database to be changed */
  11775.  
  11776. /*
  11777. **  If we're in a transaction, disallow this since it might make recovery
  11778. **  impossible.
  11779. */
  11780. if @@trancount > 0
  11781. begin
  11782.     print "Can't run sp_renamedb from within a transaction."
  11783.     return (1)
  11784. end
  11785.  
  11786. /*
  11787. **  Only the SA can do this.
  11788. */
  11789. if suser_id() != 1
  11790. begin
  11791.     print "Only the System Administrator (SA) can change the name of a database."
  11792.     return (1)
  11793. end
  11794.  
  11795. /*
  11796. **  Make sure the database exists.
  11797. */
  11798. if not exists (select *
  11799.         from master.dbo.sysdatabases
  11800.             where name = @dbname)
  11801. begin
  11802.     print "No specified database currently exists."
  11803.     return (1)
  11804. end
  11805.  
  11806. /*
  11807. **  Make sure that the @newname db doesn't already exist.
  11808. */
  11809. if exists (select *
  11810.         from master.dbo.sysdatabases
  11811.             where name = @newname)
  11812. begin
  11813.     print "A database with the new name already exists."
  11814.     return (1)
  11815. end
  11816.  
  11817. /*
  11818. **  Check to see that the @newname is valid.
  11819. */
  11820. declare @returncode    int
  11821. execute @returncode = sp_validname @newname
  11822. if @returncode != 0
  11823. begin
  11824.     select @msg = "'" + @newname + "' is not a valid name."
  11825.     print @msg
  11826.     return @returncode
  11827. end
  11828.  
  11829. /*
  11830. **  Don't allow the names of master, tempdb, and model to be changed.
  11831. */
  11832. if @dbname in ("master", "model", "tempdb")
  11833. begin
  11834.     print "The databases 'master', 'model', and 'tempdb' cannot be renamed."
  11835.     return (1)
  11836. end
  11837.  
  11838. /* Get current database id */ 
  11839. select @curdbid = db_id(@dbname) 
  11840.  
  11841. /* 
  11842. ** Check single user bit (4096) 
  11843. ** Database must be in single user mode to necessitate the rid update in the
  11844. ** database's DBTABLE
  11845. */ 
  11846. select @bitdesc = null 
  11847. select @bitdesc = v.name
  11848.         from spt_values v, master.dbo.sysdatabases d 
  11849.                 where d.dbid = @curdbid 
  11850.                         and v.type = "D" 
  11851.                         and d.status & v.number = 4096 
  11852. if @bitdesc = null 
  11853. begin 
  11854.         select @msg = "System Administrator (SA) must set database '" + @dbname + "'"
  11855.         print @msg
  11856.         print "to single-user mode with sp_dboption before using sp_renamedb."
  11857.         return(1)  
  11858. end  
  11859.  
  11860. /*
  11861. **  Everything is ok so do it.
  11862. */
  11863. update master.dbo.sysdatabases
  11864.     set name = @newname
  11865.         where name = @dbname
  11866.  
  11867. print "Database is renamed and in single-user mode."
  11868. print "System Administrator (SA) must reset it to multi-user mode with sp_dboption."
  11869.  
  11870. return (0)
  11871. go
  11872. /* serveroption 49.1 9/10/91 */
  11873. create procedure sp_serveroption
  11874. @server varchar(30) = NULL,        /* server name to change */
  11875. @optname varchar(20) = NULL,        /* option name to turn on/off */
  11876. @optvalue varchar(10) = NULL        /* true or false */
  11877. as
  11878.  
  11879. declare @srvid int            /* id of the server */
  11880. declare @statvalue smallint        /* number of option */
  11881. declare @optcount int            /* number of options like @optname */
  11882.  
  11883. /*
  11884. **  If no @server given, just list the possible server options.
  11885. **  Only certain status bits may be set or cleared.  
  11886. **  The default is to allow timeouts (bit 0 is clear).
  11887. **       settable                        not settable
  11888. **      ------------------------------  --------------------------
  11889. **      timeouts (0)
  11890. */
  11891. if @server = null
  11892. begin
  11893.     print "Settable server options."
  11894.     select server_option = name
  11895.         from spt_values
  11896.             where type = "A"
  11897.                 and number not in (-1, 1)
  11898.         order by name
  11899.     return (0)
  11900. end
  11901.  
  11902. /*
  11903. **  Verify the server name and get the @srvid
  11904. */
  11905. select @srvid = srvid
  11906.     from master.dbo.sysservers
  11907.         where srvname = @server
  11908.  
  11909. /*
  11910. **  If @server not found, say so. 
  11911. */
  11912. if @srvid = NULL
  11913. begin
  11914.     print "No such server -- run sp_helpserver to list servers."
  11915.     return (1)
  11916. end
  11917.  
  11918. /*
  11919. **  Check remaining parameters.
  11920. */
  11921. if @optname is NULL or @optvalue not in ("true", "false") or @optvalue = null
  11922. begin
  11923.     print "usage: sp_serveroption [server, optname, {true | false}]"
  11924.     return (1)
  11925. end
  11926.  
  11927. /*
  11928. **  Use @optname and try to find the right option.
  11929. **  If there isn't just one, print appropriate diagnostics and return.
  11930. */
  11931. select @optcount = count(*)
  11932.         from spt_values
  11933.         where name like "%" + @optname + "%" and type = "A"
  11934.             and number not in (-1, 1)
  11935.  
  11936. /*
  11937. **  If no option, show the user what the options are.
  11938. */
  11939. if @optcount = 0
  11940. begin
  11941.     print "Server option doesn't exist or can't be set by user."
  11942.     print "Run sp_serveroption with no parameters to see options."
  11943.     return (1)
  11944. end
  11945.  
  11946. /*
  11947. **  If more than one option like @optname, show the duplicates and return.
  11948. */
  11949. if @optcount > 1
  11950. begin
  11951.     print "Server option is not unique."
  11952.  
  11953.     select duplicate_option = name
  11954.         from spt_values
  11955.             where name like "%" + @optname + "%"
  11956.                 and type = "A"
  11957.                 and number not in (-1, 1)
  11958.  
  11959.     return (1)
  11960. end
  11961.  
  11962. /*
  11963. **  Only the SA or the dbo of @dbnbame can execute the update part
  11964. **  of this procedure so check.  Also the sproc must be executed in the master
  11965. **  database.
  11966. */
  11967. if suser_id() != 1
  11968. begin
  11969.     print "Only the System Administrator (SA) may set server options."
  11970.     return (1)
  11971. end
  11972.  
  11973. /*
  11974. **  Currently there is only one option that can be set -- no timeouts --
  11975. **  and it only applies to remote servers.  If the current server is
  11976. **  local then reject it.
  11977. */
  11978. if @srvid = 0
  11979. begin
  11980.     print "Option can be set for remote servers only -- not the local server."
  11981.     return (1)
  11982. end
  11983.  
  11984.  
  11985. /*
  11986. **  If we're in a transaction, disallow this since it might make recovery
  11987. **  impossible.
  11988. */
  11989. if @@trancount > 0
  11990. begin
  11991.     print "Can't run sp_serveroption from within a transaction."
  11992.     return (1)
  11993. end
  11994. /*
  11995. **  Get the number which is the bit value to set
  11996. */
  11997. select @statvalue = number
  11998.         from spt_values
  11999.         where name like "%" + @optname + "%" and type = "A"
  12000.             and number > 0
  12001.  
  12002.  
  12003. /*
  12004. **  Now update sysservers.
  12005. **  The timeouts option is handled a little strangely since the default
  12006. **  is timeouts.  Therefore timeouts = true means to clear the bit
  12007. **  and timeouts = false means to set the bit.
  12008. */
  12009. if @optvalue = "true"
  12010. begin
  12011.     update sysservers
  12012.         set srvstatus = srvstatus & ~@statvalue
  12013.             where srvid = @srvid
  12014. end
  12015.  
  12016. /*
  12017. **  We want to turn it off.
  12018. */
  12019. else
  12020. begin
  12021.     update sysservers
  12022.         set srvstatus = srvstatus | @statvalue
  12023.             where srvid = @srvid
  12024. end
  12025.  
  12026. return (0)
  12027. go
  12028. grant execute on sp_serveroption to public
  12029. go
  12030. dump transaction master with truncate_only
  12031. go
  12032. /* spaceused 49.1 9/10/91 */
  12033. create procedure sp_spaceused
  12034. @objname varchar(92) = null        /* the object we want size on */
  12035. as
  12036.  
  12037. declare @id    int            /* the object id of @objname */
  12038. declare @type    smallint        /* the object type */
  12039. declare    @pages    int            /* working variable for size calc. */
  12040.  
  12041. /*
  12042. **  Check to see that the objname is local.
  12043. */
  12044. if @objname != null
  12045. begin
  12046.     if @objname like "%.%.%" and
  12047.         substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  12048.     begin
  12049.         print "Object must be in 'current' database."
  12050.         return (1)
  12051.     end
  12052.  
  12053.     /*
  12054.     **  Try to find the object.
  12055.     */
  12056.     select @id = id, @type = sysstat & 7
  12057.         from sysobjects
  12058.             where id = object_id(@objname)
  12059.  
  12060.     /*
  12061.     **  Does the object exist?
  12062.     */
  12063.     if @id = null
  12064.     begin
  12065.         print "Object is not a table in the database."
  12066.         return (1)
  12067.     end
  12068.  
  12069.     /*
  12070.     **  See if it's a space object.
  12071.     **  types are:
  12072.     **    0 - trigger
  12073.     **    1 - system table
  12074.     **    2 - view
  12075.     **    3 - user table
  12076.     **    4 - sproc
  12077.     **    6 - default
  12078.     **    7 - rule
  12079.     */
  12080.     if not exists (select *
  12081.             from sysindexes
  12082.                 where @id = id
  12083.                     and indid < 2)
  12084.     begin
  12085.         if @type in (0, 4, 6, 7)
  12086.         begin
  12087.             print "Object is stored in 'sysprocedures' and has no space allocated directly."
  12088.             return (1)
  12089.         end
  12090.  
  12091.         if @type = 2
  12092.         begin
  12093.             print "Views don't have space allocated."
  12094.             return (1)
  12095.         end
  12096.     end
  12097.  
  12098. end
  12099.  
  12100. /*
  12101. **  We need to create a temp table to do the calculation.
  12102. **  reserved: sum(reserved) where indid in (0, 1, 255)
  12103. **  data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text)
  12104. **  index: sum(used) where indid in (0, 1, 255) - data
  12105. **  unused: sum(reserved) - sum(used) where indid in (0, 1, 255)
  12106. */
  12107. create table #spt_space
  12108. (
  12109.     rows        int null,
  12110.     reserved    int null,
  12111.     data        int null,
  12112.     indexp        int null,
  12113.     unused        int null
  12114. )
  12115. set nocount on
  12116.  
  12117. /*
  12118. **  If @id is null, then we want summary data.
  12119. */
  12120. if @id = null
  12121. begin
  12122.     select distinct database_name = db_name(), database_size =
  12123.         substring(convert(varchar(10),
  12124.             round( (d.low * convert(float, sum(size)))
  12125.             / 1048576, 0)) + " " + "MB", 1, 20)
  12126.         from master.dbo.sysusages, master.dbo.spt_values d
  12127.             where dbid = db_id()
  12128.                 and d.number = 1
  12129.                 and d.type = "E"
  12130.             having dbid = db_id()
  12131.                 and d.number = 1
  12132.                 and d.type = "E"
  12133.  
  12134.     /*
  12135.     **  Now calculate the summary data.
  12136.     **  reserved: sum(reserved) where indid in (0, 1, 255)
  12137.     */
  12138.     insert into #spt_space (reserved)
  12139.         select sum(reserved)
  12140.             from sysindexes
  12141.                 where indid in (0, 1, 255)
  12142.  
  12143.     /*
  12144.     ** data: sum(dpages) where indid < 2
  12145.     **    + sum(used) where indid = 255 (text)
  12146.     */
  12147.     select @pages = sum(dpages)
  12148.             from sysindexes
  12149.                 where indid < 2
  12150.     select @pages = @pages + isnull(sum(used), 0)
  12151.         from sysindexes
  12152.             where indid = 255
  12153.     update #spt_space
  12154.         set data = @pages
  12155.  
  12156.     
  12157.     /* index: sum(used) where indid in (0, 1, 255) - data */
  12158.     update #spt_space
  12159.         set indexp = (select sum(used)
  12160.                 from sysindexes
  12161.                     where indid in (0, 1, 255))
  12162.                 - data
  12163.  
  12164.     /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  12165.     update #spt_space
  12166.         set unused = reserved 
  12167.                 - (select sum(used)
  12168.                     from sysindexes
  12169.                         where indid in (0, 1, 255))
  12170.  
  12171.     select reserved = substring(convert(varchar(15),
  12172.         round((reserved * convert(float, d.low)) / 1024, 0))
  12173.             + " " + "KB", 1, 15),
  12174.         data = substring(convert(varchar(15),
  12175.         round((data * convert(float, d.low)) / 1024, 0))
  12176.             + " " + "KB", 1, 15),
  12177.         index_size = substring(convert(varchar(15),
  12178.         round((indexp * convert(float, d.low)) / 1024, 0))
  12179.             + " " + "KB", 1, 15),
  12180.         unused = substring(convert(varchar(15),
  12181.         round((unused * convert(float, d.low)) / 1024, 0))
  12182.             + " " + "KB", 1, 15)
  12183.             from #spt_space, master.dbo.spt_values d
  12184.         where d.number = 1
  12185.             and d.type = "E"
  12186. end
  12187.  
  12188. /*
  12189. **  We want a particular object.
  12190. */
  12191. else
  12192. begin
  12193.     /*
  12194.     **  Now calculate the summary data.
  12195.     **  reserved: sum(reserved) where indid in (0, 1, 255)
  12196.     */
  12197.     insert into #spt_space (reserved)
  12198.         select sum(reserved)
  12199.             from sysindexes
  12200.                 where indid in (0, 1, 255)
  12201.                     and id = @id
  12202.  
  12203.     /*
  12204.     ** data: sum(dpages) where indid < 2
  12205.     **    + sum(used) where indid = 255 (text)
  12206.     */
  12207.     select @pages = sum(dpages)
  12208.             from sysindexes
  12209.                 where indid < 2
  12210.                     and id = @id
  12211.     select @pages = @pages + isnull(sum(used), 0)
  12212.         from sysindexes
  12213.             where indid = 255
  12214.                 and id = @id
  12215.     update #spt_space
  12216.         set data = @pages
  12217.  
  12218.     
  12219.     /* index: sum(used) where indid in (0, 1, 255) - data */
  12220.     update #spt_space
  12221.         set indexp = (select sum(used)
  12222.                 from sysindexes
  12223.                     where indid in (0, 1, 255)
  12224.                         and id = @id)
  12225.                 - data
  12226.  
  12227.     /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  12228.     update #spt_space
  12229.         set unused = reserved 
  12230.                 - (select sum(used)
  12231.                     from sysindexes
  12232.                         where indid in (0, 1, 255)
  12233.                             and id = @id)
  12234.     update #spt_space
  12235.         set rows = i.rows
  12236.             from sysindexes i
  12237.                 where i.indid < 2
  12238.                     and i.id = @id
  12239.  
  12240.     select name = substring(object_name(@id), 1, 20),
  12241.         rows = convert(char(11), rows),
  12242.         reserved = substring(convert(varchar(15),
  12243.             round((reserved * convert(float, d.low)) / 1024, 0))
  12244.                 + " " + "KB", 1, 10),
  12245.         data = substring(convert(varchar(15),
  12246.             round((data * convert(float, d.low)) / 1024, 0))
  12247.                 + " " + "KB", 1, 10),
  12248.         index_size = substring(convert(varchar(15),
  12249.             round((indexp * convert(float, d.low)) / 1024, 0))
  12250.                 + " " + "KB", 1, 10),
  12251.         unused = substring(convert(varchar(15),
  12252.             round((unused * convert(float, d.low)) / 1024, 0))
  12253.                 + " " + "KB", 1, 10)
  12254.     from #spt_space, master.dbo.spt_values d
  12255.         where d.number = 1
  12256.             and d.type = "E"
  12257. end
  12258.  
  12259. return (0)
  12260. go
  12261. grant execute on sp_spaceused to public
  12262. go
  12263. /* unbindefault 49.1 9/10/91 */
  12264. create procedure sp_unbindefault
  12265. @objname varchar(61),            /* table/column or datatype name */
  12266. @futureonly varchar(15) = NULL        /* flag to indicate extent of binding */
  12267. as
  12268.  
  12269. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  12270.                     ** the binding to be limited */
  12271.  
  12272. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  12273.  
  12274. /*
  12275. **  When a default or rule is bound to a user-defined datatype, it is also
  12276. **  bound, by default, to any columns of the user datatype that are currently
  12277. **  using the existing default or rule as their default or rule.  This default
  12278. **  action may be overridden by setting @futureonly = @futurevalue when the
  12279. **  procedure is invoked.  In this case existing columns with the user
  12280. **  datatype won't have their existing default or rule changed.
  12281. */
  12282.  
  12283. /*
  12284. **  Check to see that the object names are local to the current database.
  12285. */
  12286. if @objname like "%.%.%"
  12287. begin
  12288.     print "Column or usertype must be in 'current' database."
  12289.     return (1)
  12290. end
  12291.  
  12292. /*
  12293. **  If @objname is of the form tab.col then we are unbinding a column.
  12294. **  Otherwise its a datatype.  In the column case, we need to extract
  12295. **  and verify the table and column names and make sure the user owns
  12296. **  the table that is getting the default unbound.
  12297. */
  12298. if @objname like "%.%"
  12299. begin
  12300.     declare @tabname varchar(30)        /* name of table */
  12301.     declare @colname varchar(30)        /* name of column */
  12302.  
  12303.     /*
  12304.     **  Get the table name out.
  12305.     */
  12306.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  12307.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  12308.  
  12309.     /*
  12310.     **  Find it and unbind it.
  12311.     */
  12312.     if exists (select *
  12313.             from syscolumns, sysobjects
  12314.         where syscolumns.id = object_id(@tabname)
  12315.             and syscolumns.name = @colname
  12316.             and sysobjects.id = object_id(@tabname)
  12317.             and uid = user_id()
  12318.             and sysobjects.sysstat & 7 = 3)      /* user table */
  12319.     begin
  12320.         /*
  12321.         **  Is there something bound to it?
  12322.         */
  12323.         if exists (select *
  12324.             from syscolumns, sysobjects
  12325.         where syscolumns.id = object_id(@tabname)
  12326.             and syscolumns.name = @colname
  12327.             and sysobjects.id = object_id(@tabname)
  12328.             and uid = user_id()
  12329.             and sysobjects.sysstat & 7 = 3      /* user table */
  12330.             and cdefault != 0)
  12331.         begin
  12332.             update syscolumns
  12333.                 set cdefault = 0
  12334.                     from syscolumns, sysobjects
  12335.                 where syscolumns.id = object_id(@tabname)
  12336.                     and syscolumns.name = @colname
  12337.                     and sysobjects.id = object_id(@tabname)
  12338.                     and uid = user_id()
  12339.                     and sysobjects.sysstat & 7 = 3
  12340.  
  12341.             /*
  12342.             **  Since binding a default is a schema change,
  12343.             **  update schema count
  12344.             **  for the object in the sysobjects table.
  12345.             */
  12346.             update sysobjects
  12347.                 set schema = schema + 1
  12348.                     from sysobjects
  12349.                         where id = object_id(@tabname)
  12350.                             and uid = user_id()
  12351.  
  12352.             print "Default unbound from table column."
  12353.             return (0)
  12354.         end
  12355.  
  12356.         else
  12357.         begin
  12358.             print "The specified column has no default."
  12359.             return (1)
  12360.         end
  12361.     end
  12362.  
  12363.     /*
  12364.     **  If we got here, there was no such object.
  12365.     */
  12366.     print "You do not own a table with a column of that name."
  12367.     return (1)
  12368.  
  12369. end
  12370. else
  12371. begin
  12372.     /*
  12373.     **  We're unbinding to a user type.  In this case, the @objname
  12374.     **  is really the name of the user datatype.
  12375.     **  When we unbind to a user type, any existing columns get changed
  12376.     **  to the new binding unless their current binding is not equal
  12377.     **  to the current binding for the usertype or if they set the
  12378.     **  @futureonly parameter to @futurevalue.
  12379.     */
  12380.     declare @olddefault int            /* current default for type */
  12381.  
  12382.     /*
  12383.     **  Get the current default for the datatype.
  12384.     */
  12385.     select @olddefault = tdefault
  12386.         from systypes
  12387.     where name = @objname
  12388.         and uid = user_id()
  12389.         and usertype > 100
  12390.  
  12391.     if @olddefault = null
  12392.     begin
  12393.         print "You do not own a user datatype of that name."
  12394.         return (1)
  12395.     end
  12396.  
  12397.     if @olddefault = 0
  12398.     begin
  12399.         print "The specified user datatype has no default."
  12400.         return (1)
  12401.     end
  12402.  
  12403.     update systypes
  12404.         set tdefault = 0
  12405.             from systypes
  12406.         where name = @objname
  12407.             and uid = user_id()
  12408.             and usertype > 100
  12409.  
  12410.     print "Default unbound from datatype."
  12411.  
  12412.     /*
  12413.     **  Now see if there are any columns with the usertype that
  12414.     **  need the new binding.
  12415.     */
  12416.     if isnull(@futureonly, "") != @futurevalue
  12417.         and exists (select *
  12418.             from syscolumns, systypes
  12419.                 where syscolumns.usertype = systypes.usertype
  12420.                     and systypes.name = @objname
  12421.                     and systypes.usertype > 100
  12422.                     and systypes.uid = user_id()
  12423.                     and syscolumns.cdefault = @olddefault)
  12424.     begin
  12425.         /*
  12426.         **  Update syscolumns with new binding.
  12427.         */
  12428.         update syscolumns
  12429.             set cdefault = systypes.tdefault
  12430.                 from syscolumns, systypes
  12431.             where syscolumns.usertype = systypes.usertype
  12432.                 and systypes.name = @objname
  12433.                 and systypes.usertype > 100
  12434.                 and systypes.uid = user_id()
  12435.                 and syscolumns.cdefault = @olddefault
  12436.  
  12437.         /*
  12438.         **  Update the table schema to indicate that something
  12439.         **  has changed in the table's schema.
  12440.         */
  12441.         update sysobjects
  12442.             set schema = schema + 1
  12443.                 from sysobjects, syscolumns, systypes
  12444.             where sysobjects.id = syscolumns.id
  12445.                 and syscolumns.usertype = systypes.usertype
  12446.                 and systypes.name = @objname
  12447.                 and systypes.usertype > 100
  12448.                 and systypes.uid = user_id()
  12449.                 and syscolumns.cdefault = systypes.tdefault
  12450.  
  12451.         print "Columns of the user datatype specified had their defaults unbound."
  12452.     end
  12453. end
  12454.  
  12455. return (0)
  12456. go
  12457. grant execute on sp_unbindefault to public
  12458. go
  12459. dump transaction master with truncate_only
  12460. go
  12461. /* unbindrule 49.1 9/10/91 */
  12462. create procedure sp_unbindrule
  12463. @objname varchar(61),            /* table/column or datatype name */
  12464. @futureonly varchar(15) = NULL        /* flag to indicate extent of binding */
  12465. as
  12466.  
  12467. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  12468.                     ** the binding to be limited */
  12469.  
  12470. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  12471.  
  12472. /*
  12473. **  When a default or rule is bound to a user-defined datatype, it is also
  12474. **  bound, by default, to any columns of the user datatype that are currently
  12475. **  using the existing default or rule as their default or rule.  This default
  12476. **  action may be overridden by setting @futureonly = @futurevalue when the
  12477. **  procedure is invoked.  In this case existing columns with the user
  12478. **  datatype won't have their existing default or rule changed.
  12479. */
  12480.  
  12481. /*
  12482. **  Check to see that the object names are local to the current database.
  12483. */
  12484. if @objname like "%.%.%"
  12485. begin
  12486.     print "Column or user datatype must be in 'current' database."
  12487.     return (1)
  12488. end
  12489.  
  12490. /*
  12491. **  If @objname is of the form tab.col then we are unbinding a column.
  12492. **  Otherwise its a datatype.  In the column case, we need to extract
  12493. **  and verify the table and column names and make sure the user owns
  12494. **  the table that is getting the default unbound.
  12495. */
  12496. if @objname like "%.%"
  12497. begin
  12498.     declare @tabname varchar(30)        /* name of table */
  12499.     declare @colname varchar(30)        /* name of column */
  12500.  
  12501.     /*
  12502.     **  Get the table name out.
  12503.     */
  12504.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  12505.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  12506.  
  12507.     /*
  12508.     **  Find it and unbind it.
  12509.     */
  12510.     if exists (select *
  12511.             from syscolumns, sysobjects
  12512.         where syscolumns.id = object_id(@tabname)
  12513.             and syscolumns.name = @colname
  12514.             and sysobjects.id = object_id(@tabname)
  12515.             and uid = user_id()
  12516.             and sysobjects.sysstat & 7 = 3)      /* user table */
  12517.     begin
  12518.         /*
  12519.         **  Is there something bound to it?
  12520.         */
  12521.         if exists (select *
  12522.             from syscolumns, sysobjects
  12523.         where syscolumns.id = object_id(@tabname)
  12524.             and syscolumns.name = @colname
  12525.             and sysobjects.id = object_id(@tabname)
  12526.             and uid = user_id()
  12527.             and sysobjects.sysstat & 7 = 3      /* user table */
  12528.             and domain != 0)
  12529.         begin
  12530.             update syscolumns
  12531.                 set domain = 0
  12532.                     from syscolumns, sysobjects
  12533.                 where syscolumns.id = object_id(@tabname)
  12534.                     and syscolumns.name = @colname
  12535.                     and sysobjects.id = object_id(@tabname)
  12536.                     and uid = user_id()
  12537.                     and sysobjects.sysstat & 7 = 3
  12538.  
  12539.             /*
  12540.             **  Since binding a rule is a schema change,
  12541.             **  update schema count
  12542.             **  for the object in the sysobjects table.
  12543.             */
  12544.             update sysobjects
  12545.                 set schema = schema + 1
  12546.                     from sysobjects
  12547.                         where id = object_id(@tabname)
  12548.                             and uid = user_id()
  12549.  
  12550.             print "Rule unbound from table column."
  12551.             return (0)
  12552.         end
  12553.  
  12554.         else
  12555.         begin
  12556.             print "The specified column has no rule."
  12557.             return (1)
  12558.         end
  12559.     end
  12560.  
  12561.     /*
  12562.     **  If we got here, there was no such object.
  12563.     */
  12564.     print "You do not own a table with a column of that name."
  12565.     return (1)
  12566.  
  12567. end
  12568. else
  12569. begin
  12570.     /*
  12571.     **  We're unbinding to a user type.  In this case, the @objname
  12572.     **  is really the name of the user datatype.
  12573.     **  When we unbind to a user type, any existing columns get changed
  12574.     **  to the new binding unless their current binding is not equal
  12575.     **  to the current binding for the usertype or if they set the
  12576.     **  @futureonly parameter to @futurevalue.
  12577.     */
  12578.     declare @oldrule int            /* current rule for type */
  12579.  
  12580.     /*
  12581.     **  Get the current rule for the datatype.
  12582.     */
  12583.     select @oldrule = domain
  12584.         from systypes
  12585.     where name = @objname
  12586.         and uid = user_id()
  12587.         and usertype > 100
  12588.  
  12589.     if @oldrule = null
  12590.     begin
  12591.         print "You do not own a user datatype of that name."
  12592.         return (1)
  12593.     end
  12594.  
  12595.     if @oldrule = 0
  12596.     begin
  12597.         print "The specified user datatype has no rule."
  12598.         return (1)
  12599.     end
  12600.  
  12601.     update systypes
  12602.         set domain = 0
  12603.             from systypes
  12604.         where name = @objname
  12605.             and uid = user_id()
  12606.             and usertype > 100
  12607.  
  12608.     print "Rule unbound from datatype."
  12609.  
  12610.     /*
  12611.     **  Now see if there are any columns with the usertype that
  12612.     **  need the new binding.
  12613.     */
  12614.     if isnull(@futureonly, "") != @futurevalue
  12615.         and exists (select *
  12616.             from syscolumns, systypes
  12617.                 where syscolumns.usertype = systypes.usertype
  12618.                     and systypes.name = @objname
  12619.                     and systypes.usertype > 100
  12620.                     and systypes.uid = user_id()
  12621.                     and syscolumns.domain = @oldrule)
  12622.     begin
  12623.         /*
  12624.         **  Update syscolumns with new binding.
  12625.         */
  12626.         update syscolumns
  12627.             set domain = systypes.domain
  12628.                 from syscolumns, systypes
  12629.             where syscolumns.usertype = systypes.usertype
  12630.                 and systypes.name = @objname
  12631.                 and systypes.usertype > 100
  12632.                 and systypes.uid = user_id()
  12633.                 and syscolumns.domain = @oldrule
  12634.  
  12635.         /*
  12636.         **  Update the table schema to indicate that something
  12637.         **  has changed in the table's schema.
  12638.         */
  12639.         update sysobjects
  12640.             set schema = schema + 1
  12641.                 from sysobjects, syscolumns, systypes
  12642.             where sysobjects.id = syscolumns.id
  12643.                 and syscolumns.usertype = systypes.usertype
  12644.                 and systypes.name = @objname
  12645.                 and systypes.usertype > 100
  12646.                 and systypes.uid = user_id()
  12647.                 and syscolumns.domain = systypes.domain
  12648.  
  12649.         print "Columns of the user datatype specified had their rules unbound."
  12650.     end
  12651. end
  12652.  
  12653. return (0)
  12654. go
  12655. grant execute on sp_unbindrule to public
  12656. go
  12657. /* who 49.1 9/10/91 */
  12658. create procedure sp_who @loginame varchar(30) = NULL as
  12659.  
  12660. declare @low int
  12661. declare @high int
  12662. declare @spidlow int
  12663. declare @spidhigh int
  12664.  
  12665. select @low = 0, @high = 32767, @spidlow = 0, @spidhigh = 32767
  12666.  
  12667. if @loginame is not NULL
  12668. begin
  12669.     select @low = suser_id(@loginame), @high = suser_id(@loginame)
  12670.     if @low is NULL
  12671.     begin
  12672.         if @loginame like "[0-9]%"
  12673.         begin
  12674.             select @spidlow = convert(int, @loginame),
  12675.                  @spidhigh = convert(int, @loginame),
  12676.                  @low = 0, @high = 32767
  12677.         end
  12678.         else
  12679.         begin
  12680.             print "No login exists with the supplied name."
  12681.             return (1)
  12682.         end
  12683.     end
  12684. end
  12685. select spid,status,loginame=substring(suser_name(suid),1,12),
  12686.    hostname,blk=convert(char(3),blocked),
  12687.    dbname=substring(db_name(dbid),1,10),cmd
  12688. from sysprocesses
  12689. where suid >= @low and suid <= @high
  12690. and spid >= @spidlow and spid <= @spidhigh
  12691.  
  12692. return (0)
  12693. go
  12694. grant execute on sp_who to public
  12695. go
  12696. dump transaction master with truncate_only
  12697. go
  12698. /* addprobe    49.1    9/10/91    */
  12699. /*
  12700. **  This file add the account and user named "probe".  This account is
  12701. **  used the 2-phased commit service.
  12702. */
  12703. use master
  12704. go
  12705.  
  12706. if not exists (select *
  12707.         from syslogins
  12708.             where name = "probe")
  12709. begin
  12710.     execute sp_addlogin probe
  12711. end
  12712. go
  12713.  
  12714. if not exists (select *
  12715.         from sysusers
  12716.             where name = "probe")
  12717. begin
  12718.     execute sp_adduser probe
  12719. end
  12720. go
  12721. go
  12722. /* definekey 49.1 9/10/91 */
  12723.  
  12724. /*
  12725. **  This file defines the primary, foreign, and common keys for the
  12726. **  system tables.
  12727. */
  12728.  
  12729. /*
  12730. **  First do model, then do master.
  12731. */
  12732. use model
  12733. go
  12734.  
  12735. /*
  12736. **  Do the primary keys for the system tables.
  12737. */
  12738. if exists (select *
  12739.         from syskeys
  12740.     where type = 1 and id = object_id("sysalternates"))
  12741.         execute sp_dropkey primary, sysalternates
  12742. go
  12743. sp_primarykey sysalternates, suid
  12744. go
  12745.  
  12746. if exists (select *
  12747.         from syskeys
  12748.     where type = 1 and id = object_id("syscolumns"))
  12749.         execute sp_dropkey primary, syscolumns
  12750. go
  12751. sp_primarykey syscolumns, id, number, colid
  12752. go
  12753.  
  12754. if exists (select *
  12755.         from syskeys
  12756.     where type = 1 and id = object_id("syscomments"))
  12757.         execute sp_dropkey primary, syscomments
  12758. go
  12759. sp_primarykey syscomments, id, number, colid, texttype
  12760. go
  12761.  
  12762. if exists (select *
  12763.         from syskeys
  12764.     where type = 1 and id = object_id("sysdepends"))
  12765.         execute sp_dropkey primary, sysdepends
  12766. go
  12767. sp_primarykey sysdepends, id, number, depid, depnumber
  12768. go
  12769.  
  12770. if exists (select *
  12771.         from syskeys
  12772.     where type = 1 and id = object_id("sysindexes"))
  12773.         execute sp_dropkey primary, sysindexes
  12774. go
  12775. sp_primarykey sysindexes, id, indid
  12776. go
  12777.  
  12778. if exists (select *
  12779.         from syskeys
  12780.     where type = 1 and id = object_id("syskeys"))
  12781.         execute sp_dropkey primary, syskeys
  12782. go
  12783. sp_primarykey syskeys, id
  12784. go
  12785.  
  12786. if exists (select *
  12787.         from syskeys
  12788.     where type = 1 and id = object_id("sysobjects"))
  12789.         execute sp_dropkey primary, sysobjects
  12790. go
  12791. sp_primarykey sysobjects, id
  12792. go
  12793.  
  12794. if exists (select *
  12795.         from syskeys
  12796.     where type = 1 and id = object_id("sysprocedures"))
  12797.         execute sp_dropkey primary, sysprocedures
  12798. go
  12799. sp_primarykey sysprocedures, id, number, type, sequence
  12800. go
  12801.  
  12802. if exists (select *
  12803.         from syskeys
  12804.     where type = 1 and id = object_id("sysprotects"))
  12805.         execute sp_dropkey primary, sysprotects
  12806. go
  12807. sp_primarykey sysprotects, id, uid, action
  12808. go
  12809.  
  12810. if exists (select *
  12811.         from syskeys
  12812.     where type = 1 and id = object_id("systypes"))
  12813.         execute sp_dropkey primary, systypes
  12814. go
  12815. sp_primarykey systypes, name
  12816. go
  12817.  
  12818. if exists (select *
  12819.         from syskeys
  12820.     where type = 1 and id = object_id("sysusers"))
  12821.         execute sp_dropkey primary, sysusers
  12822. go
  12823. sp_primarykey sysusers, suid
  12824. go
  12825.  
  12826. if exists (select *
  12827.         from syskeys
  12828.     where type = 1 and id = object_id("syssegments"))
  12829.         execute sp_dropkey primary, syssegments
  12830. go
  12831. sp_primarykey syssegments, segment
  12832. go
  12833.  
  12834. /*
  12835. **  Now do the foreign keys and common keys.
  12836. */
  12837. if exists (select *
  12838.         from syskeys
  12839.     where type = 2 and id = object_id("sysalternates"))
  12840.         execute sp_dropkey foreign, sysalternates, sysusers
  12841. go
  12842. sp_foreignkey sysalternates, sysusers, suid
  12843. go
  12844.  
  12845. if exists (select *
  12846.         from syskeys
  12847.     where type = 3 and id = object_id("syscolumns"))
  12848.         execute sp_dropkey common, syscolumns, syscomments
  12849. go
  12850. sp_commonkey syscolumns, syscomments, id, id
  12851. go
  12852.  
  12853. sp_dropkey common, syscolumns, syskeys
  12854. go
  12855. sp_commonkey syscolumns, syskeys, id, key1
  12856. go
  12857. sp_commonkey syscolumns, syskeys, id, depkey1
  12858. go
  12859.  
  12860. sp_dropkey common, syscolumns, sysobjects
  12861. go
  12862. sp_commonkey syscolumns, sysobjects, id, id
  12863. go
  12864.  
  12865. sp_dropkey common, syscolumns, systypes
  12866. go
  12867. sp_commonkey syscolumns, systypes, type, type
  12868. go
  12869. sp_commonkey syscolumns, systypes, usertype, usertype
  12870. go
  12871. sp_commonkey syscolumns, systypes, domain, domain
  12872. go
  12873. sp_commonkey syscolumns, systypes, cdefault, tdefault
  12874. go
  12875.  
  12876. if exists (select *
  12877.         from syskeys
  12878.     where type = 3 and id = object_id("syscomments"))
  12879.         execute sp_dropkey common, syscomments, sysobjects
  12880. go
  12881. sp_commonkey syscomments, sysobjects, id, id
  12882. go
  12883.  
  12884. if exists (select *
  12885.         from syskeys
  12886.     where type = 3 and id = object_id("sysdepends"))
  12887.         execute sp_dropkey common, sysdepends, sysobjects
  12888. go
  12889. sp_commonkey sysdepends, sysobjects, id, id
  12890. go
  12891.  
  12892. if exists (select *
  12893.         from syskeys
  12894.     where type = 3 and id = object_id("sysindexes"))
  12895.         execute sp_dropkey common, sysindexes, sysobjects
  12896. go
  12897. sp_commonkey sysindexes, sysobjects, id, id
  12898. go
  12899.  
  12900. sp_dropkey common, sysindexes, syssegments
  12901. go
  12902. sp_commonkey sysindexes, syssegments, segment, segment
  12903. go
  12904.  
  12905. if exists (select *
  12906.         from syskeys
  12907.     where type = 2 and id = object_id("syskeys"))
  12908.         execute sp_dropkey foreign, syskeys, sysobjects
  12909. go
  12910. sp_foreignkey syskeys, sysobjects, id
  12911. go
  12912.  
  12913. if exists (select *
  12914.         from syskeys
  12915.     where type = 3 and id = object_id("syskeys"))
  12916.         execute sp_dropkey common, syskeys, sysobjects
  12917. go
  12918. sp_commonkey syskeys, sysobjects, depid, id
  12919. go
  12920.  
  12921. if exists (select *
  12922.         from syskeys
  12923.     where type = 3 and id = object_id("sysobjects"))
  12924.         execute sp_dropkey common, sysobjects, sysprocedures
  12925. go
  12926. sp_commonkey sysobjects, sysprocedures, id, id
  12927. go
  12928.  
  12929. sp_dropkey common, sysobjects, sysprotects
  12930. go
  12931. sp_commonkey sysobjects, sysprotects, id, id
  12932. go
  12933.  
  12934. sp_dropkey common, sysobjects, sysusers
  12935. go
  12936. sp_commonkey sysobjects, sysusers, uid, uid
  12937. go
  12938.  
  12939. if exists (select *
  12940.         from syskeys
  12941.     where type = 3 and id = object_id("sysprotects"))
  12942.         execute sp_dropkey common, sysprotects, sysusers
  12943. go
  12944. sp_commonkey sysprotects, sysusers, uid, uid
  12945. go
  12946.  
  12947. if exists (select *
  12948.         from syskeys
  12949.     where type = 3 and id = object_id("systypes"))
  12950.         execute sp_dropkey common, systypes, sysusers
  12951. go
  12952. sp_commonkey systypes, sysusers, uid, uid
  12953. go
  12954.  
  12955. if exists (select *
  12956.         from syskeys
  12957.     where type = 3 and id = object_id("sysindexes"))
  12958.         execute sp_dropkey common, sysindexes, syssegments
  12959. go
  12960. sp_commonkey sysindexes, syssegments, segment, segment
  12961. go
  12962.  
  12963. /*
  12964. **  Now do the standard systems tables in master.
  12965. */
  12966. use master
  12967. go
  12968.  
  12969. /*
  12970. **  Do the primary keys for the system tables.
  12971. */
  12972. if exists (select *
  12973.         from syskeys
  12974.     where type = 1 and id = object_id("sysalternates"))
  12975.         execute sp_dropkey primary, sysalternates
  12976. go
  12977. sp_primarykey sysalternates, suid
  12978. go
  12979.  
  12980. if exists (select *
  12981.         from syskeys
  12982.     where type = 1 and id = object_id("syscolumns"))
  12983.         execute sp_dropkey primary, syscolumns
  12984. go
  12985. sp_primarykey syscolumns, id, number, colid
  12986. go
  12987.  
  12988. if exists (select *
  12989.         from syskeys
  12990.     where type = 1 and id = object_id("syscomments"))
  12991.         execute sp_dropkey primary, syscomments
  12992. go
  12993. sp_primarykey syscomments, id, number, colid, texttype
  12994. go
  12995.  
  12996. if exists (select *
  12997.         from syskeys
  12998.     where type = 1 and id = object_id("sysdepends"))
  12999.         execute sp_dropkey primary, sysdepends
  13000. go
  13001. sp_primarykey sysdepends, id, number, depid, depnumber
  13002. go
  13003.  
  13004. if exists (select *
  13005.         from syskeys
  13006.     where type = 1 and id = object_id("sysindexes"))
  13007.         execute sp_dropkey primary, sysindexes
  13008. go
  13009. sp_primarykey sysindexes, id, indid
  13010. go
  13011.  
  13012. if exists (select *
  13013.         from syskeys
  13014.     where type = 1 and id = object_id("syskeys"))
  13015.         execute sp_dropkey primary, syskeys
  13016. go
  13017. sp_primarykey syskeys, id
  13018. go
  13019.  
  13020. if exists (select *
  13021.         from syskeys
  13022.     where type = 1 and id = object_id("sysobjects"))
  13023.         execute sp_dropkey primary, sysobjects
  13024. go
  13025. sp_primarykey sysobjects, id
  13026. go
  13027.  
  13028. if exists (select *
  13029.         from syskeys
  13030.     where type = 1 and id = object_id("sysprocedures"))
  13031.         execute sp_dropkey primary, sysprocedures
  13032. go
  13033. sp_primarykey sysprocedures, id, number, type, sequence
  13034. go
  13035.  
  13036. if exists (select *
  13037.         from syskeys
  13038.     where type = 1 and id = object_id("sysprotects"))
  13039.         execute sp_dropkey primary, sysprotects
  13040. go
  13041. sp_primarykey sysprotects, id, uid, action
  13042. go
  13043.  
  13044. if exists (select *
  13045.         from syskeys
  13046.     where type = 1 and id = object_id("systypes"))
  13047.         execute sp_dropkey primary, systypes
  13048. go
  13049. sp_primarykey systypes, name
  13050. go
  13051.  
  13052. if exists (select *
  13053.         from syskeys
  13054.     where type = 1 and id = object_id("sysusers"))
  13055.         execute sp_dropkey primary, sysusers
  13056. go
  13057. sp_primarykey sysusers, suid
  13058. go
  13059.  
  13060. if exists (select *
  13061.         from syskeys
  13062.     where type = 1 and id = object_id("sysusages"))
  13063.         execute sp_dropkey primary, sysusages
  13064. go
  13065. sp_primarykey sysusages, dbid, lstart
  13066. go
  13067.  
  13068. if exists (select *
  13069.         from syskeys
  13070.     where type = 1 and id = object_id("sysservers"))
  13071.         execute sp_dropkey primary, sysservers
  13072. go
  13073. sp_primarykey sysservers, srvid
  13074. go
  13075.  
  13076. if exists (select *
  13077.         from syskeys
  13078.     where type = 1 and id = object_id("sysremotelogins"))
  13079.         execute sp_dropkey primary, sysremotelogins
  13080. go
  13081. sp_primarykey sysremotelogins, remoteserverid, remoteusername
  13082. go
  13083.  
  13084. /*
  13085. **  Now do the foreign keys and common keys.
  13086. */
  13087. if exists (select *
  13088.         from syskeys
  13089.     where type = 2 and id = object_id("sysalternates"))
  13090.         execute sp_dropkey foreign, sysalternates, sysusers
  13091. go
  13092. sp_foreignkey sysalternates, sysusers, suid
  13093. go
  13094.  
  13095. if exists (select *
  13096.         from syskeys
  13097.     where type = 3 and id = object_id("syscolumns"))
  13098.         execute sp_dropkey common, syscolumns, syscomments
  13099. go
  13100. sp_commonkey syscolumns, syscomments, id, id
  13101. go
  13102.  
  13103. sp_dropkey common, syscolumns, syskeys
  13104. go
  13105. sp_commonkey syscolumns, syskeys, id, key1
  13106. go
  13107. sp_commonkey syscolumns, syskeys, id, depkey1
  13108. go
  13109.  
  13110. sp_dropkey common, syscolumns, sysobjects
  13111. go
  13112. sp_commonkey syscolumns, sysobjects, id, id
  13113. go
  13114.  
  13115. sp_dropkey common, syscolumns, systypes
  13116. go
  13117. sp_commonkey syscolumns, systypes, type, type
  13118. go
  13119. sp_commonkey syscolumns, systypes, usertype, usertype
  13120. go
  13121. sp_commonkey syscolumns, systypes, domain, domain
  13122. go
  13123. sp_commonkey syscolumns, systypes, cdefault, tdefault
  13124. go
  13125.  
  13126. if exists (select *
  13127.         from syskeys
  13128.     where type = 3 and id = object_id("syscomments"))
  13129.         execute sp_dropkey common, syscomments, sysobjects
  13130. go
  13131. sp_commonkey syscomments, sysobjects, id, id
  13132. go
  13133.  
  13134. if exists (select *
  13135.         from syskeys
  13136.     where type = 3 and id = object_id("sysdepends"))
  13137.         execute sp_dropkey common, sysdepends, sysobjects
  13138. go
  13139. sp_commonkey sysdepends, sysobjects, id, id
  13140. go
  13141.  
  13142. if exists (select *
  13143.         from syskeys
  13144.     where type = 3 and id = object_id("sysindexes"))
  13145.         execute sp_dropkey common, sysindexes, sysobjects
  13146. go
  13147. sp_commonkey sysindexes, sysobjects, id, id
  13148. go
  13149.  
  13150. sp_dropkey common, sysindexes, syssegments
  13151. go
  13152. sp_commonkey sysindexes, syssegments, segment, segment
  13153. go
  13154.  
  13155. if exists (select *
  13156.         from syskeys
  13157.     where type = 2 and id = object_id("syskeys"))
  13158.         execute sp_dropkey foreign, syskeys, sysobjects
  13159. go
  13160. sp_foreignkey syskeys, sysobjects, id
  13161. go
  13162.  
  13163. if exists (select *
  13164.         from syskeys
  13165.     where type = 3 and id = object_id("syskeys"))
  13166.         execute sp_dropkey common, syskeys, sysobjects
  13167. go
  13168. sp_commonkey syskeys, sysobjects, depid, id
  13169. go
  13170.  
  13171. if exists (select *
  13172.         from syskeys
  13173.     where type = 3 and id = object_id("sysobjects"))
  13174.         execute sp_dropkey common, sysobjects, sysprocedures
  13175. go
  13176. sp_commonkey sysobjects, sysprocedures, id, id
  13177. go
  13178.  
  13179. sp_dropkey common, sysobjects, sysprotects
  13180. go
  13181. sp_commonkey sysobjects, sysprotects, id, id
  13182. go
  13183.  
  13184. sp_dropkey common, sysobjects, sysusers
  13185. go
  13186. sp_commonkey sysobjects, sysusers, uid, uid
  13187. go
  13188.  
  13189. if exists (select *
  13190.         from syskeys
  13191.     where type = 3 and id = object_id("sysprotects"))
  13192.         execute sp_dropkey common, sysprotects, sysusers
  13193. go
  13194. sp_commonkey sysprotects, sysusers, uid, uid
  13195. go
  13196.  
  13197. if exists (select *
  13198.         from syskeys
  13199.     where type = 3 and id = object_id("systypes"))
  13200.         execute sp_dropkey common, systypes, sysusers
  13201. go
  13202. sp_commonkey systypes, sysusers, uid, uid
  13203. go
  13204.  
  13205. /*
  13206. **  These are in the master database only.
  13207. */
  13208. /*  Define primary keys first */
  13209. if exists (select *
  13210.         from syskeys
  13211.     where type = 1 and id = object_id("sysconfigures"))
  13212.         execute sp_dropkey primary, sysconfigures
  13213. go
  13214. sp_primarykey sysconfigures, config
  13215. go
  13216.  
  13217. if exists (select *
  13218.         from syskeys
  13219.     where type = 1 and id = object_id("sysdatabases"))
  13220.         execute sp_dropkey primary, sysdatabases
  13221. go
  13222. sp_primarykey sysdatabases, name
  13223. go
  13224.  
  13225. if exists (select *
  13226.         from syskeys
  13227.     where type = 1 and id = object_id("sysdevices"))
  13228.         execute sp_dropkey primary, sysdevices
  13229. go
  13230. sp_primarykey sysdevices, name
  13231. go
  13232.  
  13233. if exists (select *
  13234.         from syskeys
  13235.     where type = 1 and id = object_id("syslogins"))
  13236.         execute sp_dropkey primary, syslogins
  13237. go
  13238. sp_primarykey syslogins, suid
  13239. go
  13240.  
  13241. if exists (select *
  13242.         from syskeys
  13243.     where type = 1 and id = object_id("sysmessages"))
  13244.         execute sp_dropkey primary, sysmessages
  13245. go
  13246. sp_primarykey sysmessages, error, dlevel
  13247. go
  13248.  
  13249. if exists (select *
  13250.         from syskeys
  13251.     where type = 1 and id = object_id("sysusages"))
  13252.         execute sp_dropkey primary, sysusages
  13253. go
  13254. sp_primarykey sysusages, dbid, lstart
  13255. go
  13256.  
  13257. /*
  13258. **  Now the foreign and common keys.
  13259. */
  13260. if exists (select *
  13261.         from syskeys
  13262.     where type = 3 and id = object_id("syscurconfigs"))
  13263.         execute sp_dropkey common, syscurconfigs, sysconfigures
  13264. go
  13265. sp_commonkey syscurconfigs, sysconfigures, config, config
  13266. go
  13267.  
  13268. if exists (select *
  13269.         from syskeys
  13270.     where type = 3 and id = object_id("sysdatabases"))
  13271.         execute sp_dropkey common, sysdatabases, syslocks
  13272. go
  13273. sp_commonkey sysdatabases, syslocks, dbid, dbid
  13274. go
  13275.  
  13276. sp_dropkey common, sysdatabases, syslogins
  13277. go
  13278. sp_commonkey sysdatabases, syslogins, suid, suid
  13279. go
  13280.  
  13281. sp_dropkey common, sysdatabases, sysprocesses
  13282. go
  13283. sp_commonkey sysdatabases, sysprocesses, dbid, dbid
  13284. go
  13285.  
  13286. sp_dropkey common, sysdatabases, sysusages
  13287. go
  13288. sp_commonkey sysdatabases, sysusages, dbid, dbid
  13289. go
  13290.  
  13291. if exists (select *
  13292.         from syskeys
  13293.     where type = 3 and id = object_id("syslocks"))
  13294.         execute sp_dropkey common, syslocks, sysprocesses
  13295. go
  13296. sp_commonkey syslocks, sysprocesses, spid, spid
  13297. go
  13298.  
  13299. if exists (select *
  13300.         from syskeys
  13301.     where type = 3 and id = object_id("syslogins"))
  13302.         execute sp_dropkey common, syslogins, sysprocesses
  13303. go
  13304. sp_commonkey syslogins, sysprocesses, suid, suid
  13305. go
  13306.  
  13307. if exists (select *
  13308.         from syskeys
  13309.     where type = 3 and id = object_id("sysservers"))
  13310.         execute sp_dropkey common, sysservers, sysremotelogins
  13311. go
  13312. sp_commonkey sysservers, sysremotelogins, srvid, remoteserverid
  13313. go
  13314.  
  13315. if exists (select *
  13316.         from syskeys
  13317.     where type = 3 and id = object_id("sysremotelogins"))
  13318.         execute sp_dropkey common, sysremotelogins, sysusers
  13319. go
  13320. sp_commonkey sysremotelogins, sysusers, suid, suid
  13321. go
  13322.  
  13323. dump transaction master to diskdump with truncate_only
  13324. go
  13325.  
  13326. dump transaction model to diskdump with truncate_only
  13327. go
  13328.  
  13329. go
  13330. dump transaction master with truncate_only
  13331. go
  13332. dump transaction master to diskdump with truncate_only
  13333. go
  13334. use master
  13335. go
  13336. sp_configure "allow updates",0
  13337. go
  13338. reconfigure with override
  13339. go
  13340.  
  13341.  
  13342. /* 
  13343. NOTE:  you MUST change the last row inserted into MSserver_info
  13344. to be version number of this file.  the convention is VV.vvvv, where
  13345. VV is major version number ("00" until we ship), and vvvv is minor
  13346. version number in the form of month and day (mmdd) of the date you 
  13347. check in this file.  add 12 to the month to keep in sync with the
  13348. driver version numbers.  e.g. checking in on feb 5 would mean setting
  13349. the value to 00.1405.
  13350. */
  13351.  
  13352. /****************************************************************************/
  13353. /* This portion sets up the ability to perform all the functions in this    */
  13354. /* script                                                                    */
  13355. /****************************************************************************/
  13356. use master
  13357. go
  13358. dump tran master with truncate_only
  13359. go
  13360.  
  13361. if exists (select * from sysobjects
  13362.            where name = 'sp_configure' and sysstat & 7 = 4)
  13363. begin
  13364.     execute sp_configure 'update',1
  13365. end
  13366. reconfigure with override
  13367. go
  13368. if (exists (select * from sysobjects where name = 'MSdatatype_info'))
  13369.     drop table MSdatatype_info
  13370. go
  13371. if (exists (select * from sysobjects where name = 'MStable_types'))
  13372.     drop table MStable_types
  13373. go
  13374. if (exists (select * from sysobjects where name = 'MSserver_info'))
  13375.     drop table MSserver_info
  13376. go
  13377. if (exists (select * from sysobjects where name = 'sp_tables'))
  13378.     drop proc sp_tables
  13379. go
  13380. if (exists (select * from sysobjects where name = 'sp_statistics'))
  13381.     drop proc sp_statistics
  13382. go
  13383. if (exists (select * from sysobjects where name = 'sp_columns'))
  13384.     drop proc sp_columns
  13385. go
  13386. if (exists (select * from sysobjects where name = 'sp_fkeys'))
  13387.     drop proc sp_fkeys
  13388. go
  13389. if (exists (select * from sysobjects where name = 'sp_pkeys'))
  13390.     drop proc sp_pkeys
  13391. dump tran master with truncate_only
  13392. go
  13393.  
  13394. go
  13395. if (exists (select * from sysobjects where name = 'sp_stored_procedures'))
  13396.     drop proc sp_stored_procedures
  13397. go
  13398. if (exists (select * from sysobjects where name = 'sp_sproc_columns'))
  13399.     drop proc sp_sproc_columns
  13400. go
  13401. if (exists (select * from sysobjects where name = 'sp_table_privileges'))
  13402.     drop proc sp_table_privileges
  13403. go
  13404. if (exists (select * from sysobjects where name = 'sp_column_privileges'))
  13405.     drop proc sp_column_privileges
  13406. go
  13407. if (exists (select * from sysobjects where name = 'sp_server_info'))
  13408.     drop proc sp_server_info
  13409. go
  13410. if (exists (select * from sysobjects where name = 'sp_datatype_info'))
  13411.     drop proc sp_datatype_info
  13412. go
  13413. if (exists (select * from sysobjects where name = 'sp_special_columns'))
  13414.     drop proc sp_special_columns
  13415. go
  13416. if (exists (select * from sysobjects where name = 'sp_databases'))
  13417.     drop proc sp_databases
  13418. go
  13419.  
  13420. dump tran master with truncate_only
  13421. go
  13422.  
  13423. print "creating table MSdatatype_info"
  13424. go
  13425. create table MSdatatype_info (ss_dtype       tinyint      not null,
  13426.                             type_name        varchar(32)  not null,
  13427.                             data_type        smallint     not null,
  13428.                             data_precision   int          null,
  13429.                             numeric_scale    smallint     null,
  13430.                             numeric_radix    smallint     null,
  13431.                             length           int          null,
  13432.                             literal_prefix   varchar(32)  null,
  13433.                             literal_suffix   varchar(32)  null,
  13434.                             create_params    varchar(32)  null,
  13435.                             nullable         smallint     not null,
  13436.                             case_sensitive   smallint     not null,
  13437.                             searchable       smallint     not null)
  13438. go
  13439.  
  13440. insert into MSdatatype_info
  13441. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf, cp,        nul,case,srch */
  13442. values (45,"binary",-2,   null,null,null,255,"0x",null,"max length",1,  1,   1)
  13443.  
  13444. insert into MSdatatype_info
  13445. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf, cp, nul,case,srch */
  13446. values (50,"bit",-7,        1,   0,   2,null, null,null,null,0,   1,   1)
  13447.  
  13448. insert into MSdatatype_info
  13449. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf, cp,       nul,case,srch */
  13450. values (47,"char",1,      null,null,null,255,"'", "'","max length",1, 1,   1)
  13451.  
  13452. insert into MSdatatype_info
  13453. /* ss_type,name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13454. values (61,"datetime",11,   23,   3,  10,null,"'","'",null,1,  1,   1)
  13455.  
  13456. insert into MSdatatype_info
  13457. /* ss_type,name,    data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13458. values (58,"smalldatetime",11, 23,   3,  10,null,"'","'",null,1,   1,  1)
  13459.  
  13460. insert into MSdatatype_info  /* sql server type is "datetimn" */
  13461. /* ss_type,name,    data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13462. values (111,"datetime",11,     23,   3,   10,null,"'","'",null,1,  1,  1)
  13463.  
  13464. insert into MSdatatype_info
  13465. /* ss_type, name,data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13466. values (62,"float",6,       15,  null,10,null,null,null,null,1,   1,  1)
  13467.  
  13468. insert into MSdatatype_info  /* sql server type is "floatn" */
  13469. /* ss_type, name,data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13470. values (109,"float",6,     15,  null, 10,null, null,null,null,1,  1,  1)
  13471.  
  13472. insert into MSdatatype_info
  13473. /* ss_type, name,data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13474. values (59,"real",7,        7,  null, 10,null, null,null,null,1,  1,  1)
  13475.  
  13476. insert into MSdatatype_info
  13477. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13478. values(122,"smallmoney",3,    7,   4,   10,null, null,null,null,1,  1,  1)
  13479.  
  13480. insert into MSdatatype_info
  13481. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13482. values (56,"int",  4,        10,   0,   10,null,null,null,null,1, 1,  1)
  13483.  
  13484. insert into MSdatatype_info  /* sql server type is "intn" */
  13485. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13486. values (38,"int",   4,      10,   0,   10,null,null,null,null,1, 1,  1)
  13487.  
  13488. insert into MSdatatype_info
  13489. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13490. values (60,"money", 3,       18,  4,    10,null,null,null,null,1, 1,  1)
  13491.  
  13492. insert into MSdatatype_info   /* sql server type is "moneyn" */
  13493. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13494. values (110,"money", 3,     18,   4,   10,null,null,null,null,1, 1,  1)
  13495.  
  13496. insert into MSdatatype_info
  13497. /* ss_type, name, data_type,prec,scale,rdx,len,prf, suf, cp,nul,case,srch */
  13498. values (52,"smallint",5,      5,   0,   10,null,null,null,null,1,1,   1)
  13499.  
  13500. insert into MSdatatype_info
  13501. /* ss_type,name,data_type,prec,scale,rdx,len,        prf, suf,cp,nul,case,srch */
  13502. values (35,"text",-1,     null, null,null,2147483647,"'","'",null,1,  1,   1)
  13503.  
  13504. insert into MSdatatype_info
  13505. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,     nul,case,srch */
  13506. values (37,"varbinary",-3,null,null,null,254,"0x",null,"max length",1,1,   1)
  13507.  
  13508. insert into MSdatatype_info
  13509. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,nul,case,srch */
  13510. values (48,"tinyint",-6,   3,    0,   10,null,null,null,null,1,  1,   1)
  13511.  
  13512. insert into MSdatatype_info
  13513. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,    nul,case,srch */
  13514. values (39,"varchar",12,   null,null,null,254,"'","'","max length",1,1,   1)
  13515.  
  13516. insert into MSdatatype_info
  13517. /* ss_type,name,data_type,prec,scale,rdx,len,prf, suf,   cp,nul,case,srch */
  13518. values (34,"image",-4,     null,null,null,2147483647,"0x",null,null,1,1,1)
  13519. go
  13520.  
  13521. print "creating table MStable_types"
  13522. go
  13523. create table MStable_types (ss_type char(1),sag_type varchar(32))
  13524. go
  13525.  
  13526. insert into MStable_types
  13527.     values ('S','SYSTEM TABLE')
  13528. insert into MStable_types
  13529.     values ('U','TABLE')
  13530. insert into MStable_types
  13531.     values ('V','VIEW')
  13532. go
  13533.  
  13534. dump tran master with truncate_only
  13535. go
  13536.  
  13537. print "creating table MSserver_info"
  13538. go
  13539. create table MSserver_info (attribute_id      int,
  13540.                           attribute_name  varchar(60),
  13541.                           attribute_value varchar(255))
  13542. go
  13543.  
  13544. insert into MSserver_info
  13545.     values (1,"DBMS_NAME","NETWARE SQL SERVER")
  13546. insert into MSserver_info
  13547.     values (2,"DBMS_VER","4.2.0")
  13548. insert into MSserver_info
  13549.     values (6,"DBE_NAME","")
  13550. insert into MSserver_info
  13551.     values (10,"OWNER_TERM","owner")
  13552. insert into MSserver_info
  13553.     values (11,"TABLE_TERM","table")
  13554. insert into MSserver_info
  13555.     values (12,"MAX_OWNER_NAME_LENGTH","30")
  13556. insert into MSserver_info
  13557.     values (16,"IDENTIFIER_CASE","MIXED")
  13558. insert into MSserver_info
  13559.     values (15,"COLUMN_LENGTH","30")
  13560. insert into MSserver_info
  13561.     values (13,"TABLE_LENGTH","30")
  13562. insert into MSserver_info
  13563.     values (100,"USERID_LENGTH","30")
  13564. insert into MSserver_info
  13565.     values (17,"TX_ISOLATION","2")
  13566. insert into MSserver_info
  13567.     values (18,"COLLATION_SEQ","")
  13568. insert into MSserver_info
  13569.     values (14,"MAX_QUAL_LENGTH","30")
  13570. insert into MSserver_info
  13571.     values (101,"QUALIFIER_TERM","database")
  13572. insert into MSserver_info
  13573.     values (19,"SAVEPOINT_SUPPORT","Y")
  13574. insert into MSserver_info
  13575.     values (20,"MULTI_RESULT_SETS","Y")
  13576. insert into MSserver_info
  13577.     values (102,"NAMED_TRANSACTIONS","Y")
  13578. insert into MSserver_info
  13579.     values (103,"SPROC_AS_LANGUAGE","Y")
  13580. insert into MSserver_info
  13581.     values (103,"REMOTE_SPROC","Y")
  13582. insert into MSserver_info
  13583.     values (22,"ACCESSIBLE_TABLES","Y")
  13584. insert into MSserver_info
  13585.     values (104,"ACCESSIBLE_SPROC","Y")
  13586. insert into MSserver_info
  13587.     values (105,"MAX_INDEX_COLS","16")
  13588. insert into MSserver_info
  13589.     values (106,"RENAME_TABLE","Y")
  13590. insert into MSserver_info
  13591.     values (107,"RENAME_COLUMN","Y")
  13592. insert into MSserver_info
  13593.     values (108,"DROP_COLUMN","N")
  13594. insert into MSserver_info
  13595.     values (109,"INCREASE_COLUMN_LENGTH","N")
  13596. insert into MSserver_info
  13597.     values (110,"DDL_IN_TRANSACTION","N")
  13598. insert into MSserver_info
  13599.     values (111,"DESCENDING_INDEXES","N")
  13600. insert into MSserver_info
  13601.     values (112,"SP_RENAME","Y")
  13602. insert into MSserver_info
  13603.     values (500,"SYS_SPROC_VERSION","42.1713")
  13604. go
  13605.  
  13606. print "creating sp_tables"
  13607. go
  13608.  
  13609. create procedure sp_tables(@table_name        varchar(32)  = null,
  13610.                            @table_owner     varchar(32)  = null,
  13611.                            @table_qualifier varchar(32)  = null,
  13612.                            @table_type        varchar(100) = null)
  13613. as
  13614.     declare @type1 char(1),
  13615.             @type2 char(1),
  13616.             @type3 char(1),
  13617.             @tableindex int
  13618.     if @table_qualifier is not null
  13619.     begin
  13620.         if db_name() != @table_qualifier
  13621.         begin
  13622.             print "Table qualifier must be name of current database"
  13623.             return
  13624.         end
  13625.     end
  13626.     if @table_type is null
  13627.     begin
  13628.         select @type1 = 'U'
  13629.         select @type2 = 'V'
  13630.         select @type3 = 'S'
  13631.     end
  13632.     else
  13633.     begin
  13634.         if (charindex("'TABLE'",@table_type) != 0)
  13635.             select @type1 = 'U'
  13636.         else
  13637.             select @type1 = 'X'
  13638.         if (charindex("'SYSTEM TABLE'",@table_type) != 0)
  13639.             select @type2 = 'S'
  13640.         else
  13641.             select @type2 = 'X'
  13642.         if (charindex("'VIEW'",@table_type) != 0)
  13643.             select @type3 = 'V'
  13644.         else
  13645.             select @type3 = 'X'
  13646.     end
  13647.     
  13648.     if @table_name is null
  13649.     begin
  13650.         select @table_name = '%'
  13651.     end 
  13652.     else begin
  13653.         if (@table_owner is null) and (charindex('%', @table_name) = 0)
  13654.         begin
  13655.             if exists (select * from sysobjects 
  13656.                         where uid = user_id()
  13657.                         and name = @table_name
  13658.                         and (type = 'U' or type = 'V' or type = 'S'))
  13659.             begin
  13660.                 select @table_owner = user_name()
  13661.             end
  13662.         end
  13663.     end
  13664.     if @table_owner is null
  13665.         select @table_owner = '%'
  13666.     select
  13667.         table_qualifier = db_name(),
  13668.         table_owner=user_name(o.uid),
  13669.         table_name=o.name,
  13670.         table_type = t.sag_type,
  13671.         remarks = convert(varchar(254),null)
  13672.     from
  13673.         sysusers u,sysobjects o,master.dbo.MStable_types t
  13674.     where
  13675.         o.name like @table_name
  13676.         and user_name(o.uid) like @table_owner
  13677.         and o.type = t.ss_type
  13678.         and ( o.type = @type1 or o.type = @type2 or o.type = @type3)
  13679.         and u.uid=user_id() /* constrain sysusers uid for use in subquery */
  13680.         and (suser_id()=1    /* User is the System Administrator */
  13681.              or o.uid=user_id()   /* User created the object */
  13682.              /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13683.              or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13684.                   from sysprotects p
  13685.                   /* outer join to correlate with all rows in sysobjects */
  13686.                   where p.id =* o.id
  13687.                       /* get rows for public,current user,user's group */
  13688.                       and (p.uid=0 or p.uid=user_id() or p.uid=*u.gid)
  13689.                       /* check for SELECT,EXECUTE privilege */
  13690.                       and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13691.                 )=1    /* final magic...compare Grants    */
  13692.             )
  13693. go
  13694.  
  13695. grant execute on sp_tables to public
  13696. go
  13697.  
  13698. dump tran master with truncate_only
  13699. go
  13700.  
  13701. print "creating sp_statistics"
  13702. go
  13703.  
  13704. dump tran master with truncate_only
  13705. go
  13706.  
  13707.  
  13708. CREATE PROCEDURE sp_statistics (@table_name      varchar(32),
  13709.                              @table_owner      varchar(32) = null,
  13710.                              @table_qualifier varchar(32) = null,
  13711.                              @index_name      varchar(32) = '%',
  13712.                              @is_unique       char(1) = 'N')
  13713. AS
  13714.     DECLARE @indid                int
  13715.     DECLARE @lastindid            int
  13716.     DECLARE @table_id            int
  13717.     DECLARE @full_table_name    char(70)
  13718.     if @table_qualifier is not null
  13719.     begin
  13720.         if db_name() != @table_qualifier
  13721.         begin
  13722.             print "Table qualifier must be name of current database"
  13723.             return
  13724.         end
  13725.     end
  13726.     if @@trancount != 0
  13727.     begin
  13728.         raiserror 20001 "stored procedure sp_statistics can not be run while in a transaction"
  13729.         return
  13730.     end
  13731.     create table #TmpIndex( table_qualifier varchar(32),
  13732.                             table_owner     varchar(32),
  13733.                             table_name        varchar(32),
  13734.                             index_qualifier varchar(32) null,
  13735.                             index_name        varchar(32) null,
  13736.                             non_unique        smallint null,
  13737.                             type            smallint,
  13738.                             seq_in_index    smallint null,
  13739.                             column_name     varchar(32) null,
  13740.                             collation        char(1) null,
  13741.                             remarks         varchar(255) null,
  13742.                             index_id        int null,
  13743.                             cardinality     int,
  13744.                              pages            int,
  13745.                              status         smallint)
  13746.     if @table_owner is null
  13747.     begin
  13748.         SELECT @full_table_name = @table_name
  13749.     end
  13750.     else
  13751.     begin
  13752.         SELECT @full_table_name = @table_owner + '.' + @table_name
  13753.     end
  13754.     SELECT @table_id = object_id(@full_table_name)
  13755.     SELECT    @indid=min(indid)
  13756.     FROM sysindexes
  13757.     WHERE id=@table_id
  13758.         AND indid > 0
  13759.         AND indid < 255
  13760.     
  13761.     WHILE @indid != NULL
  13762.     BEGIN
  13763.         INSERT #TmpIndex
  13764.         SELECT
  13765.             DB_NAME(),                               /* table_qualifier */
  13766.             USER_NAME(o.uid),                         /* table_owner     */
  13767.             o.name,                                   /* table_name      */
  13768.             null,                                     /* index_qualifier */
  13769.             x.name,                                   /* index_name      */
  13770.             0,                                       /* non_unique      */
  13771.             3,                                       /* type            */
  13772.             colid,                                   /* seq_in_index    */
  13773.             INDEX_COL(@full_table_name,indid,colid), /* column_name     */
  13774.             "A",                                     /* collation       */
  13775.             "",                                       /*  remarks         */
  13776.             @indid,                                   /*  index_id        */
  13777.              x.rows,                                 /*  cardinality     */
  13778.              x.dpages,                               /* pages            */
  13779.             x.status                                 /* status           */
  13780.         FROM sysindexes x,syscolumns c,sysobjects o
  13781.         WHERE x.id=@table_id
  13782.             AND x.id = o.id
  13783.             AND x.id=c.id
  13784.             AND c.colid<keycnt+ (x.status&16)/16
  13785.             AND x.indid=@indid
  13786.         /*
  13787.         **    Now move @indid to the next index.
  13788.         */
  13789.         SELECT @lastindid = @indid
  13790.         SELECT @indid = NULL
  13791.         SELECT @indid = min(indid)
  13792.         FROM sysindexes
  13793.         WHERE id = @table_id
  13794.             AND indid > @lastindid
  13795.             AND indid < 255
  13796.     END
  13797.    
  13798.     UPDATE #TmpIndex
  13799.         SET non_unique = 1
  13800.         WHERE  status&0x2 != 2
  13801.     UPDATE #TmpIndex
  13802.         SET type = 1
  13803.         WHERE  index_id = 1
  13804.  
  13805.  
  13806.      /* now add row for table statistics */
  13807.  
  13808.  
  13809.       INSERT #TmpIndex  
  13810.         SELECT
  13811.             DB_NAME(),         /* table_qualifier */
  13812.             USER_NAME(o.uid),   /* table_owner     */
  13813.             o.name,             /* table_name      */
  13814.             null,               /* index_qualifier */ 
  13815.             null,               /* index_name      */
  13816.             0,                 /* non_unique      */
  13817.             0,                 /* type            */
  13818.             0,                 /* seq_in_index    */
  13819.             null,               /* column_name     */
  13820.             null,               /* collation       */
  13821.             "",                 /*  remarks         */
  13822.             0,                 /*  index_id        */
  13823.              x.rows,           /*  cardinality     */
  13824.              x.dpages,         /* pages            */
  13825.             0                   /* status           */
  13826.         FROM sysindexes x, sysobjects o
  13827.         WHERE o.id=@table_id
  13828.             AND x.id = o.id
  13829.             AND (x.indid=0 or x.indid=1)
  13830.  
  13831.  
  13832.     if @is_unique != 'Y'
  13833.         SELECT
  13834.             table_qualifier, 
  13835.             table_owner,     
  13836.             table_name,       
  13837.             non_unique,       
  13838.             index_qualifier, 
  13839.             index_name,       
  13840.             type,             
  13841.             seq_in_index,     
  13842.             column_name,     
  13843.             collation,       
  13844.             cardinality,     
  13845.             pages             
  13846.          FROM #TmpIndex       
  13847.         WHERE index_name like @index_name or index_name is null   
  13848.         ORDER BY non_unique,type,index_name,seq_in_index         
  13849.     else
  13850.         SELECT
  13851.             table_qualifier,
  13852.             table_owner,
  13853.             table_name,
  13854.             non_unique,
  13855.             index_qualifier,
  13856.             index_name,
  13857.             type,
  13858.             seq_in_index,
  13859.             column_name,
  13860.             collation,
  13861.             cardinality,
  13862.             pages
  13863.         FROM #TmpIndex
  13864.         WHERE non_unique = 0
  13865.             and (index_name like @index_name or index_name is null)
  13866.         ORDER BY non_unique,type,index_name,seq_in_index
  13867.     DROP TABLE #TmpIndex
  13868. go
  13869.  
  13870. grant execute on sp_statistics to public
  13871. go
  13872.  
  13873. dump tran master with truncate_only
  13874. go
  13875.  
  13876. print "creating sp_columns"
  13877. go
  13878.  
  13879. CREATE PROCEDURE sp_columns (@table_name varchar(32),
  13880.                              @table_owner varchar(32) = null,
  13881.                              @table_qualifier varchar(32) = null,
  13882.                              @column_name varchar(32) = null )
  13883. AS
  13884.     if @column_name is null
  13885.         select @column_name = '%'
  13886.     DECLARE @full_table_name    char(70)
  13887.     if @table_qualifier is not null
  13888.     begin
  13889.         if db_name() != @table_qualifier
  13890.         begin
  13891.             print "Table qualifier must be name of current database"
  13892.             return
  13893.         end
  13894.     end
  13895.         if @table_name is null
  13896.         begin
  13897.             select @table_name='%'
  13898.         end
  13899.     if @table_owner is null
  13900.     begin
  13901.         SELECT @full_table_name = @table_name
  13902.     end
  13903.     else
  13904.     begin
  13905.         SELECT @full_table_name = @table_owner + '.' + @table_name
  13906.     end
  13907.     if (charindex('%',@full_table_name) = 0)
  13908.     begin
  13909.         SELECT
  13910.             table_qualifier = DB_NAME(),
  13911.             table_owner = USER_NAME(o.uid),
  13912.             table_name = o.name,
  13913.             column_name = c.name,
  13914.             d.data_type,
  13915.             type_name = t.name,
  13916.             data_precision=isnull(d.data_precision, convert(int,c.length)),
  13917.             length=convert(int,c.length),
  13918.             d.numeric_scale,
  13919.             d.numeric_radix,
  13920.             nullable=convert(smallint, convert(bit, c.status&8)),
  13921.             remarks = c.name,
  13922.             ss_data_type = c.type
  13923.         FROM
  13924.         syscolumns c,sysobjects o,master.dbo.MSdatatype_info d, systypes t
  13925.         WHERE
  13926.             o.id=object_id(@full_table_name)
  13927.             AND c.id = o.id
  13928.             AND c.type = d.ss_dtype
  13929.         AND c.usertype *= t.usertype
  13930.             AND c.name like @column_name
  13931.     end else
  13932.     begin
  13933.         if @table_owner is null
  13934.             select @table_owner = '%'
  13935.         SELECT
  13936.             table_qualifier = DB_NAME(),
  13937.             table_owner = USER_NAME(o.uid),
  13938.             table_name = o.name,
  13939.             column_name = c.name,
  13940.             d.data_type,
  13941.             d.type_name,
  13942.             data_precision=isnull(d.data_precision, convert(int, c.length)),
  13943.             length=convert(int,c.length),
  13944.             d.numeric_scale,
  13945.             d.numeric_radix,
  13946.             nullable=convert(smallint, convert(bit, c.status&8)),
  13947.             remarks = null,
  13948.             ss_data_type = c.type
  13949.         FROM
  13950.             syscolumns c,sysobjects o,master.dbo.MSdatatype_info d, systypes t
  13951.  
  13952.         WHERE
  13953.             o.name like @table_name
  13954.             AND user_name(o.uid) like @table_owner
  13955.             AND c.id = o.id
  13956.         AND c.usertype *= t.usertype
  13957.             AND c.type = d.ss_dtype
  13958.             AND c.name like @column_name
  13959.     end
  13960.  
  13961.     
  13962. go
  13963.  
  13964. grant execute on sp_columns to public
  13965. go
  13966.  
  13967. dump tran master with truncate_only
  13968. go
  13969.  
  13970. print "creating sp_fkeys"
  13971. go
  13972.  
  13973. CREATE PROCEDURE sp_fkeys( @pktable_name      varchar(32) = null,
  13974.                            @pktable_owner      varchar(32) = null,
  13975.                            @pktable_qualifier varchar(32) = null,
  13976.                            @fktable_name      varchar(32) = null,
  13977.                            @fktable_owner      varchar(32) = null,
  13978.                            @fktable_qualifier varchar(32) = null )
  13979. as
  13980.     if (@pktable_name is null) and (@fktable_name is null)
  13981.     begin
  13982.         print "pk table name or fk table name must be given"
  13983.         return
  13984.     end
  13985.     if @fktable_qualifier is not null
  13986.     begin
  13987.         if db_name() != @fktable_qualifier
  13988.         begin
  13989.             print "Foriegn Key Table qualifier must be name of current database"
  13990.             return
  13991.         end
  13992.     end
  13993.     if @pktable_qualifier is not null
  13994.     begin
  13995.         if db_name() != @pktable_qualifier
  13996.         begin
  13997.             print "Primary Key Table qualifier must be name of current database"
  13998.             return
  13999.         end
  14000.     end
  14001.  
  14002.     if @pktable_name is null
  14003.         select @pktable_name = '%'
  14004.     if @pktable_owner is null
  14005.         select @pktable_owner = '%'
  14006.     if @fktable_name is null
  14007.         select @fktable_name = '%'
  14008.     if @fktable_owner is null
  14009.         select @fktable_owner = '%'
  14010.  
  14011.     if @@trancount != 0
  14012.     begin
  14013.         raiserror 20001 "catalog procedure sp_fkeys can not be run in a transaction"
  14014.         return
  14015.     end
  14016.     create table #fkeys( pktable_qualifier varchar(32),
  14017.                          pktable_owner       varchar(32),
  14018.                          pktable_name       varchar(32),
  14019.                          pkcolumn_name      varchar(32),
  14020.                          fktable_qualifier varchar(32),
  14021.                          fktable_owner       varchar(32),
  14022.                          fktable_name       varchar(32),
  14023.                          fkcolumn_name       varchar(32),
  14024.                          key_seq           smallint)
  14025.     insert into #fkeys
  14026.         select
  14027.             db_name(),
  14028.             (select user_name(uid) from sysobjects o where o.id = k.depid),
  14029.             object_name(k.depid),
  14030.             (    select name 
  14031.                     from syscolumns
  14032.                     where id = k.depid
  14033.                     and colid = k.key1),
  14034.              db_name(),
  14035.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14036.             object_name(k.id),
  14037.             c.name,
  14038.             1
  14039.         from
  14040.             syskeys k,syscolumns c
  14041.         where
  14042.             c.id = k.id
  14043.             and k.type = 2
  14044.             and c.colid = depkey1
  14045.         if (@@rowcount    = 0)
  14046.             goto done
  14047.         insert into #fkeys
  14048.             select
  14049.                 db_name(),
  14050.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  14051.                 object_name(k.depid),
  14052.                 (select name 
  14053.                     from syscolumns
  14054.                     where id = k.depid
  14055.                     and colid = k.key2),
  14056.                 db_name(),
  14057.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14058.                 object_name(k.id),
  14059.                 c.name,
  14060.                 2
  14061.             from
  14062.                 syskeys k,syscolumns c
  14063.             where
  14064.                 c.id = k.id
  14065.                 and k.type = 2
  14066.                 and c.colid = depkey2
  14067.         if (@@rowcount = 0)
  14068.             goto done
  14069.         insert into #fkeys
  14070.             select
  14071.                 db_name(),
  14072.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  14073.                 object_name(k.depid),
  14074.                 (select name 
  14075.                     from syscolumns
  14076.                     where id = k.depid
  14077.                     and colid = k.key3),
  14078.                  db_name(),
  14079.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14080.                 object_name(k.id),
  14081.                 c.name,
  14082.                 3
  14083.             from
  14084.                 syskeys k,syscolumns c
  14085.             where
  14086.                 c.id = k.id
  14087.                 and k.type = 2
  14088.                 and c.colid = depkey3
  14089.         if (@@rowcount = 0)
  14090.             goto done
  14091.         insert into #fkeys
  14092.         select
  14093.             db_name(),
  14094.             (select user_name(uid) from sysobjects o where o.id = k.depid),
  14095.                 object_name(k.depid),
  14096.                 (select name 
  14097.                     from syscolumns
  14098.                     where id = k.depid
  14099.                     and colid = k.key4),
  14100.                  db_name(),
  14101.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14102.                 object_name(k.id),
  14103.                 c.name,
  14104.                 4
  14105.             from
  14106.                 syskeys k,syscolumns c
  14107.             where
  14108.                 c.id = k.id
  14109.                 and k.type = 2
  14110.                 and c.colid = depkey4
  14111.         if (@@rowcount = 0)
  14112.             goto done
  14113.         insert into #fkeys
  14114.             select
  14115.                 db_name(),
  14116.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  14117.                 object_name(k.depid),
  14118.                 (select name 
  14119.                     from syscolumns
  14120.                     where id = k.depid
  14121.                     and colid = k.key5),
  14122.                  db_name(),
  14123.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14124.                 object_name(k.id),
  14125.                 c.name,
  14126.                 5
  14127.             from
  14128.                 syskeys k,syscolumns c
  14129.             where
  14130.                 c.id = k.id
  14131.                 and k.type = 2
  14132.                 and c.colid = depkey5
  14133.         if (@@rowcount = 0)
  14134.             goto done
  14135.         insert into #fkeys
  14136.             select
  14137.                 db_name(),
  14138.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  14139.                 object_name(k.depid),
  14140.                 (select name 
  14141.                     from syscolumns
  14142.                     where id = k.depid
  14143.                     and colid = k.key6),
  14144.                  db_name(),
  14145.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14146.                 object_name(k.id),
  14147.                 c.name,
  14148.                 6
  14149.             from
  14150.                 syskeys k,syscolumns c
  14151.             where
  14152.                 c.id = k.id
  14153.                 and k.type = 2
  14154.                 and c.colid = depkey6
  14155.         if (@@rowcount = 0)
  14156.             goto done
  14157.         insert into #fkeys
  14158.             select
  14159.                 db_name(),
  14160.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  14161.                 object_name(k.depid),
  14162.                 (select name 
  14163.                     from syscolumns
  14164.                     where id = k.depid
  14165.                     and colid = k.key7),
  14166.                  db_name(),
  14167.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14168.                 object_name(k.id),
  14169.                 c.name,
  14170.                 7
  14171.             from
  14172.                 syskeys k,syscolumns c
  14173.             where
  14174.                 c.id = k.id
  14175.                 and k.type = 2
  14176.                 and c.colid = depkey7
  14177.         if (@@rowcount    = 0)
  14178.             goto done
  14179.         insert into #fkeys
  14180.             select
  14181.                 db_name(),
  14182.                 (select user_name(uid) from sysobjects o where o.id = k.depid),
  14183.                 object_name(k.depid),
  14184.                 (select name 
  14185.                     from syscolumns
  14186.                     where id = k.depid
  14187.                     and colid = k.key8),
  14188.                  db_name(),
  14189.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14190.                 object_name(k.id),
  14191.                 c.name,
  14192.                 8
  14193.             from
  14194.                 syskeys k,syscolumns c
  14195.             where
  14196.                 c.id = k.id
  14197.                 and k.type = 2
  14198.                 and c.colid = depkey8
  14199.     done:
  14200.     select
  14201.         pktable_qualifier,
  14202.         pktable_owner,
  14203.         pktable_name,
  14204.         pkcolumn_name,
  14205.         fktable_qualifier,
  14206.         fktable_owner,
  14207.         fktable_name,
  14208.         fkcolumn_name,
  14209.         key_seq,
  14210.         update_delete_rule = 1
  14211.     from #fkeys
  14212.     where fktable_name like @fktable_name
  14213.         and fktable_owner like @fktable_owner
  14214.         and pktable_name  like @pktable_name
  14215.         and pktable_owner like @pktable_owner
  14216.     order by fktable_name,fktable_owner,key_seq
  14217. go
  14218.  
  14219. grant execute on sp_fkeys to public
  14220. go
  14221. dump tran master with truncate_only
  14222. go
  14223.  
  14224.  
  14225.  
  14226. print "creating sp_pkeys"
  14227. go
  14228.  
  14229. CREATE PROCEDURE sp_pkeys( @table_name    varchar(32),
  14230.                            @table_owner varchar(32) = null,
  14231.                            @table_qualifier varchar(32)= null )
  14232. as
  14233.     if @table_qualifier is not null
  14234.     begin
  14235.         if db_name() != @table_qualifier
  14236.         begin
  14237.             print "Table qualifier must be name of current database"
  14238.             return
  14239.         end
  14240.     end
  14241.     if @table_owner is null
  14242.         select @table_owner = '%'
  14243.     if @@trancount != 0
  14244.     begin
  14245.         raiserror 20001 "catalog procedure sp_pkeys can not be run in a transaction"
  14246.         return
  14247.     end
  14248.     create table #pkeys( table_qualifier varchar(32),
  14249.                          table_owner     varchar(32),
  14250.                          table_name      varchar(32),
  14251.                          column_name     varchar(32),
  14252.                          key_seq         smallint,
  14253.                          update_delete_rule smallint null )
  14254.     insert into #pkeys
  14255.         select
  14256.             db_name(),
  14257.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14258.             object_name(k.id),
  14259.             c.name,
  14260.             1,
  14261.             null
  14262.         from
  14263.             syskeys k,syscolumns c
  14264.         where
  14265.             c.id = k.id
  14266.             and k.type = 1
  14267.             and c.colid = k.key1
  14268.     if (@@rowcount = 0)
  14269.         goto done
  14270.     insert into #pkeys
  14271.         select
  14272.             db_name(),
  14273.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14274.             object_name(k.id),
  14275.             c.name,
  14276.             2,
  14277.             null
  14278.         from
  14279.             syskeys k,syscolumns c
  14280.         where
  14281.             c.id = k.id
  14282.             and k.type = 1
  14283.             and c.colid = key2
  14284.     if (@@rowcount = 0)
  14285.         goto done
  14286.     insert into #pkeys
  14287.         select
  14288.             db_name(),
  14289.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14290.             object_name(k.id),
  14291.             c.name,
  14292.             3,
  14293.             null
  14294.         from
  14295.             syskeys k,syscolumns c
  14296.         where
  14297.             c.id = k.id
  14298.             and k.type = 1
  14299.             and c.colid = key3
  14300.     if (@@rowcount = 0)
  14301.         goto done
  14302.     insert into #pkeys
  14303.         select
  14304.             db_name(),
  14305.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14306.             object_name(k.id),
  14307.             c.name,
  14308.             4,
  14309.             null
  14310.         from
  14311.             syskeys k,syscolumns c
  14312.         where
  14313.             c.id = k.id
  14314.             and k.type = 1
  14315.             and c.colid = key4
  14316.     if (@@rowcount = 0)
  14317.         goto done
  14318.     insert into #pkeys
  14319.         select
  14320.                 db_name(),
  14321.                 (select user_name(uid) from sysobjects o where o.id = k.id),
  14322.                 object_name(k.id),
  14323.                 c.name,
  14324.                 5,
  14325.             null
  14326.         from
  14327.             syskeys k,syscolumns c
  14328.         where
  14329.             c.id = k.id
  14330.             and k.type = 1
  14331.             and c.colid = key5
  14332.     if (@@rowcount = 0)
  14333.         goto done
  14334.     insert into #pkeys
  14335.         select
  14336.             db_name(),
  14337.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14338.             object_name(k.id),
  14339.             c.name,
  14340.             6,
  14341.             null
  14342.         from
  14343.             syskeys k,syscolumns c
  14344.         where
  14345.             c.id = k.id
  14346.             and k.type = 1
  14347.             and c.colid = key6
  14348.     if (@@rowcount = 0)
  14349.         goto done
  14350.     insert into #pkeys
  14351.         select
  14352.             db_name(),
  14353.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14354.             object_name(k.id),
  14355.             c.name,
  14356.             7,
  14357.             null
  14358.         from
  14359.             syskeys k,syscolumns c
  14360.         where
  14361.             c.id = k.id
  14362.             and k.type = 1
  14363.             and c.colid = key7
  14364.     if (@@rowcount = 0)
  14365.         goto done
  14366.     insert into #pkeys
  14367.         select
  14368.             db_name(),
  14369.             (select user_name(uid) from sysobjects o where o.id = k.id),
  14370.             object_name(k.id),
  14371.             c.name,
  14372.             8,
  14373.             null
  14374.         from
  14375.             syskeys k,syscolumns c
  14376.         where
  14377.             c.id = k.id
  14378.             and k.type = 1
  14379.             and c.colid = key8
  14380.     done:
  14381.     select 
  14382.             table_qualifier,
  14383.             table_owner,
  14384.             table_name,
  14385.             column_name,
  14386.             key_seq
  14387.             from #pkeys
  14388.     where table_name like  @table_name
  14389.     and table_owner like @table_owner
  14390.     order by table_name,table_owner,key_seq
  14391. go
  14392.  
  14393. grant execute on sp_pkeys to public
  14394. go
  14395.  
  14396. dump tran master with truncate_only
  14397. go
  14398.  
  14399. print "creating sp_stored_procedures"
  14400. go
  14401.  
  14402. create procedure sp_stored_procedures( @sp_name      varchar(32) = null,
  14403.                                        @sp_owner     varchar(32) = null,
  14404.                                        @sp_qualifier varchar(32) = null)
  14405. as
  14406.     if @sp_qualifier is not null
  14407.     begin
  14408.         if db_name() != @sp_qualifier
  14409.         begin
  14410.             print "stored procedure qualifier must be name of current database"
  14411.             return
  14412.         end
  14413.     end
  14414.     
  14415.     if @sp_name is null
  14416.     begin
  14417.         select @sp_name = '%'
  14418.     end 
  14419.     else begin
  14420.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  14421.         begin
  14422.             if exists (select * from sysobjects 
  14423.                         where uid = user_id()
  14424.                         and name = @sp_name
  14425.                         and type = 'P')
  14426.             begin
  14427.                 select @sp_owner = user_name()
  14428.             end
  14429.         end
  14430.     end
  14431.     if @sp_owner is null
  14432.         select @sp_owner = '%'
  14433.  
  14434.     select distinct
  14435.             sp_qualifier = db_name(),
  14436.             sp_owner = user_name(o.uid),
  14437.             sp_name = o.name,
  14438.             sp_number = p.number,
  14439.             sp_num_input_params = -1,
  14440.             sp_num_output_params = -1,
  14441.             sp_num_result_sets = -1,
  14442.             remarks = null
  14443.         from
  14444.             sysobjects o,sysprocedures p,sysusers u
  14445.         where
  14446.             o.name like @sp_name
  14447.             and user_name(o.uid) like @sp_owner
  14448.             and o.type = 'P'
  14449.             and p.id = o.id
  14450.             and u.uid=user_id()   /* constrain sysusers uid for use in subquery */
  14451.             and (suser_id()=1      /* User is the System Administrator */
  14452.                     or o.uid=user_id()     /* User created the object */
  14453.                     /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14454.                     or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14455.                          from sysprotects p
  14456.                          /* outer join to correlate with all rows in sysobjects */
  14457.                          where p.id =* o.id
  14458.                              /*  get rows for public,current user,user's group */
  14459.                              and (p.uid=0 or p.uid=user_id() or p.uid=*u.gid)
  14460.                              /* check for SELECT,EXECUTE privilege */
  14461.                              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  14462.                         )=1    /* final magic...compare Grants    */
  14463.                 )
  14464. go
  14465. grant execute on sp_stored_procedures to public
  14466. go
  14467.  
  14468. dump tran master with truncate_only
  14469. go
  14470.  
  14471.  
  14472. print "creating sp_sproc_columns"
  14473. go
  14474.  
  14475. CREATE PROCEDURE sp_sproc_columns( @sp_name      varchar(32),
  14476.                                    @sp_owner     varchar(32) = null,
  14477.                                    @sp_qualifier varchar(32) = null,
  14478.                            @sp_number  smallint = 1,
  14479.                                    @column_name  varchar(32) = null )
  14480. AS
  14481.     DECLARE @full_sp_name char(70)
  14482.     if @sp_qualifier is not null
  14483.     begin
  14484.         if db_name() != @sp_qualifier
  14485.         begin
  14486.             print "Table qualifier must be name of current database"
  14487.             return
  14488.         end
  14489.     end
  14490.     if @sp_owner is null
  14491.         SELECT @full_sp_name = @sp_name
  14492.     else
  14493.         SELECT @full_sp_name = @sp_owner + '.' + @sp_name
  14494.     SELECT
  14495.         sp_qualifier = DB_NAME(),
  14496.         sp_owner = USER_NAME(o.uid),
  14497.         sp_name = o.name,
  14498.         sp_number  = c.number,
  14499.         column_name = c.name,
  14500.         d.data_type,
  14501.         type_name = t.name,
  14502.         c.length,
  14503.         d.data_precision,
  14504.         d.numeric_scale,
  14505.         d.numeric_radix,
  14506.         d.nullable,
  14507.         remarks = null,
  14508.         ss_data_type = c.type,
  14509.         column_type = 1
  14510.     FROM
  14511.         syscolumns c,sysobjects o,master.dbo.MSdatatype_info d, systypes t
  14512.     WHERE o.id=object_id(@full_sp_name)
  14513.         AND c.id = o.id
  14514.         AND c.type = d.ss_dtype
  14515.     AND c.usertype *= t.usertype
  14516.         AND c.number = @sp_number
  14517. go
  14518.  
  14519. grant execute on sp_sproc_columns to public
  14520. go
  14521.  
  14522.                 
  14523. print "creating sp_table_privileges"
  14524. go
  14525.  
  14526.  
  14527. CREATE PROCEDURE sp_table_privileges ( 
  14528.                         @table_name  varchar(32),
  14529.                         @table_owner varchar(32) = null,
  14530.                         @table_qualifier varchar(32)= null)
  14531. as        
  14532.  
  14533.     declare @table_id    int,
  14534.             @owner_id    int,
  14535.             @full_table_name char(70)
  14536.     
  14537.         
  14538.     if @table_qualifier is not null
  14539.     begin
  14540.         if db_name() != @table_qualifier
  14541.         begin
  14542.             print "Table qualifier must be name of current database"
  14543.             return
  14544.         end
  14545.     end
  14546.     if @table_owner is null
  14547.     begin
  14548.         SELECT @full_table_name = @table_name
  14549.     end
  14550.     else
  14551.     begin
  14552.         SELECT @full_table_name = @table_owner + '.' + @table_name
  14553.     end
  14554.     SELECT @table_id = object_id(@full_table_name)
  14555.     if @table_id = 0
  14556.     begin
  14557.         print "table not found"
  14558.         return
  14559.     end
  14560.  
  14561.     if @@trancount != 0
  14562.     begin
  14563.         raiserror 20001 "catalog procedure sp_table_privileges can not be run in a transaction"
  14564.         return
  14565.     end
  14566.     create table #table_privileges 
  14567.             (table_qualifier         varchar(32),
  14568.             table_owner         varchar(32),    
  14569.             table_name     varchar(32),
  14570.             grantor            varchar(32),
  14571.             grantee            varchar(32),
  14572.             select_privilege    int,
  14573.             select_grantable    int,
  14574.             insert_privilege    int,
  14575.             insert_grantable    int,
  14576.             update_privilege    int,
  14577.             update_grantable    int,
  14578.             delete_privilege    int,
  14579.             delete_grantable    int,
  14580.             references_privilege    int null,
  14581.             references_grantable    int null,
  14582.             uid                 int,
  14583.             gid                 int)
  14584.  
  14585.     insert into #table_privileges
  14586.     select distinct db_name(),
  14587.         user_name(o.uid),
  14588.         o.name,
  14589.         user_name(o.uid),
  14590.         u.name,
  14591.         0,
  14592.         0,
  14593.         0,
  14594.         0,
  14595.         0,
  14596.         0,
  14597.         0,
  14598.         0,
  14599.         null,
  14600.         null,
  14601.         u.uid,
  14602.         u.gid
  14603.     from sysusers u, sysobjects o
  14604.     where o.id = @table_id
  14605.     and u.uid != u.gid
  14606.     
  14607.     /* 
  14608.     ** now add row for table owner
  14609.     */    
  14610.     if exists (
  14611.         select * 
  14612.             from #table_privileges
  14613.             where grantor = grantee)
  14614.     begin
  14615.         update #table_privileges
  14616.             set select_privilege = 1,
  14617.                 select_grantable = 1,
  14618.                 update_privilege = 1,
  14619.                 update_grantable = 1,
  14620.                 insert_privilege = 1,
  14621.                 insert_grantable = 1,
  14622.                 delete_privilege = 1,
  14623.                 delete_grantable = 1
  14624.             where grantor = grantee
  14625.     end
  14626.     else 
  14627.     begin
  14628.         insert into #table_privileges
  14629.         select  db_name(),
  14630.             user_name(o.uid),
  14631.             o.name,
  14632.             user_name(o.uid),
  14633.             user_name(o.uid),
  14634.             1,
  14635.             1,
  14636.             1,
  14637.             1,
  14638.             1,
  14639.             1,
  14640.             1,
  14641.             1,
  14642.             null,
  14643.             null,
  14644.             o.uid,
  14645.             u.gid
  14646.         from sysobjects o, sysusers u
  14647.         where o.id = @table_id
  14648.         and u.uid = o.uid
  14649.     end     
  14650.              
  14651.     update #table_privileges 
  14652.         set select_privilege = 1 
  14653.         where exists (select * from sysprotects
  14654.             where id = @table_id
  14655.             and (#table_privileges.uid = uid
  14656.                 or #table_privileges.gid = uid
  14657.                 or uid = 0)
  14658.             and protecttype = 205
  14659.             and action = 193)
  14660.         and not exists (select * from sysprotects
  14661.             where id = @table_id
  14662.             and (#table_privileges.uid = uid
  14663.                 or #table_privileges.gid = uid
  14664.                 or uid = 0)
  14665.             and protecttype = 206
  14666.             and action = 193)
  14667.  
  14668.  
  14669.     update #table_privileges 
  14670.         set insert_privilege = 1 
  14671.         where exists (select * from sysprotects 
  14672.             where id = @table_id
  14673.             and (#table_privileges.uid = uid
  14674.                 or #table_privileges.gid = uid
  14675.                 or uid = 0)
  14676.             and protecttype = 205
  14677.             and action = 195)
  14678.         and not exists (select * from sysprotects
  14679.             where id = @table_id
  14680.             and (#table_privileges.uid = uid
  14681.                 or #table_privileges.gid = uid
  14682.                 or uid = 0)
  14683.             and protecttype = 206
  14684.             and action = 195)
  14685.  
  14686.     update #table_privileges 
  14687.         set delete_privilege = 1 
  14688.         where exists (select * from sysprotects 
  14689.             where id = @table_id
  14690.             and (#table_privileges.uid = uid
  14691.                 or #table_privileges.gid = uid
  14692.                 or uid = 0)
  14693.             and protecttype = 205
  14694.             and action = 196)
  14695.         and not exists (select * from sysprotects
  14696.             where id = @table_id
  14697.             and (#table_privileges.uid = uid
  14698.                 or #table_privileges.gid = uid
  14699.                 or uid = 0)
  14700.             and protecttype = 206
  14701.             and action = 196)
  14702.  
  14703.   
  14704.  
  14705.  
  14706.     update #table_privileges 
  14707.         set update_privilege = 1 
  14708.         where exists (select * from sysprotects 
  14709.             where id = @table_id
  14710.             and (#table_privileges.uid = uid
  14711.                 or #table_privileges.gid = uid
  14712.                 or uid = 0)
  14713.             and protecttype = 205
  14714.             and action = 197)
  14715.         and not exists (select * from sysprotects
  14716.             where id = @table_id
  14717.             and (#table_privileges.uid = uid
  14718.                 or #table_privileges.gid = uid
  14719.                 or uid = 0)
  14720.             and protecttype = 206
  14721.             and action = 197)
  14722.  
  14723.     select 
  14724.             table_qualifier,
  14725.             table_owner,
  14726.             table_name,
  14727.             grantor,
  14728.             grantee,
  14729.             select_privilege,
  14730.             select_grantable,
  14731.             insert_privilege,
  14732.             insert_grantable,
  14733.             update_privilege,
  14734.             update_grantable,
  14735.             delete_privilege,
  14736.             delete_grantable,
  14737.             references_privilege,
  14738.             references_grantable
  14739.     from #table_privileges
  14740.         where select_privilege != 0
  14741.         or insert_privilege != 0
  14742.         or delete_privilege != 0
  14743.         or update_privilege != 0
  14744.  
  14745.  
  14746. go
  14747. grant execute on sp_table_privileges to public
  14748. go
  14749. dump tran master with truncate_only
  14750. go
  14751.                
  14752. print "creating sp_column_privileges"
  14753. go
  14754.  
  14755. CREATE PROCEDURE sp_column_privileges ( 
  14756.                         @table_name  varchar(32),
  14757.                         @table_owner varchar(32) = null,
  14758.                         @table_qualifier varchar(32)= null,
  14759.                         @column_name varchar(32) = null)
  14760. as        
  14761.  
  14762.     declare @table_id    int,
  14763.         @owner_id    int
  14764.     DECLARE @full_table_name    char(70)
  14765.  
  14766.     declare @low int            /* range of userids to check */
  14767.     declare @high int
  14768.     declare @objid int            /* id of @name if object */
  14769.     declare @owner_name varchar(32)
  14770.  
  14771.     select @low = 0, @high = 32767
  14772.  
  14773.     if @column_name is null
  14774.         select @column_name = '%'
  14775.  
  14776.     if @table_qualifier is not null
  14777.     begin
  14778.         if db_name() != @table_qualifier
  14779.         begin
  14780.             print "Table qualifier must be name of current database"
  14781.             return
  14782.         end
  14783.     end
  14784.     if @table_owner is null
  14785.     begin
  14786.         SELECT @full_table_name = @table_name
  14787.     end
  14788.     else
  14789.     begin
  14790.         SELECT @full_table_name = @table_owner + '.' + @table_name
  14791.     end
  14792.     select @table_id = 0    
  14793.     select @table_id = object_id(@full_table_name) 
  14794.     if @table_id = 0
  14795.     begin
  14796.         print "table not found"
  14797.         return
  14798.     end
  14799.  
  14800.     if @@trancount != 0
  14801.     begin
  14802.         raiserror 20001 "catalog procedure sp_column_privileges can not be run in a transaction"
  14803.         return
  14804.     end
  14805.  
  14806.     /*
  14807.     ** We need to create a table which will contain a row for every row to
  14808.     ** be returned to the client.  
  14809.     */
  14810.  
  14811.     create table #column_privileges 
  14812.             (table_qualifier     varchar(32),
  14813.             table_owner         varchar(32),    
  14814.             table_name         varchar(32),
  14815.             column_name         varchar(32),
  14816.             grantor            varchar(32),
  14817.             grantee            varchar(32),
  14818.             select_privilege    int,
  14819.             select_grantable    int,
  14820.             insert_privilege    int,
  14821.             insert_grantable    int,
  14822.             update_privilege    int,
  14823.             update_grantable    int,
  14824.             delete_privilege    int,
  14825.             delete_grantable    int,
  14826.             references_privilege    int null,
  14827.             references_grantable    int null,
  14828.             uid                 int,
  14829.             gid                 int)
  14830.  
  14831. /* 
  14832. ** insert a row for the table owner (who has all permissions)
  14833. */
  14834.     select @owner_name = (select user_name(uid) 
  14835.                             from sysobjects 
  14836.                                 where id = @table_id)
  14837.  
  14838.  
  14839.     insert into #column_privileges
  14840.     select
  14841.             db_name(),
  14842.             @owner_name,
  14843.             @table_name,
  14844.             name,
  14845.             @owner_name,
  14846.             @owner_name,
  14847.             1,
  14848.             1,
  14849.             1,
  14850.             1,
  14851.             1,
  14852.             1,
  14853.             1,
  14854.             1,
  14855.             null,
  14856.             null,
  14857.             user_id(@owner_name),
  14858.             0
  14859.         from syscolumns 
  14860.         where id = @table_id
  14861.  
  14862. /* 
  14863. ** now stick a row in the table for every user in the database        
  14864. ** we will need to weed out those who have no permissions later
  14865. ** (and yes this is a cartesion product: the uid field in sysprotects
  14866. ** can also have a group id, in which case we need to extend those 
  14867. ** privileges to all group members).
  14868. */
  14869.     
  14870.     insert into #column_privileges
  14871.     select distinct 
  14872.         db_name(),
  14873.         user_name(o.uid),
  14874.         @table_name,
  14875.         c.name,
  14876.         user_name(o.uid),
  14877.         u.name,
  14878.         0,
  14879.         0,
  14880.         0,
  14881.         0,
  14882.         0,
  14883.         0,
  14884.         0,
  14885.         0,
  14886.         null,
  14887.         null,
  14888.         u.uid,
  14889.         u.gid
  14890.     from sysusers u, syscolumns c, sysobjects o
  14891.     where o.id = @table_id
  14892.         and c.id = o.id
  14893.         and u.gid != u.uid
  14894.         and u.name != @owner_name
  14895.  
  14896.     /*
  14897.     ** we need to create another temporary table to contain all the various
  14898.     ** protection information for the table in question
  14899.     */
  14900.     create table #protects (uid smallint,
  14901.                             action tinyint,
  14902.                             protecttype tinyint,
  14903.                             name    varchar(32))
  14904.     insert into #protects
  14905.     select  p.uid,
  14906.             p.action,
  14907.             p.protecttype,
  14908.             isnull(col_name(id, c.number), "All")
  14909.         from sysprotects p, master.dbo.spt_values c,
  14910.                 master.dbo.spt_values a, master.dbo.spt_values b
  14911.             where convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  14912.                 & c.high != 0
  14913.                 and c.number <=
  14914.                     (select count(*)
  14915.                         from syscolumns
  14916.                         where id = @table_id)
  14917.                 and a.type = "T"
  14918.                 and a.number = p.action
  14919.                 and b.type = "T"
  14920.                 and b.number = p.protecttype
  14921.                 and p.id = @table_id
  14922.                 and p.uid between @low and @high
  14923.  
  14924.  
  14925.     update #column_privileges 
  14926.         set select_privilege = 1 
  14927.         where exists 
  14928.             (select * from #protects
  14929.             where protecttype = 205
  14930.             and action = 193
  14931.             and ( name = #column_privileges.column_name
  14932.                   or name = 'All')
  14933.             and ( uid = 0 
  14934.                   or uid = #column_privileges.gid
  14935.                   or uid = #column_privileges.uid))
  14936.         and not exists (select * from #protects
  14937.             where protecttype = 206
  14938.             and action = 193
  14939.             and ( name = #column_privileges.column_name
  14940.                   or name = 'All')
  14941.             and ( uid = 0 
  14942.                   or uid = #column_privileges.gid
  14943.                   or uid = #column_privileges.uid))
  14944.  
  14945.   
  14946.           
  14947.     update #column_privileges 
  14948.         set insert_privilege = 1 
  14949.         where exists (select * from #protects 
  14950.             where protecttype = 205
  14951.             and action = 195
  14952.             and ( name = #column_privileges.column_name
  14953.                   or name = 'All')
  14954.             and  ( uid = 0 
  14955.                   or uid = #column_privileges.gid
  14956.                   or uid = #column_privileges.uid))
  14957.         and not exists (select * from #protects
  14958.             where protecttype = 206
  14959.             and action = 195
  14960.             and ( name = #column_privileges.column_name
  14961.                   or name = 'All')
  14962.             and ( uid = 0 
  14963.                   or uid = #column_privileges.gid
  14964.                   or uid = #column_privileges.uid))
  14965.  
  14966.  
  14967.             
  14968.     update #column_privileges 
  14969.         set insert_privilege = 1 
  14970.         where exists (select * from #protects 
  14971.             where protecttype = 205
  14972.             and action = 196
  14973.             and ( name = #column_privileges.column_name
  14974.                   or name = 'All')
  14975.             and ( uid = 0 
  14976.                   or uid = #column_privileges.gid
  14977.                   or uid = #column_privileges.uid))
  14978.         and not exists (select * from #protects
  14979.             where protecttype = 206
  14980.             and action = 196
  14981.             and ( name = #column_privileges.column_name
  14982.                   or name = 'All')
  14983.             and ( uid = 0 
  14984.                   or uid = #column_privileges.gid
  14985.                   or uid = #column_privileges.uid))
  14986.  
  14987.  
  14988.     update #column_privileges
  14989.         set update_privilege = 1 
  14990.         where exists (select * from #protects 
  14991.             where protecttype = 205
  14992.             and action = 197
  14993.             and ( name = #column_privileges.column_name
  14994.                   or name = 'All')
  14995.             and ( uid = 0 
  14996.                   or uid = #column_privileges.gid
  14997.                   or uid = #column_privileges.uid))
  14998.         and not exists (select * from #protects
  14999.             where protecttype = 206
  15000.             and action = 197
  15001.             and ( name = #column_privileges.column_name
  15002.                   or name = 'All')
  15003.             and ( uid = 0 
  15004.                   or uid = #column_privileges.gid
  15005.                   or uid = #column_privileges.uid))
  15006.  
  15007.  
  15008.           
  15009.     select 
  15010.             table_qualifier,
  15011.             table_owner,
  15012.             table_name,
  15013.             column_name,
  15014.             grantor,
  15015.             grantee,
  15016.             select_privilege,
  15017.             select_grantable,
  15018.             insert_privilege,
  15019.             insert_grantable,
  15020.             update_privilege,
  15021.             update_grantable,
  15022.             delete_privilege,
  15023.             delete_grantable,
  15024.             references_privilege,
  15025.             references_grantable
  15026.     from #column_privileges
  15027.         where column_name like @column_name
  15028.         and ( select_privilege != 0
  15029.         or insert_privilege != 0
  15030.         or delete_privilege != 0
  15031.         or update_privilege != 0)
  15032.  
  15033.  
  15034.  
  15035.  
  15036. drop table #column_privileges
  15037. drop table #protects
  15038. go
  15039. grant execute on sp_column_privileges to public
  15040. go
  15041. dump tran master with truncate_only
  15042.  
  15043.  
  15044.  
  15045. print "creating sp_server_info"
  15046. if (exists (select * from sysobjects where name = 'sp_server_info'))
  15047.     drop proc sp_server_info
  15048. go
  15049.  
  15050. create proc sp_server_info (@attribute_id  int = null)
  15051. as
  15052.     if @attribute_id is not null
  15053.         select *
  15054.         from master.dbo.MSserver_info
  15055.         where attribute_id = @attribute_id
  15056.     else
  15057.         select *
  15058.         from master.dbo.MSserver_info
  15059.  
  15060. go
  15061.  
  15062. grant execute on sp_server_info to public
  15063. go
  15064.  
  15065. print "creating sp_datatype_info"
  15066. if (exists (select * from sysobjects where name = 'sp_datatype_info'))
  15067.     drop proc sp_datatype_info
  15068. go
  15069.  
  15070. /* the messiness of 'data_type' was to get around the problem of
  15071. returning the correct lengths for user defined types.  the join
  15072. on the type name ensures all user defined types are returned, but
  15073. this puts a null in the data_type column.  by forcing an embedded
  15074. select and correlating it with the current row in systypes, we get
  15075. the correct data_type mapping even for user defined types  (kwg) */
  15076.  
  15077. create proc sp_datatype_info (@data_type int = 0)
  15078. as
  15079.     if @data_type = 0
  15080.         select
  15081.             type_name = t.name,
  15082.             d.data_type,
  15083.             t.length,
  15084.             d.literal_prefix,
  15085.             d.literal_suffix,
  15086.             d.create_params,
  15087.             d.nullable,
  15088.             d.case_sensitive,
  15089.             d.searchable
  15090.         from master.dbo.MSdatatype_info d, systypes t
  15091.         where d.ss_dtype = t.type and
  15092.           t.type not in (111,109,38,110)  /* get rid of nullable types */
  15093.     else
  15094.         select
  15095.             type_name = t.name,
  15096.             d.data_type,
  15097.             t.length,
  15098.             d.literal_prefix,
  15099.             d.literal_suffix,
  15100.             d.create_params,
  15101.             d.nullable,
  15102.             d.case_sensitive,
  15103.             d.searchable
  15104.         from master.dbo.MSdatatype_info d, systypes t
  15105.         where data_type = @data_type and
  15106.              d.ss_dtype = t.type and
  15107.           t.type not in (111,109,38,110)
  15108. go
  15109.  
  15110. grant execute on sp_datatype_info to public
  15111. go
  15112.  
  15113. dump tran master with truncate_only
  15114. go
  15115.  
  15116. print "creating sp_special_columns"
  15117. if (exists (select * from sysobjects where name = 'sp_special_columns'))
  15118.     drop proc sp_special_columns
  15119. go
  15120.  
  15121. dump tran master with truncate_only
  15122. go
  15123.  
  15124.  
  15125. CREATE PROCEDURE sp_special_columns (@table_name      varchar(32),
  15126.                              @table_owner      varchar(32) = null,
  15127.                              @table_qualifier varchar(32) = null,
  15128.                              @col_type       char(1) = 'R')
  15129. AS
  15130.     DECLARE @indid                int
  15131.     DECLARE @table_id            int
  15132.     DECLARE @full_table_name    char(70)
  15133.     DECLARE @msg          char(70)
  15134.  
  15135.     if @table_qualifier is not null
  15136.     begin
  15137.         if db_name() != @table_qualifier
  15138.         begin
  15139.             print "Table qualifier must be name of current database"
  15140.             return
  15141.         end
  15142.     end
  15143.     if @table_owner is null
  15144.     begin
  15145.         SELECT @full_table_name = @table_name
  15146.     end
  15147.     else
  15148.     begin
  15149.         SELECT @full_table_name = @table_owner + '.' + @table_name
  15150.     end
  15151.     SELECT @table_id = object_id(@full_table_name)
  15152.     
  15153.   /* if we just need ROWVER, just run that query */
  15154.     if @col_type = 'V'
  15155.   BEGIN
  15156.     SELECT   
  15157.         scope = 1,
  15158.         column_name = c.name,
  15159.         d.data_type,
  15160.             type_name = t.name,
  15161.             data_precision=isnull(d.data_precision, c.length),
  15162.             c.length,
  15163.             d.numeric_scale
  15164.       FROM
  15165.             systypes t,syscolumns c,  master.dbo.MSdatatype_info d
  15166.       WHERE
  15167.           c.id=@table_id
  15168.           AND c.type = d.ss_dtype
  15169.       AND t.type = c.type 
  15170.       AND t.usertype = 80 
  15171.     RETURN
  15172.   END
  15173.   /* now find the id of the 'best' index for this table */
  15174.  
  15175.   SELECT @indid = (
  15176.     SELECT MIN(indid) 
  15177.       FROM sysindexes
  15178.           WHERE  status&2 = 2
  15179.       AND id = @table_id
  15180.       AND indid > 0)
  15181.  
  15182.    SELECT
  15183.       scope = 3,
  15184.       column_name = INDEX_COL(@full_table_name,indid,colid),
  15185.       d.data_type,
  15186.        d.type_name,
  15187.       data_precision=isnull(d.data_precision, c.length),
  15188.       c.length,
  15189.       d.numeric_scale
  15190.     FROM sysindexes x,syscolumns c,  master.dbo.MSdatatype_info d
  15191.     WHERE x.id=@table_id
  15192.         AND x.id=c.id
  15193.         AND c.colid<keycnt+ (x.status&16)/16
  15194.         AND x.indid=@indid
  15195.       AND c.type = d.ss_dtype
  15196.  
  15197. go  
  15198. grant execute on sp_special_columns to public
  15199. go
  15200.  
  15201.  
  15202.  
  15203.  
  15204. print "creating sp_databases"
  15205. if (exists (select * from sysobjects where name = 'sp_databases'))
  15206.   drop proc sp_databases
  15207. go
  15208.  
  15209. create procedure sp_databases
  15210.         as
  15211.         create table #databases ( database_name varchar(32),
  15212.                                   size int)
  15213.         insert into #databases                          
  15214.         select  name,
  15215.             (select sum(size) from master.dbo.sysusages
  15216.           where dbid = d.dbid )
  15217.         from master.dbo.sysdatabases d
  15218.         select database_name, database_size = size*2, remarks = null
  15219.         from #databases
  15220. go
  15221. grant execute on sp_databases to public
  15222. go
  15223.  
  15224. dump tran master with truncate_only
  15225. go
  15226.  
  15227.  
  15228.  
  15229.  
  15230.  
  15231.  
  15232. /*******************************************************************************/
  15233. /* This portion returns everything back to normal                   */
  15234. /*******************************************************************************/
  15235. use master
  15236. go
  15237.  
  15238. if exists (select * from sysobjects
  15239.            where name = 'sp_configure' and sysstat & 7 = 4)
  15240. begin
  15241.     execute sp_configure 'update',0
  15242. end
  15243. reconfigure with override
  15244. go
  15245. checkpoint
  15246. go
  15247. print 'Loading of master database is complete.'
  15248. go
  15249.