home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a061 / 7.img / INSTMSTR.SQ@ / INSTMSTR.bin
Encoding:
Text File  |  1992-02-26  |  318.9 KB  |  13,337 lines

  1. /* $Header:   S:/DBMS/OS2/SCRIPTS/PVCS/INSTMSTR.SQV   4.44   27 Feb 1992 17:21:48   ANKUR  $ */
  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 3MB 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. if exists (select *
  19.     from sysobjects
  20.         where name = 'sp_configure'
  21.             and sysstat & 7 = 4)
  22. begin
  23.     execute sp_configure 'update', 1
  24. end
  25. reconfigure with override
  26. go
  27. /*
  28. ** @(#)instmstr.sql    82.1    3/11/91
  29. **
  30. ** Copyright Sybase, Inc. 1986, 1987, 1988, 1989
  31. **    All Rights Reserved.
  32. ** Use, duplication, or disclosure by the United States Government
  33. ** is subject to restrictions as set forth in subdivision (c) (1) (ii)
  34. ** of the Rights in Technical Data and Computer Software clause
  35. ** at CFR 252.227-7013. Sybase, Inc. 6475 Christie Avenue, Emeryville,
  36. ** CA 94608.
  37. **
  38. **
  39. ** Add sysmessage 157
  40. */
  41. insert into master..sysmessages values (157,15,2,
  42.     "An aggregate may not appear in the set list of an update statement.",
  43.     null)
  44.  
  45. /*
  46. ** Stored procedures for Data Dictionary Operations
  47. */
  48. go
  49. /* a_permitboth 27.1 3/5/90 */
  50. grant select on sysalternates to public
  51. grant select on syscolumns to public
  52. grant select on syscomments to public
  53. grant select on sysdepends to public
  54. grant select on sysindexes to public
  55. grant select on syskeys to public
  56. grant select on sysobjects to public
  57. grant select on sysprocedures to public
  58. grant select on sysprotects to public
  59. grant select on syssegments to public
  60. grant select on systypes to public
  61. grant select on sysusers to public
  62. grant select on syslogs to public
  63. go
  64. dump transaction master with truncate_only
  65. go
  66. /* a_permitmaster 27.1 3/5/90 */
  67. grant select on sysdatabases to public
  68. grant select on sysdevices to public
  69. grant select on syslocks to public
  70. grant select on syslogins to public
  71. revoke select on syslogins(password) to public
  72. grant select on sysmessages to public
  73. grant select on sysprocesses to public
  74. grant select on sysusages to public
  75. grant select on sysconfigures to public
  76. grant select on syscurconfigs to public
  77. grant select on sysservers to public
  78. grant select on sysremotelogins to public
  79. grant select on syslanguages to public
  80. grant select on syscharsets to public
  81. go
  82. /* a_values 27.1 3/5/90 */
  83. set nocount on
  84. go
  85. use master
  86. go
  87. if exists (select *
  88.     from sysobjects
  89.         where name = "spt_values")
  90. begin
  91.     drop table spt_values
  92. end
  93. go
  94. if exists (select *
  95.         from sysobjects
  96.             where name = "spt_monitor")
  97. begin
  98.     drop table spt_monitor
  99. end
  100. go
  101. if exists (select *
  102.     from sysobjects
  103.         where sysstat & 7 = 4
  104.             and name = 'sp_validname')
  105. begin
  106.     drop procedure sp_validname
  107. end
  108. go
  109. if exists (select *
  110.     from sysobjects
  111.         where sysstat & 7 = 4
  112.             and name = 'sp_validlang')
  113. begin
  114.     drop procedure sp_validlang
  115. end
  116. go
  117. if exists (select *
  118.     from sysobjects
  119.         where sysstat & 7 = 4
  120.             and name = 'sp_validaltlang')
  121. begin
  122.     drop procedure sp_validaltlang
  123. end
  124. go
  125. if exists (select *
  126.     from sysobjects
  127.         where sysstat & 7 = 4
  128.             and name = 'sp_chklangparam')
  129. begin
  130.     drop procedure sp_chklangparam
  131. end
  132. go
  133. if exists (select *
  134.     from sysobjects
  135.         where sysstat & 7 = 4
  136.             and name = 'sp_namecrack')
  137. begin
  138.     drop procedure sp_namecrack
  139. end
  140. go
  141. if exists (select *
  142.     from sysobjects
  143.         where sysstat & 7 = 4
  144.             and name = 'sp_addalias')
  145. begin
  146.     drop procedure sp_addalias
  147. end
  148. go
  149. if exists (select *
  150.     from sysobjects
  151.         where sysstat & 7 = 4
  152.             and name = 'sp_addgroup')
  153. begin
  154.     drop procedure sp_addgroup
  155. end
  156. go
  157. if exists (select *
  158.     from sysobjects
  159.         where sysstat & 7 = 4
  160.             and name = 'sp_addlogin')
  161. begin
  162.     drop procedure sp_addlogin
  163. end
  164. go
  165. dump transaction master with truncate_only
  166. go
  167. if exists (select *
  168.     from sysobjects
  169.         where sysstat & 7 = 4
  170.             and name = 'sp_addtype')
  171. begin
  172.     drop procedure sp_addtype
  173. end
  174. go
  175. if exists (select *
  176.     from sysobjects
  177.         where sysstat & 7 = 4
  178.             and name = 'sp_addumpdevice')
  179. begin
  180.     drop procedure sp_addumpdevice
  181. end
  182. go
  183. if exists (select *
  184.     from sysobjects
  185.         where sysstat & 7 = 4
  186.             and name = 'sp_adduser')
  187. begin
  188.     drop procedure sp_adduser
  189. end
  190. go
  191. if exists (select *
  192.     from sysobjects
  193.         where sysstat & 7 = 4
  194.             and name = 'sp_addremotelogin')
  195. begin
  196.     drop procedure sp_addremotelogin
  197. end
  198. go
  199. if exists (select *
  200.     from sysobjects
  201.         where sysstat & 7 = 4
  202.             and name = 'sp_addsegment')
  203. begin
  204.     drop procedure sp_addsegment
  205. end
  206. go
  207. if exists (select *
  208.     from sysobjects
  209.         where sysstat & 7 = 4
  210.             and name = 'sp_addserver')
  211. begin
  212.     drop procedure sp_addserver
  213. end
  214. go
  215. if exists (select *
  216.     from sysobjects
  217.         where sysstat & 7 = 4
  218.             and name = 'sp_addlanguage')
  219. begin
  220.     drop procedure sp_addlanguage
  221. end
  222. go
  223. if exists (select *
  224.     from sysobjects
  225.         where sysstat & 7 = 4
  226.             and name = 'sp_defaultlanguage')
  227. begin
  228.     drop procedure sp_defaultlanguage
  229. end
  230. go
  231. if exists (select *
  232.     from sysobjects
  233.         where sysstat & 7 = 4
  234.             and name = 'sp_helplanguage')
  235. begin
  236.     drop procedure sp_helplanguage
  237. end
  238. go
  239. if exists (select *
  240.     from sysobjects
  241.         where sysstat & 7 = 4
  242.             and name = 'sp_droplanguage')
  243. begin
  244.     drop procedure sp_droplanguage
  245. end
  246. go
  247. dump transaction master with truncate_only
  248. go
  249. if exists (select *
  250.     from sysobjects
  251.         where sysstat & 7 = 4
  252.             and name = 'sp_setlangalias')
  253. begin
  254.     drop procedure sp_setlangalias
  255. end
  256. go
  257. if exists (select *
  258.     from sysobjects
  259.         where sysstat & 7 = 4
  260.             and name = 'sp_bindefault')
  261. begin
  262.     drop procedure sp_bindefault
  263. end
  264. go
  265. if exists (select *
  266.     from sysobjects
  267.         where sysstat & 7 = 4
  268.             and name = 'sp_bindrule')
  269. begin
  270.     drop procedure sp_bindrule
  271. end
  272. go
  273. if exists (select *
  274.     from sysobjects
  275.         where sysstat & 7 = 4
  276.             and name = 'sp_changedbowner')
  277. begin
  278.     drop procedure sp_changedbowner
  279. end
  280. go
  281. if exists (select *
  282.     from sysobjects
  283.         where sysstat & 7 = 4
  284.             and name = 'sp_changegroup')
  285. begin
  286.     drop procedure sp_changegroup
  287. end
  288. go
  289. if exists (select *
  290.     from sysobjects
  291.         where sysstat & 7 = 4
  292.             and name = 'sp_checknames')
  293. begin
  294.     drop procedure sp_checknames
  295. end
  296. go
  297. if exists (select *
  298.     from sysobjects
  299.         where sysstat & 7 = 4
  300.             and name = 'sp_start_xact')
  301. begin
  302.     drop procedure sp_start_xact
  303. end
  304. go
  305. if exists (select *
  306.     from sysobjects
  307.         where sysstat & 7 = 4
  308.             and name = 'sp_commit_xact')
  309. begin
  310.     drop procedure sp_commit_xact
  311. end
  312. go
  313. if exists (select *
  314.     from sysobjects
  315.         where sysstat & 7 = 4
  316.             and name = 'sp_abort_xact')
  317. begin
  318.     drop procedure sp_abort_xact
  319. end
  320. go
  321. if exists (select *
  322.     from sysobjects
  323.         where sysstat & 7 = 4
  324.             and name = 'sp_remove_xact')
  325. begin
  326.     drop procedure sp_remove_xact
  327. end
  328. go
  329. dump transaction master with truncate_only
  330. go
  331. if exists (select *
  332.     from sysobjects
  333.         where sysstat & 7 = 4
  334.             and name = 'sp_stat_xact')
  335. begin
  336.     drop procedure sp_stat_xact
  337. end
  338. go
  339. if exists (select *
  340.     from sysobjects
  341.         where sysstat & 7 = 4
  342.             and name = 'sp_probe_xact')
  343. begin
  344.     drop procedure sp_probe_xact
  345. end
  346. go
  347. if exists (select *
  348.     from sysobjects
  349.         where sysstat & 7 = 4
  350.             and name = 'sp_scan_xact')
  351. begin
  352.     drop procedure sp_scan_xact
  353. end
  354. go
  355. /* commit 27.1 3/5/90 */
  356. if exists (select *
  357.         from sysobjects
  358.             where name = "spt_committab"
  359.                 and sysstat & 7 = 3)
  360. begin
  361.     drop table spt_committab
  362. end
  363. go
  364. if exists (select *
  365.     from sysobjects
  366.         where sysstat & 7 = 4
  367.             and name = 'sp_commonkey')
  368. begin
  369.     drop procedure sp_commonkey
  370. end
  371. go
  372. if exists (select *
  373.     from sysobjects
  374.         where sysstat & 7 = 4
  375.             and name = 'sp_configure')
  376. begin
  377.     drop procedure sp_configure
  378. end
  379. go
  380. if exists (select *
  381.     from sysobjects
  382.         where sysstat & 7 = 4
  383.             and name = 'sp_dboption')
  384. begin
  385.     drop procedure sp_dboption
  386. end
  387. go
  388. if exists (select *
  389.     from sysobjects
  390.         where sysstat & 7 = 4
  391.             and name = 'sp_defaultdb')
  392. begin
  393.     drop procedure sp_defaultdb
  394. end
  395. go
  396. if exists (select *
  397.     from sysobjects
  398.         where sysstat & 7 = 4
  399.             and name = 'sp_depends')
  400. begin
  401.     drop procedure sp_depends
  402. end
  403. go
  404. if exists (select *
  405.     from sysobjects
  406.         where sysstat & 7 = 4
  407.             and name = 'sp_diskdefault')
  408. begin
  409.     drop procedure sp_diskdefault
  410. end
  411. go
  412. dump transaction master with truncate_only
  413. go
  414. if exists (select *
  415.     from sysobjects
  416.         where sysstat & 7 = 4
  417.             and name = 'sp_dropalias')
  418. begin
  419.     drop procedure sp_dropalias
  420. end
  421. go
  422. if exists (select *
  423.     from sysobjects
  424.         where sysstat & 7 = 4
  425.             and name = 'sp_dropdevice')
  426. begin
  427.     drop procedure sp_dropdevice
  428. end
  429. go
  430. if exists (select *
  431.     from sysobjects
  432.         where sysstat & 7 = 4
  433.             and name = 'sp_dropdumpdevice')
  434. begin
  435.     drop procedure sp_dropdumpdevice
  436. end
  437. go
  438. if exists (select *
  439.     from sysobjects
  440.         where sysstat & 7 = 4
  441.             and name = 'sp_dropgroup')
  442. begin
  443.     drop procedure sp_dropgroup
  444. end
  445. go
  446. if exists (select *
  447.     from sysobjects
  448.         where sysstat & 7 = 4
  449.             and name = 'sp_dropkey')
  450. begin
  451.     drop procedure sp_dropkey
  452. end
  453. go
  454. if exists (select *
  455.     from sysobjects
  456.         where sysstat & 7 = 4
  457.             and name = 'sp_droplogin')
  458. begin
  459.     drop procedure sp_droplogin
  460. end
  461. go
  462. if exists (select *
  463.     from sysobjects
  464.         where sysstat & 7 = 4
  465.             and name = 'sp_droptype')
  466. begin
  467.     drop procedure sp_droptype
  468. end
  469. go
  470. if exists (select *
  471.     from sysobjects
  472.         where sysstat & 7 = 4
  473.             and name = 'sp_dropuser')
  474. begin
  475.     drop procedure sp_dropuser
  476. end
  477. go
  478. if exists (select *
  479.     from sysobjects
  480.         where sysstat & 7 = 4
  481.             and name = 'sp_dropremotelogin')
  482. begin
  483.     drop procedure sp_dropremotelogin
  484. end
  485. go
  486. if exists (select *
  487.     from sysobjects
  488.         where sysstat & 7 = 4
  489.             and name = 'sp_dropsegment')
  490. begin
  491.     drop procedure sp_dropsegment
  492. end
  493. go
  494. dump transaction master with truncate_only
  495. go
  496. if exists (select *
  497.     from sysobjects
  498.         where sysstat & 7 = 4
  499.             and name = 'sp_dropserver')
  500. begin
  501.     drop procedure sp_dropserver
  502. end
  503. go
  504. if exists (select *
  505.     from sysobjects
  506.         where sysstat & 7 = 4
  507.             and name = 'sp_extendsegment')
  508. begin
  509.     drop procedure sp_extendsegment
  510. end
  511. go
  512. if exists (select *
  513.     from sysobjects
  514.         where sysstat & 7 = 4
  515.             and name = 'sp_fixindex')
  516. begin
  517.     drop procedure sp_fixindex
  518. end
  519. go
  520. if exists (select *
  521.     from sysobjects
  522.         where sysstat & 7 = 4
  523.             and name = 'sp_foreignkey')
  524. begin
  525.     drop procedure sp_foreignkey
  526. end
  527. go
  528. if exists (select *
  529.     from sysobjects
  530.         where sysstat & 7 = 4
  531.             and name = 'sp_helpdb')
  532. begin
  533.     drop procedure sp_helpdb
  534. end
  535. go
  536. if exists (select *
  537.     from sysobjects
  538.         where sysstat & 7 = 4
  539.             and name = 'sp_helpdevice')
  540. begin
  541.     drop procedure sp_helpdevice
  542. end
  543. go
  544. if exists (select *
  545.     from sysobjects
  546.         where sysstat & 7 = 4
  547.             and name = 'sp_helpgroup')
  548. begin
  549.     drop procedure sp_helpgroup
  550. end
  551. go
  552. if exists (select *
  553.     from sysobjects
  554.         where sysstat & 7 = 4
  555.             and name = 'sp_helplog')
  556. begin
  557.     drop procedure sp_helplog
  558. end
  559. go
  560. if exists (select *
  561.     from sysobjects
  562.         where sysstat & 7 = 4
  563.             and name = 'sp_helpindex')
  564. begin
  565.     drop procedure sp_helpindex
  566. end
  567. go
  568. if exists (select *
  569.     from sysobjects
  570.         where sysstat & 7 = 4
  571.             and name = 'sp_helpjoins')
  572. begin
  573.     drop procedure sp_helpjoins
  574. end
  575. go
  576. dump transaction master with truncate_only
  577. go
  578. if exists (select *
  579.     from sysobjects
  580.         where sysstat & 7 = 4
  581.             and name = 'sp_helpkey')
  582. begin
  583.     drop procedure sp_helpkey
  584. end
  585. go
  586. if exists (select *
  587.     from sysobjects
  588.         where sysstat & 7 = 4
  589.             and name = 'sp_objectsegment')
  590. begin
  591.     drop procedure sp_objectsegment
  592. end
  593. go
  594. if exists (select *
  595.     from sysobjects
  596.         where sysstat & 7 = 4
  597.             and name = 'sp_help')
  598. begin
  599.     drop procedure sp_help
  600. end
  601. go
  602. if exists (select *
  603.     from sysobjects
  604.         where sysstat & 7 = 4
  605.             and name = 'sp_helprotect')
  606. begin
  607.     drop procedure sp_helprotect
  608. end
  609. go
  610. if exists (select *
  611.     from sysobjects
  612.         where sysstat & 7 = 4
  613.             and name = 'sp_helptext')
  614. begin
  615.     drop procedure sp_helptext
  616. end
  617. go
  618. if exists (select *
  619.     from sysobjects
  620.         where sysstat & 7 = 4
  621.             and name = 'sp_helpuser')
  622. begin
  623.     drop procedure sp_helpuser
  624. end
  625. go
  626. if exists (select *
  627.     from sysobjects
  628.         where sysstat & 7 = 4
  629.             and name = 'sp_lock')
  630. begin
  631.     drop procedure sp_lock
  632. end
  633. go
  634. if exists (select *
  635.     from sysobjects
  636.         where sysstat & 7 = 4
  637.             and name = 'sp_logdevice')
  638. begin
  639.     drop procedure sp_logdevice
  640. end
  641. go
  642. if exists (select *
  643.     from sysobjects
  644.         where sysstat & 7 = 4
  645.             and name = 'sp_lookup')
  646. begin
  647.     drop procedure sp_lookup
  648. end
  649. go
  650. if exists (select *
  651.     from sysobjects
  652.         where sysstat & 7 = 4
  653.             and name = 'sp_helpremotelogin')
  654. begin
  655.     drop procedure sp_helpremotelogin
  656. end
  657. go
  658. dump transaction master with truncate_only
  659. go
  660. if exists (select *
  661.     from sysobjects
  662.         where sysstat & 7 = 4
  663.             and name = 'sp_helpsegment')
  664. begin
  665.     drop procedure sp_helpsegment
  666. end
  667. go
  668. if exists (select *
  669.     from sysobjects
  670.         where sysstat & 7 = 4
  671.             and name = 'sp_helpsort')
  672. begin
  673.     drop procedure sp_helpsort
  674. end
  675. go
  676. if exists (select *
  677.     from sysobjects
  678.         where sysstat & 7 = 4
  679.             and name = 'sp_helpserver')
  680. begin
  681.     drop procedure sp_helpserver
  682. end
  683. go
  684. if exists (select *
  685.     from sysobjects
  686.         where sysstat & 7 = 4
  687.             and name = 'sp_markreport')
  688. begin
  689.     drop procedure sp_markreport
  690. end
  691. go
  692. if exists (select *
  693.     from sysobjects
  694.         where sysstat & 7 = 4
  695.             and name = 'sp_monitor')
  696. begin
  697.     drop procedure sp_monitor
  698. end
  699. go
  700. if exists (select *
  701.     from sysobjects
  702.         where sysstat & 7 = 4
  703.             and name = 'sp_password')
  704. begin
  705.     drop procedure sp_password
  706. end
  707. go
  708. if exists (select *
  709.     from sysobjects
  710.         where sysstat & 7 = 4
  711.             and name = 'sp_placeobject')
  712. begin
  713.     drop procedure sp_placeobject
  714. end
  715. go
  716. if exists (select *
  717.     from sysobjects
  718.         where sysstat & 7 = 4
  719.             and name = 'sp_primarykey')
  720. begin
  721.     drop procedure sp_primarykey
  722. end
  723. go
  724. if exists (select *
  725.     from sysobjects
  726.         where sysstat & 7 = 4
  727.             and name = 'sp_recompile')
  728. begin
  729.     drop procedure sp_recompile
  730. end
  731. go
  732. if exists (select *
  733.     from sysobjects
  734.         where sysstat & 7 = 4
  735.             and name = 'sp_remoteoption')
  736. begin
  737.     drop procedure sp_remoteoption
  738. end
  739. go
  740. dump transaction master with truncate_only
  741. go
  742. if exists (select *
  743.     from sysobjects
  744.         where sysstat & 7 = 4
  745.             and name = 'sp_rename')
  746. begin
  747.     drop procedure sp_rename
  748. end
  749. go
  750. if exists (select *
  751.     from sysobjects
  752.         where sysstat & 7 = 4
  753.             and name = 'sp_renamedb')
  754. begin
  755.     drop procedure sp_renamedb
  756. end
  757. go
  758. if exists (select *
  759.     from sysobjects
  760.         where sysstat & 7 = 4
  761.             and name = 'sp_serveroption')
  762. begin
  763.     drop procedure sp_serveroption
  764. end
  765. go
  766. if exists (select *
  767.     from sysobjects
  768.         where sysstat & 7 = 4
  769.             and name = 'sp_spaceused')
  770. begin
  771.     drop procedure sp_spaceused
  772. end
  773. go
  774. if exists (select *
  775.     from sysobjects
  776.         where sysstat & 7 = 4
  777.             and name = 'sp_unbindefault')
  778. begin
  779.     drop procedure sp_unbindefault
  780. end
  781. go
  782. if exists (select *
  783.     from sysobjects
  784.         where sysstat & 7 = 4
  785.             and name = 'sp_unbindrule')
  786. begin
  787.     drop procedure sp_unbindrule
  788. end
  789. go
  790. if exists (select *
  791.     from sysobjects
  792.         where sysstat & 7 = 4
  793.             and name = 'sp_who')
  794. begin
  795.     drop procedure sp_who
  796. end
  797. go
  798. dump transaction master with truncate_only
  799. go
  800.  
  801. create table spt_values (
  802. name    varchar(20) NULL,
  803. number    int,
  804. type    char(1),
  805. low    int NULL,
  806. high    int NULL)
  807. go
  808. create clustered index spt_valuesclust on spt_values(number, type)
  809. go
  810. insert spt_values(low, high, number, type) values (1, 1, 0, 'P')
  811. go
  812. set nocount on
  813. /*
  814. **insert spt_values(name, number, type)
  815. **    values ("BIT POSITIONS", -1, 'P')
  816. **go
  817. */
  818. insert spt_values(low, high, number, type) values (1, 2, 1, 'P')
  819. go
  820. insert spt_values(low, high, number, type) values (1, 4, 2, 'P')
  821. go
  822. insert spt_values(low, high, number, type) values (1, 8, 3, 'P')
  823. go
  824. insert spt_values(low, high, number, type) values (1, 16, 4, 'P')
  825. go
  826. insert spt_values(low, high, number, type) values (1, 32, 5, 'P')
  827. go
  828. insert spt_values(low, high, number, type) values (1, 64, 6, 'P')
  829. go
  830. insert spt_values(low, high, number, type) values (1, 128, 7, 'P')
  831. go
  832. /* 8 - 15 */
  833. insert spt_values(low, high, number, type)
  834. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P'
  835. from spt_values
  836. where type = 'P' and number < 8 and number >= 0
  837. go
  838. /* 16 - 31 */
  839. insert spt_values(low, high, number, type)
  840. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P'
  841. from spt_values
  842. where type = 'P' and number < 16 and number >= 0
  843. go
  844. /* 32 - 63 */
  845. insert spt_values(low, high, number, type)
  846. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P'
  847. from spt_values
  848. where type = 'P' and number < 32 and number >= 0
  849. go
  850. /* 64 - 127 */
  851. insert spt_values(low, high, number, type)
  852. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P'
  853. from spt_values
  854. where type = 'P' and number < 64 and number >= 0
  855. go
  856. /* 128 - 255 */
  857. insert spt_values(low, high, number, type)
  858. select max(low) + 1 + (number / 8), high, number + 1 + max(number), 'P'
  859. from spt_values
  860. where type = 'P' and number < 128 and number >= 0
  861. go
  862.  
  863. /*
  864. **  Configuration options.
  865. */
  866. insert spt_values(name, number, type)
  867.   values ("CONFIGURATION OPTIONS", -1, 'C')
  868. go
  869. insert spt_values(name, number, type, low, high)
  870.   values ("recovery interval", 101, 'C', 1, 32767)
  871. go
  872. insert spt_values(name, number, type, low, high)
  873.   values ("allow updates", 102, 'C', 0, 1)
  874. go
  875. insert spt_values(name, number, type, low, high)
  876.   values ("user connections", 103, 'C', 5, 25)
  877. go
  878. insert spt_values(name, number, type, low, high)
  879.   values ("memory", 104, 'C', 1000, 32000)
  880. go
  881. insert spt_values(name, number, type, low, high)
  882.   values ("open databases", 105, 'C', 5, 100)
  883. go
  884. insert spt_values(name, number, type, low, high)
  885.   values ("locks", 106, 'C', 5000, 500000)
  886. go
  887. insert spt_values(name, number, type, low, high)
  888.   values ("open objects", 107, 'C', 100, 10000)
  889. go
  890. insert spt_values(name, number, type, low, high)
  891.   values ("procedure cache", 108, 'C', 1, 99)
  892. go
  893. insert spt_values(name, number, type, low, high)
  894.   values ("fill factor", 109, 'C', 0, 100)
  895. go
  896. insert spt_values(name, number, type, low, high)
  897.   values ("time slice", 110, 'C', 50, 1000)
  898. go
  899. insert spt_values(name, number, type, low, high)
  900.   values ("database size", 111, 'C', 2, 10000)
  901. go
  902. insert spt_values(name, number, type, low, high)
  903.  values ("media retention", 112, 'C', 0, 365)
  904. go
  905. insert spt_values(name, number, type, low, high)
  906.   values ("recovery flags", 113, 'C', 0, 1)
  907. go
  908. insert spt_values(name, number, type, low, high)
  909.   values ("serial number", 114, 'C', 1, 999999)
  910. go
  911. insert spt_values(name, number, type, low, high)
  912.   values ("nested triggers", 115, 'C', 0, 1)
  913. go
  914. insert spt_values(name, number, type, low, high)
  915.   values ("devices", 116, 'C', 4, 256)
  916. go
  917. insert spt_values(name, number, type, low, high)
  918.   values ("remote access", 117, 'C', 0, 1)
  919. go
  920. insert spt_values(name, number, type, low, high)
  921.   values ("remote logins", 118, 'C', 0, 4096)
  922. go
  923. insert spt_values(name, number, type, low, high)
  924.   values ("remote sites", 119, 'C', 0, 256)
  925. go
  926. insert spt_values(name, number, type, low, high)
  927.   values ("remote connections", 120, 'C', 0, 4096)
  928. go
  929. insert spt_values(name, number, type, low, high)
  930.   values ("pre-read packets", 121, 'C', 0, 256)
  931. go
  932. insert spt_values(name, number, type, low, high)
  933.   values ("upgrade version", 122, 'C', 0, 2147483647)
  934. go
  935. insert spt_values(name, number, type, low, high)
  936.   values ("default sortorder id", 123, 'C', 0, 255)
  937. go
  938. insert spt_values(name, number, type, low, high)
  939.   values ("default language", 124, 'C', 0, 9999)
  940. go
  941. insert spt_values(name, number, type, low, high)
  942.   values ("language in cache", 125, 'C', 3, 100)
  943. go
  944. insert spt_values(name, number, type, low, high)
  945.   values ("kernel language", 132, 'C', 0, 9999)
  946. go
  947. insert spt_values(name, number, type, low, high)
  948.   values ("infect ticks", 133, 'C', 100, 100000)
  949. go
  950.  
  951. /* Token values */
  952. insert spt_values(name, number, type)
  953.   values ("TOKEN VALUES", -1, 'T')
  954. go
  955. insert spt_values(name, number, type)
  956.   values ("Update", 197, 'T')
  957. go
  958. insert spt_values(name, number, type)
  959.   values ("Delete", 196, 'T')
  960. go
  961. insert spt_values(name, number, type)
  962.   values ("Select", 193, 'T')
  963. go
  964. insert spt_values(name, number, type)
  965.   values ("Insert", 195, 'T')
  966. go
  967. insert spt_values(name, number, type)
  968.   values ("Execute", 224, 'T')
  969. go
  970. insert spt_values(name, number, type)
  971.   values ("Grant", 205, 'T')
  972. go
  973. insert spt_values(name, number, type)
  974.   values ("Revoke", 206, 'T')
  975. go
  976. insert spt_values(name, number, type)
  977.   values ("Create Database", 203, 'T')
  978. go
  979. insert spt_values(name, number, type)
  980.   values ("Create Default", 233, 'T')
  981. go
  982. insert spt_values(name, number, type)
  983.   values ("Create Procedure", 222, 'T')
  984. go
  985. insert spt_values(name, number, type)
  986.   values ("Create Rule", 236, 'T')
  987. go
  988. insert spt_values(name, number, type)
  989.   values ("Create Table", 198, 'T')
  990. go
  991. insert spt_values(name, number, type)
  992.   values ("Create View", 207, 'T')
  993. go
  994. insert spt_values(name, number, type)
  995.   values ("Dump Database", 228, 'T')
  996. go
  997. insert spt_values(name, number, type)
  998.   values ("Dump Transaction", 235, 'T')
  999. go
  1000.  
  1001. /* Values for syslocks.type */
  1002. insert spt_values(name, number, type)
  1003.   values ("LOCK TYPES", -1, 'L')
  1004. go
  1005. insert spt_values(name, number, type)
  1006.   values ("Ex_table", 1, 'L')
  1007. go
  1008. insert spt_values(name, number, type)
  1009.   values ("Sh_table", 2, 'L')
  1010. go
  1011. insert spt_values(name, number, type)
  1012.   values ("Ex_intent", 3, 'L')
  1013. go
  1014. insert spt_values(name, number, type)
  1015.   values ("Sh_intent", 4, 'L')
  1016. go
  1017. insert spt_values(name, number, type)
  1018.   values ("Ex_page", 5, 'L')
  1019. go
  1020. insert spt_values(name, number, type)
  1021.   values ("Sh_page", 6, 'L')
  1022. go
  1023. insert spt_values(name, number, type)
  1024.   values ("Update_page", 7, 'L')
  1025. go
  1026. insert spt_values(name, number, type)
  1027.   values ("Ex_extent", 8, 'L')
  1028. go
  1029. insert spt_values(name, number, type)
  1030.   values ("Sh_extent", 9, 'L')
  1031. go
  1032. insert spt_values(name, number, type)
  1033.   values ("Ex_table-blk", 256+1, 'L')
  1034. go
  1035. insert spt_values(name, number, type)
  1036.   values ("Sh_table-blk", 256+2, 'L')
  1037. go
  1038. insert spt_values(name, number, type)
  1039.   values ("Ex_intent-blk", 256+3, 'L')
  1040. go
  1041. insert spt_values(name, number, type)
  1042.   values ("Sh_intent-blk", 256+4, 'L')
  1043. go
  1044. insert spt_values(name, number, type)
  1045.   values ("Ex_page-blk", 256+5, 'L')
  1046. go
  1047. insert spt_values(name, number, type)
  1048.   values ("Sh_page-blk", 256+6, 'L')
  1049. go
  1050. insert spt_values(name, number, type)
  1051.   values ("Update_page-blk", 256+7, 'L')
  1052. go
  1053. insert spt_values(name, number, type)
  1054.   values ("Ex_extent-blk", 256+8, 'L')
  1055. go
  1056. insert spt_values(name, number, type)
  1057.   values ("Sh_extent-blk", 256+9, 'L')
  1058. go
  1059. insert spt_values(name, number, type)
  1060.   values ("Ex_table-demand", 512+1, 'L')
  1061. go
  1062. insert spt_values(name, number, type)
  1063.   values ("Sh_table-demand", 512+2, 'L')
  1064. go
  1065. insert spt_values(name, number, type)
  1066.   values ("Ex_intent-demand", 512+3, 'L')
  1067. go
  1068. insert spt_values(name, number, type)
  1069.   values ("Sh_intent-demand", 512+4, 'L')
  1070. go
  1071. insert spt_values(name, number, type)
  1072.   values ("Ex_page-demand", 512+5, 'L')
  1073. go
  1074. insert spt_values(name, number, type)
  1075.   values ("Sh_page-demand", 512+6, 'L')
  1076. go
  1077. insert spt_values(name, number, type)
  1078.   values ("Update_page-demand", 512+7, 'L')
  1079. go
  1080. insert spt_values(name, number, type)
  1081.   values ("Ex_extent-demand", 512+8, 'L')
  1082. go
  1083. insert spt_values(name, number, type)
  1084.   values ("Sh_extent-demand", 512+9, 'L')
  1085. go
  1086.  
  1087. /*
  1088. **  These values define the status bits in sysindexes.
  1089. */
  1090. insert spt_values (name, number, type)
  1091.     values ("INDEX TYPES", -1, "I")
  1092. go
  1093. insert spt_values (name, number, type)
  1094.     values ("nonclustered", 0, "I")
  1095. go
  1096. insert spt_values (name, number, type)
  1097.     values ("ignore duplicate keys", 1, "I")
  1098. go
  1099. insert spt_values (name, number, type)
  1100.     values ("unique", 2, "I")
  1101. go
  1102. insert spt_values (name, number, type)
  1103.     values ("ignore duplicate rows", 4, "I")
  1104. go
  1105. insert spt_values (name, number, type)
  1106.     values ("clustered", 16, "I")
  1107. go
  1108. insert spt_values (name, number, type)
  1109.     values ("allow duplicate rows", 64, "I")
  1110. go
  1111.  
  1112. /*
  1113. **  These values define the object type.  The number made from the low
  1114. **  3 bits in sysobjects.sysstats indicate the type of object.
  1115. */
  1116. insert spt_values (name, number, type)
  1117.     values ("OBJECT TYPES", -1, "O")
  1118. go
  1119. insert spt_values (name, number, type)
  1120.     values ("trigger", 0, "O")
  1121. go
  1122. insert spt_values (name, number, type)
  1123.     values ("system table", 1, "O")
  1124. go
  1125. insert spt_values (name, number, type)
  1126.     values ("view", 2, "O")
  1127. go
  1128. insert spt_values (name, number, type)
  1129.     values ("user table", 3, "O")
  1130. go
  1131. insert spt_values (name, number, type)
  1132.     values ("stored procedure",4, "O")
  1133. go
  1134. /* no number 5 */
  1135. insert spt_values (name, number, type)
  1136.     values ("default", 6, "O")
  1137. go
  1138. insert spt_values (name, number, type)
  1139.     values ("rule", 7, "O")
  1140. go
  1141.  
  1142. /*
  1143. **  These values translate the object type's userstat bits.  If the high
  1144. **  bit is set for a sproc, then it's a report.
  1145. */
  1146. insert spt_values (name, number, type)
  1147.     values ("REPORT TYPES", -1, "R")
  1148. go
  1149. insert spt_values (name, number, type)
  1150.     values ("", 0, "R")
  1151. go
  1152. insert spt_values (name, number, type)
  1153.     values (" (rpt)", -32768, "R")
  1154. go
  1155.  
  1156. /*
  1157. **  Database status bits.
  1158. **  NB -- If you add a bit here make sure you add the value to the value
  1159. **    of the ALL SETTABLE option.
  1160. */
  1161. insert spt_values (name, number, type)
  1162.     values ("DATABASE STATUS", -1, "D")
  1163. go
  1164. insert spt_values (name, number, type)
  1165.     values ("select into/bulkcopy", 4, "D")
  1166. go
  1167. insert spt_values (name, number, type)
  1168.     values ("no chkpt on recovery", 16, "D")
  1169. go
  1170. insert spt_values (name, number, type)
  1171.     values ("don't recover", 32, "D")
  1172. go
  1173. insert spt_values (name, number, type)
  1174.     values ("not recovered", 256, "D")
  1175. go
  1176. /*
  1177. **  NOT IMPLEMENTED BY DATASERVER
  1178. **insert spt_values (name, number, type)
  1179. **    values ("load only", 512, "D")
  1180. **go
  1181. */
  1182. insert spt_values (name, number, type)
  1183.     values ("read only", 1024, "D")
  1184. go
  1185. insert spt_values (name, number, type)
  1186.     values ("dbo use only", 2048, "D")
  1187. go
  1188. insert spt_values (name, number, type)
  1189.     values ("single user", 4096, "D")
  1190. go
  1191. insert spt_values (name, number, type)
  1192.     values ("dbname has changed", 16384, "D")
  1193. go
  1194. insert spt_values (name, number, type)
  1195.     values ("trunc. log on chkpt.", 8, "D")
  1196. go
  1197. insert spt_values (name, number, type)
  1198.     values ("ALL SETTABLE OPTIONS", 7196, "D")
  1199. go
  1200.  
  1201. /*
  1202. **  Yes or no.
  1203. */
  1204. insert spt_values (name, number, type)
  1205.     values ("YES OR NO", -1, "B")
  1206. go
  1207. insert spt_values (name, number, type)
  1208.     values ("no", 0, "B")
  1209. go
  1210. insert spt_values (name, number, type)
  1211.     values ("yes", 1, "B")
  1212. go
  1213.  
  1214. /*
  1215. **  Status bits in sysservers
  1216. */
  1217. insert spt_values (name, number, type)
  1218.     values ("SYSSERVERS TYPES", -1, "A")
  1219. go
  1220. insert spt_values (name, number, type)
  1221.     values ("timeouts", 0, "A")
  1222. go
  1223. insert spt_values (name, number, type)
  1224.     values ("no timeouts", 1, "A")
  1225. go
  1226.  
  1227. /*
  1228. **  Status bits in sysremotelogins
  1229. */
  1230. insert spt_values (name, number, type)
  1231.     values ("SYSREMOTELOGINS TYPES", -1, "F")
  1232. go
  1233. insert spt_values (name, number, type)
  1234.     values ("", 0, "F")
  1235. go
  1236. insert spt_values (name, number, type)
  1237.     values ("trusted", 1, "F")
  1238. go
  1239.  
  1240. /*
  1241. **  Translate syskeys.type to english.
  1242. */
  1243. insert spt_values (name, number, type)
  1244.     values ("SYSKEYS TYPES", -1, "K")
  1245. go
  1246. insert spt_values (name, number, type)
  1247.     values ("primary", 1, "K")
  1248. go
  1249. insert spt_values (name, number, type)
  1250.     values ("foreign", 2, "K")
  1251. go
  1252. insert spt_values (name, number, type)
  1253.     values ("common", 3, "K")
  1254. go
  1255.  
  1256. /*
  1257. **  Translate sysdevices.status to english.
  1258. */
  1259. insert spt_values (name, number, type)
  1260.     values ("SYSDEVICES STATUS", -1, "V")
  1261. go
  1262. insert spt_values (name, number, type)
  1263.     values ("default disk", 1, "V")
  1264. go
  1265. insert spt_values (name, number, type)
  1266.     values ("physical disk", 2, "V")
  1267. go
  1268. insert spt_values (name, number, type)
  1269.     values ("logical disk", 4, "V")
  1270. go
  1271. insert spt_values (name, number, type)
  1272.     values ("skip header", 8, "V")
  1273. go
  1274. insert spt_values (name, number, type)
  1275.     values ("dump device", 16, "V")
  1276. go
  1277. insert spt_values (name, number, type)
  1278.     values ("serial writes", 32, "V")
  1279. go
  1280. insert spt_values (name, number, type)
  1281.     values ("device mirrored", 64, "V")
  1282. go
  1283. insert spt_values (name, number, type)
  1284.     values ("reads mirrored", 128, "V")
  1285. go
  1286. insert spt_values (name, number, type)
  1287.     values ("half-mirror only", 256, "V")
  1288. go
  1289. insert spt_values (name, number, type)
  1290.     values ("mirror enabled", 512, "V")
  1291. go
  1292.  
  1293. /*
  1294. **  Translate @@options bit to english
  1295. */
  1296. insert spt_values (name, number, type)
  1297.     values ("OPTIONS BITS", -1, "N")
  1298. go
  1299. insert spt_values (name, number, type)
  1300.     values ("showplan", 4, "N")
  1301. go
  1302. insert spt_values (name, number, type)
  1303.     values ("noexec", 8, "N")
  1304. go
  1305. insert spt_values (name, number, type)
  1306.     values ("arithignore", 16, "N")
  1307. go
  1308. insert spt_values (name, number, type)
  1309.     values ("arithabort", 32, "N")
  1310. go
  1311. insert spt_values (name, number, type)
  1312.     values ("background", 64, "N")
  1313. go
  1314. insert spt_values (name, number, type)
  1315.     values ("control", 1024, "N")
  1316. go
  1317. insert spt_values (name, number, type)
  1318.     values ("offsets", 2048, "N")
  1319. go
  1320. insert spt_values (name, number, type)
  1321.     values ("statistics io and time", 4096, "N")
  1322. go
  1323. insert spt_values (name, number, type)
  1324.     values ("parseonly", 8192, "N")
  1325. go
  1326. insert spt_values (name, number, type)
  1327.     values ("procid", 32768, "N")
  1328. go
  1329. insert spt_values (name, number, type)
  1330.     values ("rowcount", 131072, "N")
  1331. go
  1332. insert spt_values (name, number, type)
  1333.     values ("nocount", 524288, "N")
  1334. go
  1335.  
  1336. /*
  1337. **  Set the DataServer machine type
  1338. **  spt_values.low is the number of bytes in a DataServer page for the
  1339. **  particular DataServer machine.
  1340. */
  1341. insert spt_values (name, number, type, low)
  1342.     values ("SQLSERVER HOST TYPE", -1, "E", 0)
  1343. go
  1344.  
  1345. /*
  1346. **  List the physical types that are compatible.
  1347. */
  1348. insert spt_values (name, number, type)
  1349.     values ("COMPATIBLE TYPES", -1, "J")
  1350. go
  1351. insert spt_values (name, number, low, type)
  1352.     values ("binary", 1, 45, "J")
  1353. go
  1354. insert spt_values (name, number, low, type)
  1355.     values ("varbinary", 1, 37, "J")
  1356. go
  1357. insert spt_values (name, number, low, type)
  1358.     values ("bit", 2, 50, "J")
  1359. go
  1360. insert spt_values (name, number, low, type)
  1361.     values ("char", 3, 47, "J")
  1362. go
  1363. insert spt_values (name, number, low, type)
  1364.     values ("varchar", 3, 39, "J")
  1365. go
  1366. insert spt_values (name, number, low, type)
  1367.     values ("datetime", 4, 61, "J")
  1368. go
  1369. insert spt_values (name, number, low, type)
  1370.     values ("datetimn", 4, 111, "J")
  1371. go
  1372. insert spt_values (name, number, low, type)
  1373.     values ("smalldatetime", 4, 58, "J")
  1374. go
  1375. insert spt_values (name, number, low, type)
  1376.     values ("float", 5, 62, "J")
  1377. go
  1378. insert spt_values (name, number, low, type)
  1379.     values ("floatn", 5, 109, "J")
  1380. go
  1381. insert spt_values (name, number, low, type)
  1382.     values ("real", 5, 59, "J")
  1383. go
  1384. insert spt_values (name, number, low, type)
  1385.     values ("int", 6, 56, "J")
  1386. go
  1387. insert spt_values (name, number, low, type)
  1388.     values ("intn", 6, 38, "J")
  1389. go
  1390. insert spt_values (name, number, low, type)
  1391.     values ("smallint", 6, 52, "J")
  1392. go
  1393. insert spt_values (name, number, low, type)
  1394.     values ("tinyint", 6, 48, "J")
  1395. go
  1396. insert spt_values (name, number, low, type)
  1397.     values ("money", 7, 60, "J")
  1398. go
  1399. insert spt_values (name, number, low, type)
  1400.     values ("moneyn", 7, 110, "J")
  1401. go
  1402. insert spt_values (name, number, low, type)
  1403.     values ("smallmoney", 7, 122, "J")
  1404. go
  1405.  
  1406. /*
  1407. **  Add some text for sysusages.segmap.
  1408. */
  1409. insert into spt_values (name, type, number)
  1410.     values ('SYSUSAGES SEGMAP', 'S', -1)
  1411. insert into spt_values (name, type, number)
  1412.     values ('data only', 'S', 0)
  1413. insert into spt_values (name, type, number)
  1414.     values ('data only', 'S', 1)
  1415. insert into spt_values (name, type, number)
  1416.     values ('data only', 'S', 2)
  1417. insert into spt_values (name, type, number)
  1418.     values ('data only', 'S', 3)
  1419. insert into spt_values (name, type, number)
  1420.     values ('log only', 'S', 4)
  1421. insert into spt_values (name, type, number)
  1422.     values ('data only', 'S', 5)
  1423. insert into spt_values (name, type, number)
  1424.     values ('data only', 'S', 6)
  1425. insert into spt_values (name, type, number)
  1426.     values ('data and log', 'S', 7)
  1427. go
  1428.  
  1429. /*
  1430. **  Create and initialize the table used by the sp_monitor procedure.
  1431. */
  1432. create table spt_monitor
  1433. (
  1434.     lastrun     datetime,
  1435.     cpu_busy     int,
  1436.     io_busy     int,
  1437.     idle         int,
  1438.     pack_received     int,
  1439.     pack_sent     int,
  1440.     connections     int,
  1441.     pack_errors    int,
  1442.     total_read     int,
  1443.     total_write     int,
  1444.     total_errors     int
  1445. )
  1446. go
  1447.  
  1448. grant select on spt_monitor to public
  1449. go
  1450.  
  1451. insert into spt_monitor
  1452.     select
  1453.     lastrun = getdate(),
  1454.     cpu_busy = @@cpu_busy,
  1455.     io_busy = @@io_busy,
  1456.     idle = @@idle,
  1457.     pack_received = @@pack_received,
  1458.     pack_sent = @@pack_sent,
  1459.     connections = @@connections,
  1460.     pack_errors = @@packet_errors,
  1461.     total_read = @@total_read,
  1462.     total_write = @@total_write,
  1463.     total_errors = @@total_errors
  1464. go
  1465.  
  1466. set nocount off
  1467. go
  1468. grant select on spt_values to public
  1469. go
  1470. go
  1471. dump transaction master with truncate_only
  1472. go
  1473. /* server_os/2    27.1    3/5/90 */
  1474. /*
  1475. **  This file contains the necessary modifications to be made for
  1476. **  various DataServer host machines.
  1477. **  It sets the DataServer host environment to be for os/2.
  1478. **  spt_values.low is the number of bytes in a DataServer page.
  1479. */
  1480. use master
  1481. go
  1482.  
  1483. delete spt_values
  1484.     where type = "E" and number > 0
  1485. go
  1486. /* data page size for os/2 */
  1487. insert into spt_values (name, number, type, low)
  1488.     values ("OS/2", 1, "E", 2048)
  1489.  
  1490. /* Value to set and clear the high bit for int datatypes for os/2.
  1491. ** Would like to enter -2,147,483,648 to avoid byte order issues, but
  1492. ** the server won't take it, even in exponential notation.
  1493. */
  1494. insert into spt_values (name, number, type, low)
  1495.     values ("int high bit", 2, "E", 0x80000000)
  1496.  
  1497. /* Value which gives the byte position of the high byte for int datatypes for
  1498. ** os/2.  This value was changed from 4 (the usual Intel 80x86 order) to 1
  1499. ** when binary convert routines were changed to reverse the byte order.  So
  1500. ** this value is accurate ONLY when ints are converted to binary datatype.
  1501. */
  1502. insert into spt_values (name, number, type, low)
  1503.     values ("int4 high byte", 3, "E", 1)
  1504. go
  1505.  
  1506. /* command to fix up freeoff on page 24 in all databases */
  1507. dbcc dbrepair (master, fixsysindex)
  1508.  
  1509. go
  1510. /* validname    27.1    3/5/90 */
  1511. create procedure sp_validname
  1512. @name    varchar(30)
  1513. as
  1514.  
  1515. /*
  1516. **  Check to see that the new name is a legal identifier.
  1517. */
  1518.  
  1519. declare @current    int
  1520. declare @last        int
  1521. declare @curchar    tinyint
  1522. declare @underscore    tinyint
  1523. declare @poundsign    tinyint
  1524. declare @dollarsign    tinyint
  1525. declare @big_A        tinyint
  1526. declare @small_a    tinyint
  1527. declare @big_Z        tinyint
  1528. declare @small_z    tinyint
  1529. declare @digit_0    tinyint
  1530. declare @digit_9    tinyint
  1531.  
  1532. declare @csid            tinyint
  1533. declare @alpha_2_start        tinyint
  1534. declare @alpha_2_end        tinyint
  1535. declare @alpha_3_start        tinyint
  1536. declare @alpha_3_end        tinyint
  1537. declare @alpha_4_start        tinyint
  1538. declare @alpha_4_end        tinyint
  1539. declare @alpha_5_start        tinyint
  1540. declare @alpha_5_end        tinyint
  1541. declare @alpha_6_start        tinyint
  1542. declare @alpha_6_end        tinyint
  1543. declare @alpha_7_start        tinyint
  1544. declare @alpha_7_end        tinyint
  1545. declare @alpha_s_1        tinyint
  1546. declare @alpha_s_2        tinyint
  1547. declare @alpha_s_3        tinyint
  1548.  
  1549. /*
  1550. **  Ensure that the name specified is non-null.
  1551. */
  1552. if (@name is null)
  1553. begin
  1554.     return (1)
  1555. end
  1556.  
  1557.  
  1558. /*
  1559. **  Set up the codepoints that we will need.
  1560. */
  1561. select @underscore = 0x5F,
  1562.         @poundsign = 0x23,
  1563.        @dollarsign = 0x24,
  1564.             @big_A = 0x41,
  1565.           @small_a = 0x61,
  1566.             @big_Z = 0x5A,
  1567.           @small_z = 0x7A,
  1568.           @digit_0 = 0x30,
  1569.           @digit_9 = 0x39
  1570.  
  1571. /* Get the Character Set id (csid) */
  1572. select @csid = csid from master..syscharsets
  1573.     where id = (select value from master..sysconfigures
  1574.         where config = 123)
  1575.  
  1576.     if @csid = 1
  1577.         /* Set up the alphabetic codepoint ranges for iso_1. */
  1578.         select @alpha_2_start = 0xC0, @alpha_2_end = 0xD6,
  1579.            @alpha_3_start = 0xD8, @alpha_3_end = 0xF6,
  1580.                @alpha_4_start = 0xF8, @alpha_4_end = 0xFF,
  1581.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1582.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1583.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1584.            @alpha_s_1 = 0x41, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41
  1585.  
  1586.     if @csid = 2
  1587.         /* Set up the alphabetic codepoint ranges for cp850. */
  1588.         select @alpha_2_start = 0x80, @alpha_2_end = 0x9D,
  1589.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1590.                @alpha_4_start = 0xB5, @alpha_4_end = 0xB7,
  1591.                @alpha_5_start = 0xC6, @alpha_5_end = 0xC7,
  1592.                @alpha_6_start = 0xD0, @alpha_6_end = 0xD8,
  1593.                @alpha_7_start = 0xE0, @alpha_7_end = 0xED,
  1594.            @alpha_s_1 = 0xDE, @alpha_s_2 = 0xF4, @alpha_s_3 = 0xF5
  1595.  
  1596.     if @csid = 3
  1597.         /* Set up the alphabetic codepoint ranges for cp437. */
  1598.         select @alpha_2_start = 0x80, @alpha_2_end = 0x9A,
  1599.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1600.                @alpha_4_start = 0xE0, @alpha_4_end = 0xEE,
  1601.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1602.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1603.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1604.            @alpha_s_1 = 0x9E, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41
  1605.  
  1606. /*
  1607. **  Check the 1st character.
  1608. **  It can be an ASCII letter, #, _, or any character from 0x80-0xFF.
  1609. */
  1610. select @curchar = convert(binary(1), substring(@name, 1, 1))
  1611.  
  1612. if (not (   (@curchar >= @big_A    and  @curchar <= @big_Z)
  1613.      or (@curchar >= @small_a  and  @curchar <= @small_z)
  1614.      or (@curchar between @alpha_2_start and @alpha_2_end)
  1615.      or (@curchar between @alpha_3_start and @alpha_3_end)
  1616.      or (@curchar between @alpha_4_start and @alpha_4_end)
  1617.      or (@curchar between @alpha_5_start and @alpha_5_end)
  1618.      or (@curchar between @alpha_6_start and @alpha_6_end)
  1619.      or (@curchar between @alpha_7_start and @alpha_7_end)
  1620.      or @curchar = @alpha_s_1
  1621.      or @curchar = @alpha_s_2
  1622.      or @curchar = @alpha_s_3
  1623.      or @curchar = @poundsign
  1624.      or @curchar = @underscore
  1625.     )
  1626.    )
  1627. begin
  1628.     return (1)
  1629. end
  1630.  
  1631. /*
  1632. **  Figure out how long the string is.
  1633. **  N.B. - replace this once we have strlen()
  1634. */
  1635. if (charindex("!", @name) != 0)
  1636. begin
  1637.     return (1)
  1638. end
  1639. select @current = 2,
  1640.        @last = charindex("!", @name + "!") - 1
  1641.  
  1642. /*
  1643. **  Now check the rest of the characters.
  1644. **  They can be ASCII letters, #, _, $, or any character from 0x80-0xFF.
  1645. */
  1646. while @current <= @last
  1647. begin
  1648.     select @curchar = convert(binary(1), substring(@name, @current, 1))
  1649.     if (not (   (@curchar >= @big_A    and  @curchar <= @big_Z)
  1650.          or (@curchar >= @small_a  and  @curchar <= @small_z)
  1651.          or (@curchar >= @digit_0  and  @curchar <= @digit_9)
  1652.          or (@curchar between @alpha_2_start and @alpha_2_end)
  1653.          or (@curchar between @alpha_3_start and @alpha_3_end)
  1654.          or (@curchar between @alpha_4_start and @alpha_4_end)
  1655.          or (@curchar between @alpha_5_start and @alpha_5_end)
  1656.          or (@curchar between @alpha_6_start and @alpha_6_end)
  1657.          or (@curchar between @alpha_7_start and @alpha_7_end)
  1658.          or @curchar = @alpha_s_1
  1659.          or @curchar = @alpha_s_2
  1660.          or @curchar = @alpha_s_3
  1661.          or @curchar = @poundsign
  1662.          or @curchar = @dollarsign
  1663.          or @curchar = @underscore
  1664.         )
  1665.        )
  1666.     begin
  1667.         return (1)
  1668.     end
  1669.  
  1670.     select @current = @current + 1
  1671. end
  1672.  
  1673. return (0)
  1674. go
  1675. grant execute on sp_validname to public
  1676. go
  1677. dump transaction master with truncate_only
  1678. go
  1679. /* validlang 27.1 3/5/90 */
  1680. create procedure sp_validlang
  1681. @name    varchar(30)
  1682. as
  1683.  
  1684. /* Check to see if this language is in Syslanguages. */
  1685. if exists (select *
  1686.         from master.dbo.syslanguages
  1687.         where name = @name)
  1688. begin
  1689.     return 0
  1690. end
  1691. return 1
  1692. go
  1693. grant execute on sp_validlang to public
  1694. go
  1695. /* validaltlang 27.1 3/5/90 */
  1696. create procedure sp_validaltlang
  1697. @name    varchar(30)
  1698. as
  1699.  
  1700. /* Check to see if this alias is in Syslanguages. */
  1701. if exists (select *
  1702.         from master.dbo.syslanguages
  1703.         where alias = @name)
  1704. begin
  1705.     return 0
  1706. end
  1707. return 1
  1708. go
  1709. grant execute on sp_validaltlang to public
  1710. go
  1711. dump transaction master with truncate_only
  1712. go
  1713. /* chklangparam 27.2 5/24/90 */
  1714. /*
  1715. ** sp_checklangparam is called by sp_addlanguage to check the lists of
  1716. ** full month names, short month names, and day names.  It returns a status
  1717. ** indicating whether any errors were found:
  1718. **    0 - name list is valid
  1719. **    1 - spaces were found, which are not allowed
  1720. **    2 - not enough names in the list (must be exactly @numnames of them)
  1721. **    3 - too many names in the list (must be exactly @numnames of them)
  1722. **    4 - some name(s) are too long
  1723. **    5 - some name(s) have non-aphabetic characters
  1724. */
  1725. create procedure sp_chklangparam
  1726. @namelist varchar(251),        /* month or day name list */
  1727. @msgparam varchar(30),        /* name of list, for error messages */
  1728. @numnames int,            /* correct number of names in the list */
  1729. @maxnamelen int            /* maximum length of any name in list */
  1730. as
  1731.  
  1732.     declare @totallen        int
  1733.     declare @mthlen            int
  1734.     declare @mthname        varchar(251)
  1735.     declare @msg            varchar(255)
  1736.     declare @csid            tinyint
  1737.     declare @csname            varchar(30)
  1738.     declare @alpha_0_start        tinyint
  1739.     declare @alpha_0_end        tinyint
  1740.     declare @alpha_1_start        tinyint
  1741.     declare @alpha_1_end        tinyint
  1742.     declare @alpha_2_start        tinyint
  1743.     declare @alpha_2_end        tinyint
  1744.     declare @alpha_3_start        tinyint
  1745.     declare @alpha_3_end        tinyint
  1746.     declare @alpha_4_start        tinyint
  1747.     declare @alpha_4_end        tinyint
  1748.     declare @alpha_5_start        tinyint
  1749.     declare @alpha_5_end        tinyint
  1750.     declare @alpha_6_start        tinyint
  1751.     declare @alpha_6_end        tinyint
  1752.     declare @alpha_7_start        tinyint
  1753.     declare @alpha_7_end        tinyint
  1754.     declare @alpha_s_1        tinyint
  1755.     declare @alpha_s_2        tinyint
  1756.     declare @alpha_s_3        tinyint
  1757.     declare @alpha_s_4        tinyint
  1758.     declare @curchar        tinyint
  1759.     declare @next            int
  1760.  
  1761.     /* Get the Character Set id (csid) */
  1762.     select @csid = csid from master..syscharsets
  1763.         where id = (select value from master..sysconfigures
  1764.             where config = 123)
  1765.  
  1766.     /* Get the Character Set Name (csname) */
  1767.     select @csname = name from master..syscharsets
  1768.         where id = @csid and type < 2000
  1769.  
  1770.     /* Make sure the list has no blanks. */
  1771.     if ( charindex( " ", @namelist ) != 0 )
  1772.     begin
  1773.         select @msg = "List of " + @msgparam
  1774.             + " names contains spaces, which are not allowed."
  1775.         print @msg
  1776.         return 1
  1777.     end
  1778.  
  1779.     if @csid = 1
  1780.         /* Set up the alphabetic codepoint ranges for iso_1. */
  1781.         select @alpha_0_start = 0x41, @alpha_0_end = 0x5A,
  1782.            @alpha_1_start = 0x61, @alpha_1_end = 0x7A,
  1783.            @alpha_2_start = 0xC0, @alpha_2_end = 0xD6,
  1784.            @alpha_3_start = 0xD8, @alpha_3_end = 0xF6,
  1785.                @alpha_4_start = 0xF8, @alpha_4_end = 0xFF,
  1786.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1787.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1788.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1789.            @alpha_s_1 = 0x41, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41,
  1790.            @alpha_s_4 = 0x2D
  1791.  
  1792.     if @csid = 2
  1793.         /* Set up the alphabetic codepoint ranges for cp850. */
  1794.         select @alpha_0_start = 0x41, @alpha_0_end = 0x5A,
  1795.            @alpha_1_start = 0x61, @alpha_1_end = 0x7A,
  1796.            @alpha_2_start = 0x80, @alpha_2_end = 0x9D,
  1797.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1798.                @alpha_4_start = 0xB5, @alpha_4_end = 0xB7,
  1799.                @alpha_5_start = 0xC6, @alpha_5_end = 0xC7,
  1800.                @alpha_6_start = 0xD0, @alpha_6_end = 0xD8,
  1801.                @alpha_7_start = 0xE0, @alpha_7_end = 0xED,
  1802.            @alpha_s_1 = 0xDE, @alpha_s_2 = 0xF4, @alpha_s_3 = 0xF5,
  1803.            @alpha_s_4 = 0x2D
  1804.  
  1805.     if @csid = 3
  1806.         /* Set up the alphabetic codepoint ranges for cp437. */
  1807.         select @alpha_0_start = 0x41, @alpha_0_end = 0x5A,
  1808.            @alpha_1_start = 0x61, @alpha_1_end = 0x7A,
  1809.            @alpha_2_start = 0x80, @alpha_2_end = 0x9A,
  1810.            @alpha_3_start = 0xA0, @alpha_3_end = 0xA5,
  1811.                @alpha_4_start = 0xE0, @alpha_4_end = 0xEE,
  1812.                @alpha_5_start = 0x41, @alpha_5_end = 0x5A,
  1813.                @alpha_6_start = 0x61, @alpha_6_end = 0x7A,
  1814.                @alpha_7_start = 0x61, @alpha_7_end = 0x7A,
  1815.            @alpha_s_1 = 0x9E, @alpha_s_2 = 0x41, @alpha_s_3 = 0x41,
  1816.            @alpha_s_4 = 0x2D
  1817.  
  1818.     select @totallen = datalength( @namelist )
  1819.     while ( @totallen > 0 )
  1820.     begin
  1821.         /* Move to the next name in the list. */
  1822.         select @namelist = right( @namelist, @totallen )
  1823.  
  1824.         /* If this is not the last name in the list then there must
  1825.         ** be another comma.
  1826.         */
  1827.         if ( @numnames > 1 )
  1828.         begin
  1829.             select @mthlen = charindex( ",", @namelist ) - 1
  1830.             if ( @mthlen < 0 )
  1831.             begin
  1832.                 select @msg = "List of " + @msgparam
  1833.                     + " names has too few names."
  1834.                 print @msg
  1835.                 return 2
  1836.             end
  1837.         end
  1838.  
  1839.         /* This is the last name in the list, so there cannot be
  1840.         ** another comma.  This name is all remaining characters
  1841.         ** in the list.
  1842.         */
  1843.         else
  1844.         begin
  1845.             if ( charindex( ",", @namelist ) != 0 )
  1846.             begin
  1847.                 select @msg = "List of " + @msgparam
  1848.                     + " names has too many names."
  1849.                 print @msg
  1850.                 return 3
  1851.             end
  1852.             select @mthlen = @totallen
  1853.         end
  1854.  
  1855.         /* Check the name length. */
  1856.         if ( @mthlen > @maxnamelen )
  1857.         begin
  1858.             select @msg = "List of " + @msgparam
  1859.                 + " names has name(s) which are too long."
  1860.             print @msg
  1861.             return 4
  1862.         end
  1863.  
  1864.         /*
  1865.         ** Check to see that the names contain only true
  1866.         ** ALPHA-betic characters; not even DIGITs are allowed.
  1867.         */
  1868.         select @next = 1
  1869.         while ( @next <= @mthlen )
  1870.         begin
  1871.             select @curchar = convert(binary(1),
  1872.                         substring(@namelist, @next, 1))
  1873.             if (not
  1874.             (
  1875.                (@curchar between @alpha_0_start and @alpha_0_end)
  1876.             or (@curchar between @alpha_1_start and @alpha_1_end)
  1877.             or (@curchar between @alpha_2_start and @alpha_2_end)
  1878.             or (@curchar between @alpha_3_start and @alpha_3_end)
  1879.             or (@curchar between @alpha_4_start and @alpha_4_end)
  1880.             or (@curchar between @alpha_5_start and @alpha_5_end)
  1881.             or (@curchar between @alpha_6_start and @alpha_6_end)
  1882.             or (@curchar between @alpha_7_start and @alpha_7_end)
  1883.             or @curchar = @alpha_s_1
  1884.             or @curchar = @alpha_s_2
  1885.             or @curchar = @alpha_s_3
  1886.             or @curchar = @alpha_s_4
  1887.             )
  1888.             )
  1889.             begin
  1890.             select @msg = 'List of ' + @msgparam
  1891.                 + ' names contains name(s) which have "'
  1892.                 + @csname + '" non-alphabetic characters.'
  1893.             print @msg
  1894.             return 5
  1895.             end
  1896.  
  1897.             select @next = @next + 1
  1898.         end
  1899.  
  1900.         /* Move to the next name in the list. */
  1901.         select @numnames = @numnames - 1
  1902.         select @totallen = @totallen - @mthlen - 1
  1903.     end
  1904.  
  1905.     /* No problems were found with the name list. */
  1906.     return 0
  1907. go
  1908. /* namecrack 27.1 3/5/90 */
  1909. create procedure sp_namecrack
  1910. @name    varchar(123),
  1911. @site    varchar(30) = null output,
  1912. @db    varchar(30) = null output,
  1913. @owner    varchar(30) = null output,
  1914. @object    varchar(30) = null output
  1915. as
  1916.  
  1917. select @site = null, @db = null, @owner = null, @object = null
  1918.  
  1919. /*
  1920. **  Is there a site name?
  1921. */
  1922. if @name like "%.%.%.%"
  1923. begin
  1924.     select @site = substring(@name, 1, charindex(".", @name) - 1)
  1925.     select @name = substring(@name, charindex(".", @name) + 1, 125)
  1926. end
  1927.  
  1928. /*
  1929. **  Is there a database name?
  1930. */
  1931. if @name like "%.%.%"
  1932. begin
  1933.     select @db = substring(@name, 1, charindex(".", @name) - 1)
  1934.     select @name = substring(@name, charindex(".", @name) + 1, 125)
  1935. end
  1936.  
  1937. /*
  1938. **  Is there a owner?
  1939. */
  1940. if @name like "%.%"
  1941. begin
  1942.     select @owner = substring(@name, 1, charindex(".", @name) - 1)
  1943.     select @name = substring(@name, charindex(".", @name) + 1, 125)
  1944. end
  1945.  
  1946. select @object = @name
  1947.  
  1948. return (0)
  1949. go
  1950. grant execute on sp_namecrack to public
  1951. go
  1952. dump transaction master with truncate_only
  1953. go
  1954. /* addalias 27.1 3/5/90 */
  1955. create procedure sp_addalias
  1956. @loginame varchar(30),        /* the name of the pretender */
  1957. @name_in_db varchar(30)        /* who the pretender wants to pretend to be */
  1958. as
  1959.  
  1960. declare @suid int        /* the suid of the pretender */
  1961. declare @asuid int        /* the suid of the person to impersonate */
  1962. declare    @msg    varchar(250)
  1963.  
  1964. /*
  1965. **  You must be SA or the dbo to execute this sproc.
  1966. */
  1967. if suser_id() != 1 and user_id() != 1
  1968. begin
  1969.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  1970.     return (1)
  1971. end
  1972.  
  1973. /*
  1974. **  Make sure that the pretender has an account.
  1975. */
  1976. select @suid = suid
  1977.     from master.dbo.syslogins
  1978. where name = @loginame
  1979.  
  1980. if @suid is NULL
  1981. begin
  1982.     print "No login exists with the specified login name."
  1983.     return (1)
  1984. end
  1985.  
  1986. /*
  1987. **  Get the suid of the person we want to pretend to be.
  1988. **  All user ids are <= 16383 and group ids are > 16383.
  1989. */
  1990. select @asuid = suid
  1991.     from sysusers
  1992. where name = @name_in_db
  1993.     and uid <= 16383
  1994.  
  1995. /*
  1996. **  Does the user to be impersonated exist in the current database?
  1997. */
  1998. if @asuid is NULL
  1999. begin
  2000.     print "No user with the specified name exists in the current database."
  2001.     return (1)
  2002. end
  2003.  
  2004. /*
  2005. **  Does the login to do the impersonating already a user in the current db?
  2006. */
  2007. if exists (select *
  2008.         from sysusers
  2009.             where suid = @suid)
  2010. begin
  2011.     select @msg = "'" + name
  2012.         from sysusers
  2013.             where suid = @suid
  2014.     select @msg = @msg + "' is already a user in the current database."
  2015.     print @msg
  2016.     return (1)
  2017. end
  2018.  
  2019. /*
  2020. **  Is the person already aliased to a user?
  2021. */
  2022. if exists (select *
  2023.         from sysalternates
  2024.     where suid = @suid)
  2025. begin
  2026.     print "The specified user name is already aliased."
  2027.     return (1)
  2028. end
  2029.  
  2030. /*
  2031. **  Add the alias.
  2032. */
  2033. insert into sysalternates (suid, altsuid)
  2034.     values (@suid, @asuid)
  2035.  
  2036. print "Alias user added."
  2037.  
  2038. return (0)
  2039. go
  2040. grant execute on sp_addalias to public
  2041. go
  2042. /* addgroup 27.1 3/5/90 */
  2043. create procedure sp_addgroup
  2044. @grpname varchar(30)            /* new group name */
  2045. as
  2046.  
  2047. declare @gid int            /* group id */
  2048. declare @msg varchar(250)
  2049.  
  2050. /*
  2051. **  You must be SA or the dbo to execute this sproc.
  2052. */
  2053. if suser_id() != 1 and user_id() != 1
  2054. begin
  2055.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  2056.     return (1)
  2057. end
  2058.  
  2059. /*
  2060. **  Check to see that the @grpname is valid.
  2061. */
  2062. declare @returncode    int
  2063. execute @returncode = sp_validname @grpname
  2064. if @returncode != 0
  2065. begin
  2066.     select @msg = "'" + @grpname + "' is not a valid name."
  2067.     print @msg
  2068.     return @returncode
  2069. end
  2070.  
  2071. /*
  2072. **  See if the new group name is already being used as a user or group name.
  2073. */
  2074. select @gid = uid
  2075.     from sysusers
  2076. where name = @grpname
  2077.  
  2078. /*
  2079. **  User ids are >0 and  <= 16383 and group ids are > 16383.
  2080. */
  2081. if @gid is not null
  2082. begin
  2083.     if @gid > 0 and @gid <= 16383
  2084.         print "A user with the specified group name already exists."
  2085.     else
  2086.         print "A group with the specified name already exists."
  2087.     return (1)
  2088. end
  2089.  
  2090. /*
  2091. **  Now get the group id for the new group.  It is the current maximum group
  2092. **  number + 1.  If this is the first group use the lowest possible group id
  2093. **  -- 16384.
  2094. */
  2095. select @gid = max(uid)+1
  2096.     from sysusers
  2097.  
  2098. /*
  2099. **  This is the first group.
  2100. */
  2101. if @gid < 16384 or @gid = NULL
  2102.     select @gid = 16384
  2103.  
  2104. /*
  2105. **  Create the group.
  2106. */
  2107. insert into sysusers (uid, suid, gid, name, environ)
  2108. values (@gid, -@gid, @gid, @grpname, "")
  2109.  
  2110. print "New group added."
  2111.  
  2112. return (0)
  2113. go
  2114. grant execute on sp_addgroup to public
  2115. go
  2116. dump transaction master with truncate_only
  2117. go
  2118. /* addlogin 27.1 3/5/90 */
  2119. create procedure sp_addlogin
  2120. @loginame varchar(30),            /* login name of the new user */
  2121. @passwd varchar(30) = NULL,        /* password of the new user */
  2122. @defdb varchar(30) = "master",        /* default db for the new user */
  2123. @deflanguage varchar(30) = NULL        /* default language for the new user */
  2124. as
  2125.  
  2126. declare @msg varchar(250)
  2127.  
  2128. /*
  2129. **  If we're in a transaction, disallow this since it might make recovery
  2130. **  impossible.
  2131. */
  2132. if @@trancount > 0
  2133. begin
  2134.     print "Can't run sp_addlogin from within a transaction."
  2135.     return (1)
  2136. end
  2137.  
  2138. /*
  2139. **  Only the system administrator (sa) can run this command.
  2140. **  Check to make sure the executor is the sa.
  2141. */
  2142. if suser_id() != 1
  2143. begin
  2144.     print "Only the System Administrator (SA) may execute this procedure."
  2145.     return (1)
  2146. end
  2147.  
  2148. /*
  2149. **  Check to see that the @loginame is valid.
  2150. */
  2151. declare @returncode    int
  2152. execute @returncode = sp_validname @loginame
  2153. if @returncode != 0
  2154. begin
  2155.     select @msg = "'" + @loginame + "' is not a valid name."
  2156.     print @msg
  2157.     return @returncode
  2158. end
  2159.  
  2160. /*
  2161. **  Check to see that the @deflanguage is valid.
  2162. */
  2163. if @deflanguage is not null
  2164. begin
  2165.     select @returncode = 0
  2166.     execute @returncode = sp_validlang @deflanguage
  2167.     if @returncode != 0
  2168.     begin
  2169.         /* Us_english is always valid */
  2170.         if @deflanguage != "us_english"
  2171.         begin
  2172.             select @msg = "'" + @deflanguage + "' is not a valid official language name."
  2173.             print @msg
  2174.             return @returncode
  2175.         end
  2176.     end
  2177. end
  2178.  
  2179. /*
  2180. **  Make sure the login doesn't already exist.
  2181. */
  2182. if exists (select *
  2183.           from syslogins
  2184.           where name = @loginame)
  2185. begin
  2186.     print "A user with the specified login name already exists."
  2187.     return (1)
  2188. end
  2189.  
  2190. /*
  2191. **  Check that the database name is valid.
  2192. */
  2193. if not exists (select *
  2194.             from sysdatabases
  2195.         where name = @defdb)
  2196. begin
  2197.     print "Database name not valid -- login not added."
  2198.     return (1)
  2199. end
  2200.  
  2201. /*
  2202. **  Create the login.
  2203. */
  2204. insert into syslogins(suid, status, accdate, totcpu, totio,
  2205.     spacelimit, timelimit, resultlimit, dbname, name, password, language)
  2206. select max(suid)+1, 0, getdate(), 0, 0,
  2207.     0, 0, 0, @defdb, @loginame, @passwd, @deflanguage
  2208. from syslogins
  2209.  
  2210. print "New login created."
  2211.  
  2212. return (0)
  2213. go
  2214. /* addtype 27.1 3/5/90 */
  2215. create procedure sp_addtype
  2216. @typename varchar(30),        /* name of user-defined type */
  2217. @phystype varchar(30),        /* physical system type of user-defined type */
  2218. @nulltype varchar(8) = "1"    /* default is NO NULLs */
  2219. as
  2220.  
  2221. declare @len int        /* length of user type */
  2222. declare @type tinyint        /* typeid of physical type */
  2223. declare @tlen tinyint        /* length of physical type */
  2224. declare @typeid smallint    /* user typeid of physical type */
  2225. declare @var bit        /* is physical type variable length? */
  2226. declare @nonull bit        /* default is to allow NO NULLs */
  2227. declare @nullegal bit        /* does physical type allow NULLs? */
  2228. declare @msg varchar(250)
  2229.  
  2230.  
  2231. /*
  2232. **  Does the user type allow NULLs?  The default is to NOT allow NULLs.
  2233. **  this is the same default the create table statement uses.
  2234. */
  2235. if @nulltype = "null" or @nulltype = "NULL" or @nulltype = null
  2236.     select @nonull = 0
  2237. else if @nulltype = "not null" or @nulltype = "NOT NULL" or @nulltype = "nonull"
  2238.     or @nulltype = "NONULL" or @nulltype = "1"
  2239.     select @nonull = 1
  2240. else
  2241. begin
  2242.     print "Usage: sp_addtype name, 'datatype' [,null | nonull]"
  2243.     return (1)
  2244. end
  2245.  
  2246. /*
  2247. **  Check to see that the @typename is valid.
  2248. */
  2249. declare @returncode    int
  2250. execute @returncode = sp_validname @typename
  2251. if @returncode != 0
  2252. begin
  2253.     select @msg = "'" + @typename + "' is not a valid type name."
  2254.     print @msg
  2255.     return @returncode
  2256. end
  2257.  
  2258. /*
  2259. **  Check to see if the type already exists.
  2260. */
  2261. if exists (select *
  2262.         from systypes
  2263.        where name = @typename)
  2264. begin
  2265.     print "A type with the specified name already exists."
  2266.     return (1)
  2267. end
  2268.  
  2269. /*
  2270. **  If a length was given with the user datatype, extract it.
  2271. */
  2272. if @phystype like "_%(%)"
  2273. begin
  2274.     select @len = convert(int, substring(@phystype,
  2275.         charindex("(",@phystype) + 1,
  2276.         charindex(")",@phystype) - 1 - charindex("(",@phystype)))
  2277.  
  2278.     /*
  2279.     ** Extract the physical type name
  2280.     */
  2281.     select @phystype = substring(@phystype, 1,
  2282.            charindex("(", @phystype) - 1)
  2283. end
  2284.  
  2285. /*
  2286. **  Make sure that the physical type exists and get its characteristics.
  2287. **  DataServer physical types have a usertype < 100 and are owned by the
  2288. **  dbo (userid = 1).
  2289. */
  2290. select @type = type, @tlen = length, @typeid = usertype,
  2291.     @var = variable, @nullegal = allownulls
  2292. from systypes
  2293.     where usertype < 100 and name = @phystype and uid = 1
  2294.  
  2295. if @type = NULL
  2296. begin
  2297.     print "Physical datatype does not exist."
  2298.     return (1)
  2299. end
  2300.  
  2301. /*
  2302. **  Disallow user-defined datatypes on timestamps.  This is done because
  2303. **  a timestamp is not a basic type but is really a binary.  There is,
  2304. **  therefore, no way to tell if a user-defined datatype is mapped to
  2305. **  a binary or a timestamp.  Timestamps can't have rules or defaults.
  2306. */
  2307. if @phystype = "timestamp"
  2308. begin
  2309.     print "User-defined datatypes based on the 'timestamp' datatype are not allowed."
  2310.     return (1)
  2311. end
  2312.  
  2313. /*
  2314. **  Check if the NULL status of the user type is consistent with the NULL status
  2315. **  of the physical type.  Here are the possible cases.
  2316. **
  2317. **           physical type
  2318. **          NULLs      NONULLs
  2319. **            -----------------
  2320. ** user    NULLs    |  ok    |  no
  2321. ** type NONULLs    |  ok    |  ok
  2322. */
  2323. if @nonull = 0 and @nullegal = 0
  2324. begin
  2325.     print "Physical datatype does not allow nulls."
  2326.     return (1)
  2327. end
  2328.  
  2329. /*
  2330. **  We'll use the variable @nullegal when we update the systypes table
  2331. **  so we need to set it to reflect if NULLs are allowed (@nonull = 0)
  2332. **  or NO NULLs are allowed (@nonull = 1).
  2333. */
  2334. if @nonull = 0
  2335. begin
  2336.     select @nullegal = 1
  2337. end
  2338. else
  2339. begin
  2340.     select @nullegal = 0
  2341. end
  2342.  
  2343. /*
  2344. **  Typeids 1 (char), 2 (varchar), 3 (binary) and 4 (varbinary) are the
  2345. **  only ones which allow a length to be specified.
  2346. */
  2347. if @typeid > 4
  2348. begin
  2349.     /*
  2350.     **  We can't use a length and we got one.
  2351.     */
  2352.     if @len > 0
  2353.     begin
  2354.        print "Physical type is fixed length. You cannot specify the length."
  2355.        return (1)
  2356.     end
  2357.  
  2358.     /*
  2359.     **  Use the fixed length of the physical type.
  2360.     */
  2361.     select @len = @tlen
  2362. end
  2363. else
  2364. begin
  2365.     /*
  2366.     **  We need a length and we didn't get one.
  2367.     */
  2368.     if @len = NULL
  2369.     begin
  2370.         print "You must specify a length with this physical type."
  2371.         return (1)
  2372.     end
  2373.  
  2374.     if @len <= 0 or @len > 255
  2375.     begin
  2376.         print "Illegal length specified -- must be between 1 and 255."
  2377.         return (1)
  2378.     end
  2379.  
  2380. end
  2381.  
  2382.  
  2383. /*
  2384. **  Finally, get the maximum existing user type so we use it + 1 for this
  2385. **  new type.
  2386. */
  2387. select @typeid = max(usertype)
  2388.     from systypes
  2389.  
  2390. /*
  2391. **  There are no user defined types yet so use the first number (101).
  2392. */
  2393. if @typeid < 100
  2394.     select @typeid = 100
  2395.  
  2396. insert systypes (uid, variable, type, length, tdefault,
  2397.     domain, name, usertype, allownulls)
  2398. select user_id(), @var, @type, @len, 0, 0, @typename, @typeid + 1, @nullegal
  2399.  
  2400. print "Type added."
  2401.  
  2402. return (0)
  2403. go
  2404. grant execute on sp_addtype to public
  2405. go
  2406. dump transaction master with truncate_only
  2407. go
  2408. /* addumpdevice 27.1 3/5/90 */
  2409. create procedure sp_addumpdevice
  2410. @devtype    varchar(10),        /* disk, tape, or diskette */
  2411. @logicalname    varchar(30),        /* logical name of the device */
  2412. @physicalname    varchar(127),        /* physical name of the device */
  2413. @cntrltype    smallint,        /* controller number */
  2414. @devstatus    varchar(40) = "noskip",    /* device characteristics */
  2415. @mediasize    int = null        /* capacity of tape in MB */
  2416. as
  2417.  
  2418. declare @status smallint        /* status bits for device */
  2419. declare @msg varchar(250)
  2420.  
  2421. /*
  2422. **  If we're in a transaction, disallow this since it might make recovery
  2423. **  impossible.
  2424. */
  2425. if @@trancount > 0
  2426. begin
  2427.     print "Can't run sp_addumpdevice from within a transaction."
  2428.     return (1)
  2429. end
  2430.  
  2431. /*
  2432. **  You must be SA to execute this sproc.
  2433. */
  2434. if suser_id() != 1
  2435. begin
  2436.     print "You must be the System Administrator (SA) to execute this procedure."
  2437.     return (1)
  2438. end
  2439.  
  2440. /*
  2441. **  Check to see that the @logicalname is valid.
  2442. */
  2443. declare @returncode    int
  2444. execute @returncode = sp_validname @logicalname
  2445. if @returncode != 0
  2446. begin
  2447.     select @msg = "'" + @logicalname + "' is not a valid name."
  2448.     print @msg
  2449.     return @returncode
  2450. end
  2451.  
  2452. /*
  2453. **  The rules for updating sysdevices are as follows:
  2454. **    devtype -  May be disk,, diskette or tape.
  2455. **        If disk then cntrltype will be 2,
  2456. **        If diskette then 3 or 4
  2457. **        If tape then 5
  2458. **        NOTE:  These are completely host dependent.  They probably
  2459. **        need to be changed for each DataServer host type.
  2460. **    logicalname - may be any identifier but must be unique
  2461. **    physicalname - may be anything and doesn't have to be unique
  2462. **        If it contains illegal id characters, e.g., '/', then
  2463. **        it must be in quotes.
  2464. **    status - for a tape device it may be 'skip'.  The default is 'noskip'.
  2465. **        For disk devices it is ignored.
  2466. **    Mediasize - for a tape device, this is the tape capacity in MB.  It
  2467. **        is ignored on VMS and OS/2.
  2468. */
  2469.  
  2470. /*
  2471. **  Check out the @devtype.
  2472. **    check out the @devtype.
  2473. */
  2474.  
  2475. if @devtype = null
  2476. begin
  2477.     print "Unknown device type.  Use 'disk', 'tape' or 'diskette'."
  2478.     return (1)
  2479. end
  2480.  
  2481. if @devtype not in ("disk", "tape", "diskette")
  2482. begin
  2483.     print "Unknown device type.  Use 'disk', 'tape' or 'diskette'."
  2484.     return (1)
  2485. end
  2486.  
  2487. /*
  2488. **  Check the args are not NULL.
  2489. */
  2490. if @logicalname = null
  2491. begin
  2492.     print "@logicalname may not be NULL."
  2493.     return
  2494. end
  2495. if @physicalname = null
  2496. begin
  2497.     print "@physicalname may not be NULL."
  2498.     return
  2499. end
  2500. if @cntrltype = null
  2501. begin
  2502.     print "@cntrltype may not be NULL."
  2503.     return
  2504. end
  2505.  
  2506. /*
  2507. **  Make sure that a device with @logicalname doesn't already exist.
  2508. */
  2509. if exists (select *
  2510.         from master.dbo.sysdevices
  2511.             where name = @logicalname)
  2512. begin
  2513.     print "Device with same logical name already exists."
  2514.     return (1)
  2515. end
  2516.  
  2517. /*
  2518. **  Check @cntrltype.  On UNIX, it must be 2 if @devtype = disk
  2519. **  3 or 4 for diskette and 5 for @devtype = tape.  For other hosts, it
  2520. **  will undoubtedly be different.
  2521. */
  2522. if @devtype = "disk" and @cntrltype != 2
  2523. begin
  2524.     print "For disk devices, controller number parameter must be 2."
  2525.     return (1)
  2526. end
  2527.  
  2528. if @devtype = "diskette" and @cntrltype not between 3 and 4
  2529. begin
  2530.     print "For diskette devices, controller number parameter must be 3 or 4."
  2531.     return (1)
  2532. end
  2533.  
  2534. if @devtype = "tape" and @cntrltype != 5
  2535. begin
  2536.     print "For tape devices, controller number parameter must be 5."
  2537.     return (1)
  2538. end
  2539.  
  2540. /*
  2541. **  Always turn on the dump status bit.
  2542. */
  2543. select @status = 16
  2544.  
  2545. /*
  2546. **  If @devtype is a diskette then print a clarification if the media size is
  2547. **  not zero
  2548. */
  2549. if @devtype = "diskette"
  2550. begin
  2551.  
  2552.     /*  If this is OS/2 and the media size is given, print a message
  2553.     **  saying that the media size will be ignored.    
  2554.     */
  2555.  
  2556.     if exists (select *
  2557.             from master.dbo.spt_values
  2558.                 where type = "E"
  2559.                     and name = "OS/2")
  2560.     begin
  2561.         if @mediasize != null
  2562.         begin
  2563.               print "Media size is being ignored on the OS/2 SQL Server"
  2564.         end
  2565.         select @mediasize = 0
  2566.  
  2567.     end
  2568.     if @devstatus != "noskip"
  2569.     begin
  2570.         print "The only legal option for a diskette device is 'noskip'."
  2571.         return (1)
  2572.     end
  2573. end
  2574.  
  2575. /*
  2576. **  If @devtype is a tape then check to see if devstatus is 'skip'.
  2577. */
  2578. if @devtype = "tape"
  2579. begin
  2580.  
  2581.     /*  If this is OS/2 and the media size is given, print a message
  2582.     **  saying that the media size will be ignored.    
  2583.     */
  2584.  
  2585.     if exists (select *
  2586.             from master.dbo.spt_values
  2587.                 where type = "E"
  2588.                     and name = "OS/2")
  2589.     begin
  2590.         if @mediasize != null
  2591.         begin
  2592.               print "Media size is being ignored on the OS/2 SQL Server"
  2593.         end
  2594.         select @mediasize = 0
  2595.  
  2596.     end
  2597.     else
  2598.     begin
  2599.         /*
  2600.         **  If this is not VMS, then @mediasize is required.
  2601.         */
  2602.         if not exists (select *
  2603.             from master.dbo.spt_values
  2604.                 where type = "E"
  2605.                     and name = "vms")
  2606.         begin
  2607.         if @mediasize = null or @mediasize < 1
  2608.         begin
  2609.             print "Please specify media capacity in megabytes (1 MB minimum)."
  2610.             return (1)
  2611.         end
  2612.  
  2613.         /*
  2614.         **  @mediasize is given in MB but sysdevices expects number
  2615.         **  of 62k blocks so do the conversion.
  2616.         */
  2617.         select @mediasize = convert(int, (@mediasize * 1048576.) / 63488)
  2618.         end
  2619.         else
  2620.         begin
  2621.         select @mediasize = 0
  2622.         end
  2623.     end
  2624.  
  2625.  
  2626.     if @devstatus != "noskip" and @devstatus != "skip"
  2627.     begin
  2628.         print "The only legal option for a tape device is 'skip' or 'noskip'."
  2629.         return (1)
  2630.     end
  2631.     if @devstatus = "skip"
  2632.         select @status = @status | 8
  2633. end
  2634. else select @mediasize = 0
  2635.  
  2636. /*
  2637. **  If a disk then the cntrltype = 2, at least on SUN UNIX and OS/2
  2638. */
  2639. if @devtype = "disk"
  2640. begin
  2641.     insert into sysdevices (low, high, status, cntrltype, name, phyname)
  2642.         values (0, @mediasize, @status, @cntrltype, @logicalname, @physicalname)
  2643.  
  2644.     print "'Disk' device added."
  2645. end
  2646.  
  2647.  
  2648. /*
  2649. **  If a diskette then the cntrltype between 3 and 4
  2650. */
  2651. if @devtype = "diskette"
  2652. begin
  2653.     insert into sysdevices (low, high, status, cntrltype, name, phyname)
  2654.         values (0, @mediasize, @status, @cntrltype, @logicalname, @physicalname)
  2655.  
  2656.     print "'Diskette' device added."
  2657. end
  2658.  
  2659. /*
  2660. **  Must be a tape device.
  2661. */
  2662. if @devtype = "tape"
  2663. begin
  2664.     insert into sysdevices (low, high, status, cntrltype, name, phyname)
  2665.         values (0, @mediasize, @status, @cntrltype, @logicalname,
  2666.             @physicalname)
  2667.  
  2668.     print "'Tape' device added."
  2669. end
  2670.  
  2671. /*
  2672. **  Warn about duplicate physical names.
  2673. */
  2674. if (select count(*)
  2675.     from master.dbo.sysdevices
  2676.         where phyname = @physicalname) > 1
  2677. begin
  2678.     select @msg = "WARNING: physical device name '" + @physicalname +
  2679.         "' is not unique."
  2680.     print @msg
  2681. end
  2682.  
  2683. return (0)
  2684. go
  2685. /* adduser 27.1 3/5/90 */
  2686. create procedure sp_adduser
  2687. @loginame varchar(30),            /* user's login name in syslogins */
  2688. @name_in_db varchar(30) = NULL,        /* user's name to add to current db */
  2689. @grpname varchar(30) = NULL        /* group to put new user in */
  2690. as
  2691.  
  2692. declare @suid int            /* user's system id */
  2693. declare @grpid int            /* group id of group to put user in */
  2694. declare @uid int            /* new user's id */
  2695. declare @msg varchar(250)
  2696.  
  2697. /*
  2698. **  You must be SA or the dbo to execute this sproc.
  2699. */
  2700. if suser_id() != 1 and user_id() != 1
  2701. begin
  2702.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  2703.     return (1)
  2704. end
  2705.  
  2706. /*
  2707. **  If no new user name is given, use the login name.
  2708. */
  2709. if @name_in_db = NULL
  2710.     select @name_in_db = @loginame
  2711.  
  2712. /*
  2713. **  Check to see that the @name_in_db is valid.
  2714. */
  2715. declare @returncode    int
  2716. execute @returncode = sp_validname @name_in_db
  2717. if @returncode != 0
  2718. begin
  2719.     select @msg = "'" + @name_in_db + "' is not a valid name."
  2720.     print @msg
  2721.     return @returncode
  2722. end
  2723.  
  2724. /*
  2725. **  The name guest is a special case.  If it doesn't have a login it
  2726. **  can still be a valid user.  We'll catch it here and special case it.
  2727. */
  2728. if @loginame = "guest" and not exists
  2729.     (select name
  2730.         from master.dbo.syslogins
  2731.             where name = @loginame)
  2732. begin
  2733.     if exists (select *
  2734.             from sysusers
  2735.            where name = @loginame)
  2736.     begin
  2737.         print "A user with the same name already exists in the database."
  2738.         return (1)
  2739.     end
  2740.  
  2741.     /*
  2742.     **  Add the guest user and return.
  2743.     */
  2744.     insert into sysusers (uid, suid, gid, name)
  2745.         values (2, -1, 0, "guest")
  2746.  
  2747.     return (0)
  2748. end
  2749.  
  2750. /*
  2751. **  Check to see that the user has a login name.
  2752. **  We'll also initialize @grpid to 0 while we're here.
  2753. */
  2754. select @suid = suid, @grpid = 0
  2755.     from master.dbo.syslogins
  2756. where name = @loginame
  2757.  
  2758. if @suid = NULL
  2759. begin
  2760.     print "No login with the specified name exists."
  2761.     return (1)
  2762. end
  2763.  
  2764. /*
  2765. **  Now check to see if the user already exists in the database.
  2766. */
  2767. if exists (select *
  2768.            from sysusers
  2769.        where name = @name_in_db)
  2770. begin
  2771.     print "A user with the same name already exists in the database."
  2772.     return (1)
  2773. end
  2774.  
  2775. /*
  2776. **  See if the user already has an account under a different name.
  2777. **  That is, is the user's suid already in the sysusers table.
  2778. */
  2779. if exists (select *
  2780.        from sysusers
  2781.        where suid = @suid)
  2782. begin
  2783.     print "User already has a login under a different name."
  2784.     return (1)
  2785. end
  2786.  
  2787. /*
  2788. **  See if the user is known in the database already with an alias.
  2789. **  That is, does the user's suid appear in the sysalternates table.
  2790. */
  2791. if exists (select *
  2792.        from sysalternates
  2793.        where suid = @suid)
  2794. begin
  2795.     print "User already has alias access to the database."
  2796.     return (1)
  2797. end
  2798.  
  2799. /*
  2800. **  If a group name is given, check to see that it is valid.
  2801. **  User ids are > 0 and <= 16383 and group ids are > 16383.
  2802. */
  2803. if @grpname != NULL
  2804. begin
  2805.     select @grpid = -1
  2806.     select @grpid = gid
  2807.         from sysusers
  2808.     where name = @grpname and (uid > 16383 or uid = 0)
  2809.  
  2810.     if @grpid = -1
  2811.     begin
  2812.         print "No group with the specified name exists."
  2813.         return (1)
  2814.     end
  2815. end
  2816.  
  2817. /*
  2818. **  Add the user to the sysusers table.
  2819. **  Check to see if the special user 'guest' (uid = 2) has already
  2820. **  been added.  If not then the uid to use is 3, otherwise max(uid) + 1.
  2821. */
  2822. select @uid = max(uid) + 1
  2823.     from sysusers
  2824.         where uid < 16384 and uid > 2
  2825.  
  2826. /* check if there's any more user id's available */
  2827. if @uid >= 16384
  2828. begin
  2829.     print "All user ids have been assigned."
  2830.     return (1)
  2831. end
  2832.  
  2833. /*
  2834. **  This is the first regular user to be added so give them the first
  2835. **  number.
  2836. */
  2837. if @uid = NULL
  2838.     select @uid = 3
  2839.  
  2840. insert into sysusers (uid, suid, gid, name)
  2841.     values (@uid, @suid, @grpid, @name_in_db)
  2842.  
  2843. print "New user added."
  2844.  
  2845. return (0)
  2846. go
  2847. grant execute on sp_adduser to public
  2848. go
  2849. dump transaction master with truncate_only
  2850. go
  2851. /* addremotelogin 27.1 3/5/90 */
  2852. create procedure sp_addremotelogin
  2853. @remoteserver    varchar(30),        /* name of remote server */
  2854. @loginame varchar(30) = NULL,        /* user's remote name */
  2855. @remotename varchar(30) = NULL        /* user's local user name */
  2856. as
  2857.  
  2858. declare    @msg    varchar(200)
  2859.  
  2860. /*
  2861. **  If we're in a transaction, disallow this since it might make recovery
  2862. **  impossible.
  2863. */
  2864. if @@trancount > 0
  2865. begin
  2866.     print "Can't run sp_addremotelogin from within a transaction."
  2867.     return (1)
  2868. end
  2869.  
  2870. /*
  2871. **  Check that the server name is valid.
  2872. */
  2873. if not exists (select *
  2874.             from master.dbo.sysservers
  2875.         where srvname = @remoteserver)
  2876. begin
  2877.     select @msg = "There is not a server named '" + @remoteserver + "'."
  2878.     print @msg
  2879.     return (1)
  2880. end
  2881.  
  2882. /*
  2883. **  If it's the local server don't bother.
  2884. */
  2885. if exists (select *
  2886.         from master.dbo.sysservers
  2887.             where srvname = @remoteserver
  2888.                 and srvid = 0)
  2889. begin
  2890.     select @msg = "'" + @remoteserver + "' is the local server - remote login not applicable."
  2891.     print @msg
  2892.     return (1)
  2893. end
  2894.  
  2895. /*
  2896. **  There are three cases to handle.
  2897. **
  2898. **    1) if only @remoteserver is given then a entry is made in
  2899. **        sysremotelogins that means anyone that doesn't have
  2900. **        an exact of mapped match in sysremotelogins will use
  2901. **        their remotename as their local name and it will be looked
  2902. **        up in syslogins.
  2903. **
  2904. **    2) if @remotename is omitted then it means that anyone from the
  2905. **        remote server logging in that doesn't have a complete
  2906. **        match in sysremotelogins will be mapped to @loginame.
  2907. **
  2908. **    3) if @remotename and @loginame are given then it is a straight
  2909. **        remote login for sysremotelogins.
  2910. */
  2911.  
  2912. /*
  2913. **  Case 1:  Only @remoteserver given.
  2914. */
  2915. if @loginame = null and @remotename = null
  2916. begin
  2917.     /*
  2918.     **  Check that there is not already an entry for local mapping.
  2919.     */
  2920.     if exists (select *
  2921.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  2922.             where l.remoteserverid = s.srvid
  2923.                 and s.srvname = @remoteserver
  2924.                 and l.remoteusername = null)
  2925.     begin
  2926.         select @msg = "There is already a default-name mapping of a remote login from remote server '"
  2927.             + @remoteserver + "'."
  2928.         print @msg
  2929.         return (1)
  2930.     end
  2931.  
  2932.     /*
  2933.     **  Add the entry.
  2934.     */
  2935.     insert into master.dbo.sysremotelogins
  2936.         (remoteserverid, remoteusername, suid, status)
  2937.     select srvid, null, -1, 0
  2938.         from master.dbo.sysservers
  2939.             where srvname = @remoteserver
  2940.     print "New remote login created."
  2941.     return (0)
  2942.  
  2943. end
  2944.  
  2945. /*
  2946. **  Check that the @loginame is valid.  These is needed for both
  2947. **  case 2 and 3.
  2948. */
  2949. if not exists (select *
  2950.         from master.dbo.syslogins
  2951.             where name = @loginame)
  2952. begin
  2953.     select @msg = "'" + @loginame + "' isn't a local user -- remote login denied."
  2954.     print @msg
  2955.     return (1)
  2956. end
  2957.  
  2958. /*
  2959. **  Check to make sure that there is not already a @remotename for
  2960. **  the @remoteserver.
  2961. */
  2962. if exists (select *
  2963.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  2964.             where l.remoteserverid = s.srvid
  2965.                 and s.srvname = @remoteserver
  2966.                 and l.remoteusername = @remotename)
  2967. begin
  2968.     select @msg = "There is already a remote user named '"
  2969.         + isnull(@remotename, "NULL") + "' for remote server '"
  2970.         + @remoteserver + "'."
  2971.     print @msg
  2972.     return (1)
  2973. end
  2974.     
  2975.  
  2976. /*
  2977. **  Case 2: We want to make an entry into sysremotelogins that will map
  2978. **    any non-exact matches to a particular local user.
  2979. */
  2980. if @remotename = null
  2981. begin
  2982.     /*
  2983.     **  Check that there is not already an entry for local mapping.
  2984.     */
  2985.     if exists (select *
  2986.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  2987.             where l.remoteserverid = s.srvid
  2988.                 and s.srvname = @remoteserver
  2989.                 and l.remoteusername = null)
  2990.     begin
  2991.         select @msg = "There is already a default-name mapping of a remote login from remote server '"
  2992.             + @remoteserver + "'."
  2993.         print @msg
  2994.         return (1)
  2995.     end
  2996.  
  2997.     /*
  2998.     **  Go ahead and make the entry.
  2999.     */
  3000.     insert into master.dbo.sysremotelogins
  3001.         (remoteserverid, remoteusername, suid, status)
  3002.     select srvid, null, suser_id(@loginame), 0
  3003.         from master.dbo.sysservers
  3004.                 where srvname = @remoteserver
  3005.  
  3006.     print "New remote login created."
  3007.     return (0)
  3008. end
  3009.  
  3010. /*
  3011. **  Case 3:  All the parameters have been supplied.  All we need to check
  3012. **    is that the entry isn't already in sysremotelogins.
  3013. **    We've verified the @remoteserver and @loginame above.
  3014. */
  3015. if @loginame != null and @remotename != null
  3016. begin
  3017.     /*
  3018.     **  Make sure that the @loginame and @remotename are a
  3019.     **  unique combination.
  3020.     */
  3021.     if exists (select *
  3022.         from master.dbo.sysremotelogins l, master.dbo.sysservers s
  3023.             where l.remoteusername = @remotename
  3024.                 and l.remoteserverid = s.srvid
  3025.                 and s.srvname = @remoteserver
  3026.                 and l.suid = suser_id(@loginame))
  3027.     begin
  3028.         select @msg = "There is already a remote user named '"
  3029.             + @remotename + "' for remote server '"
  3030.             + @remoteserver + "'."
  3031.         print @msg
  3032.         return (1)
  3033.     end
  3034.  
  3035.     /*
  3036.     **  Go ahead the do the insert.
  3037.     */
  3038.     insert into master.dbo.sysremotelogins
  3039.         (remoteserverid, remoteusername, suid, status)
  3040.     select srvid, @remotename, suser_id(@loginame), 0
  3041.         from master.dbo.sysservers
  3042.             where srvname = @remoteserver
  3043.     print "New remote login created."
  3044.     return (0)
  3045. end
  3046.  
  3047. /*
  3048. **  We got here because the syntax was incorrect.
  3049. */
  3050. print "Usage: sp_addremotelogin remoteserver [, loginame [,remotename]]"
  3051. return (1)
  3052. go
  3053. /* addsegment    27.1    3/5/90 */
  3054. create procedure sp_addsegment
  3055. @segname    varchar(30),    /* segment name */
  3056. @devname    varchar(30)    /* device name to put segment on */
  3057. as
  3058.  
  3059. declare @dbuid    smallint    /* id of the owner of the database */
  3060. declare @segbit    int        /* this is the bit to turn on in sysusages */
  3061. declare    @msg    char(100)
  3062. declare @retcode    int
  3063.  
  3064. /*
  3065. **  If we're in a transaction, disallow this since it might make recovery
  3066. **  impossible.
  3067. */
  3068. if @@trancount > 0
  3069. begin
  3070.     print "Can't run sp_addsegment from within a transaction."
  3071.     return (1)
  3072. end
  3073.  
  3074. /*
  3075. **  You must be SA or the dbo to execute this sproc.
  3076. */
  3077. if suser_id() != 1 and user_id() != 1
  3078. begin
  3079.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  3080.     return (1)
  3081. end
  3082.  
  3083. /*
  3084. **  See if the device exists.
  3085. */
  3086. if not exists (select *
  3087.         from master.dbo.sysdevices
  3088.             where name like @devname)
  3089. begin
  3090.     print "No such device exists -- run sp_helpdb to list the devices for the current database."
  3091.     return (1)
  3092. end
  3093.  
  3094. /*
  3095. **    Check that the device is a database device.
  3096. */
  3097.  
  3098. if not exists (select *
  3099.         from master.dbo.sysdevices
  3100.             where name like @devname and cntrltype = 0)
  3101. begin
  3102.     print "The specified device is not a database device."
  3103.     return (1)
  3104. end
  3105.  
  3106. /*
  3107. **  Now see if the @dbname uses the @devname
  3108. */
  3109. if not exists (select *
  3110.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3111.             where d.name = @devname
  3112.                 and u.vstart between d.low and d.high
  3113.                 and u.dbid = db_id())
  3114. begin
  3115.     print "The specified device is not used by the database."
  3116.     return (1)
  3117. end
  3118.  
  3119. /*
  3120. **  Check to see if the device is marked as a log device.
  3121. **  If so, print error.
  3122. */
  3123. if exists (select *
  3124.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3125.             where d.name = @devname
  3126.                 and u.vstart between d.low and d.high
  3127.                 and u.dbid = db_id()
  3128.                 and segmap = 4)
  3129. begin
  3130.     select @msg = "'" + @devname + "' is reserved exclusively as a log device."
  3131.     print @msg
  3132.     return (1)
  3133. end
  3134.  
  3135. /*
  3136. ** Check for valid identifier.
  3137. */
  3138. execute @retcode = sp_validname @segname
  3139. if (@retcode != 0)
  3140. begin
  3141.     select @msg = "'" + @segname + "' is not a valid identifier."
  3142.     print @msg
  3143.     return (1)
  3144. end
  3145.  
  3146. /*
  3147. **  Now go ahead and define the new segment and add it to the segmap
  3148. **  of sysusages.
  3149. **  NOTE: Don't update master..sysusages and syssegments as a xact since
  3150. **      it could cause problems for recovery.
  3151. */
  3152.  
  3153. /*
  3154. **  Check that @segname doesn't already exist.
  3155. */
  3156. if exists (select *
  3157.         from syssegments holdlock
  3158.             where name = @segname)
  3159. begin
  3160.     select @msg = "There is already a segment named '"
  3161.         + @segname + "'."
  3162.     print @msg
  3163.     return (1)
  3164. end
  3165.  
  3166. /*
  3167. **  Figure out the next segment number to use.
  3168. **  Segment number may be 0-31.
  3169. */
  3170. select @segbit = 3
  3171. while @segbit < 32
  3172. begin
  3173.     /*
  3174.     **  Did we find one?
  3175.     */
  3176.     if exists (select *
  3177.             from syssegments
  3178.                 where segment = @segbit)
  3179.     begin
  3180.         select @segbit = @segbit + 1
  3181.     end
  3182.  
  3183.     /*
  3184.     **  We found an opening so break out.
  3185.     */
  3186.     else break
  3187.  
  3188. end
  3189.  
  3190. if @segbit >= 32
  3191. begin
  3192.     print "The maximum number of segments for the current database are already defined."
  3193.     return (1)
  3194. end
  3195.  
  3196. /*
  3197. **  Add the new segment.
  3198. */
  3199. insert into syssegments (segment, name, status)
  3200.     values (@segbit, @segname, 0)
  3201.  
  3202. /*
  3203. **  Now set the segments on @devname sysusages.
  3204. */
  3205. if (@segbit < 31)
  3206.     select @segbit = power(2, @segbit)
  3207. else
  3208.     /*
  3209.     **  Since this is segment 31, power(2, 31) will overflow
  3210.     **  since segmap is an int.  We'll grab the machine-dependent
  3211.     **  bit mask from spt_values to set the right bit.
  3212.     */
  3213.     select @segbit = low
  3214.         from master.dbo.spt_values
  3215.             where type = "E"
  3216.                 and number = 2
  3217. update master.dbo.sysusages
  3218.     set segmap = segmap | @segbit
  3219.         from master.dbo.sysusages u,
  3220.             master.dbo.sysdevices d
  3221.         where d.name = @devname
  3222.             and u.vstart between d.low and d.high
  3223.             and u.dbid = db_id()
  3224.  
  3225. /*
  3226. **  Now we need to activate the new segment map.
  3227. */
  3228. declare @dbname    varchar(30)
  3229. select @dbname = db_name()
  3230. dbcc dbrepair (@dbname, remap)
  3231. print "Segment created."
  3232.  
  3233. return (0)
  3234. go
  3235. grant execute on sp_addsegment to public
  3236. go
  3237. dump transaction master with truncate_only
  3238. go
  3239. /* addserver    27.1    3/5/90 */
  3240. create procedure sp_addserver
  3241. @server    varchar(30),        /* server name */
  3242. @local    varchar(10) = NULL    /* local server */
  3243. as
  3244.  
  3245. declare    @msg    varchar(100)
  3246.  
  3247. /*
  3248. **  If we're in a transaction, disallow this since it might make recovery
  3249. **  impossible.
  3250. */
  3251. if @@trancount > 0
  3252. begin
  3253.     print "Can't run sp_addserver from within a transaction."
  3254.     return (1)
  3255. end
  3256.  
  3257. /*
  3258. **  Server names must be unique so check.
  3259. */
  3260. if exists (select *
  3261.         from master.dbo.sysservers
  3262.             where srvname = @server)
  3263. begin
  3264.     select @msg = "There is already a server named '" + @server + "'."
  3265.     print @msg
  3266.     return (1)
  3267. end
  3268.  
  3269. /*
  3270. **  Check to see that the @server is valid.
  3271. */
  3272. declare @returncode    int
  3273. execute @returncode = sp_validname @server
  3274. if @returncode != 0
  3275. begin
  3276.     select @msg = "'" + @server + "' is not a valid name."
  3277.     print @msg
  3278.     return @returncode
  3279. end
  3280.  
  3281. /*
  3282. **  If this is the not the local server, then its srvid = max(srvid) + 1.
  3283. */
  3284. if @local = null
  3285. begin    
  3286.     insert into master.dbo.sysservers (srvid, srvstatus, srvname, srvnetname)
  3287.             select isnull(max(srvid), 0) + 1, 0, @server, @server
  3288.                 from master.dbo.sysservers
  3289. end
  3290.  
  3291. /*
  3292. **  If @local = "local" then this is the local server and it's
  3293. **  srvid = 0.
  3294. */
  3295. else
  3296. begin
  3297.     if @local != "local" and @local != "LOCAL"
  3298.     begin
  3299.         print "Usage: sp_addserver servername [, 'local']"
  3300.         return (1)
  3301.     end
  3302.  
  3303.     if exists (select *
  3304.             from master.dbo.sysservers
  3305.                 where srvid = 0)
  3306.     begin
  3307.         print "There is already a local server."
  3308.         return (1)
  3309.     end
  3310.  
  3311.     insert into master.dbo.sysservers (srvid, srvstatus, srvname, srvnetname)
  3312.             values (0, 0, @server, @server)
  3313. end
  3314.  
  3315. select @msg = "Server added."
  3316. print @msg
  3317. return (0)
  3318. go
  3319. /* addlanguage 27.1 3/5/90 */
  3320. create procedure sp_addlanguage
  3321. @language varchar(30),
  3322. @alias varchar(30) = NULL,
  3323. @months varchar(251),
  3324. @shortmons varchar(119),
  3325. @days varchar(216),
  3326. @datefmt char(3),
  3327. @datefirst tinyint
  3328. as
  3329.  
  3330. declare @msg varchar(250)
  3331. declare @nextlangid smallint
  3332. declare @returncode    int
  3333.  
  3334. /*  Check to see if the language exists. */
  3335. select @returncode = 0
  3336. execute @returncode = sp_validlang @language
  3337. if @returncode = 0
  3338. begin
  3339.     select @msg = "'" + @language + "' already exists in Syslanguages."
  3340.     print @msg
  3341.     return 1
  3342. end
  3343.  
  3344. /* Check to see that the list of full month names is valid. */
  3345. execute @returncode = sp_chklangparam @months, "full month", 12, 20
  3346. if @returncode != 0
  3347.     return 1
  3348.  
  3349. /* Check to see that the list of short month names is valid. */
  3350. execute @returncode = sp_chklangparam @shortmons, "short month", 12, 9
  3351. if @returncode != 0
  3352.     return 1
  3353.  
  3354. /* Check to see that the list of day names is valid. */
  3355. execute @returncode = sp_chklangparam @days, "day", 7, 30
  3356. if @returncode != 0
  3357.     return 1
  3358.  
  3359. /* Check to see that the @datefmt is valid. */
  3360. if (@datefmt != 'mdy' and @datefmt != 'dmy' and @datefmt != 'ymd' and @datefmt != 'ydm'
  3361.     and @datefmt != 'myd' and @datefmt != 'dym')
  3362. begin
  3363.     select @msg = "'" + @datefmt + "' is not a valid date order."
  3364.     print @msg
  3365.     return 1
  3366. end
  3367.  
  3368. /*
  3369. **  Check to see that the @datefirst is valid.
  3370. */
  3371. if (@datefirst < 1 or @datefirst > 7)
  3372. begin
  3373.     select @msg = "'" + convert(char(3), @datefirst) + "' is not a valid first day."
  3374.     print @msg
  3375.     return 1
  3376. end
  3377.  
  3378. /*
  3379. **  If the user didn't specify the alias name, the alias name is same as
  3380. **  the official name.
  3381. */
  3382. if @alias is null
  3383. begin
  3384.     select @alias =  @language
  3385. end
  3386.  
  3387. /*  Check to see if the alias exists. */
  3388. select @returncode = 0
  3389. execute @returncode = sp_validaltlang @alias
  3390. if @returncode = 0
  3391. begin
  3392.     select @msg = "'" + @alias + "' alias already exists in Syslanguages."
  3393.     print @msg
  3394.     return 1
  3395. end
  3396.  
  3397. if @language = 'us_english'
  3398. begin
  3399.     /*  The language id of us_english is 0. */
  3400.     insert syslanguages (langid, dateformat, datefirst, upgrade,
  3401.         name, alias, months, shortmonths, days)
  3402.     select 0, @datefmt, @datefirst, 0,
  3403.         @language, @alias, @months, @shortmons, @days
  3404. end
  3405. else
  3406. begin
  3407.  
  3408.     if (select max(langid) from syslanguages) is null
  3409.     begin
  3410.         select @nextlangid = 1
  3411.     end
  3412.     else
  3413.     begin
  3414.         select @nextlangid = (select max(langid)+1
  3415.         from syslanguages)
  3416.     end
  3417.  
  3418.     insert syslanguages (langid, dateformat, datefirst, upgrade,
  3419.         name, alias, months, shortmonths, days)
  3420.     select @nextlangid, @datefmt, @datefirst, 0,
  3421.         @language, @alias, @months, @shortmons, @days
  3422. end
  3423.  
  3424.  
  3425. /* If the insert failed, say so. */
  3426. if @@error != 0
  3427. begin
  3428.     print "Language not inserted."
  3429.     return
  3430. end
  3431.  
  3432. print "New language inserted."
  3433. go
  3434. dump transaction master with truncate_only
  3435. go
  3436. /* defaultlanguage 27.1 3/5/90 */
  3437. create procedure sp_defaultlanguage
  3438. @loginame varchar(30),            /* login name of the user */
  3439. @language varchar(30) = NULL        /* default language for the new user */
  3440. as
  3441.  
  3442. declare @msg varchar(250)
  3443. declare @returncode int
  3444.  
  3445. /*
  3446. **  Only the system administrator (sa) can run this command.
  3447. **  Check to make sure the executor is the sa.
  3448. */
  3449. if (suser_id() != 1 and suser_id() != suser_id(@loginame))
  3450. begin
  3451.     select @msg = "Only the System Administrator (SA) or '" + @loginame + "' can change " + @loginame + "'s default language."
  3452.     print @msg
  3453.     return 1
  3454. end
  3455.  
  3456. /*
  3457. **  Check to see that the @language is valid.
  3458. */
  3459. if @language is not null
  3460. begin
  3461.     select @returncode = 0
  3462.     execute @returncode = sp_validlang @language
  3463.     if @returncode != 0
  3464.     begin
  3465.         if @language != "us_english"
  3466.         begin
  3467.             select @msg = "'" + @language + "' is not a valid official language name."
  3468.             print @msg
  3469.             return @returncode
  3470.         end
  3471.     end
  3472. end
  3473.  
  3474. /*
  3475. **  Make sure the login already exist.
  3476. */
  3477. if not exists (select *
  3478.           from syslogins
  3479.           where name = @loginame)
  3480. begin
  3481.     print "This user does not exist.  Run sp_addlogin to add this user in."
  3482.     return 1
  3483. end
  3484.  
  3485. /*
  3486. **  Set the default language for this user.
  3487. */
  3488. update syslogins
  3489.     set language = @language
  3490.          where name = @loginame
  3491.  
  3492. select @msg =  @loginame + "'s default language is changed to " + @language
  3493. print @msg
  3494.  
  3495. return 0
  3496. go
  3497. grant execute on sp_defaultlanguage to public
  3498. go
  3499. /* helplanguage 27.1 3/5/90 */
  3500. create procedure sp_helplanguage
  3501. @language varchar(30) = NULL
  3502. as
  3503.  
  3504. declare @msg varchar(250)
  3505.  
  3506. /* Print all languages if the user didn't give the language name. */
  3507. if @language is null
  3508. begin
  3509.     if exists (select * from syslanguages)
  3510.         select * from syslanguages
  3511.     else
  3512.         print "No alternate languages are available."
  3513.  
  3514.     /* Find out whether us_english is there or not. */
  3515.     if not exists (select * from syslanguages
  3516.             where name = 'us_english')
  3517.         print "us_english is always available, even though it is not in master.dbo.syslanguages."
  3518.  
  3519.     return 0
  3520. end
  3521.  
  3522. /*  Report information on this language. */
  3523. if exists (select * from syslanguages
  3524.                 where name = @language)
  3525. begin
  3526.         select * from syslanguages
  3527.                 where name = @language
  3528.         return 0
  3529. end
  3530.  
  3531. if exists (select * from syslanguages
  3532.         where alias = @language)
  3533. begin
  3534.     select * from syslanguages
  3535.         where alias = @language
  3536.     return 0
  3537. end
  3538.  
  3539. /* Couldn't find this language. */
  3540. if @language = 'us_english'
  3541. begin
  3542.     print "us_english is always available, even though it is not in master.dbo.syslanguages."
  3543.     return 0
  3544. end
  3545. else
  3546. begin
  3547.     select @msg = "'" + @language + "' is not in Syslanguages."
  3548.     print @msg
  3549.     return 1
  3550. end
  3551. go
  3552. grant execute on sp_helplanguage to public
  3553. go
  3554. dump transaction master with truncate_only
  3555. go
  3556. /* droplanguage 27.1 3/5/90 */
  3557. create procedure sp_droplanguage
  3558. @language varchar(30),
  3559. @dropmessages varchar(30) = NULL
  3560. as
  3561.  
  3562. declare @msg varchar(250)
  3563. declare @langid smallint
  3564. declare @dropmsgs int
  3565.  
  3566.  
  3567. /*  Check to see if the language exists. */
  3568. declare @returncode    int
  3569. select @returncode = 0
  3570. execute @returncode = sp_validlang @language
  3571. if @returncode != 0
  3572. begin
  3573.     select @msg = "'" + @language + "' is not in Syslanguages."
  3574.     print @msg
  3575.     return 1
  3576. end
  3577.  
  3578. /* Get language id from syslanguages. */
  3579. select @langid = (select langid
  3580.             from syslanguages
  3581.                 where name = @language)
  3582.  
  3583. /*  Check to see if "dropmessages" is requested. */
  3584. select @dropmsgs = 0
  3585. if @dropmessages is null
  3586. begin
  3587.     if exists (select * from sysmessages
  3588.             where langid = @langid)
  3589.     begin
  3590.         /*
  3591.         **  Cannot drop a language if the language has associated
  3592.         **  entries in the master.dbo.sysmessages.
  3593.         */
  3594.         select @msg = "Can't drop '" + @language + "' because there are associated entries in master.dbo.sysmessages.  Run sp_droplanguage with dropmessages flag."
  3595.         print @msg
  3596.         return 1
  3597.     end
  3598. end
  3599. else
  3600. begin
  3601.     if @dropmessages != "dropmessages"
  3602.     begin
  3603.         print "The only legal value for @dropmessages is 'dropmessages'."
  3604.         return 1
  3605.     end
  3606.  
  3607.     select @dropmsgs = 1
  3608. end
  3609.  
  3610. /*
  3611. **  Drop the language and delete messages from Sysmessages if there is any.
  3612. */
  3613. if @dropmsgs = 1
  3614. begin
  3615.     delete sysmessages
  3616.         where langid = @langid
  3617. end
  3618.  
  3619. delete syslanguages
  3620.     where langid = @langid
  3621.  
  3622. print "Language deleted."
  3623.  
  3624. go
  3625. /* setlangalias 27.1 3/5/90 */
  3626. create procedure sp_setlangalias
  3627. @language varchar(30),
  3628. @alias varchar(30)
  3629. as
  3630.  
  3631. declare @msg varchar(250)
  3632.  
  3633. /*  Check to see if the language exists. */
  3634. declare @returncode    int
  3635. select @returncode = 0
  3636. execute @returncode = sp_validlang @language
  3637. if @returncode != 0
  3638. begin
  3639.     select @msg = "'" + @language + "' is not a valid language."
  3640.     print @msg
  3641.     return 1
  3642. end
  3643.  
  3644. /*  Check to see if the alias exists. */
  3645. select @returncode = 0
  3646. execute @returncode = sp_validaltlang @alias
  3647. if @returncode = 0
  3648. begin
  3649.     select @msg = "'" + @alias + "' alias already exists in Syslanguages."
  3650.     print @msg
  3651.     return 1
  3652. end
  3653.  
  3654. /* Reset the alternate language name. */
  3655. update syslanguages
  3656.     set alias = @alias
  3657.         where name = @language
  3658.  
  3659. /* If the update failed, say so. */
  3660. if @@error != 0
  3661. begin
  3662.     print "Language alias not changed."
  3663.     return
  3664. end
  3665.  
  3666. print "Language alias reset."
  3667. go
  3668. dump transaction master with truncate_only
  3669. go
  3670. /* bindefault 27.1 3/5/90 */
  3671. create procedure sp_bindefault
  3672. @defname varchar(92),            /* name of the default */
  3673. @objname varchar(61),            /* table or usertype name */
  3674. @futureonly varchar(15) = NULL        /* flag to indicate extent of binding */
  3675. as
  3676.  
  3677. declare @defid int            /* id of the default to bind */
  3678. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  3679.                     ** the binding to be limited */
  3680.  
  3681. set nocount on
  3682.  
  3683. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  3684.  
  3685. /*
  3686. **  When a default or rule is bound to a user-defined datatype, it is also
  3687. **  bound, by default, to any columns of the user datatype that are currently
  3688. **  using the existing default or rule as their default or rule.  This default
  3689. **  action may be overridden by setting @futureonly = @futurevalue when the
  3690. **  procedure is invoked.  In this case existing columns with the user
  3691. **  datatype won't have their existing default or rule changed.
  3692. */
  3693.  
  3694. /*
  3695. **  Check to see that the object names are local to the current database.
  3696. */
  3697. if (@objname like "%.%.%")
  3698.    or
  3699.    (@defname like "%.%.%" and
  3700.     substring(@defname, 1, charindex(".", @defname) - 1) != db_name())
  3701. begin
  3702.     print "Default and table or usertype must be in 'current' database."
  3703.     return (1)
  3704. end
  3705.  
  3706. /*
  3707. **  Check that the @futureonly argument, if supplied, is correct.
  3708. */
  3709. if (@futureonly != null)
  3710. begin
  3711.     if (@futureonly != @futurevalue)
  3712.     begin
  3713.         print "Usage: bindefault defaultname, objectname [, 'futureonly']"
  3714.         return (1)
  3715.     end
  3716. end
  3717.  
  3718. /*
  3719. **  Check to see that the default exists and get it's id.
  3720. */
  3721. select @defid = id
  3722.     from sysobjects
  3723. where id = object_id(@defname)
  3724.     and sysstat & 7 = 6        /* default object */
  3725.  
  3726. if @defid is NULL
  3727. begin
  3728.     print "No such default exists.  You must create the default first."
  3729.     return (1)
  3730. end
  3731.  
  3732. /*
  3733. **  If @objname is of the form tab.col then we are binding to a column.
  3734. **  Otherwise its a datatype.  In the column case, we need to extract
  3735. **  and verify the table and column names and make sure the user owns
  3736. **  the table that is getting the default bound.
  3737. */
  3738. if @objname like "%.%"
  3739. begin
  3740.     declare @tabname varchar(30)        /* name of table */
  3741.     declare @colname varchar(30)        /* name of column */
  3742.  
  3743.     /*
  3744.     **  Get the table name out.
  3745.     */
  3746.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  3747.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  3748.  
  3749.     /*
  3750.     **  If the column type is timestamp, disallow the bind.
  3751.     **  Defaults can't be bound to timestamp columns.
  3752.     */
  3753.     if exists (select *
  3754.             from sysobjects o, syscolumns c
  3755.         where c.id = object_id(@tabname)
  3756.             and c.name = @colname
  3757.             and c.name = @colname
  3758.             and o.id = object_id(@tabname)
  3759.             and o.sysstat & 7 = 3
  3760.             and c.usertype = 80)
  3761.     begin
  3762.         print "You can't bind a default to a timestamp datatype column."
  3763.         return (1)
  3764.     end
  3765.  
  3766.     update syscolumns
  3767.         set cdefault = @defid
  3768.             from syscolumns, sysobjects
  3769.         where syscolumns.id = object_id(@tabname)
  3770.             and syscolumns.name = @colname
  3771.             and sysobjects.id = object_id(@tabname)
  3772.             and uid = user_id()
  3773.             and sysobjects.sysstat & 7 = 3      /* user table */
  3774.  
  3775.     /*
  3776.     **  Did the bind happen?
  3777.     */
  3778.     if @@rowcount != 1
  3779.     begin
  3780.         print "You do not own a column of that name."
  3781.         return (1)
  3782.     end
  3783.  
  3784.     /*
  3785.     **  Since binding a default is a schema change, update schema count
  3786.     **  for the object in the sysobjects table.
  3787.     */
  3788.     update sysobjects
  3789.         set schema = schema + 1
  3790.             from sysobjects
  3791.                 where id = object_id(@tabname)
  3792.                     and uid = user_id()
  3793.  
  3794.     print "Default bound to column."
  3795.  
  3796. end
  3797. else
  3798. begin
  3799.     /*
  3800.     **  We're binding to a user type.  In this case, the @objname
  3801.     **  is really the name of the user datatype.
  3802.     **  When we bind to a user type, any existing columns get changed
  3803.     **  to the new binding unless their current binding is not equal
  3804.     **  to the current binding for the usertype or if they set the
  3805.     **  @futureonly parameter to @futurevalue.
  3806.     */
  3807.     declare @olddefault int            /* current default for type */
  3808.  
  3809.     /*
  3810.     **  Get the current default for the datatype.
  3811.     */
  3812.     select @olddefault = tdefault
  3813.         from systypes
  3814.     where name = @objname
  3815.         and uid = user_id()
  3816.         and usertype > 100
  3817.  
  3818.     if @olddefault = null
  3819.     begin
  3820.         print "You do not own a datatype of that name."
  3821.         return (1)
  3822.     end
  3823.  
  3824.     update systypes
  3825.         set tdefault = @defid
  3826.             from systypes
  3827.         where name = @objname
  3828.             and uid = user_id()
  3829.             and usertype > 100
  3830.  
  3831.  
  3832.     print "Default bound to datatype."
  3833.  
  3834.     /*
  3835.     **  Now see if there are any columns with the usertype that
  3836.     **  need the new binding.
  3837.     */
  3838.     if isnull(@futureonly, "") != @futurevalue
  3839.         and exists (select *
  3840.             from syscolumns, systypes
  3841.                 where syscolumns.usertype = systypes.usertype
  3842.                     and systypes.name = @objname
  3843.                     and systypes.usertype > 100
  3844.                     and systypes.uid = user_id()
  3845.                     and (syscolumns.cdefault = @olddefault
  3846.                     or syscolumns.cdefault = 0 ) )
  3847.     begin
  3848.         /*
  3849.         **  Update syscolumns with new binding.
  3850.         */
  3851.         update syscolumns
  3852.             set cdefault = systypes.tdefault
  3853.                 from syscolumns, systypes
  3854.             where syscolumns.usertype = systypes.usertype
  3855.                 and systypes.name = @objname
  3856.                 and systypes.usertype > 100
  3857.                 and systypes.uid = user_id()
  3858.                                 and (syscolumns.cdefault = @olddefault
  3859.                                           or  syscolumns.cdefault = 0 )
  3860.  
  3861.         /*
  3862.         **  Update the table schema to indicate that something
  3863.         **  has changed in the table's schema.
  3864.         */
  3865.         update sysobjects
  3866.             set schema = schema + 1
  3867.                 from sysobjects, syscolumns, systypes
  3868.             where sysobjects.id = syscolumns.id
  3869.                 and syscolumns.usertype = systypes.usertype
  3870.                 and systypes.name = @objname
  3871.                 and systypes.usertype > 100
  3872.                 and systypes.uid = user_id()
  3873.                                 and (syscolumns.cdefault = @olddefault
  3874.                                           or  syscolumns.cdefault = 0 )
  3875.  
  3876.         print "The new default has been bound to columns(s) of the specified user datatype."
  3877.     end
  3878. end
  3879.  
  3880. return (0)
  3881. go
  3882. grant execute on sp_bindefault to public
  3883. go
  3884. /* bindrule 27.1 3/5/90 */
  3885. create procedure sp_bindrule
  3886. @rulename varchar(92),            /* name of the rule */
  3887. @objname varchar(61),            /* table or usertype name */
  3888. @futureonly varchar(15) = NULL        /* column name */
  3889. as
  3890.  
  3891. declare @ruleid int            /* id of the rule to bind */
  3892. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  3893.                     ** the binding to be limited */
  3894.  
  3895. set nocount on
  3896.  
  3897. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  3898.  
  3899. /*
  3900. **  When a default or rule is bound to a user-defined datatype, it is also
  3901. **  bound, by default, to any columns of the user datatype that are currently
  3902. **  using the existing default or rule as their default or rule.  This default
  3903. **  action may be overridden by setting @futureonly = @futurevalue when the
  3904. **  procedure is invoked.  In this case existing columns with the user
  3905. **  datatype won't have their existing default or rule changed.
  3906. */
  3907.  
  3908. /*
  3909. **  Check to see that the object names are local to the current database.
  3910. */
  3911. if (@objname like "%.%.%")
  3912.    or
  3913.    (@rulename like "%.%.%" and
  3914.     substring(@rulename, 1, charindex(".", @rulename) - 1) != db_name())
  3915. begin
  3916.     print "Rule and table or usertype must be in 'current' database."
  3917.     return (1)
  3918. end
  3919.  
  3920. /*
  3921. **  Check that the @futureonly argument, if supplied, is correct.
  3922. */
  3923. if (@futureonly != null)
  3924. begin
  3925.     if (@futureonly != @futurevalue)
  3926.     begin
  3927.         print "Usage: bindrule rulename, objectname [, 'futureonly']"
  3928.         return (1)
  3929.     end
  3930. end
  3931.  
  3932. /*
  3933. **  Check to see that the rule exists and get it's id.
  3934. */
  3935. select @ruleid = id
  3936.     from sysobjects
  3937. where id = object_id(@rulename)
  3938.     and sysstat & 7 = 7        /* rule object */
  3939.  
  3940. if @ruleid is NULL
  3941. begin
  3942.     print "No such rule exists.  You must create the rule first."
  3943.     return
  3944. end
  3945.  
  3946. /*
  3947. **  If @objname is of the form tab.col then we are binding to a column.
  3948. **  Otherwise its a datatype.  In the column case, we need to extract
  3949. **  and verify the table and column names and make sure the user owns
  3950. **  the table that is getting the rule bound.
  3951. */
  3952. if @objname like "%.%"
  3953. begin
  3954.     declare @tabname varchar(30)        /* name of table */
  3955.     declare @colname varchar(30)        /* name of column */
  3956.  
  3957.     /*
  3958.     **  Get the table name out.
  3959.     */
  3960.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  3961.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  3962.  
  3963.     /*
  3964.     **  If the column type is image, text, or timestamp, disallow the bind.
  3965.     **  Rules can't be bound to image, text, or timestamp columns.
  3966.     **  The qualification to check for types is a little strange because
  3967.     **  text and image are real, basic data types while timestamp is not.
  3968.     **  Timestamp is really a binary.  The types are checked in case
  3969.     **  there is a user-defined datatype that is an image or text.
  3970.     **  User-defined datatypes mapping to timestamp are not allowed
  3971.     **  by sp_addtype.
  3972.     */
  3973.     if exists (select *
  3974.             from sysobjects o, syscolumns c
  3975.         where c.id = object_id(@tabname)
  3976.             and c.name = @colname
  3977.             and c.name = @colname
  3978.             and o.id = object_id(@tabname)
  3979.             and o.sysstat & 7 = 3
  3980.             and (c.type in (35, 34) or c.usertype = 80))
  3981.     begin
  3982.         print "You can't bind a rule to a text, image, or timestamp datatype column."
  3983.         return (1)
  3984.     end
  3985.  
  3986.     update syscolumns
  3987.         set domain = @ruleid
  3988.             from syscolumns, sysobjects
  3989.         where syscolumns.id = object_id(@tabname)
  3990.             and syscolumns.name = @colname
  3991.             and sysobjects.id = object_id(@tabname)
  3992.             and uid = user_id()
  3993.             and sysobjects.sysstat & 7 = 3      /* user table */
  3994.  
  3995.     /*
  3996.     **  Did the bind happen?
  3997.     */
  3998.     if @@rowcount != 1
  3999.     begin
  4000.         print "You do not own a column of that name."
  4001.         return (1)
  4002.     end
  4003.  
  4004.     /*
  4005.     **  Since binding a rule is a schema change, update schema count
  4006.     **  for the object in the sysobjects table.
  4007.     */
  4008.     update sysobjects
  4009.         set schema = schema + 1
  4010.             from sysobjects
  4011.                 where id = object_id(@tabname)
  4012.                     and uid = user_id()
  4013.  
  4014.     print "Rule bound to table column."
  4015.  
  4016. end
  4017. else
  4018. begin
  4019.     /*
  4020.     **  We're binding to a user type.  In this case, the @objname
  4021.     **  is really the name of the user datatype.
  4022.     **  When we bind to a user type, any existing columns get changed
  4023.     **  to the new binding unless their current binding is not equal
  4024.     **  to the current binding for the usertype or if they set the
  4025.     **  @futureonly parameter to @futurevalue.
  4026.     */
  4027.     declare @oldrule int            /* current rule for type */
  4028.  
  4029.     /*
  4030.     **  If the column type is image, text, or timestamp, disallow the bind.
  4031.     **  Rules can't be bound to image or text columns.
  4032.     */
  4033.     if exists (select *
  4034.             from systypes
  4035.                 where name = @objname
  4036.                     and (type in (35, 34) or usertype = 80))
  4037.     begin
  4038.         print "You can't bind a rule to a text, image, or timestamp datatype."
  4039.         return (1)
  4040.     end
  4041.  
  4042.     /*
  4043.     **  Get the current rule for the datatype.
  4044.     */
  4045.     select @oldrule = domain
  4046.         from systypes
  4047.     where name = @objname
  4048.         and uid = user_id()
  4049.         and usertype > 100
  4050.  
  4051.     if @oldrule = null
  4052.     begin
  4053.         print "You do not own a datatype of that name."
  4054.         return (1)
  4055.     end
  4056.  
  4057.     update systypes
  4058.         set domain = @ruleid
  4059.             from systypes
  4060.         where name = @objname
  4061.             and uid = user_id()
  4062.             and usertype > 100
  4063.  
  4064.  
  4065.     print "Rule bound to datatype."
  4066.  
  4067.     /*
  4068.     **  Now see if there are any columns with the usertype that
  4069.     **  need the new binding.
  4070.     */
  4071.     if isnull(@futureonly, "") != @futurevalue
  4072.         and exists (select *
  4073.             from syscolumns, systypes
  4074.                 where syscolumns.usertype = systypes.usertype
  4075.                     and systypes.name = @objname
  4076.                     and systypes.usertype > 100
  4077.                     and systypes.uid = user_id()
  4078.                                         and (syscolumns.domain = @oldrule
  4079.                                           or  syscolumns.domain = 0 )     )
  4080.     begin
  4081.         print "The new rule has been bound to column(s) of the specified user datatype."
  4082.         /*
  4083.         **  Update syscolumns with new binding.
  4084.         */
  4085.         update syscolumns
  4086.             set domain = systypes.domain
  4087.                 from syscolumns, systypes
  4088.             where syscolumns.usertype = systypes.usertype
  4089.                 and systypes.name = @objname
  4090.                 and systypes.usertype > 100
  4091.                 and systypes.uid = user_id()
  4092.                                 and (syscolumns.domain = @oldrule
  4093.                                   or  syscolumns.domain = 0 )
  4094.  
  4095.         /*
  4096.         **  Update the table schema to indicate that something
  4097.         **  has changed in the table's schema.
  4098.         */
  4099.         update sysobjects
  4100.             set schema = schema + 1
  4101.                 from sysobjects, syscolumns, systypes
  4102.             where sysobjects.id = syscolumns.id
  4103.                 and syscolumns.usertype = systypes.usertype
  4104.                 and systypes.name = @objname
  4105.                 and systypes.usertype > 100
  4106.                 and systypes.uid = user_id()
  4107.                                 and (syscolumns.domain = @oldrule
  4108.                                   or  syscolumns.domain = 0 )
  4109.     end
  4110. end
  4111.  
  4112. return (0)
  4113. go
  4114. grant execute on sp_bindrule to public
  4115. go
  4116. dump transaction master with truncate_only
  4117. go
  4118. /* changedbowner 27.1 3/5/90 */
  4119. create procedure sp_changedbowner
  4120. @loginame varchar(30),         /* login to become dbo */
  4121. @map      varchar(10) = NULL   /* True to map aliases, else drop  */
  4122. as
  4123.  
  4124. declare @suid    smallint
  4125. declare @oldsuid smallint
  4126.  
  4127. /*
  4128. **  Only the SA can change the owner of a database.
  4129. */
  4130. if suser_id() != 1
  4131. begin
  4132.     print "Only the System Administrator (SA) can change to owner of a database."
  4133.     return(1)
  4134. end
  4135.  
  4136. /*
  4137. **  Can't change the owner of the master database.
  4138. */
  4139. if db_id() = 1
  4140. begin
  4141.     print "Can't change the owner of the master database."
  4142.     return(1)
  4143. end
  4144.  
  4145. /*
  4146. **  Make sure that @loginame exists and has a login.
  4147. */
  4148. select @suid = suid
  4149.     from master.dbo.syslogins
  4150.         where name = @loginame
  4151.  
  4152. if @suid = NULL
  4153. begin
  4154.     print "No login with the specified name exists."
  4155.     return(1)
  4156. end
  4157.  
  4158. /*
  4159. **  Make sure that @loginame isn't already a user or alias in the database.
  4160. */
  4161. if exists (select *
  4162.         from sysusers
  4163.             where suid = @suid)
  4164. begin
  4165.     print "The proposed new db owner already is a user in the database."
  4166.     return(1)
  4167. end
  4168. if exists (select *
  4169.         from sysalternates
  4170.             where suid = @suid)
  4171. begin
  4172.     print "The proposed new db owner already is aliased in the database."
  4173.     return(1)
  4174. end
  4175.  
  4176. /*
  4177. ** find old (current) dbo's suid
  4178. */
  4179. select @oldsuid = suid
  4180.     from sysusers
  4181.         where uid = 1
  4182.  
  4183. begin transaction
  4184.  
  4185. /*
  4186. **  Now change the suid of the owner of the database to the suid of @loginame.
  4187. */
  4188. update sysusers
  4189.     set suid = @suid
  4190.         where uid = 1
  4191.  
  4192. /*
  4193. ** if the user requested that aliases be mapped to new dbo, do that.
  4194. */
  4195. if @map = "true"
  4196. begin
  4197.  
  4198.     if exists (select *
  4199.             from sysalternates
  4200.                 where altsuid = @oldsuid)
  4201.     begin
  4202.         update sysalternates
  4203.             set altsuid = @suid
  4204.                 where altsuid = @oldsuid
  4205.  
  4206.         print "The dependent aliases were mapped to the new dbo."
  4207.     end
  4208. end
  4209. /* else drop the aliases to the old dbo */
  4210. else
  4211. begin
  4212.  
  4213.     if exists (select *
  4214.             from sysalternates
  4215.                 where altsuid = @oldsuid)
  4216.     begin
  4217.         delete from sysalternates
  4218.             where altsuid = @oldsuid
  4219.  
  4220.         print "The dependent aliases were dropped."
  4221.     end
  4222. end
  4223.  
  4224.  
  4225. /*
  4226. **  Reflect the new owner of the database in master.dbo.sysdatabases.
  4227. */
  4228. update master.dbo.sysdatabases
  4229.     set suid = @suid
  4230.         where dbid = db_id()
  4231.  
  4232. commit transaction
  4233.  
  4234. /*
  4235. **  Checkpoint the database so that the in-memory data structure for the
  4236. **  database will be consistent with sysdatabases.
  4237. */
  4238. checkpoint
  4239.  
  4240. print "Database owner changed."
  4241.  
  4242. return (0)
  4243. go
  4244. /* changegroup 27.1 3/5/90 */
  4245. create procedure sp_changegroup
  4246. @grpname varchar(30),            /* group name */
  4247. @username varchar(30)            /* user name to add to group */
  4248. as
  4249.  
  4250. declare @gid int            /* group id */
  4251. declare @uid int            /* user id */
  4252.  
  4253. /*
  4254. **  You must be SA or the dbo to execute this sproc.
  4255. */
  4256. if suser_id() != 1 and user_id() != 1
  4257. begin
  4258.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  4259.     return (1)
  4260. end
  4261.  
  4262. /*
  4263. **  See if the group name exists.
  4264. */
  4265. select @gid = uid
  4266.     from sysusers
  4267. where name = @grpname
  4268.     and (uid > 16383 or uid = 0)
  4269.  
  4270. /*
  4271. **  If no such group, quit.
  4272. **  User ids are <= 16383 and group ids are > 16383 or = 0.
  4273. */
  4274. if @gid = NULL
  4275. begin
  4276.     print "No such group exists."
  4277.     return (1)
  4278. end
  4279.  
  4280. /*
  4281. **  See if the user name exists.
  4282. */
  4283. select @uid = uid
  4284.     from sysusers
  4285. where name = @username
  4286.     and (uid <= 16383 and uid > 0)
  4287.  
  4288. /*
  4289. **  If no such user in the database, quit.
  4290. */
  4291. if @uid = NULL
  4292. begin
  4293.     print "No such user exists."
  4294.     return (1)
  4295. end
  4296.  
  4297. /*
  4298. **  Everything is consistent so change the group.
  4299. */
  4300. update sysusers
  4301.     set gid = @gid
  4302.         from sysusers
  4303.     where uid = @uid
  4304.  
  4305. /*
  4306. **  We need to invalidate the protection cache since objects have
  4307. **  changed ownership.  This command will invalidate the current
  4308. **  protection cache so when protections are checked the new and
  4309. **  correct protections will be used.
  4310. */
  4311. grant all to null
  4312.  
  4313. print "Group changed."
  4314.  
  4315. return (0)
  4316. go
  4317. grant execute on sp_changegroup to public
  4318. go
  4319. dump transaction master with truncate_only
  4320. go
  4321. /* checknames 27.1 3/5/90 */
  4322. create procedure sp_checknames
  4323. @mode varchar(20) = NULL        /* mode of operation; e.g. 'silent' */
  4324. as
  4325.  
  4326. declare @msilent    int        /* set to 1 if 'silent' mode is on */
  4327. declare @ret_val    int        /* set to 1 if we find funny char */
  4328. declare @codepoint    tinyint        /* set to 1 if we find funny char */
  4329. declare @dbname        varchar(30)    /* holds database name */
  4330. declare @msg        varchar(90)    /* used for messages to user */
  4331. declare @pat        varchar(132)    /* holds the pattern to search for */
  4332.  
  4333. set nocount on
  4334.  
  4335. if (@mode like '%help%')
  4336. begin
  4337.     print 'sp_checknames is used to search for non 7-bit ASCII characters'
  4338.     print 'in several important columns of system tables.  The following'
  4339.     print 'columns are searched:'
  4340.     print ' '
  4341.     print '    In "master":'
  4342.     print '        sysdatabases.name'
  4343.     print '        sysdevices.name'
  4344.     print '        syslogins.name'
  4345.     print '        syslogins.dbname'
  4346.     print '        syslogins.password'
  4347.     print '        sysremotelogins.remoteusername'
  4348.     print '        sysservers.srvname'
  4349.     print '        sysservers.srvnetname'
  4350.     print ' '
  4351.     print '    In all databases:'
  4352.     print '        syscolumns.name'
  4353.     print '        sysindexes.name'
  4354.     print '        sysobjects.name'
  4355.     print '        syssegments.name'
  4356.     print '        systypes.name'
  4357.     print '        sysusers.name'
  4358.     print ' '
  4359.  
  4360.     return (0)
  4361. end
  4362.  
  4363. /*
  4364. **  First, initialize return value, and set up mode variables:
  4365. */
  4366. select @ret_val = 0
  4367.  
  4368. if (@mode like '%silent%')
  4369.     select @msilent = 1
  4370. else
  4371.     select @msilent = 0
  4372.  
  4373.  
  4374. /*
  4375. **  Now, initialize the pattern string we will search for:
  4376. */
  4377. select @pat = '%[', @codepoint = 127
  4378. while (@codepoint < 255)
  4379. begin
  4380.     select @codepoint = @codepoint + 1
  4381.     select @pat = @pat + char(@codepoint)
  4382. end
  4383. select @pat = @pat + ']%'
  4384.  
  4385.  
  4386. /*
  4387. **  Get the database name we are in:
  4388. */
  4389. select @dbname = db_name()
  4390.  
  4391. if (@msilent = 0)
  4392. begin
  4393.     print ' '
  4394.     select @msg = 'Looking for non 7-bit ASCII characters in the system ' +
  4395.             'tables of database:  "' + @dbname + '"'
  4396.     print @msg
  4397.     print ' '
  4398. end
  4399.  
  4400.  
  4401. /*
  4402. **  Look through these only if in the master database:
  4403. */
  4404. if (@dbname = 'master')
  4405. begin
  4406.     if exists (select name from master.dbo.sysdatabases
  4407.             where name like @pat)
  4408.     begin
  4409.     if (@msilent = 1)
  4410.         return (1)
  4411.  
  4412.     select @ret_val = 1
  4413.     print ' '
  4414.     print '==============================================================='
  4415.     print 'Table.Column name:  "sysdatabases.name"'
  4416.     print ' '
  4417.     print 'The following database names contain non 7-bit ASCII characters.'
  4418.     print 'If you wish to change their names, use "sp_renamedb":'
  4419.     print ' '
  4420.     select dbid,name from master.dbo.sysdatabases
  4421.             where name like @pat
  4422.     end
  4423.  
  4424.     if exists (select name from master.dbo.sysdevices
  4425.             where name like @pat)
  4426.     begin
  4427.     if (@msilent = 1)
  4428.         return (1)
  4429.  
  4430.     select @ret_val = 1
  4431.     print ' '
  4432.     print '==============================================================='
  4433.     print 'Table.Column name:  "sysdevices.name"'
  4434.     print ' '
  4435.     print 'The following device names contain non 7-bit ASCII characters.'
  4436.     print 'If you wish to change their names, use "UPDATE":'
  4437.         print ' '
  4438.     select name from master.dbo.sysdevices
  4439.             where name like @pat
  4440.     end
  4441.  
  4442.     if exists (select name from master.dbo.syslogins
  4443.             where name like @pat)
  4444.     begin
  4445.     if (@msilent = 1)
  4446.         return (1)
  4447.  
  4448.     select @ret_val = 1
  4449.     print ' '
  4450.     print '==============================================================='
  4451.     print 'Table.Column name:  "syslogins.name"'
  4452.         print ' '
  4453.     print 'The following login names contain non 7-bit ASCII characters.'
  4454.     print 'If you wish to change these names, use "sp_droplogin" and'
  4455.     print '"sp_addlogin":'
  4456.         print ' '
  4457.     select suid,name from master.dbo.syslogins
  4458.             where name like @pat
  4459.     end
  4460.  
  4461.     if exists (select dbname from master.dbo.syslogins
  4462.             where dbname 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.dbname"'
  4471.     print ' '
  4472.     print 'The following logins have default database names that contain'
  4473.     print 'non 7-bit ASCII characters.  If you wish to change them use'
  4474.     print '"sp_defaultdb":'
  4475.         print ' '
  4476.     select suid,name,dbname from master.dbo.syslogins
  4477.             where dbname like @pat
  4478.     end
  4479.  
  4480.     if exists (select password from master.dbo.syslogins
  4481.             where password 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.password"'
  4490.     print ' '
  4491.     print 'The following logins have passwords that contain non 7-bit'
  4492.     print 'ASCII characters.  If you wish to change them use "sp_password";'
  4493.     print 'Remember, only the sa and the login itself may examine or change'
  4494.     print 'the syslogins.password column:'
  4495.         print ' '
  4496.     select suid,name from master.dbo.syslogins
  4497.             where password like @pat
  4498.     end
  4499.  
  4500.     if exists (select remoteusername from master.dbo.sysremotelogins
  4501.             where remoteusername like @pat)
  4502.     begin
  4503.     if (@msilent = 1)
  4504.         return (1)
  4505.  
  4506.     select @ret_val = 1
  4507.     print ' '
  4508.     print '==============================================================='
  4509.     print 'Table.Column name:  "sysremotelogins.remoteusername"'
  4510.         print ' '
  4511.     print 'The following remote login names contain non 7-bit ASCII'
  4512.     print 'characters.  If you wish to change these names, use'
  4513.     print '"sp_dropremotelogin" and "sp_addremotelogin":'
  4514.         print ' '
  4515.     select remoteserverid,remoteusername from master.dbo.sysremotelogins
  4516.             where remoteusername like @pat
  4517.     end
  4518.  
  4519.     if exists (select srvname from master.dbo.sysservers
  4520.             where srvname 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:  "sysservers.srvname"'
  4529.         print ' '
  4530.     print 'The following server names contain non 7-bit ASCII characters.'
  4531.     print 'If you wish to change their names, use "sp_dropserver" and'
  4532.     print '"sp_addserver":'
  4533.         print ' '
  4534.     select srvid,srvname from master.dbo.sysservers
  4535.             where srvname like @pat
  4536.     end
  4537.  
  4538.     if exists (select srvnetname from master.dbo.sysservers
  4539.             where srvnetname 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.srvnetname"'
  4548.         print ' '
  4549.     print 'The following servers have "initialization file" names that contain'
  4550.     print 'non 7-bit ASCII characters.  If you wish to change these names,'
  4551.     print 'use "UPDATE":'
  4552.         print ' '
  4553.     select srvid,srvname,srvnetname from master.dbo.sysservers
  4554.             where srvnetname like @pat
  4555.     end
  4556.  
  4557. end
  4558.  
  4559.  
  4560. /*
  4561. **  For *ALL* databases, we want to look through these:
  4562. */
  4563. if exists (select name from dbo.syscolumns
  4564.         where name like @pat)
  4565. begin
  4566.     if (@msilent = 1)
  4567.     return (1)
  4568.  
  4569.     select @ret_val = 1
  4570.     print ' '
  4571.     print '==============================================================='
  4572.     print 'Table.Column name:  "syscolumns.name"'
  4573.     print ' '
  4574.     print 'The following column and parameter names contain non 7-bit ASCII'
  4575.     print 'characters.  If you wish to change these names, use "sp_rename":'
  4576.     print ' '
  4577.     select objname=o.name,colname=c.name from dbo.syscolumns c, dbo.sysobjects o
  4578.         where c.name like @pat and o.id = c.id
  4579. end
  4580.  
  4581. if exists (select name from dbo.sysindexes
  4582.         where name like @pat
  4583.               and indid > 0)
  4584. begin
  4585.     if (@msilent = 1)
  4586.     return (1)
  4587.  
  4588.     select @ret_val = 1
  4589.     print ' '
  4590.     print '==============================================================='
  4591.     print 'Table.Column name:  "sysindexes.name"'
  4592.     print ' '
  4593.     print 'The following index names contain non 7-bit ASCII characters.'
  4594.     print 'If you wish to change these names, use "UPDATE":'
  4595.     print ' '
  4596.     select id,indid,name from dbo.sysindexes
  4597.         where name like @pat
  4598.         and indid > 0
  4599. end
  4600.  
  4601. if exists (select name from dbo.sysobjects
  4602.         where name like @pat)
  4603. begin
  4604.     if (@msilent = 1)
  4605.     return (1)
  4606.  
  4607.     select @ret_val = 1
  4608.     print ' '
  4609.     print '==============================================================='
  4610.     print 'Table.Column name:  "sysobjects.name"'
  4611.     print ' '
  4612.     print 'The following object names contain non 7-bit ASCII characters.'
  4613.     print 'If you wish to change these names, use "sp_rename":'
  4614.     print ' '
  4615.     select owner=u.name,o.name from dbo.sysobjects o,dbo.sysusers u
  4616.         where o.name like @pat and o.uid=u.uid
  4617. end
  4618.  
  4619. if exists (select name from dbo.syssegments
  4620.         where name like @pat)
  4621. begin
  4622.     if (@msilent = 1)
  4623.     return (1)
  4624.  
  4625.     select @ret_val = 1
  4626.     print ' '
  4627.     print '==============================================================='
  4628.     print 'Table.Column name:  "syssegments.name"'
  4629.     print ' '
  4630.     print 'The following segment names contain non 7-bit ASCII characters.'
  4631.     print 'If you wish to change these names, use "UPDATE":'
  4632.     print ' '
  4633.     select segment,name from dbo.syssegments
  4634.         where name like @pat
  4635. end
  4636.  
  4637. if exists (select name from dbo.systypes
  4638.         where name like @pat)
  4639. begin
  4640.     if (@msilent = 1)
  4641.     return (1)
  4642.  
  4643.     select @ret_val = 1
  4644.     print ' '
  4645.     print '==============================================================='
  4646.     print 'Table.Column name:  "systypes.name"'
  4647.     print ' '
  4648.     print 'The following datatype names contain non 7-bit ASCII characters.'
  4649.     print 'If you wish to change these names, use "sp_rename":'
  4650.     print ' '
  4651.     select name from dbo.systypes
  4652.         where name like @pat
  4653. end
  4654.  
  4655. if exists (select name from dbo.sysusers
  4656.         where name like @pat)
  4657. begin
  4658.     if (@msilent = 1)
  4659.     return (1)
  4660.  
  4661.     select @ret_val = 1
  4662.     print ' '
  4663.     print '==============================================================='
  4664.     print 'Table.Column name:  "sysusers.name"'
  4665.     print ' '
  4666.     print 'The following user or group names contain non 7-bit ASCII'
  4667.     print 'characters.  If you wish to change these names, use "UPDATE":'
  4668.     print ' '
  4669.     select suid,uid,name from dbo.sysusers
  4670.         where name like @pat
  4671. end
  4672.  
  4673. if (@ret_val = 0  and  @msilent = 0)
  4674. begin
  4675.     select @msg = 'Good news!  Database "' + @dbname + '" has no obj/user/etc.'
  4676.     print @msg
  4677.     print 'names that contain non 7-bit ASCII characters.'
  4678. end
  4679.  
  4680. return (@ret_val)
  4681. go
  4682. grant execute on sp_checknames to public
  4683. go
  4684. CREATE TABLE    spt_committab
  4685. (
  4686.     commid    int, /* id used to refer to transaction */
  4687.     start    datetime, /* time transaction started */
  4688.     lastchange datetime, /* last time this row was updated */
  4689.     totnum    int,       /* number of servers
  4690.               ** initially involved in xact
  4691.               */
  4692.     outnum    int,       /* number of servers
  4693.               ** who still have not completed
  4694.               */
  4695.     status    char(1), /* 'a'- abort, 'c'- commit, 'b'- begin */
  4696.     applname varchar(30), /* application name */
  4697.     xactname varchar(30), /* transaction name */
  4698.     password varchar(30) NULL /* password protection for updating */
  4699. )
  4700. go
  4701. create unique clustered index commitclust on spt_committab(commid)
  4702. go
  4703.  
  4704. create procedure sp_start_xact
  4705.     @applname varchar(30),
  4706.     @xactname varchar(30),
  4707.     @count      int,
  4708.     @password varchar(30) = NULL
  4709. AS
  4710.  
  4711. set nocount on
  4712.  
  4713. BEGIN
  4714.     BEGIN TRANSACTION
  4715.         INSERT spt_committab VALUES
  4716.         (
  4717.             0,
  4718.             getdate(),
  4719.             getdate(),
  4720.             @count,
  4721.             @count,
  4722.             'b',
  4723.             @applname,
  4724.             @xactname,
  4725.             @password
  4726.         )
  4727.  
  4728.         UPDATE spt_committab
  4729.             set commid = 1 + (select max(commid) from spt_committab)
  4730.             where commid = 0
  4731.  
  4732.         SELECT max(commid) from spt_committab
  4733.     COMMIT TRANSACTION
  4734. END
  4735. go
  4736.  
  4737. create procedure sp_commit_xact
  4738.     @commid    int,
  4739.     @password varchar(30) = NULL
  4740. AS
  4741. if exists (select *
  4742.     from spt_committab
  4743.     where commid = @commid
  4744.     and password = @password)
  4745. begin
  4746.     /*
  4747.     **  If status is aborted, return a 1 to indicate a failure.
  4748.     */
  4749.     if exists (select *
  4750.             from spt_committab
  4751.                 where commid = @commid
  4752.                     and status = 'a')
  4753.     begin
  4754.         select 1
  4755.         return (1)
  4756.     end
  4757.  
  4758.     /*  Mark the xact as committed. */
  4759.     UPDATE spt_committab
  4760.         SET status = 'c', lastchange = getdate()
  4761.         WHERE commid = @commid
  4762.     select 0
  4763.     return (0)
  4764. end
  4765. else
  4766. begin
  4767.     /* Error so return 1 */
  4768.     raiserror 30000 "Commit service xact id doesn't exist."
  4769.     select 1
  4770.     return (1)
  4771. end
  4772. go
  4773.  
  4774. CREATE PROCEDURE sp_abort_xact
  4775.     @commid    int,
  4776.     @password varchar(30) = NULL
  4777. AS
  4778. if exists (select *
  4779.     from spt_committab
  4780.     where commid = @commid
  4781.     and password = @password)
  4782. begin
  4783.     UPDATE spt_committab
  4784.         SET status = 'a' , lastchange = getdate()
  4785.         WHERE commid = @commid
  4786. END
  4787. else
  4788. begin
  4789.     raiserror 30001 "Commit service xact id doesn't exist."
  4790. end
  4791. go
  4792.  
  4793. CREATE PROCEDURE sp_remove_xact
  4794.     @commid    int,
  4795.     @count    int,
  4796.     @password varchar(30) = NULL
  4797. AS
  4798. if exists (select *
  4799.     from spt_committab
  4800.     where commid = @commid
  4801.     and password = @password)
  4802. begin
  4803.     /*
  4804.     ** Decrement outnum by @count.
  4805.     */
  4806.     UPDATE spt_committab
  4807.         SET outnum = outnum - @count, lastchange = GETDATE()
  4808.         WHERE commid = @commid
  4809.  
  4810.     /*
  4811.     **  Delete the row if outnum < 1
  4812.     */
  4813.     DELETE spt_committab
  4814.         WHERE commid = @commid
  4815.             and outnum < 1
  4816.  
  4817. end
  4818. else
  4819. begin
  4820.     raiserror 30002 "Commit service xact id doesn't exist."
  4821. end
  4822. go
  4823.  
  4824. create procedure sp_stat_xact
  4825.     @commid    int
  4826. AS
  4827. BEGIN
  4828.     IF EXISTS
  4829.     (
  4830.         SELECT status
  4831.             FROM spt_committab
  4832.             WHERE commid = @commid
  4833.     )
  4834.         SELECT status
  4835.             FROM spt_committab
  4836.             WHERE commid = @commid
  4837.     ELSE
  4838.         SELECT status = 'u'
  4839. END
  4840. go
  4841.  
  4842. create procedure sp_probe_xact
  4843.     @commid    int
  4844. AS
  4845. BEGIN
  4846.     declare @stat    char(1)
  4847.  
  4848.     begin tran
  4849.         IF EXISTS ( SELECT status
  4850.                 FROM spt_committab
  4851.                     WHERE commid = @commid)
  4852.         begin
  4853.             SELECT @stat = status
  4854.                 FROM spt_committab
  4855.                     WHERE commid = @commid
  4856.             
  4857.             if (@stat != 'c')
  4858.             begin
  4859.                 update spt_committab
  4860.                     set status = 'a'
  4861.                         where commid = @commid
  4862.                 select status = 'a'
  4863.             end
  4864.             else select status = @stat
  4865.         end
  4866.         else select status = 'u'
  4867.     commit tran
  4868.  
  4869. END
  4870. go
  4871.  
  4872. create procedure sp_scan_xact
  4873.     @commid    int = NULL
  4874. as
  4875. BEGIN
  4876.     IF    @commid = -1 or @commid is NULL
  4877.  
  4878.         SELECT commid,start,lastchange,totnum,outnum,
  4879.                status,applname,xactname
  4880.         FROM spt_committab
  4881.     ELSE
  4882.         SELECT commid,start,lastchange,totnum,outnum,
  4883.                status,applname,xactname
  4884.         FROM spt_committab
  4885.         WHERE @commid = commid
  4886. END
  4887. go
  4888. go
  4889. grant execute on sp_start_xact to public
  4890. go
  4891. grant execute on sp_commit_xact to public
  4892. go
  4893. grant execute on sp_abort_xact to public
  4894. go
  4895. grant execute on sp_remove_xact to public
  4896. go
  4897. grant execute on sp_stat_xact to public
  4898. go
  4899. grant execute on sp_probe_xact to public
  4900. go
  4901. grant execute on sp_scan_xact to public
  4902. go
  4903. dump transaction master with truncate_only
  4904. go
  4905. /* commonkey 27.1 3/5/90 */
  4906. create procedure sp_commonkey
  4907. @tabaname  varchar(92),        /* name of first table in the key */
  4908. @tabbname varchar(92),        /* name of second table in the key */
  4909. @col1a     varchar(30),     /* first column name of first table */
  4910. @col1b     varchar(30),     /* first column name of second table */
  4911. @col2a     varchar(30) = NULL,
  4912. @col2b     varchar(30) = NULL,
  4913. @col3a     varchar(30) = NULL,
  4914. @col3b     varchar(30) = NULL,
  4915. @col4a     varchar(30) = NULL,
  4916. @col4b     varchar(30) = NULL,
  4917. @col5a     varchar(30) = NULL,
  4918. @col5b     varchar(30) = NULL,
  4919. @col6a     varchar(30) = NULL,
  4920. @col6b     varchar(30) = NULL,
  4921. @col7a     varchar(30) = NULL,
  4922. @col7b     varchar(30) = NULL,
  4923. @col8a     varchar(30) = NULL,
  4924. @col8b     varchar(30) = NULL
  4925. as
  4926.  
  4927. declare @objida int        /* id of table we are doing */
  4928. declare @objidb int        /* id of table with primary key */
  4929. declare @uida smallint        /* owner of the first table */
  4930. declare @cnt    int        /* how many columns are in the key */
  4931.  
  4932. declare @key1a int        /* colids of the first table */
  4933. declare @key2a int
  4934. declare @key3a int
  4935. declare @key4a int
  4936. declare @key5a int
  4937. declare @key6a int
  4938. declare @key7a int
  4939. declare @key8a int
  4940.  
  4941. declare @key1b int        /* colids of the second table */
  4942. declare @key2b int
  4943. declare @key3b int
  4944. declare @key4b int
  4945. declare @key5b int
  4946. declare @key6b int
  4947. declare @key7b int
  4948. declare @key8b int
  4949.  
  4950. /*
  4951. **  Check to see that the tabnames are local.
  4952. */
  4953. if @tabaname like "%.%.%"
  4954. begin
  4955.     if substring(@tabaname, 1, charindex(".", @tabaname) - 1) != db_name()
  4956.     begin
  4957.         print "Table or view name must be in 'current' database."
  4958.         return (1)
  4959.     end
  4960. end
  4961. if @tabbname like "%.%.%"
  4962. begin
  4963.     if substring(@tabbname, 1, charindex(".", @tabbname) - 1) != db_name()
  4964.     begin
  4965.         print "Table or view name must be in 'current' database."
  4966.         return (1)
  4967.     end
  4968. end
  4969.  
  4970. /*
  4971. **  See if we can find the objects.  They must be a system table, user table,
  4972. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the
  4973. **  object type is -- it's more reliable than using sysobjects.type which
  4974. **  could change.
  4975. */
  4976. select @objida = id, @uida = uid
  4977.     from sysobjects
  4978.         where id = object_id(@tabaname)
  4979.             and (sysstat & 7 = 1        /* system table */
  4980.                 or sysstat & 7 = 2    /* view */
  4981.                 or sysstat & 7 = 3)    /* user table */
  4982. /*
  4983. **  If either of the tables don't exist, quit.
  4984. */
  4985. if @objida is NULL
  4986. begin
  4987.     print "First table in the common key doesn't exist."
  4988.     return (1)
  4989. end
  4990. select @objidb = id
  4991.     from sysobjects
  4992.         where id = object_id(@tabbname)
  4993.             and (sysstat & 7 = 1
  4994.                 or sysstat & 7 = 2
  4995.                 or sysstat & 7 = 3)
  4996. if @objidb is NULL
  4997. begin
  4998.     print "Second table in the common key doesn't exist."
  4999.     return (1)
  5000. end
  5001.  
  5002. /*
  5003. **  Only the owner of the table can define it's common keys.
  5004. */
  5005. if @uida != user_id()
  5006. begin
  5007.     print "Only the table owner may define its common keys."
  5008.     return (1)
  5009. end
  5010.  
  5011. /*
  5012. **  Now check to see that the first key columns exist and have compatible types.
  5013. */
  5014. select @cnt = 1, @key1a = a.colid, @key1b = b.colid
  5015.     from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5016.         master.dbo.spt_values z
  5017.         where a.name = @col1a
  5018.             and a.id = @objida
  5019.             and b.name = @col1b
  5020.             and b.id = @objidb
  5021.             and y.type = "J"
  5022.             and a.type = y.low
  5023.             and z.type = "J"
  5024.             and b.type = z.low
  5025.             and y.number = z.number
  5026. if @key1a = NULL
  5027. begin
  5028.     print "The tables have no such first column or the columns are of different types."
  5029.     return (1)
  5030. end
  5031.  
  5032. if @col2a != NULL
  5033. begin
  5034.     select @cnt = @cnt + 1, @key2a = a.colid, @key2b = b.colid
  5035.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5036.             master.dbo.spt_values z
  5037.             where a.name = @col2a
  5038.                 and a.id = @objida
  5039.                 and b.name = @col2b
  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 @key2a = NULL
  5047.     begin
  5048.         print "The tables have no such second column or the columns are of different types."
  5049.         return (1)
  5050.     end
  5051. end
  5052. else goto keys_ok
  5053.  
  5054. if @col3a != NULL
  5055. begin
  5056.     select @cnt = @cnt + 1, @key3a = a.colid, @key3b = b.colid
  5057.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5058.             master.dbo.spt_values z
  5059.             where a.name = @col3a
  5060.                 and a.id = @objida
  5061.                 and b.name = @col3b
  5062.                 and b.id = @objidb
  5063.                 and y.type = "J"
  5064.                 and a.type = y.low
  5065.                 and z.type = "J"
  5066.                 and b.type = z.low
  5067.                 and y.number = z.number
  5068.     if @key3a = NULL
  5069.     begin
  5070.         print "The tables have no such third column or the columns are of different types."
  5071.         return (1)
  5072.     end
  5073. end
  5074. else goto keys_ok
  5075.  
  5076. if @col4a != NULL
  5077. begin
  5078.     select @cnt = @cnt + 1, @key4a = a.colid, @key4b = b.colid
  5079.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5080.             master.dbo.spt_values z
  5081.             where a.name = @col4a
  5082.                 and a.id = @objida
  5083.                 and b.name = @col4b
  5084.                 and b.id = @objidb
  5085.                 and y.type = "J"
  5086.                 and a.type = y.low
  5087.                 and z.type = "J"
  5088.                 and b.type = z.low
  5089.                 and y.number = z.number
  5090.     if @key4a = NULL
  5091.     begin
  5092.         print "The tables have no such fourth column or the columns are of different types."
  5093.         return (1)
  5094.     end
  5095. end
  5096. else goto keys_ok
  5097.  
  5098. if @col5a != NULL
  5099. begin
  5100.     select @cnt = @cnt + 1, @key5a = a.colid, @key5b = b.colid
  5101.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5102.             master.dbo.spt_values z
  5103.             where a.name = @col5a
  5104.                 and a.id = @objida
  5105.                 and b.name = @col5b
  5106.                 and b.id = @objidb
  5107.                 and y.type = "J"
  5108.                 and a.type = y.low
  5109.                 and z.type = "J"
  5110.                 and b.type = z.low
  5111.                 and y.number = z.number
  5112.     if @key5a = NULL
  5113.     begin
  5114.         print "The tables have no such fifth column or the columns are of different types."
  5115.         return (1)
  5116.     end
  5117. end
  5118. else goto keys_ok
  5119.  
  5120. if @col6a != NULL
  5121. begin
  5122.     select @cnt = @cnt + 1, @key6a = a.colid, @key6b = b.colid
  5123.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5124.             master.dbo.spt_values z
  5125.             where a.name = @col6a
  5126.                 and a.id = @objida
  5127.                 and b.name = @col6b
  5128.                 and b.id = @objidb
  5129.                 and y.type = "J"
  5130.                 and a.type = y.low
  5131.                 and z.type = "J"
  5132.                 and b.type = z.low
  5133.                 and y.number = z.number
  5134.     if @key6a = NULL
  5135.     begin
  5136.         print "The tables have no such sixth column or the columns are of different types."
  5137.         return (1)
  5138.     end
  5139. end
  5140. else goto keys_ok
  5141.  
  5142. if @col7a != NULL
  5143. begin
  5144.     select @cnt = @cnt + 1, @key7a = a.colid, @key7b = b.colid
  5145.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5146.             master.dbo.spt_values z
  5147.             where a.name = @col7a
  5148.                 and a.id = @objida
  5149.                 and b.name = @col7b
  5150.                 and b.id = @objidb
  5151.                 and y.type = "J"
  5152.                 and a.type = y.low
  5153.                 and z.type = "J"
  5154.                 and b.type = z.low
  5155.                 and y.number = z.number
  5156.     if @key7a = NULL
  5157.     begin
  5158.         print "The tables have no such seventh column or the columns are of different types."
  5159.         return (1)
  5160.     end
  5161. end
  5162. else goto keys_ok
  5163.  
  5164. if @col8a != NULL
  5165. begin
  5166.     select @cnt = @cnt + 1, @key8a = a.colid, @key8b = b.colid
  5167.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  5168.             master.dbo.spt_values z
  5169.             where a.name = @col8a
  5170.                 and a.id = @objida
  5171.                 and b.name = @col8b
  5172.                 and b.id = @objidb
  5173.                 and y.type = "J"
  5174.                 and a.type = y.low
  5175.                 and z.type = "J"
  5176.                 and b.type = z.low
  5177.                 and y.number = z.number
  5178.     if @key8a = NULL
  5179.     begin
  5180.         print "The tables have no such eighth column or the columns are of different types."
  5181.         return (1)
  5182.     end
  5183. end
  5184.  
  5185. /*
  5186. **  If we made it this far then all the columns for the common key are ok.
  5187. **  Everything is consistent so add the common key to syskeys.
  5188. */
  5189. keys_ok:
  5190.  
  5191. insert syskeys (id, type, depid, keycnt, size, key1, key2, key3, key4, key5,
  5192.     key6, key7, key8, depkey1, depkey2, depkey3, depkey4, depkey5,
  5193.     depkey6, depkey7, depkey8)
  5194. values (@objida, 3, @objidb, @cnt, 0, @key1a, @key2a, @key3a, @key4a, @key5a,
  5195.     @key6a, @key7a, @key8a, @key1b, @key2b, @key3b, @key4b, @key5b,
  5196.     @key6b, @key7b, @key8b)
  5197.  
  5198. print "New common key added."
  5199.  
  5200. return (0)
  5201. go
  5202. grant execute on sp_commonkey to public
  5203. go
  5204. /* configure 27.2 5/11/90 */
  5205. create procedure sp_configure
  5206. @configname varchar(20) = NULL,        /* option name to configure */
  5207. @configvalue int = NULL            /* configure value */
  5208. as
  5209.  
  5210. declare @confignum int            /* number of option to be configured */
  5211. declare @configcount int        /* number of options like @configname */
  5212.  
  5213. /*
  5214. **  If no option name is given, the procedure will just print out all the
  5215. **  options and their values.
  5216. */
  5217. if @configname is NULL
  5218. begin
  5219.     select name, minimum = low, maximum = high,
  5220.         config_value = sysconfigures.value,
  5221.         run_value = syscurconfigs.value
  5222.     from spt_values, sysconfigures, syscurconfigs
  5223.         where type = "C"
  5224.             and number *= sysconfigures.config
  5225.             and number *= syscurconfigs.config
  5226.             and number >= 0
  5227.  
  5228.     return (0)
  5229. end
  5230.  
  5231. set nocount on
  5232.  
  5233. /*
  5234. **  Use @configname and try to find the right option.
  5235. **  If there isn't just one, print appropriate diagnostics and return.
  5236. */
  5237. select @configcount = count(*)
  5238.         from spt_values
  5239.         where name like "%" + @configname + "%" and type = "C"
  5240.             and number >= 0
  5241.  
  5242. /*
  5243. **  If no option, show the user what the options are.
  5244. */
  5245. if @configcount = 0
  5246. begin
  5247.     print "Configuration option doesn't exist."
  5248.  
  5249.     /*
  5250.     ** Show the user what the options are.
  5251.     */
  5252.     select name, minimum = low, maximum = high,
  5253.         config_value = sysconfigures.value,
  5254.         run_value = syscurconfigs.value
  5255.     from spt_values, sysconfigures, syscurconfigs
  5256.         where type = "C"
  5257.             and number *= sysconfigures.config
  5258.             and number *= syscurconfigs.config
  5259.             and number >= 0
  5260.     return (0)
  5261. end
  5262.  
  5263. /*
  5264. **  If more than one option like @configname, show the duplicates and return.
  5265. */
  5266. if @configcount > 1
  5267. begin
  5268.     print "Configuration option is not unique."
  5269.  
  5270.     select duplicate_options = name
  5271.         from spt_values
  5272.             where name like "%" + @configname + "%"
  5273.                 and type = "C"
  5274.                 and number >= 0
  5275.  
  5276.     return (1)
  5277. end
  5278.  
  5279. /*
  5280. ** If @configvalue is NULL, just show the current state of the option.
  5281. */
  5282. if @configvalue = NULL
  5283. begin
  5284.     select name, minimum = low, maximum = high,
  5285.         config_value = sysconfigures.value,
  5286.         run_value = syscurconfigs.value
  5287.     from spt_values, sysconfigures, syscurconfigs
  5288.         where type = "C"
  5289.             and number *= sysconfigures.config
  5290.             and number *= syscurconfigs.config
  5291.             and name like "%" + @configname + "%"
  5292.             and number >= 0
  5293.     return (0)
  5294. end
  5295.  
  5296. /*
  5297. **  Only the SA can execute the update part of this procedure so check.
  5298. */
  5299. if suser_id() != 1
  5300. begin
  5301.     print "Only the System Administrator (SA) may change configuration parameters."
  5302.     return (1)
  5303. end
  5304.  
  5305. /*
  5306. **  Now get the configuration number.
  5307. */
  5308. select @confignum = number
  5309.     from spt_values
  5310.         where type = "C" and (@configvalue between low and high
  5311.             or @configvalue = 0)
  5312.             and name like "%" + @configname + "%"
  5313.             and number >= 0
  5314.  
  5315.  
  5316. /*
  5317. **  If this is the number of devices configuration parameter,
  5318. **  we want to make sure that it's not being set to lower than the
  5319. **  number of devices in sysdevices.
  5320. */
  5321. if @confignum = 116
  5322. begin
  5323.     /*
  5324.     **  Get the max vdevno.
  5325.     */
  5326.     declare @maxvdevno    int
  5327.     select @maxvdevno = max(convert(tinyint, substring(convert(binary(4),
  5328.         d.low), v.low, 1)))
  5329.     from master.dbo.sysdevices d, master.dbo.spt_values v
  5330.         where v.type = "E"
  5331.         and v.number = 3
  5332.  
  5333.     /*
  5334.     **  If @configvalue == 0, then set it to the current number of devices.
  5335.     */
  5336.     if @configvalue = 0
  5337.     select @configvalue = @maxvdevno + 1
  5338.     if (select low
  5339.         from master.dbo.spt_values
  5340.             where type = "C"
  5341.                 and number = 116) > @configvalue
  5342.     begin
  5343.         select @configvalue = low
  5344.             from master.dbo.spt_values
  5345.                 where type = "C"
  5346.                     and number = 116
  5347.     end
  5348.  
  5349.     if ((@maxvdevno + 1) > @configvalue)
  5350.     begin
  5351.         print "You can't set the number of devices to be less than the number of devices already defined in sysdevices."
  5352.         return (1)
  5353.     end
  5354. end
  5355.  
  5356. /*
  5357. **  If this is the number of default language, we want to make sure
  5358. **  that the new value is a valid language id in Syslanguages.
  5359. */
  5360. if @confignum = 124
  5361. begin
  5362.     if not exists (select *
  5363.         from syslanguages
  5364.             where langid = @configvalue)
  5365.     begin
  5366.         /* 0 is Sybase default language, us_english */
  5367.         if @configvalue != 0
  5368.         begin
  5369.             print "You can't set the default language to a language ID that is not defined in Syslanguages."
  5370.             return (1)
  5371.         end
  5372.     end
  5373. end
  5374.  
  5375. /*
  5376. **  If this is the number of kernel language, we want to make sure
  5377. **  that the new value is a valid language id in Syslanguages.
  5378. */
  5379. if @confignum = 132
  5380. begin
  5381.     if not exists (select *
  5382.         from syslanguages
  5383.             where langid = @configvalue)
  5384.     begin
  5385.         /* 0 is Sybase default language, us_english */
  5386.         if @configvalue != 0
  5387.         begin
  5388.             print "You can't set the kernel language to a language ID that is not defined in Syslanguages."
  5389.             return (1)
  5390.         end
  5391.     end
  5392. end
  5393. /*
  5394. **  Although the @configname is good, @configvalue wasn't in range.
  5395. */
  5396. if @confignum is NULL
  5397. begin
  5398.     print "Configuration option value is not legal."
  5399.     return (1)
  5400. end
  5401.  
  5402. /*
  5403. **  If we're in a transaction, disallow this since it might make recovery
  5404. **  impossible.
  5405. */
  5406. if @@trancount > 0
  5407. begin
  5408.     print "Can't run sp_configure from within a transaction."
  5409.     return (1)
  5410. end
  5411.  
  5412. /*
  5413. **  Now update sysconfigure.
  5414. */
  5415. update sysconfigures
  5416.     set value = @configvalue
  5417.         where config = @confignum
  5418.  
  5419. print "Configuration option changed. Run the RECONFIGURE command to install."
  5420.  
  5421. return (0)
  5422. go
  5423. grant execute on sp_configure to public
  5424. go
  5425. dump transaction master with truncate_only
  5426. go
  5427. /* dboption 27.1 3/5/90 */
  5428. create procedure sp_dboption
  5429. @dbname varchar(30) = NULL,        /* database name to change */
  5430. @optname varchar(20) = NULL,        /* option name to turn on/off */
  5431. @optvalue varchar(10) = NULL        /* true or false */
  5432. as
  5433.  
  5434. declare @dbid int            /* dbid of the database */
  5435. declare @dbuid int            /* id of the owner of the database */
  5436. declare @statvalue smallint        /* number of option */
  5437. declare @optcount int            /* number of options like @optname */
  5438.  
  5439. /*
  5440. **  If no @dbname given, just list the possible dboptions.
  5441. **  Only certain status bits may be set or cleared.
  5442. **       settable                        not settable
  5443. **      ------------------------------  --------------------------
  5444. **    allow select into/bulkcopy (4)    load only (512)
  5445. **    read only (1024)        not recovered (256)
  5446. **    dbo use only (2048)        dbname has changed (16384)
  5447. **    single user (4096)        don't recover (32)
  5448. **    truncate log on checkpoint (8)
  5449. **    no checkpoint on recovery (16)
  5450. **      ALL SETTABLE OPTIONS (7196)
  5451. */
  5452. if @dbname = null
  5453. begin
  5454.     print "Settable database options."
  5455.     select database_options = name
  5456.         from spt_values
  5457.             where type = "D"
  5458.                 and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5459.                 and number > 0
  5460.         order by name
  5461.     return (0)
  5462. end
  5463.  
  5464. /*
  5465. **  Verify the database name and get the @dbid and @dbuid
  5466. */
  5467. select @dbid = dbid, @dbuid = suid
  5468.     from sysdatabases
  5469.         where name = @dbname
  5470.  
  5471. /*
  5472. **  If @dbname not found, say so and list the databases.
  5473. */
  5474. if @dbid = NULL
  5475. begin
  5476.     print "No such database -- run sp_helpdb to list databases."
  5477.     return (1)
  5478. end
  5479.  
  5480. /*
  5481. **  You can not change any of the options in master.  If the user tries to
  5482. **  do so tell them they can't.
  5483. */
  5484. if @dbid = 1
  5485. begin
  5486.     print "The 'master' database's options can not be changed."
  5487.     return (1)
  5488. end
  5489.  
  5490. /*
  5491. **  Check remaining parameters.
  5492. */
  5493. if @optname is NULL or @optvalue not in ("true", "false") or @optvalue = null
  5494. begin
  5495.     print "usage: sp_dboption [dbname, optname, {true | false}]"
  5496.     return (1)
  5497. end
  5498.  
  5499. /*
  5500. **  Use @optname and try to find the right option.
  5501. **  If there isn't just one, print appropriate diagnostics and return.
  5502. */
  5503. select @optcount = count(*)
  5504.         from spt_values
  5505.         where name like "%" + @optname + "%" and type = "D"
  5506.             and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5507.  
  5508. /*
  5509. **  If no option, show the user what the options are.
  5510. */
  5511. if @optcount = 0
  5512. begin
  5513.     print "Database option doesn't exist or can't be set by user."
  5514.     print "Run sp_dboption with no parameters to see options."
  5515.     return (0)
  5516. end
  5517.  
  5518. /*
  5519. **  If more than one option like @optname, show the duplicates and return.
  5520. */
  5521. if @optcount > 1
  5522. begin
  5523.     print "Database option is not unique."
  5524.  
  5525.     select duplicate_options = name
  5526.         from spt_values
  5527.             where name like "%" + @optname + "%"
  5528.                 and type = "D"
  5529.                 and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5530.                 and number > 0
  5531.  
  5532.     return (1)
  5533. end
  5534.  
  5535. /*
  5536. **  Only the SA or the dbo of @dbnbame can execute the update part
  5537. **  of this procedure so check.  Also the sproc must be executed in the master
  5538. **  database.
  5539. */
  5540. if suser_id() != 1 and suser_id() != @dbuid
  5541. begin
  5542.     print "Only the System Administrator (SA) or the owner of the database may set db options."
  5543.     return (1)
  5544. end
  5545. if db_name() != "master"
  5546. begin
  5547.     print "You must be in the 'master' database in order to change database options."
  5548.     return (1)
  5549. end
  5550.  
  5551. /*
  5552. **  If we're in a transaction, disallow this since it might make recovery
  5553. **  impossible.
  5554. */
  5555. if @@trancount > 0
  5556. begin
  5557.     print "Can't run sp_dboption from within a transaction."
  5558.     return (1)
  5559. end
  5560.  
  5561. /*
  5562. **  Get the number which is the bit value to set
  5563. */
  5564. select @statvalue = number
  5565.         from spt_values
  5566.         where name like "%" + @optname + "%" and type = "D"
  5567.             and number in (4, 8, 16, 1024, 2048, 4096, 7196)
  5568.             and number > 0
  5569.  
  5570.  
  5571. /*
  5572. **  Now update sysdatabases.
  5573. */
  5574. if @optvalue = "true"
  5575. begin
  5576.     /*
  5577.     **  If this the option to make the database read only,
  5578.     **  we need to do some checking first.
  5579.     **  Unless it's the master db, no one can be using it.
  5580.     **  If it's the master db, only the SA may be using it.
  5581.     */
  5582.     if (@statvalue = 1024) and (select count(*)
  5583.                     from master.dbo.sysprocesses
  5584.                         where dbid = @dbid) > 0
  5585.     begin
  5586.         print "The database is currently in use -- 'read only' option disallowed."
  5587.         return (1)
  5588.     end
  5589.  
  5590.     update sysdatabases
  5591.         set status = status | @statvalue
  5592.             where dbid = @dbid
  5593. end
  5594.  
  5595. /*
  5596. **  We want to turn it off.
  5597. */
  5598. else
  5599. begin
  5600.     update sysdatabases
  5601.         set status = status & ~@statvalue
  5602.             where dbid = @dbid
  5603. end
  5604.  
  5605. /*
  5606. **  Advise the user to run the CHECKPOINT command in the database that
  5607. **  was changed.
  5608. */
  5609. print "Run the CHECKPOINT command in the database that was changed."
  5610.  
  5611. return (0)
  5612. go
  5613. grant execute on sp_dboption to public
  5614. go
  5615. /* defaultdb 27.1 3/5/90 */
  5616. create procedure sp_defaultdb
  5617. @loginame varchar(30),            /* login name of the user */
  5618. @defdb varchar(30)             /* default db for the user */
  5619. as
  5620.  
  5621. declare @suid int        /* the suid to use to qualify the change */
  5622.  
  5623. /*
  5624. **  If we're in a transaction, disallow this since it might make recovery
  5625. **  impossible.
  5626. */
  5627. if @@trancount > 0
  5628. begin
  5629.     print "Can't run sp_defaultdb from within a transaction."
  5630.     return (1)
  5631. end
  5632.  
  5633. /*
  5634. **  Check that the account exists.
  5635. */
  5636. if not exists (select *
  5637.             from syslogins
  5638.         where name = @loginame)
  5639. begin
  5640.     print "No such login exists."
  5641.     return (1)
  5642. end
  5643.  
  5644. /*
  5645. **  Check that the database name is valid.
  5646. */
  5647. if not exists (select *
  5648.             from sysdatabases
  5649.         where name = @defdb)
  5650. begin
  5651.     print "Database name not valid -- default not changed."
  5652.     return (1)
  5653. end
  5654.  
  5655. /*
  5656. **  Both the owner of the account (@loginame) and the SA can run this
  5657. **  command.  If the SA is running it, set @suid to the suid of @loginame.
  5658. */
  5659. select @suid = suser_id()
  5660. if @suid = 1
  5661. begin
  5662.     /*
  5663.     **  Get the suid for @loginame.  We know it exists because we
  5664.     **  checked above.
  5665.     */
  5666.     select @suid = suid
  5667.         from syslogins
  5668.             where name = @loginame
  5669. end
  5670.             
  5671. /*
  5672. **  Change the database
  5673. */
  5674. update syslogins
  5675.     set dbname = @defdb
  5676.         where suid = @suid
  5677.             and name = @loginame
  5678.  
  5679. /*
  5680. **  If nothing got updated then it means that the the user tried to change
  5681. **  someone else's default.
  5682. */
  5683. if @@rowcount < 1
  5684. begin
  5685.     print "You can't change someone else's default database."
  5686.     return (1)
  5687. end
  5688.  
  5689. print "Default database changed."
  5690.  
  5691. return (0)
  5692. go
  5693. grant execute on sp_defaultdb to public
  5694. go
  5695. dump transaction master with truncate_only
  5696. go
  5697. /* depends 27.1 3/5/90 */
  5698. create procedure sp_depends
  5699. @objname varchar(92)        /* the object we want to check */
  5700. as
  5701.  
  5702. declare @objid int            /* the id of the object we want */
  5703. declare @found_some bit            /* flag for dependencies found */
  5704.  
  5705. /*
  5706. **  Make sure the @objname is local to the current database.
  5707. */
  5708. if @objname like "%.%.%" and
  5709.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  5710. begin
  5711.     print "Object must be in your current database."
  5712.     return (1)
  5713. end
  5714.  
  5715. /*
  5716. **  See if @objname exists.
  5717. */
  5718. select @objid = object_id(@objname)
  5719. if @objid = null
  5720. begin
  5721.     print "No such object in the current database."
  5722.     return (1)
  5723. end
  5724.  
  5725. /*
  5726. **  Initialize @found_some to indicate that we haven't seen any dependencies.
  5727. */
  5728. select @found_some = 0
  5729.  
  5730. set nocount on
  5731.  
  5732. /*
  5733. **  Print out the particulars about the local dependencies.
  5734. */
  5735. if exists (select *
  5736.         from sysdepends
  5737.             where id = @objid)
  5738. begin
  5739.     print "Things the object references in the current database."
  5740.     select object = substring((s.name + "." + o.name), 1, 40),
  5741.         type = substring(v.name, 1, 16),
  5742.         updated = substring(u.name, 1, 7),
  5743.         selected = substring(w.name, 1, 8)
  5744.             from sysobjects o, master.dbo.spt_values v, sysdepends d,
  5745.                 master.dbo.spt_values u, master.dbo.spt_values w,
  5746.                 sysusers s
  5747.             where o.id = d.depid
  5748.                 and o.sysstat & 7 = v.number and v.type = 'O'
  5749.                 and u.type = 'B' and u.number = d.resultobj
  5750.                 and w.type = 'B' and w.number = d.readobj
  5751.                 and d.id = @objid
  5752.                 and o.uid = s.uid
  5753.     select @found_some = 1
  5754. end
  5755.  
  5756. /*
  5757. **  Now check for things that the objects depends upon
  5758. */
  5759. if exists (select *
  5760.         from sysdepends
  5761.             where depid = @objid)
  5762. begin
  5763.     print "Things inside the current database that reference the object."
  5764.     select distinct object = substring((s.name + "." + o.name), 1, 40),
  5765.         type = substring(v.name, 1, 16)
  5766.             from sysobjects o, master.dbo.spt_values v, sysdepends d,
  5767.                 sysusers s
  5768.             where o.id = d.id
  5769.                 and o.sysstat & 7 = v.number and v.type = 'O'
  5770.                 and d.depid = @objid
  5771.                 and o.uid = s.uid
  5772.     select @found_some = 1
  5773. end
  5774.  
  5775. /*
  5776. **  Did we find anything in sysdepends?
  5777. */
  5778. if @found_some = 0
  5779. begin
  5780.     print "Object doesn't reference any object and no objects reference it."
  5781. end
  5782.  
  5783. set nocount off
  5784.  
  5785. return (0)
  5786. go
  5787. grant execute on sp_depends to public
  5788. go
  5789. /* diskdefault 27.1 3/5/90 */
  5790. create procedure sp_diskdefault
  5791. @logicalname    varchar(30),        /* logical name of the device */
  5792. @defstatus    varchar(15)        /* turn on or off */
  5793. as
  5794.  
  5795. /*
  5796. **  If we're in a transaction, disallow this since it might make recovery
  5797. **  impossible.
  5798. */
  5799. if @@trancount > 0
  5800. begin
  5801.     print "Can't run sp_diskdefault from within a transaction."
  5802.     return (1)
  5803. end
  5804.  
  5805. /*
  5806. **  Only the SA can run this sproc.
  5807. */
  5808. if suser_id() != 1
  5809. begin
  5810.     print "Only the System Administrator (SA) may execute this procedure."
  5811.     return (1)
  5812. end
  5813.  
  5814. /*
  5815. **  Make sure that a device with @logicalname exists.
  5816. */
  5817. if not exists (select *
  5818.         from master.dbo.sysdevices
  5819.             where name = @logicalname)
  5820. begin
  5821.     print "No such device exists."
  5822.     return (1)
  5823. end
  5824.  
  5825. /*
  5826. **  Make sure that is a database disk and not a dump device.
  5827. */
  5828. if exists (select *
  5829.         from master.dbo.sysdevices
  5830.             where name = @logicalname
  5831.                 and status & 16 = 16)
  5832. begin
  5833.     print "The device name supplied is not a database disk."
  5834.     return (1)
  5835. end
  5836.  
  5837. if @defstatus = "defaulton"
  5838. begin
  5839.     update master.dbo.sysdevices
  5840.         set status = status | 1
  5841.             where name = @logicalname
  5842.     return (0)
  5843. end
  5844.  
  5845. if @defstatus = "defaultoff"
  5846. begin
  5847.     update master.dbo.sysdevices
  5848.         set status = status & ~1
  5849.             where name = @logicalname
  5850.     return (0)
  5851. end
  5852.  
  5853. /*
  5854. **  @defstatus must be "defaulton" or "defaultoff"
  5855. */
  5856. print "Usage: sp_diskdefault logicalname {defaulton | defaultoff}."
  5857.  
  5858. return (1)
  5859. go
  5860. dump transaction master with truncate_only
  5861. go
  5862. /* dropalias 27.1 3/5/90 */
  5863. create procedure sp_dropalias
  5864. @loginame varchar(30)        /* account name of the user with the alias */
  5865. as
  5866.  
  5867. /*
  5868. **  You must be SA or the dbo to execute this sproc.
  5869. */
  5870. if suser_id() != 1 and user_id() != 1
  5871. begin
  5872.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  5873.     return (1)
  5874. end
  5875.  
  5876. /*
  5877. **  Check to make sure that the @loginame has an account.
  5878. */
  5879. if (select suser_id(@loginame)) = NULL
  5880. begin
  5881.     print "No login with the specified name exists."
  5882.     return (1)
  5883. end
  5884.  
  5885. /*
  5886. **  Delete the alias, if any, from sysalternates.
  5887. */
  5888. delete from sysalternates
  5889.     where suid = suser_id(@loginame)
  5890.  
  5891. /*
  5892. **  If nothing happened (rowcount = 0), there was no alias.
  5893. */
  5894. if @@rowcount > 0
  5895. begin
  5896.     print "Alias user dropped."
  5897.     return (0)
  5898. end
  5899. else
  5900. begin
  5901.     print "No alias for specified user exists."
  5902.     return (1)
  5903. end
  5904.  
  5905. go
  5906. grant execute on sp_dropalias to public
  5907. go
  5908. /* dropdevice    27.1    3/5/90 */
  5909. create procedure sp_dropdevice
  5910. @logicalname    varchar(30)        /* logical name of the device */
  5911. as
  5912.  
  5913. /*
  5914. **  If we're in a transaction, disallow this since it might make recovery
  5915. **  impossible.
  5916. */
  5917. if @@trancount > 0
  5918. begin
  5919.     print "Can't run sp_dropdevice from within a transaction."
  5920.     return (1)
  5921. end
  5922.  
  5923. /*
  5924. **  Only the system administrator (SA) can run this command.
  5925. **  Check to make sure the executor is the sa.
  5926. */
  5927. if suser_id() != 1
  5928. begin
  5929.     print "Only the System Administrator (SA) may execute this procedure."
  5930.     return (1)
  5931. end
  5932.  
  5933. /*
  5934. **  Check and make sure that the device actually exists.
  5935. */
  5936. if not exists (select *
  5937.         from master.dbo.sysdevices
  5938.             where name = @logicalname)
  5939. begin
  5940.     print "No such device exists -- use sp_helpdevice."
  5941.     return (1)
  5942. end
  5943.  
  5944. /*
  5945. **  Check and make sure that no database is using the device.
  5946. **  If so, print out who is using it and exit.
  5947. */
  5948. if exists (select *
  5949.     from sysdatabases, sysusages, sysdevices, spt_values
  5950.         where sysdatabases.dbid = sysusages.dbid
  5951.             and segmap >= 0
  5952.             and sysdevices.low <= size + vstart
  5953.             and sysdevices.high >= size + vstart - 1
  5954.             and sysdevices.status & 2 = 2
  5955.             and type = "S"
  5956.             and sysdevices.name = @logicalname)
  5957. begin
  5958.     print "Device is being used by a database. You can't drop it."
  5959.  
  5960.     select database_name = substring(sysdatabases.name, 1, 23),
  5961.         device = substring(sysdevices.name, 1, 21),
  5962.         size = convert(varchar(8), size / 500) + " MB",
  5963.         usage = spt_values.name
  5964.     from sysdatabases, sysusages, sysdevices, spt_values
  5965.         where sysdatabases.dbid = sysusages.dbid
  5966.             and segmap >= 0
  5967.             and sysdevices.low <= size + vstart
  5968.             and sysdevices.high >= size + vstart - 1
  5969.             and sysdevices.status & 2 = 2
  5970.             and type = "S"
  5971.             and sysdevices.name = @logicalname
  5972.     order by sysdatabases.name, sysdevices.name, spt_values.name,
  5973.         size
  5974.  
  5975.     return (1)
  5976. end
  5977.  
  5978. /*
  5979. **  Everything's ok so drop the device.
  5980. */
  5981. delete sysdevices
  5982.     where name = @logicalname
  5983.  
  5984. print "Device dropped."
  5985.  
  5986. return (0)
  5987. go
  5988. dump transaction master with truncate_only
  5989. go
  5990. /* dropdumpdevice 27.1 3/5/90 */
  5991. create procedure sp_dropdumpdevice
  5992. @logicalname varchar(30)        /* name of device to drop */
  5993. as
  5994.  
  5995. /*
  5996. **  N.B.  This sproc is redundant due to the addition of sp_dropdevice.
  5997. **        However, it is still included in installmaster for backwards
  5998. **      compatibility with existing programs.
  5999. */
  6000.  
  6001. /*
  6002. **  If we're in a transaction, disallow this since it might make recovery
  6003. **  impossible.
  6004. */
  6005. if @@trancount > 0
  6006. begin
  6007.     print "Can't run sp_dropdumpdevice from within a transaction."
  6008.     return (1)
  6009. end
  6010.  
  6011. /*
  6012. **  Only the system administrator (SA) can run this command.
  6013. **  Check to make sure the executor is the sa.
  6014. */
  6015. if suser_id() != 1
  6016. begin
  6017.     print "Only the System Administrator (SA) may execute this procedure."
  6018.     return (1)
  6019. end
  6020.  
  6021. /*
  6022. **  Delete the device.  Only dump devices may be dropped.
  6023. */
  6024. delete from master.dbo.sysdevices
  6025.     where name = @logicalname
  6026.         and status & 16 = 16
  6027.  
  6028. /*
  6029. ** Check @@rowcount when it works
  6030. */
  6031. if @@rowcount > 0
  6032. begin
  6033.     print "Device dropped."
  6034.     return (0)
  6035. end
  6036. else
  6037. begin
  6038.     /*
  6039.     **  Nothing happened because the device doesn't exist or isn't
  6040.     **  a dump device.
  6041.     */
  6042.     if not exists (select *
  6043.             from sysdevices
  6044.                 where name = @logicalname)
  6045.     begin
  6046.         print "No device with specified logical name exists."
  6047.     end
  6048.     else print "Device is not a dump device and may not be dropped."
  6049.  
  6050.     return (1)
  6051. end
  6052. go
  6053. /* dropgroup 27.1 3/5/90 */
  6054. create procedure sp_dropgroup
  6055. @grpname varchar(30)        /* group to be dropped */
  6056. as
  6057.  
  6058. declare @gid int        /* group id of the group to be dropped */
  6059.  
  6060. /*
  6061. **  You must be SA or the dbo to execute this sproc.
  6062. */
  6063. if suser_id() != 1 and user_id() != 1
  6064. begin
  6065.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  6066.     return (1)
  6067. end
  6068.  
  6069. /*
  6070. **  See if the group exists.
  6071. **  User ids are <= 16383 and group ids are > 16383.
  6072. */
  6073. select @gid = uid
  6074.     from sysusers
  6075.         where name = @grpname
  6076.             and (uid > 16383 or uid = 0)
  6077.  
  6078. if @gid = NULL
  6079. begin
  6080.     print "No group with the specified name exists."
  6081.     return (1)
  6082. end
  6083.  
  6084. /*
  6085. **  Can't drop the group public.
  6086. */
  6087. if @gid = 0
  6088. begin
  6089.     print "Can't drop the group 'public'."
  6090.     return (1)
  6091. end
  6092.  
  6093. /*
  6094. **  Check to see if the group owns anything.  If so, return.
  6095. */
  6096. if exists (select *
  6097.            from sysobjects
  6098.                where uid = @gid)
  6099. begin
  6100.     print "You cannot drop group because it owns objects in database."
  6101.  
  6102.     /*
  6103.     **  Show what is owned by the group.
  6104.     */
  6105.     select s.name, s.type, owner = u.name
  6106.         from sysobjects s, sysusers u
  6107.             where s.id = @gid
  6108.                 and u.uid = @gid
  6109.     return (1)
  6110. end
  6111.  
  6112.  
  6113. /*
  6114. **  Check to see that nobody is in the group.  If so, return.
  6115. */
  6116. if (select count(*)
  6117.     from sysusers
  6118.         where gid = @gid
  6119.             and uid <= 16383) != 0
  6120. begin
  6121.     print "Group has members.  It must be empty before it can be dropped."
  6122.  
  6123.     /*
  6124.     **  Show who is in the group.
  6125.     */
  6126.     select name
  6127.         from sysusers
  6128.             where gid = @gid
  6129.                 and uid <= 16383
  6130.     
  6131.     return (1)
  6132. end
  6133.  
  6134. /*
  6135. **  Drop the group.
  6136. **  Also drop any references to the group in the sysprotects table.
  6137. */
  6138. begin transaction
  6139.  
  6140.     delete from sysusers
  6141.         where uid = @gid
  6142.  
  6143.     delete from sysprotects
  6144.         where uid = @gid
  6145.  
  6146. commit transaction
  6147.  
  6148. print "Group has been dropped."
  6149.  
  6150. return (0)
  6151. go
  6152. grant execute on sp_dropgroup to public
  6153. go
  6154. dump transaction master with truncate_only
  6155. go
  6156. /* dropkey 27.1 3/5/90 */
  6157. create procedure sp_dropkey
  6158. @keytype varchar(10),             /* type of key to drop */
  6159. @tabname varchar(92),            /* table with the key */
  6160. @deptabname varchar(92) = null        /* dependent table */
  6161. as
  6162.  
  6163. declare @id int                /* id of @tabname */
  6164. declare @uid smallint            /* owner of @tabname */
  6165. declare @depid int            /* id of related table */
  6166. declare @depuid smallint        /* owner of @related table */
  6167.  
  6168. /*
  6169. **  First make sure that the key type is ok.
  6170. */
  6171. if @keytype not in ("primary", "foreign", "common")
  6172. begin
  6173.     print "Usage: sp_dropkey {primary | foreign | common}, tabname [,deptabname]."
  6174.     print "Type must be 'primary', 'foreign', or 'common'."
  6175.     return (1)
  6176. end
  6177.  
  6178. /*
  6179. **  Check to see that the tabname is local.
  6180. */
  6181. if @tabname like "%.%.%"
  6182. begin
  6183.     if substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  6184.     begin
  6185.         print "Table or view name must be in 'current' database."
  6186.         return (1)
  6187.     end
  6188. end
  6189. if @deptabname like "%.%.%"
  6190. begin
  6191.     if substring(@deptabname, 1, charindex(".", @deptabname) - 1) != db_name()
  6192.     begin
  6193.         print "Table or view name must be in 'current' database."
  6194.         return (1)
  6195.     end
  6196. end
  6197.  
  6198. /*
  6199. **  Get the ids of the @tabname and @deptabname.
  6200. */
  6201. select @id = id, @depid = object_id(@deptabname), @uid = uid
  6202.         from sysobjects
  6203.             where id = object_id(@tabname)
  6204. if @id = null
  6205. begin
  6206.     print "The table or view named doesn't exist in the current database."
  6207.     return (1)
  6208. end
  6209.  
  6210. if @uid != user_id()
  6211. begin
  6212.     print "You must be the owner of the table or view to drop its key."
  6213.     return (1)
  6214. end
  6215.  
  6216. /*
  6217. **  If primary key, just drop it.
  6218. */
  6219. if @keytype = "primary"
  6220. begin
  6221.     delete from syskeys
  6222.         where id = @id
  6223.             and type = 1
  6224.  
  6225.     if @@rowcount = 0
  6226.     begin
  6227.         print "No primary key for the table or view exists."
  6228.         return (1)
  6229.     end
  6230.  
  6231.     else
  6232.     begin
  6233.         print "Primary key for the table or view dropped."
  6234.     end
  6235.  
  6236.     /*
  6237.     **  Check to see if there are any foreign keys dependent on the
  6238.     **  primary key.  If so -- drop them.
  6239.     */
  6240.     delete from syskeys
  6241.         where depid = @id
  6242.             and type = 2
  6243.     
  6244.     if @@rowcount != 0
  6245.     begin
  6246.         print "Dependent foreign keys were also dropped."
  6247.     end
  6248.  
  6249.     return (0)
  6250. end
  6251.  
  6252. /*
  6253. **  It's either a foreign or common key so we need to verify the
  6254. **  existence of the @deptabname.
  6255. */
  6256. if @depid = null
  6257. begin
  6258.     /*
  6259.     **  Was the @deptabname supplied?
  6260.     */
  6261.     if @deptabname = null
  6262.     begin
  6263.         print "You need to supply the dependent table or view as the third parameter."
  6264.         return (1)
  6265.     end
  6266.  
  6267.     /*
  6268.     **  It was supplied but it doesn't exist.
  6269.     */
  6270.     print "The dependent table or view doesn't exist in the current database."
  6271.     return (1)
  6272. end
  6273.  
  6274.  
  6275. /*
  6276. **  If foreign key, get rid of it.
  6277. */
  6278. if @keytype = "foreign"
  6279. begin
  6280.     /*
  6281.     **  Get rid of the foreign key entry.
  6282.     */
  6283.     delete from syskeys
  6284.         where type = 2
  6285.             and id = @id
  6286.             and depid = @depid
  6287.     if @@rowcount = 0
  6288.     begin
  6289.         print "No foreign key for the table or view exists."
  6290.         return (1)
  6291.     end
  6292.  
  6293.     print "Foreign key dropped."
  6294.  
  6295.     return (0)
  6296. end
  6297.  
  6298. /*
  6299. **  Key type must be common so just get rid of the common keys
  6300. **  with the right ids and depids.  Since whenever a common key is defined
  6301. **  it is added to both of the tables involved, we'll get rid of both of
  6302. **  those entries.
  6303. */
  6304. delete from syskeys
  6305.     where type = 3
  6306.         and id = @id
  6307.         and depid = @depid
  6308.  
  6309. if @@rowcount = 0
  6310. begin
  6311.     print "No common keys exist between the two tables or views supplied."
  6312.     return (1)
  6313. end
  6314.  
  6315. /*
  6316. **  Now get rid of the inverse common key entries.
  6317. */
  6318. delete from syskeys
  6319.     where type = 3
  6320.         and id = @depid
  6321.         and depid = @id
  6322.  
  6323. print "Common keys dropped."
  6324.  
  6325. return (0)
  6326. go
  6327. grant execute on sp_dropkey to public
  6328. go
  6329. /* droplogin 27.1 3/5/90 */
  6330. create procedure sp_droplogin
  6331. @loginame varchar(30)            /* name of login to drop */
  6332. as
  6333.  
  6334. /*
  6335. **  This procedure makes a weak attempt to check for any dependencies.
  6336. **  It looks in the current database to see if the suid is used in
  6337. **  sysusers or sysalternates.  If so, a diagnostic is issued and the
  6338. **  login is not dropped.
  6339. **
  6340. **  Ideally, this procedure should check each database to see if the login
  6341. **  is a user.  However, this is not yet possible since procedures do not
  6342. **  allow parameters to be used for database or table names.
  6343. */
  6344.  
  6345. /*
  6346. **  If we're in a transaction, disallow this since it might make recovery
  6347. **  impossible.
  6348. */
  6349. if @@trancount > 0
  6350. begin
  6351.     print "Can't run sp_droplogin from within a transaction."
  6352.     return (1)
  6353. end
  6354.  
  6355. /*
  6356. **  Only the system administrator (SA) can run this command.
  6357. **  Check to make sure the executor is the sa.
  6358. */
  6359. if suser_id() != 1
  6360. begin
  6361.     print "Only the System Administrator (sa) may execute this procedure."
  6362.     return (1)
  6363. end
  6364.  
  6365. /*
  6366. **  Check sysusers.
  6367. */
  6368. if exists (select *
  6369.         from sysusers
  6370.             where suid = suser_id(@loginame))
  6371. begin
  6372.     print "User exists in current database.  Drop user before dropping login."
  6373.     select database_name = db_name(), login_name = @loginame,
  6374.         name_in_database = name
  6375.             from sysusers where suser_id(@loginame) = suid
  6376.     return (1)
  6377. end
  6378.  
  6379. /*
  6380. **  Check sysalternates.
  6381. */
  6382. if exists (select *
  6383.         from sysalternates
  6384.             where suid = suser_id(@loginame))
  6385. begin
  6386.     print "User exists as an alias in current database.  Drop alias before dropping login."
  6387.     select database_name = db_name(), login_name = @loginame,
  6388.         name_in_database = u.name
  6389.             from sysusers u, sysalternates a
  6390.                 where suser_id(@loginame) = a.suid
  6391.                     and a.altsuid = u.suid
  6392.     return (1)
  6393. end
  6394.  
  6395. /*
  6396. **  Delete the login.
  6397. */
  6398. delete from syslogins
  6399.     where name = @loginame
  6400.  
  6401. /*
  6402. ** Check @@rowcount when it works
  6403. */
  6404. if @@rowcount > 0
  6405. begin
  6406.     print "Login dropped."
  6407.     return (0)
  6408. end
  6409. else
  6410. begin
  6411.     print "No login exists with supplied name."
  6412.     return (1)
  6413. end
  6414. go
  6415. dump transaction master with truncate_only
  6416. go
  6417. /* droptype 27.1 3/5/90 */
  6418. create procedure sp_droptype
  6419. @typename varchar(30)            /* the user type to drop */
  6420. as
  6421.  
  6422. declare @typeid smallint        /* the typeid of the usertype to drop */
  6423.  
  6424. /*
  6425. **  Initialize @typeid so we can tell if we can't find it.
  6426. */
  6427. select @typeid = 0
  6428.  
  6429. /*
  6430. **  Find the user type with @typename.  It must be a user type (usertype > 99)
  6431. **  and it must be owned by the person (or dbo) running the procedure.
  6432. */
  6433. select @typeid = usertype
  6434.     from systypes
  6435.         where (uid = user_id() or user_id() = 1)
  6436.             and name = @typename
  6437.             and usertype > 99
  6438.  
  6439. if @typeid = 0
  6440. begin
  6441.     print "The type doesn't exist or you don't own it."
  6442.     return (1)
  6443. end
  6444.  
  6445. /*
  6446. **  Check to see if the type is being used.  If it is, it can't be dropped.
  6447. */
  6448. if exists (select *
  6449.            from syscolumns
  6450.                where usertype = @typeid)
  6451. begin
  6452.     print "Type is being used. You cannot drop it."
  6453.  
  6454.     /*
  6455.     **  Show where it's being used.
  6456.     */
  6457.     select object = o.name, type = o.type, owner = u.name, column = c.name,
  6458.         datatype = t.name
  6459.     from syscolumns c, systypes t, sysusers u, sysobjects o
  6460.         where c.usertype = @typeid
  6461.             and t.usertype = @typeid
  6462.             and o.uid = u.uid
  6463.             and c.id = o.id
  6464.     order by object, column
  6465.  
  6466.     return (1)
  6467. end
  6468.  
  6469. /*
  6470. **  Everything is consistent so drop the type.
  6471. */
  6472. delete from systypes
  6473.     where usertype = @typeid
  6474.  
  6475. print "Type has been dropped."
  6476.  
  6477. return (0)
  6478. go
  6479. grant execute on sp_droptype to public
  6480. go
  6481. /* dropuser 27.1 3/5/90 */
  6482. create procedure sp_dropuser
  6483. @name_in_db varchar(30)            /* user name to drop */
  6484. as
  6485.  
  6486. declare @suid int            /* suid of the user */
  6487. declare @uid int            /* uid of the user */
  6488. declare @objectcount int        /* count of objects user owns */
  6489. declare @typecount int            /* count of types user owns */
  6490. declare    @userdropped int        /* flag to indicate user was dropped */
  6491.  
  6492. /*
  6493. **  You must be SA or the dbo to execute this sproc.
  6494. */
  6495. if suser_id() != 1 and user_id() != 1
  6496. begin
  6497.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  6498.     return (1)
  6499. end
  6500.  
  6501. /*
  6502. **  See if the user exists in the database.
  6503. **  User ids are <= 16383 and group ids are > 16383.
  6504. */
  6505. select @uid = uid, @suid = suid
  6506.     from sysusers
  6507.         where name = @name_in_db
  6508.             and uid <= 16383 and uid > 0
  6509.  
  6510. /*
  6511. **  No such user so return.
  6512. */
  6513. if @uid = NULL
  6514. begin
  6515.     print "No user exists with the supplied name."
  6516.     return (1)
  6517. end
  6518.  
  6519. /*
  6520. **  Big trouble if dbo is dropped so check. Only check if uid = 1 is not enough,
  6521. **  since if SA drops a user in master database, uid is not equal to 1, but the
  6522. **  user could be the dbo of the other database(s). So check to see whether suid
  6523. **  in sysdatabases.
  6524. */
  6525. if exists (select * from sysdatabases
  6526.         where suid = @suid)
  6527. begin
  6528.     print "You cannot drop the 'database owner'."
  6529.     return (1)
  6530. end
  6531.  
  6532. /*
  6533. **  Trouble if guest gets dropped from master or tempdb, so check.
  6534. */
  6535. if (@name_in_db = "guest" and (db_id() = 1 or db_id() = 2))
  6536. begin
  6537.     print "You cannot drop the 'guest' user from master or tempdb."
  6538.     return(1)
  6539. end
  6540.  
  6541. /*
  6542. **  If the user owns any objects or datatypes and we're not
  6543. **  forcing the drop, return without doing anything.
  6544. */
  6545. select @objectcount = count(*)
  6546.        from sysobjects
  6547.            where uid = @uid
  6548. select @typecount = count(*)
  6549.     from systypes
  6550.         where uid = @uid
  6551.  
  6552. if @objectcount > 0 or @typecount > 0
  6553. begin
  6554.     if @objectcount > 0
  6555.     begin
  6556.         print "You cannot drop user because he or she owns objects in database."
  6557.         select name, type
  6558.             from sysobjects
  6559.                 where uid = @uid
  6560.     end
  6561.  
  6562.     if @typecount > 0
  6563.     begin
  6564.         print "You cannot drop user because he or she owns types in database."
  6565.         select user_type = a.name, physical_type = b.name
  6566.             from systypes a, systypes b
  6567.                 where a.uid = @uid
  6568.                     and a.type = b.type
  6569.                     and b.usertype < 100
  6570.     end
  6571.  
  6572.     return (1)
  6573. end
  6574.  
  6575.  
  6576. /*
  6577. **  Drop the user.
  6578. **  Also drop any references to the user in the sysprotects table.
  6579. **  If anyone is aliased to the user, drop them also.
  6580. */
  6581. begin transaction
  6582.  
  6583.     delete from sysusers
  6584.         where suid = @suid
  6585.     select @userdropped = @@rowcount
  6586.  
  6587.     delete from sysprotects
  6588.         where uid = @uid
  6589.  
  6590.     /*
  6591.     **  Drop any dependent aliases.
  6592.     */
  6593.     if exists (select *
  6594.             from sysalternates
  6595.                 where altsuid = @suid)
  6596.     begin
  6597.         delete from sysalternates
  6598.             where altsuid = @suid
  6599.  
  6600.         print "The dependent aliases were also dropped."
  6601.     end
  6602.  
  6603. if @userdropped = 1
  6604.     commit transaction
  6605. else rollback transaction
  6606.  
  6607. print "User has been dropped from current database."
  6608.  
  6609. return (0)
  6610. go
  6611. grant execute on sp_dropuser to public
  6612. go
  6613. dump transaction master with truncate_only
  6614. go
  6615. /* dropremotelogin 27.1 3/5/90 */
  6616. create procedure sp_dropremotelogin
  6617. @remoteserver    varchar(30),        /* name of remote server */
  6618. @loginame varchar(30) = NULL,        /* user's remote name */
  6619. @remotename varchar(30) = NULL        /* user's local user name */
  6620. as
  6621.  
  6622. declare    @msg    varchar(100)
  6623. declare @suid    smallint
  6624.  
  6625. /*
  6626. **  If we're in a transaction, disallow this since it might make recovery
  6627. **  impossible.
  6628. */
  6629. if @@trancount > 0
  6630. begin
  6631.     print "Can't run sp_dropremotelogin from within a transaction."
  6632.     return (1)
  6633. end
  6634.  
  6635. /*
  6636. **  If @loginame is NULL then we want to set @suid = -1. Otherwise get
  6637. **  it real value.
  6638. */
  6639. if @loginame = null
  6640.     select @suid = -1
  6641. else select @suid = suser_id(@loginame)
  6642.  
  6643. /*
  6644. **  Delete the remote login.
  6645. */
  6646. delete master.dbo.sysremotelogins
  6647.     from master.dbo.sysremotelogins l, master.dbo.sysservers s
  6648.     where l.remoteserverid = s.srvid
  6649.         and s.srvname = @remoteserver
  6650.         and l.remoteusername = @remotename
  6651.         and l.suid = @suid
  6652.  
  6653. /*
  6654. ** Check @@rowcount when it works
  6655. */
  6656. if @@rowcount > 0
  6657. begin
  6658.     print "Remote login dropped."
  6659.     return (0)
  6660. end
  6661. else
  6662. begin
  6663.     select @msg = "There is no remote user '" + isnull(@remotename, "NULL")
  6664.         + "' mapped to local user '" + isnull(@loginame, "NULL")
  6665.         + "' from the remote server '" + @remoteserver + "'."
  6666.     print @msg
  6667.     return (1)
  6668. end
  6669. go
  6670. /* dropsegment    27.1    3/5/90 */
  6671. create procedure sp_dropsegment
  6672. @segname    varchar(30),        /* segment name */
  6673. @device        varchar(30) = null    /* device name */
  6674. as
  6675.  
  6676. declare @dbuid    smallint    /* id of the owner of the database */
  6677. declare @segbit    int        /* this is the bit to turn on in sysusages */
  6678. declare    @msg    varchar(200)
  6679.  
  6680. /*
  6681. **  If we're in a transaction, disallow this since it might make recovery
  6682. **  impossible.
  6683. */
  6684. if @@trancount > 0
  6685. begin
  6686.     print "Can't run sp_dropsegment from within a transaction."
  6687.     return (1)
  6688. end
  6689.  
  6690. /*
  6691. **  You must be SA or the dbo to execute this sproc.
  6692. */
  6693. if suser_id() != 1 and user_id() != 1
  6694. begin
  6695.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  6696.     return (1)
  6697. end
  6698.  
  6699. /*
  6700. **  There are two drop cases:
  6701. **    1) dropping the segment
  6702. **    2) dropping a segment reference to a device
  6703. **  The sproc does things differently depending on which we're doing.
  6704. */
  6705.  
  6706. /*
  6707. **  Check to see if the segment exists.
  6708. */
  6709. if not exists (select *
  6710.         from syssegments
  6711.             where name = @segname)
  6712. begin
  6713.     select @msg = "There is no such segment as '" + @segname + "'."
  6714.     print @msg
  6715.     return (1)
  6716. end
  6717.  
  6718. /*
  6719. **  Get the segment number of @segname.
  6720. */
  6721. select @segbit = segment
  6722.     from syssegments
  6723.         where name = @segname
  6724.  
  6725. /*
  6726. **  Now convert the segment number to the right bit for segmap.
  6727. */
  6728. if (@segbit < 31)
  6729.     select @segbit = power(2, @segbit)
  6730. else
  6731.     /*
  6732.     **  Since this is segment 31, power(2, 31) will overflow
  6733.     **  since segmap is an int.  We'll grab the machine-dependent
  6734.     **  bit mask from spt_values to clear the right bit.
  6735.     */
  6736.     select @segbit = low
  6737.         from master.dbo.spt_values
  6738.             where type = "E"
  6739.                 and number = 2
  6740.  
  6741. /*
  6742. **  Don't allow the segments 'default', 'system', or 'logsegment' to
  6743. **  be dropped completely.
  6744. */
  6745. if @segname in ("system", "default", "logsegment")
  6746. begin
  6747.     if @device = null
  6748.     begin
  6749.         select @msg = "Can't drop the '" + @segname
  6750.             + "' segment completely."
  6751.         print @msg
  6752.         return (1)
  6753.     end
  6754.  
  6755.     /*
  6756.     **  For the case where we are dropping the logsegment from a
  6757.     **  device, the logic below works OK.  If this is the last
  6758.     **  device for the the segment, it won't let the segment be
  6759.     **  dropped.
  6760.     */
  6761. end
  6762.  
  6763.  
  6764. /*
  6765. **  If we are dropping the segment, check to see that the segment isn't
  6766. **  being used by anyone.
  6767. */
  6768. if @device = null
  6769. begin
  6770.  
  6771.     /*
  6772.     **  Check to see if the segment is being used.
  6773.     */
  6774.     if exists (select *
  6775.             from sysindexes i, syssegments s
  6776.                 where s.name = @segname
  6777.                     and i.segment = s.segment)
  6778.     begin
  6779.         select @msg = "The segment '" + @segname + "' is being used."
  6780.         print @msg
  6781.         return (1)
  6782.     end
  6783. end
  6784.  
  6785. /*
  6786. **  If we are dropping the segment reference, check to see that arguments are
  6787. **  reasonable.
  6788. */
  6789. if @device != null
  6790. begin
  6791.     /*
  6792.     **  Now see if the database even uses the @device
  6793.     */
  6794.     if not exists (select *
  6795.             from sysusages u, sysdevices d
  6796.                 where d.name = @device
  6797.                     and u.vstart between d.low and d.high
  6798.                     and u.dbid = db_id())
  6799.     begin
  6800.         select @msg = "The device '" + @device
  6801.             + "' is not used by the database."
  6802.         print @msg
  6803.         return (1)
  6804.     end
  6805.  
  6806.     /*
  6807.     **  Now check that the segment references the device.
  6808.     */
  6809.     if not exists (select *
  6810.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6811.                 where u.segmap & @segbit = @segbit
  6812.                 and d.name = @device
  6813.                 and d.low <= u.size + u.vstart
  6814.                 and d.high >= u.size + u.vstart - 1
  6815.                 and u.dbid = db_id()
  6816.                 and d.status & 2 = 2)
  6817.     begin
  6818.         select @msg = "Segment '" + @segname
  6819.             + "' does not reference device '" + @device + "'."
  6820.         print @msg
  6821.         return (1)
  6822.     end
  6823.  
  6824.     /*
  6825.     **  Now check to see if this is the last unique device reference for the
  6826.     **  segment.  If it is then we don't want to drop the reference.
  6827.     */
  6828.     select distinct d.name into #temptable
  6829.         from master.dbo.sysusages u, master.dbo.sysdevices d
  6830.             where u.segmap & @segbit = @segbit
  6831.                 and d.low <= u.size + u.vstart
  6832.                 and d.high >= u.size + u.vstart - 1
  6833.                 and u.dbid = db_id()
  6834.                 and d.status & 2 = 2
  6835.  
  6836.     if (select count(*) from #temptable) <= 1
  6837.     begin
  6838.         select @msg = "There is only one unique device mapping for the segment '"
  6839.             + @segname + "' -- use sp_dropsegment with no device argument."
  6840.         print @msg
  6841.         return (1)
  6842.     end
  6843.  
  6844.     drop table #temptable
  6845. end
  6846.  
  6847. /*
  6848. **  If we're dropping the segment, go ahead drop the segment and
  6849. **  clear its bit in sysusages.
  6850. */
  6851. if @device = null
  6852. begin
  6853.     /*
  6854.     **  NOTE: Don't update master..sysusages and syssegments as a xact since
  6855.     **      it could cause problems for recovery.
  6856.     */
  6857.  
  6858.     delete syssegments
  6859.         where name = @segname
  6860.  
  6861.     /*
  6862.     **  Now clear the segment from device in sysusages.
  6863.     */
  6864.     update master.dbo.sysusages
  6865.         set segmap = segmap & (~@segbit)
  6866.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6867.                 where d.low <= u.size + u.vstart
  6868.                     and d.high >= u.size + u.vstart - 1
  6869.                     and u.dbid = db_id()
  6870.                     and d.status & 2 = 2
  6871. end
  6872.  
  6873. /*
  6874. **  We want to just clear the segment reference from the device.
  6875. */
  6876. else
  6877. begin
  6878.     update master.dbo.sysusages
  6879.         set segmap = segmap & (~@segbit)
  6880.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6881.                 where d.low <= u.size + u.vstart
  6882.                     and d.high >= u.size + u.vstart - 1
  6883.                     and u.dbid = db_id()
  6884.                     and d.status & 2 = 2
  6885.                     and d.name = @device
  6886.  
  6887. end
  6888.  
  6889. /*
  6890. **  Now we need to activate the new segment map.
  6891. */
  6892. declare @dbname    varchar(30)
  6893. select @dbname = db_name()
  6894. dbcc dbrepair (@dbname, remap)
  6895.  
  6896. if @device = null
  6897.     print "Segment dropped."
  6898. else print "Segment reference to device dropped."
  6899.  
  6900. /*
  6901. **  If there are no more segment references to the device,
  6902. **  print a warning.
  6903. */
  6904. if exists (select *
  6905.         from master.dbo.sysusages
  6906.             where segmap = 0
  6907.                 and dbid = db_id())
  6908. begin
  6909.     declare @nosegs    varchar(255),    /* list of unreferenced devices */
  6910.         @curdevice    varchar(30),
  6911.         @i    int
  6912.     
  6913.     select @curdevice = min(d.name)
  6914.         from master.dbo.sysusages u, master.dbo.sysdevices d
  6915.             where d.low <= u.size + u.vstart
  6916.                 and d.high >= u.size + u.vstart - 1
  6917.                 and u.dbid = db_id()
  6918.                 and d.status & 2 = 2
  6919.                 and segmap = 0
  6920.     select @nosegs = @curdevice, @i = 1
  6921.     while (@curdevice != null)
  6922.     begin
  6923.         select @curdevice = min(d.name)
  6924.             from master.dbo.sysusages u, master.dbo.sysdevices d
  6925.                 where d.low <= u.size + u.vstart
  6926.                     and d.high >= u.size + u.vstart - 1
  6927.                     and u.dbid = db_id()
  6928.                     and d.status & 2 = 2
  6929.                     and segmap = 0
  6930.                     and d.name > @curdevice
  6931.         if @curdevice != null
  6932.         begin
  6933.             select @nosegs = @nosegs + ", " + @curdevice
  6934.             select @i = @i + 1
  6935.         end
  6936.     end
  6937.  
  6938.     /*
  6939.     **  Get the device names.
  6940.     */
  6941.     select @msg =  "WARNING: There are no longer any segments referencing "
  6942.     if @i > 1
  6943.         select @msg = @msg + "devices '"
  6944.     else select @msg = @msg + "device '"
  6945.     select @msg = @msg + @nosegs + "'. "
  6946.     if @i > 1
  6947.         select @msg = @msg + "These devices "
  6948.     else select @msg = @msg + "This device "
  6949.     select @msg = @msg + "will no longer be used for space allocation."
  6950.     print @msg
  6951. end
  6952. return (0)
  6953. go
  6954. grant execute on sp_dropsegment to public
  6955. go
  6956. dump transaction master with truncate_only
  6957. go
  6958. /* dropserver    27.1    3/5/90 */
  6959. create procedure sp_dropserver
  6960. @server    varchar(30),            /* server name */
  6961. @droplogins    char(10) = NULL        /* drop all related logins? */
  6962. as
  6963.  
  6964. declare    @msg    varchar(200)
  6965.  
  6966. /*
  6967. **  If we're in a transaction, disallow this since it might make recovery
  6968. **  impossible.
  6969. */
  6970. if @@trancount > 0
  6971. begin
  6972.     print "Can't run sp_dropserver from within a transaction."
  6973.     return (1)
  6974. end
  6975.  
  6976. /*
  6977. **  Check to see if the server exists.
  6978. */
  6979. if not exists (select *
  6980.         from master.dbo.sysservers
  6981.             where srvname = @server)
  6982. begin
  6983.     select @msg = "There is no such server as '" + @server + "'."
  6984.     print @msg
  6985.     return (1)
  6986. end
  6987.  
  6988. /*
  6989. **  Check to see if there are any related logins in sysremotelogins.
  6990. */
  6991. if @droplogins = NULL
  6992. begin
  6993.     if exists (select *
  6994.             from master.dbo.sysremotelogins l,
  6995.                 master.dbo.sysservers s
  6996.             where s.srvid = l.remoteserverid
  6997.                 and s.srvname = @server)
  6998.     begin
  6999.         select @msg = "There are still remote logins for the server '"
  7000.             + @server + "'."
  7001.         print @msg
  7002.         return (1)
  7003.     end
  7004. end
  7005.  
  7006. /*
  7007. **  If @droplogins is true then drop any associated logins
  7008. */
  7009. else if @droplogins = "droplogins"
  7010. begin
  7011.     begin transaction dropserver
  7012.  
  7013.         delete master.dbo.sysremotelogins
  7014.             from master.dbo.sysremotelogins l,
  7015.                 master.dbo.sysservers s
  7016.             where s.srvid = l.remoteserverid
  7017.                 and s.srvname = @server
  7018.         if @@rowcount > 0
  7019.         begin
  7020.             select @msg = "Remote logins for remote server '"
  7021.                 + @server + "' have been dropped."
  7022.             print @msg
  7023.         end
  7024.  
  7025.         delete master.dbo.sysservers
  7026.             where srvname = @server
  7027.  
  7028.         print "Server dropped."
  7029.  
  7030.     commit transaction dropserver
  7031.  
  7032.     return (0)
  7033. end
  7034.  
  7035. /*
  7036. **  Bad argument to @droplogins.
  7037. */
  7038. else
  7039. begin
  7040.     print "Usage: sp_dropserver server [, droplogins]"
  7041.     return (1)
  7042. end
  7043.         
  7044. /*
  7045. **  Drop the server.
  7046. */
  7047. delete master.dbo.sysservers
  7048.     where srvname = @server
  7049.  
  7050. print "Server dropped."
  7051.  
  7052. return (0)
  7053. go
  7054. /* extendsegment    27.1    3/5/90 */
  7055. create procedure sp_extendsegment
  7056. @segname    varchar(30),    /* segment name */
  7057. @devname    varchar(30)    /* device name to put segment on */
  7058. as
  7059.  
  7060. declare @dbuid    smallint    /* id of the owner of the database */
  7061. declare @segbit    int        /* this is the bit to turn on in sysusages */
  7062. declare    @msg    char(100)
  7063. declare @retcode    int
  7064.  
  7065. /*
  7066. **  If we're in a transaction, disallow this since it might make recovery
  7067. **  impossible.
  7068. */
  7069. if @@trancount > 0
  7070. begin
  7071.     print "Can't run sp_extendsegment from within a transaction."
  7072.     return (1)
  7073. end
  7074.  
  7075. /*
  7076. **  You must be SA or the dbo to execute this sproc.
  7077. */
  7078. if suser_id() != 1 and user_id() != 1
  7079. begin
  7080.     print "You must be the System Administrator (SA) or the Database Owner (dbo) to execute this procedure."
  7081.     return (1)
  7082. end
  7083.  
  7084. /*
  7085. **  Check to see if the segment exists.
  7086. */
  7087. if not exists (select *
  7088.         from syssegments
  7089.             where name = @segname)
  7090. begin
  7091.     select @msg = "There is no such segment as '" + @segname + "'."
  7092.     print @msg
  7093.     return (1)
  7094. end
  7095.  
  7096. /*
  7097. **  See if the device exists.
  7098. */
  7099. if not exists (select *
  7100.         from master.dbo.sysdevices
  7101.             where name like @devname)
  7102. begin
  7103.     print "No such device exists -- run sp_helpdb to list the devices for the current database."
  7104.     return (1)
  7105. end
  7106.  
  7107. /*
  7108. **  Now see if the @dbname uses the @devname
  7109. */
  7110. if not exists (select *
  7111.         from master.dbo.sysusages u, master.dbo.sysdevices d
  7112.             where d.name = @devname
  7113.                 and u.vstart between d.low and d.high
  7114.                 and u.dbid = db_id())
  7115. begin
  7116.     print "The specified device is not used by the database."
  7117.     return (1)
  7118. end
  7119.  
  7120. /*
  7121. **  Check to see if the device is marked as a log device.
  7122. **  If so, print error.
  7123. */
  7124. if exists (select *
  7125.         from master.dbo.sysusages u, master.dbo.sysdevices d
  7126.             where d.name = @devname
  7127.                 and u.vstart between d.low and d.high
  7128.                 and u.dbid = db_id()
  7129.                 and segmap = 4)
  7130. begin
  7131.     select @msg = "'" + @devname + "' is reserved exclusively as a log device."
  7132.     print @msg
  7133.     return (1)
  7134. end
  7135.  
  7136. /*
  7137. ** Check for valid identifier.
  7138. */
  7139. execute @retcode = sp_validname @segname
  7140. if (@retcode != 0)
  7141. begin
  7142.     select @msg = "'" + @segname + "' is not a valid identifier."
  7143.     print @msg
  7144.     return (1)
  7145. end
  7146.  
  7147. /*
  7148. **  Now go ahead and define the new segment and add it to the segmap
  7149. **  of sysusages.
  7150. **  NOTE: Don't update master..sysusages and syssegments as a xact since
  7151. **      it could cause problems for recovery.
  7152. */
  7153.  
  7154. /*
  7155. **  Get the segment number for @segname.
  7156. */
  7157. select @segbit = segment
  7158.     from syssegments
  7159.         where name = @segname
  7160.  
  7161. /*
  7162. **  If @segbit is 2 then it's the logsegment and the logsegment
  7163. **  doesn't share devices with other segments.  In this case,
  7164. **  don't OR the @segbit in but just set segmap to 4.
  7165. */
  7166. if @segbit = 2
  7167. begin
  7168.     update master.dbo.sysusages
  7169.         set segmap = 4
  7170.             from master.dbo.sysusages u,
  7171.                 master.dbo.sysdevices d
  7172.             where d.name = @devname
  7173.                 and u.vstart between d.low and d.high
  7174.                 and u.dbid = db_id()
  7175. end
  7176.  
  7177. else
  7178. begin
  7179.     if (@segbit < 31)
  7180.         select @segbit = power(2, @segbit)
  7181.     else
  7182.         /*
  7183.         **  Since this is segment 31, power(2, 31) will
  7184.         **  overflow since segmap is an int.  We'll grab the
  7185.         **  machine-dependent bit mask from spt_values to set
  7186.         **  the right bit.
  7187.         */
  7188.         select @segbit = low
  7189.             from master.dbo.spt_values
  7190.                 where type = "E"
  7191.                     and number = 2
  7192.     /*
  7193.     **  Add the segment to @devname in sysusages.
  7194.     */
  7195.     update master.dbo.sysusages
  7196.         set segmap = segmap | @segbit
  7197.             from master.dbo.sysusages u,
  7198.                 master.dbo.sysdevices d
  7199.             where d.name = @devname
  7200.                 and u.vstart between d.low and d.high
  7201.                 and u.dbid = db_id()
  7202. end
  7203.  
  7204. /*
  7205. **  Now we need to activate the new segment map.
  7206. */
  7207. declare @dbname    varchar(30)
  7208. select @dbname = db_name()
  7209. dbcc dbrepair (@dbname, remap)
  7210. print "Segment extended."
  7211.  
  7212. /*
  7213. **  Get the segment number for @segname.
  7214. */
  7215. select @segbit = segment
  7216.     from syssegments
  7217.         where name = @segname
  7218.  
  7219. /*
  7220. **  If we are extending the logsegment, print out a message about what it
  7221. **  means to do so.
  7222. */
  7223. if @segbit = 2
  7224. begin
  7225.     select @msg = "Device '" + @devname + "' is now exclusively used by '"
  7226.         + @segname + "'."
  7227.     print @msg
  7228. end
  7229.  
  7230. return (0)
  7231. go
  7232. grant execute on sp_extendsegment to public
  7233. go
  7234. dump transaction master with truncate_only
  7235. go
  7236. /* fixindex 27.1 3/5/90 */
  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. /* foreignkey    27.1    3/5/90 */
  7324. create procedure sp_foreignkey
  7325. @tabname  varchar(92),        /* name of table that we are doing */
  7326. @pktabname varchar(92),        /* name of table with primary key */
  7327. @col1     varchar(30),         /* column name of table we are doing */
  7328. @col2     varchar(30) = NULL,
  7329. @col3     varchar(30) = NULL,
  7330. @col4     varchar(30) = NULL,
  7331. @col5     varchar(30) = NULL,
  7332. @col6     varchar(30) = NULL,
  7333. @col7     varchar(30) = NULL,
  7334. @col8     varchar(30) = NULL
  7335. as
  7336.  
  7337. declare @objida int        /* id of table we are doing */
  7338. declare @objidb int        /* id of table with primary key */
  7339. declare @uid smallint        /* id of owner of the talbe */
  7340. declare @cnt    int        /* how many columns are in foreign key */
  7341.  
  7342. declare @pkey1 int        /* colids of the foreign key */
  7343. declare @pkey2 int
  7344. declare @pkey3 int
  7345. declare @pkey4 int
  7346. declare @pkey5 int
  7347. declare @pkey6 int
  7348. declare @pkey7 int
  7349. declare @pkey8 int
  7350.  
  7351. declare @fkey1 int        /* colids of the primary key */
  7352. declare @fkey2 int
  7353. declare @fkey3 int
  7354. declare @fkey4 int
  7355. declare @fkey5 int
  7356. declare @fkey6 int
  7357. declare @fkey7 int
  7358. declare @fkey8 int
  7359.  
  7360. /*
  7361. **  Check to see that the tabname is local.
  7362. */
  7363. if @tabname like "%.%.%"
  7364. begin
  7365.     if substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  7366.     begin
  7367.         print "Table or view name must be in 'current' database."
  7368.         return (1)
  7369.     end
  7370. end
  7371. if @pktabname like "%.%.%"
  7372. begin
  7373.     if substring(@pktabname, 1, charindex(".", @pktabname) - 1) != db_name()
  7374.     begin
  7375.         print "Table or view name must be in 'current' database."
  7376.         return (1)
  7377.     end
  7378. end
  7379.  
  7380. /*
  7381. **  See if we can find the objects.  They must be a system table, user table,
  7382. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the
  7383. **  object type is -- it's more reliable than using sysobjects.type which
  7384. **  could change.
  7385. */
  7386. select @objida = id, @uid = uid
  7387.     from sysobjects
  7388.         where id = object_id(@tabname)
  7389.             and (sysstat & 7 = 1        /* system table */
  7390.                 or sysstat & 7 = 2    /* view */
  7391.                 or sysstat & 7 = 3)    /* user table */
  7392. /*
  7393. **  If either of the tables don't exist, quit.
  7394. */
  7395. if @objida is NULL
  7396. begin
  7397.     print "Foreign key table doesn't exist."
  7398.     return (1)
  7399. end
  7400. select @objidb = id
  7401.     from sysobjects
  7402.         where id = object_id(@pktabname)
  7403.             and (sysstat & 7 = 1
  7404.                 or sysstat & 7 = 2
  7405.                 or sysstat & 7 = 3)
  7406. if @objidb is NULL
  7407. begin
  7408.     print "Primary key table doesn't exist."
  7409.     return (1)
  7410. end
  7411.  
  7412. /*
  7413. **  Only the owner of the table can define it's foreign keys.
  7414. */
  7415. if @uid != user_id()
  7416. begin
  7417.     print "Only the owner of the table may define a foreign key."
  7418.     return (1)
  7419. end
  7420.  
  7421. /*
  7422. **  Now check to see that the foreign key columns exist.
  7423. */
  7424. select @cnt = 1, @fkey1 = colid
  7425.     from syscolumns
  7426.         where name = @col1
  7427.             and id = @objida
  7428. if @fkey1 = NULL
  7429. begin
  7430.     print "The table has no such first column."
  7431.     return (1)
  7432. end
  7433.  
  7434. if @col2 != NULL
  7435. begin
  7436.     select @cnt = @cnt + 1, @fkey2 = colid
  7437.         from syscolumns
  7438.             where name = @col2
  7439.                 and id = @objida
  7440.     if @fkey2 is NULL
  7441.     begin
  7442.         print "The table has no such second column."
  7443.         return (1)
  7444.     end
  7445. end
  7446. else goto foreign_ok
  7447.  
  7448. if @col3 != NULL
  7449. begin
  7450.     select @cnt = @cnt + 1, @fkey3 = colid
  7451.         from syscolumns
  7452.             where name = @col3
  7453.                 and id = @objida
  7454.     if @fkey3 is NULL
  7455.     begin
  7456.         print "The table has no such third column."
  7457.         return (1)
  7458.     end
  7459. end
  7460. else goto foreign_ok
  7461.  
  7462. if @col4 != NULL
  7463. begin
  7464.     select @cnt = @cnt + 1, @fkey4 = colid
  7465.         from syscolumns
  7466.             where name = @col4
  7467.                 and id = @objida
  7468.     if @fkey4 is NULL
  7469.     begin
  7470.         print "The table has no such fourth column."
  7471.         return (1)
  7472.     end
  7473. end
  7474. else goto foreign_ok
  7475.  
  7476. if @col5 != NULL
  7477. begin
  7478.     select @cnt = @cnt + 1, @fkey5 = colid
  7479.         from syscolumns
  7480.             where name = @col5
  7481.                 and id = @objida
  7482.     if @fkey5 is NULL
  7483.     begin
  7484.         print "The table has no such fifth column."
  7485.         return (1)
  7486.     end
  7487. end
  7488. else goto foreign_ok
  7489.  
  7490. if @col6 != NULL
  7491. begin
  7492.     select @cnt = @cnt + 1, @fkey6 = colid
  7493.         from syscolumns
  7494.             where name = @col6
  7495.                 and id = @objida
  7496.     if @fkey6 is NULL
  7497.     begin
  7498.         print "The table has no such sixth column."
  7499.         return (1)
  7500.     end
  7501. end
  7502. else goto foreign_ok
  7503.  
  7504. if @col7 != NULL
  7505. begin
  7506.     select @cnt = @cnt + 1, @fkey7 = colid
  7507.         from syscolumns
  7508.             where name = @col7
  7509.                 and id = @objida
  7510.     if @fkey7 is NULL
  7511.     begin
  7512.         print "The table has no such seventh column."
  7513.         return (1)
  7514.     end
  7515. end
  7516. else goto foreign_ok
  7517.  
  7518. if @col8 != NULL
  7519. begin
  7520.     select @cnt = @cnt + 1, @fkey8 = colid
  7521.         from syscolumns
  7522.             where name = @col8
  7523.                 and id = @objida
  7524.     if @fkey8 is NULL
  7525.     begin
  7526.         print "The table has no such eighth column."
  7527.         return (1)
  7528.     end
  7529. end
  7530.  
  7531.  
  7532. /*
  7533. **  If we made it this far then all the columns for the foreign key are ok.
  7534. */
  7535. foreign_ok:
  7536.  
  7537. /*
  7538. **  Now let's check out the primary key that the foreign key is on.
  7539. **  There must be the same number of columns in the key and the
  7540. **  base types of the columns must agree.
  7541. */
  7542. select @pkey1 = key1, @pkey2 = key2, @pkey3 = key3, @pkey4 = key4,
  7543.     @pkey5 = key5, @pkey6 = key6, @pkey7 = key7, @pkey8 = key8
  7544.         from syskeys
  7545.     where id = @objidb
  7546.         and type = 1
  7547.         and keycnt = @cnt
  7548.  
  7549. /*
  7550. **  If @pkey1 is null then there is no such primary key or the number of
  7551. **  columns in the primary key are not the same as the number of columns
  7552. **  in the foreign key.
  7553. */
  7554. if @pkey1 = NULL
  7555. begin
  7556.     if exists (select *
  7557.             from syskeys
  7558.         where id = @objidb
  7559.             and type = 1)
  7560.     begin
  7561.         print "Primary key does not exist with the same number of columns as the foreign key."
  7562.     end
  7563.     else
  7564.     begin
  7565.         print "Primary key does not exist."
  7566.     end
  7567.     return (1)
  7568. end
  7569.  
  7570. /*
  7571. **  Since we have the right number of columns in the foreign and primary keys,
  7572. **  check that there base datatypes agree.
  7573. */
  7574. if not exists (select *
  7575.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7576.             master.dbo.spt_values z
  7577.         where a.colid = @fkey1
  7578.             and a.id = @objida
  7579.             and b.colid = @pkey1
  7580.             and b.id = @objidb
  7581.             and y.type = "J"
  7582.             and a.type = y.low
  7583.             and z.type = "J"
  7584.             and b.type = z.low
  7585.             and y.number = z.number)
  7586. begin
  7587.     print "Datatypes of the first column in the keys are different."
  7588.     return (1)
  7589. end
  7590.  
  7591. if @fkey2 != NULL and not exists (select *
  7592.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7593.             master.dbo.spt_values z
  7594.         where a.colid = @fkey2
  7595.             and a.id = @objida
  7596.             and b.colid = @pkey2
  7597.             and b.id = @objidb
  7598.             and y.type = "J"
  7599.             and a.type = y.low
  7600.             and z.type = "J"
  7601.             and b.type = z.low
  7602.             and y.number = z.number)
  7603. begin
  7604.     print "Datatypes of the second column in the keys are different."
  7605.     return (1)
  7606. end
  7607. else goto doinsert
  7608.  
  7609. if @fkey3 != NULL and not exists (select *
  7610.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7611.             master.dbo.spt_values z
  7612.         where a.colid = @fkey3
  7613.             and a.id = @objida
  7614.             and b.colid = @pkey3
  7615.             and b.id = @objidb
  7616.             and y.type = "J"
  7617.             and a.type = y.low
  7618.             and z.type = "J"
  7619.             and b.type = z.low
  7620.             and y.number = z.number)
  7621. begin
  7622.     print "Datatypes of the third column in the keys are different."
  7623.     return (1)
  7624. end
  7625. else goto doinsert
  7626.  
  7627. if @fkey4 != NULL and not exists (select *
  7628.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7629.             master.dbo.spt_values z
  7630.         where a.colid = @fkey4
  7631.             and a.id = @objida
  7632.             and b.colid = @pkey4
  7633.             and b.id = @objidb
  7634.             and y.type = "J"
  7635.             and a.type = y.low
  7636.             and z.type = "J"
  7637.             and b.type = z.low
  7638.             and y.number = z.number)
  7639. begin
  7640.     print "Datatypes of the fourth column in the keys are different."
  7641.     return (1)
  7642. end
  7643.  
  7644. if @fkey5 != NULL and not exists (select *
  7645.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7646.             master.dbo.spt_values z
  7647.         where a.colid = @fkey5
  7648.             and a.id = @objida
  7649.             and b.colid = @pkey5
  7650.             and b.id = @objidb
  7651.             and y.type = "J"
  7652.             and a.type = y.low
  7653.             and z.type = "J"
  7654.             and b.type = z.low
  7655.             and y.number = z.number)
  7656. begin
  7657.     print "Datatypes of the fifth column in the keys are different."
  7658.     return (1)
  7659. end
  7660. else goto doinsert
  7661.  
  7662. if @fkey6 != NULL and not exists (select *
  7663.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7664.             master.dbo.spt_values z
  7665.         where a.colid = @fkey6
  7666.             and a.id = @objida
  7667.             and b.colid = @pkey6
  7668.             and b.id = @objidb
  7669.             and y.type = "J"
  7670.             and a.type = y.low
  7671.             and z.type = "J"
  7672.             and b.type = z.low
  7673.             and y.number = z.number)
  7674. begin
  7675.     print "Datatypes of the sixth column in the keys are different."
  7676.     return (1)
  7677. end
  7678. else goto doinsert
  7679.  
  7680. if @fkey7 != NULL and not exists (select *
  7681.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7682.             master.dbo.spt_values z
  7683.         where a.colid = @fkey7
  7684.             and a.id = @objida
  7685.             and b.colid = @pkey7
  7686.             and b.id = @objidb
  7687.             and y.type = "J"
  7688.             and a.type = y.low
  7689.             and z.type = "J"
  7690.             and b.type = z.low
  7691.             and y.number = z.number)
  7692. begin
  7693.     print "Datatypes of the seventh column in the keys are different."
  7694.     return (1)
  7695. end
  7696. else goto doinsert
  7697.  
  7698. if @fkey8 != NULL and not exists (select *
  7699.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  7700.             master.dbo.spt_values z
  7701.         where a.colid = @fkey8
  7702.             and a.id = @objida
  7703.             and b.colid = @pkey8
  7704.             and b.id = @objidb
  7705.             and y.type = "J"
  7706.             and a.type = y.low
  7707.             and z.type = "J"
  7708.             and b.type = z.low
  7709.             and y.number = z.number)
  7710. begin
  7711.     print "Datatypes of the eighth column in the keys are different."
  7712.     return (1)
  7713. end
  7714.  
  7715. /*
  7716. **  Everything is consistent so add the foreign key to syskeys.
  7717. */
  7718. doinsert:
  7719.  
  7720. insert syskeys (id, type, depid, keycnt, size, key1, key2, key3, key4, key5,
  7721.     key6, key7, key8, depkey1, depkey2, depkey3, depkey4, depkey5,
  7722.     depkey6, depkey7, depkey8)
  7723. values (@objida, 2, @objidb, @cnt, 0, @fkey1, @fkey2, @fkey3, @fkey4, @fkey5,
  7724.     @fkey6, @fkey7, @fkey8, @pkey1, @pkey2, @pkey3, @pkey4, @pkey5,
  7725.     @pkey6, @pkey7, @pkey8)
  7726.  
  7727. print "New foreign key added."
  7728.  
  7729. return (0)
  7730. go
  7731. grant execute on sp_foreignkey to public
  7732. go
  7733. dump transaction master with truncate_only
  7734. go
  7735. /* helpdb 27.1 3/5/90 */
  7736. create procedure sp_helpdb
  7737. @dbname varchar(30) = NULL            /* database name to change */
  7738. as
  7739.  
  7740. declare @showdev    bit
  7741. declare @allopts    int
  7742.  
  7743. set nocount on
  7744.  
  7745. /*
  7746. **  If no database name given, get 'em all.
  7747. */
  7748. if @dbname = null
  7749.     select @dbname = "%", @showdev = 0
  7750. else select @showdev = 1
  7751.  
  7752. /*
  7753. **  Sure the database exists
  7754. */
  7755. if not exists (select *
  7756.     from sysdatabases
  7757.         where name like @dbname)
  7758. begin
  7759.     print "No such database exists."
  7760.     return (1)
  7761. end
  7762.  
  7763. /*
  7764. **  Set allopts to be the sum of all possible user-settable database status
  7765. **  bits.
  7766. */
  7767. select @allopts = 7196
  7768.  
  7769. /*
  7770. **  Since we examine the status bits in sysdatabase and turn them
  7771. **  into english, we need a temporary table to build the descriptions.
  7772. */
  7773. create table #spdbdesc
  7774. (
  7775.     dbid    smallint null,
  7776.     dbdesc    varchar(102) null
  7777. )
  7778.  
  7779. /*
  7780. **  Initialize #spdbdesc from sysdatabases
  7781. */
  7782. insert into #spdbdesc (dbid)
  7783.         select dbid
  7784.             from sysdatabases
  7785.                 where name like @dbname
  7786.  
  7787. /*
  7788. **  Now for each dbid in #spdbdesc, build the database status
  7789. **  description.
  7790. */
  7791. declare @curdbid smallint        /* the one we're currently working on */
  7792. declare @dbdesc varchar(102)        /* the total description for the db */
  7793. declare @bitdesc varchar(30)        /* the bit description for the db */
  7794.  
  7795. /*
  7796. **  Set @curdbid to the first dbid.
  7797. */
  7798. select @curdbid = min(dbid)
  7799.     from #spdbdesc
  7800. while @curdbid != NULL
  7801. begin
  7802.     /*
  7803.     **  Initialize @dbdesc.
  7804.     */
  7805.     select @dbdesc = ""
  7806.  
  7807.     /*
  7808.     **  Check select into/bulk copy bit (4)
  7809.     */
  7810.     select @bitdesc = null
  7811.     select @bitdesc = v.name
  7812.         from spt_values v, sysdatabases d
  7813.             where d.dbid = @curdbid
  7814.                 and v.type = "D"
  7815.                 and d.status & v.number = 4
  7816.                 and v.number != @allopts       /* all options */
  7817.     if @bitdesc != null
  7818.     begin
  7819.         if @dbdesc != ""
  7820.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7821.         else select @dbdesc = @bitdesc
  7822.     end
  7823.  
  7824.     /*
  7825.     **  Check no checkpoint on recovery bit (16)
  7826.     */
  7827.     select @bitdesc = null
  7828.     select @bitdesc = v.name
  7829.         from spt_values v, sysdatabases d
  7830.             where d.dbid = @curdbid
  7831.                 and v.type = "D"
  7832.                 and d.status & v.number = 16
  7833.                 and v.number != @allopts /* all options */
  7834.     if @bitdesc != null
  7835.     begin
  7836.         if @dbdesc != ""
  7837.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7838.         else select @dbdesc = @bitdesc
  7839.     end
  7840.  
  7841.     /*
  7842.     **  Check single user bit (4096)
  7843.     */
  7844.     select @bitdesc = null
  7845.     select @bitdesc = v.name
  7846.         from spt_values v, sysdatabases d
  7847.             where d.dbid = @curdbid
  7848.                 and v.type = "D"
  7849.                 and d.status & v.number = 4096
  7850.                 and v.number != @allopts       /* all options */
  7851.     if @bitdesc != null
  7852.     begin
  7853.         if @dbdesc != ""
  7854.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7855.         else select @dbdesc = @bitdesc
  7856.     end
  7857.  
  7858.     /*
  7859.     **  Check dbo only bit (2048)
  7860.     */
  7861.     select @bitdesc = null
  7862.     select @bitdesc = v.name
  7863.         from spt_values v, sysdatabases d
  7864.             where d.dbid = @curdbid
  7865.                 and v.type = "D"
  7866.                 and d.status & v.number = 2048
  7867.                 and v.number != @allopts       /* all options */
  7868.     if @bitdesc != null
  7869.     begin
  7870.         if @dbdesc != ""
  7871.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7872.         else select @dbdesc = @bitdesc
  7873.     end
  7874.  
  7875.     /*
  7876.     **  Check read only bit (1024)
  7877.     */
  7878.     select @bitdesc = null
  7879.     select @bitdesc = v.name
  7880.         from spt_values v, sysdatabases d
  7881.             where d.dbid = @curdbid
  7882.                 and v.type = "D"
  7883.                 and d.status & v.number = 1024
  7884.                 and v.number != @allopts       /* all options */
  7885.     if @bitdesc != null
  7886.     begin
  7887.         if @dbdesc != ""
  7888.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7889.         else select @dbdesc = @bitdesc
  7890.     end
  7891.  
  7892.     /*
  7893.     **  Check load only bit (512)
  7894.     */
  7895.     select @bitdesc = null
  7896.     select @bitdesc = v.name
  7897.         from spt_values v, sysdatabases d
  7898.             where d.dbid = @curdbid
  7899.                 and v.type = "D"
  7900.                 and d.status & v.number = 512
  7901.                 and v.number != @allopts       /* all options */
  7902.     if @bitdesc != null
  7903.     begin
  7904.         if @dbdesc != ""
  7905.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7906.         else select @dbdesc = @bitdesc
  7907.     end
  7908.  
  7909.     /*
  7910.     **  Check not recovered only bit (256)
  7911.     */
  7912.     select @bitdesc = null
  7913.     select @bitdesc = v.name
  7914.         from spt_values v, sysdatabases d
  7915.             where d.dbid = @curdbid
  7916.                 and v.type = "D"
  7917.                 and d.status & v.number = 256
  7918.                 and v.number != @allopts       /* all options */
  7919.     if @bitdesc != null
  7920.     begin
  7921.         if @dbdesc != ""
  7922.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7923.         else select @dbdesc = @bitdesc
  7924.     end
  7925.  
  7926.     /*
  7927.     **  Check don't recover bit (32)
  7928.     */
  7929.     select @bitdesc = null
  7930.     select @bitdesc = v.name
  7931.         from spt_values v, sysdatabases d
  7932.             where d.dbid = @curdbid
  7933.                 and v.type = "D"
  7934.                 and d.status & v.number = 32
  7935.                 and v.number != @allopts       /* all options */
  7936.     if @bitdesc != null
  7937.     begin
  7938.         if @dbdesc != ""
  7939.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7940.         else select @dbdesc = @bitdesc
  7941.     end
  7942.  
  7943.     /*
  7944.     **  Check truncate log on checkpoint bit (8)
  7945.     */
  7946.     select @bitdesc = null
  7947.     select @bitdesc = v.name
  7948.         from spt_values v, sysdatabases d
  7949.             where d.dbid = @curdbid
  7950.                 and v.type = "D"
  7951.                 and d.status & v.number = 8
  7952.                 and v.number != @allopts       /* all options */
  7953.     if @bitdesc != null
  7954.     begin
  7955.         if @dbdesc != ""
  7956.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  7957.         else select @dbdesc = @bitdesc
  7958.     end
  7959.  
  7960.     /*
  7961.     **  If not flags are set, say so.
  7962.     */
  7963.     if @dbdesc = ""
  7964.     begin
  7965.         select @dbdesc = "no options set"
  7966.     end
  7967.  
  7968.     /*
  7969.     **  Save the description.
  7970.     */
  7971.     update #spdbdesc
  7972.         set dbdesc = @dbdesc
  7973.             from #spdbdesc
  7974.                 where dbid = @curdbid
  7975.  
  7976.     /*
  7977.     **  Now get the next, if any dbid.
  7978.     */
  7979.     select @curdbid = min(dbid)
  7980.         from #spdbdesc
  7981.             where dbid > @curdbid
  7982. end
  7983.  
  7984. /*
  7985. **  Now #spdbdesc is complete so we can print out the db info
  7986. */
  7987. select distinct name = substring(sysdatabases.name, 1, 24),
  7988.         db_size = substring(convert(varchar(10),
  7989.             round(
  7990.             (spt_values.low * convert(float, sum(sysusages.size)))
  7991.             / 1048576, 0))
  7992.             + " " + "MB", 1, 8),
  7993.         owner = substring(syslogins.name, 1, 24),
  7994.         dbid = sysdatabases.dbid,
  7995.         created = convert(char(11), sysdatabases.crdate),
  7996.         status = #spdbdesc.dbdesc
  7997.             from sysdatabases, syslogins, sysusages, #spdbdesc,
  7998.                 spt_values
  7999.         where sysdatabases.dbid = #spdbdesc.dbid
  8000.             and sysdatabases.suid = syslogins.suid
  8001.             and #spdbdesc.dbid = sysusages.dbid
  8002.             and spt_values.type = "E"
  8003.             and spt_values.number = 1
  8004.         group by #spdbdesc.dbid
  8005.         having sysdatabases.dbid = #spdbdesc.dbid
  8006.             and sysdatabases.suid = syslogins.suid
  8007.             and #spdbdesc.dbid = sysusages.dbid
  8008.             and spt_values.type = "E"
  8009.             and spt_values.number = 1
  8010.         order by sysdatabases.name
  8011.  
  8012. /*
  8013. **  If we are looking at one database, show it's device allocation.
  8014. */
  8015. if @showdev = 1
  8016. begin
  8017.     select device_fragments = sysdevices.name, size =
  8018.         convert(varchar(10),
  8019.             round(
  8020.             (spt_values.low * convert(float, size))
  8021.             / 1048576, 0)) + " " + "MB",
  8022.         usage = b.name
  8023.     from sysdatabases, sysusages, sysdevices, spt_values a, spt_values b
  8024.         where sysdatabases.dbid = sysusages.dbid
  8025.             and sysdevices.low <= size + vstart
  8026.             and sysdevices.high >= size + vstart - 1
  8027.             and sysdevices.status & 2 = 2
  8028.             and sysdatabases.name = @dbname
  8029.             and a.type = "E"
  8030.             and a.number = 1
  8031.             and b.type = "S"
  8032.             and sysusages.segmap & 7 = b.number
  8033.     order by 1
  8034.  
  8035.     /*
  8036.     **  If there is only one database and we are in it, show the
  8037.     **  segments.
  8038.     */
  8039.     if exists (select *
  8040.             from #spdbdesc
  8041.                 where db_id() = dbid)
  8042.     begin
  8043.         declare @curdevice    varchar(30),
  8044.             @curseg        smallint,
  8045.             @segbit        int
  8046.  
  8047.         delete #spdbdesc
  8048.  
  8049.         select @curdevice = min(d.name)
  8050.             from  master.dbo.sysusages u, master.dbo.sysdevices d
  8051.                 where u.dbid = db_id()
  8052.                     and d.low <= size + vstart
  8053.                     and d.high >= size + vstart - 1
  8054.                     and d.status & 2 = 2
  8055.         while (@curdevice != null)
  8056.         begin
  8057.             /*
  8058.             ** We need an inner loop here to go through
  8059.             **  all the possible segment.
  8060.             */
  8061.             select @curseg = min(segment)
  8062.                     from syssegments
  8063.             while (@curseg != null)
  8064.             begin
  8065.                 if (@curseg < 31)
  8066.                     select @segbit = power(2, @curseg)
  8067.                 else select @segbit = low
  8068.                     from master.dbo.spt_values
  8069.                         where type = "E"
  8070.                             and number = 2
  8071.                 insert into #spdbdesc
  8072.                     select @curseg, @curdevice
  8073.                         from master.dbo.sysusages u,
  8074.                             master.dbo.sysdevices d,
  8075.                             master.dbo.spt_values v
  8076.                     where u.segmap & @segbit = @segbit
  8077.                         and d.low <= u.size + u.vstart
  8078.                         and d.high >= u.size + u.vstart - 1
  8079.                         and u.dbid = db_id()
  8080.                         and d.status & 2 = 2
  8081.                         and v.number = 1
  8082.                         and v.type = "E"
  8083.                         and d.name = @curdevice
  8084.                 select @curseg = min(segment)
  8085.                         from syssegments
  8086.                             where segment > @curseg
  8087.             end
  8088.  
  8089.             select @curdevice = min(d.name)
  8090.                 from  master.dbo.sysusages u,
  8091.                     master.dbo.sysdevices d
  8092.                 where u.dbid = db_id()
  8093.                     and d.low <= size + vstart
  8094.                     and d.high >= size + vstart - 1
  8095.                     and d.status & 2 = 2
  8096.                     and d.name > @curdevice
  8097.         end
  8098.  
  8099.         /*
  8100.         **  One last check for any devices that have no segments.
  8101.         */
  8102.         insert into #spdbdesc
  8103.             select null, d.name
  8104.                 from master.dbo.sysusages u,
  8105.                     master.dbo.sysdevices d
  8106.             where u.segmap = 0
  8107.                 and d.low <= u.size + u.vstart
  8108.                 and d.high >= u.size + u.vstart - 1
  8109.                 and u.dbid = db_id()
  8110.                 and d.status & 2 = 2
  8111.  
  8112.         select distinct device = substring(dbdesc, 1, 30),
  8113.             segment = isnull(name, " -- unused by any segments --")
  8114.         from #spdbdesc, syssegments
  8115.             where dbid *= segment
  8116.         order by 1, 2
  8117.     end
  8118.  
  8119. end
  8120.  
  8121. drop table #spdbdesc
  8122. return (0)
  8123. go
  8124. grant execute on sp_helpdb to public
  8125. go
  8126. /* helpdevice 27.1 3/5/90 */
  8127. create procedure sp_helpdevice
  8128. @devname varchar(30) = "%"        /* device to check out */
  8129. as
  8130.  
  8131. /*
  8132. **  See if the device exists.
  8133. */
  8134. if not exists (select *
  8135.             from master.dbo.sysdevices
  8136.                 where name like @devname)
  8137. begin
  8138.     print "No such i/o device exists."
  8139.     return (1)
  8140. end
  8141.  
  8142. /*
  8143. **  Create a temporary table where we can build up a translation of
  8144. **  the device status bits.
  8145. */
  8146. create table #spdevtab
  8147. (
  8148.     name varchar(30),
  8149.     statusdesc varchar(250) null
  8150. )
  8151.  
  8152. set nocount on
  8153.  
  8154. /*
  8155. **  Initialize the temporary table with the names of the devices.
  8156. */
  8157. insert into #spdevtab (name)
  8158.     select name
  8159.         from master.dbo.sysdevices
  8160.             where name like @devname
  8161.  
  8162.  
  8163. /*
  8164. **  Now figure out what kind of controller type it is.  The type are
  8165. **  COMPLETELY machine dependent and are for UNIX.
  8166. **  cntrltype = 0    special
  8167. **            2    disk
  8168. **          3-4    floppy
  8169. **        5    tape
  8170. */
  8171. update #spdevtab
  8172.     set statusdesc = "special"
  8173.         from master.dbo.sysdevices d, #spdevtab
  8174.             where d.cntrltype = 0
  8175.                 and #spdevtab.name = d.name
  8176. update #spdevtab
  8177.     set statusdesc = "disk"
  8178.         from master.dbo.sysdevices d, #spdevtab
  8179.             where d.cntrltype = 2
  8180.                 and #spdevtab.name = d.name
  8181. update #spdevtab
  8182.     set statusdesc = "diskette"
  8183.         from master.dbo.sysdevices d, #spdevtab
  8184.             where d.cntrltype between 3 and 4
  8185.                 and #spdevtab.name = d.name
  8186. update #spdevtab
  8187.     set statusdesc = "tape"
  8188.         from master.dbo.sysdevices d, #spdevtab
  8189.             where d.cntrltype = 5
  8190.                 and #spdevtab.name = d.name
  8191. /*
  8192. **  If a floppy device, also figure out the floppy capacity which is listed
  8193. **  in sysdevices.high in number of 62k blocks.
  8194. */
  8195. if exists (select *
  8196.         from master.dbo.sysdevices d, #spdevtab
  8197.             where d.cntrltype between 3 and 4
  8198.                 and d.high > 0
  8199.                 and #spdevtab.name = d.name)
  8200. begin
  8201.     update #spdevtab
  8202.         set statusdesc = statusdesc + ", "
  8203.             + ltrim(str(((d.high * 63488.0) / 1000000.0), 5,1))
  8204.             + " MB"
  8205.         from master.dbo.sysdevices d, #spdevtab
  8206.             where d.cntrltype between 3 and 4
  8207.                 and d.high > 0
  8208.                 and #spdevtab.name = d.name
  8209. end
  8210.  
  8211. /*
  8212. **  If a tape device, also figure out the tape capacity which is listed
  8213. **  in sysdevices.high in number of 62k blocks.
  8214. */
  8215. if exists (select *
  8216.         from master.dbo.sysdevices d, #spdevtab
  8217.             where d.cntrltype = 5
  8218.                 and d.high > 0
  8219.                 and #spdevtab.name = d.name)
  8220. begin
  8221.     update #spdevtab
  8222.         set statusdesc = statusdesc + ", " + convert(varchar(10),
  8223.             round((d.high * convert(float, 63488)) / 1048576, 0))
  8224.             + " MB"
  8225.         from master.dbo.sysdevices d, #spdevtab
  8226.             where d.cntrltype = 5
  8227.                 and d.high > 0
  8228.                 and #spdevtab.name = d.name
  8229. end
  8230.  
  8231. update #spdevtab
  8232.     set statusdesc = "UNKNOWN DEVICE"
  8233.         from master.dbo.sysdevices d, #spdevtab
  8234.             where d.cntrltype > 8
  8235.                 and #spdevtab.name = d.name
  8236.  
  8237. /*
  8238. **  Now check out the status bits and turn them into english.
  8239. **  The mirror status bits are >= 32.
  8240. */
  8241. if exists (select *
  8242.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8243.         where v.type = "V" and v.number > -1
  8244.             and d.status >= 32
  8245.             and #spdevtab.name = d.name)
  8246. begin
  8247.     /*
  8248.     **  Check to see if the mirror is enabled. 0x200 (512) bit in status.
  8249.     */
  8250.     if exists (select *
  8251.         from master.dbo.sysdevices d, #spdevtab
  8252.             where d.status & 512 = 512
  8253.                 and #spdevtab.name = d.name)
  8254.     begin
  8255.         update #spdevtab
  8256.             set statusdesc = statusdesc + ", MIRROR ENABLED"
  8257.         from master.dbo.sysdevices d, #spdevtab
  8258.             where d.status & 512 = 512
  8259.                 and #spdevtab.name = d.name
  8260.     end
  8261.  
  8262.     if exists (select *
  8263.         from master.dbo.sysdevices d, #spdevtab
  8264.             where d.status & 512 != 512
  8265.                 and d.status >= 32
  8266.                 and #spdevtab.name = d.name)
  8267.     begin
  8268.         update #spdevtab
  8269.             set statusdesc = statusdesc + ", MIRROR DISABLED"
  8270.         from master.dbo.sysdevices d, #spdevtab
  8271.             where d.status & 512 != 512
  8272.                 and d.status >= 32
  8273.                 and #spdevtab.name = d.name
  8274.     end
  8275.  
  8276.     /* add mirror name */
  8277.     update #spdevtab
  8278.         set statusdesc = statusdesc + ", mirrored on '" + d.mirrorname
  8279.             + "'"
  8280.         from master.dbo.sysdevices d, #spdevtab
  8281.             where #spdevtab.name = d.name
  8282.                 and d.status >= 32
  8283. end
  8284.  
  8285. /*
  8286. **  Check to see if there is a mirrorname entry but mirroring not enabled.
  8287. **  If so, then one side of the mirror is off-line.
  8288. */
  8289. else if exists (select *
  8290.     from master.dbo.sysdevices d, #spdevtab
  8291.         where #spdevtab.name = d.name
  8292.         and d.mirrorname != null)
  8293. begin
  8294.     /*
  8295.     **  Figure out which side of the mirror is disabled.
  8296.     **  If 0x100 is on then phyname is disabled and mirrorname is enabled.
  8297.     */
  8298.     if exists (select *
  8299.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8300.             where v.type = "V" and v.number > -1
  8301.                 and d.status & v.number = 256
  8302.                 and #spdevtab.name = d.name)
  8303.     begin
  8304.         update #spdevtab
  8305.             set statusdesc = "only device '" + d.mirrorname
  8306.                 + "' of mirror is enabled -- device '"
  8307.                 + d.phyname + "' is disabled"
  8308.             from master.dbo.sysdevices d, master.dbo.spt_values v,
  8309.                 #spdevtab
  8310.             where v.type = "V" and v.number > -1
  8311.                 and d.status & v.number = 256
  8312.                 and #spdevtab.name = d.name
  8313.     end
  8314.     else if exists (select *
  8315.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8316.             where v.type = "V" and v.number > -1
  8317.                 and d.status & v.number = 64
  8318.                 and #spdevtab.name = d.name)
  8319.     begin
  8320.         update #spdevtab
  8321.             set statusdesc = "only device '" + d.phyname
  8322.                 + "' of mirror is enabled -- device '"
  8323.                 + d.mirrorname + "' is disabled"
  8324.             from master.dbo.sysdevices d, master.dbo.sysdevices e,
  8325.                 master.dbo.spt_values v, #spdevtab
  8326.             where v.type = "V" and v.number > -1
  8327.                 and d.status & v.number = 64
  8328.                 and #spdevtab.name = d.name
  8329.                 and e.status & 256 != 256
  8330.                 and #spdevtab.name = e.name
  8331.     end
  8332. end
  8333.  
  8334. /*
  8335. **  Status of 0x20 is "serial writes" for mirrored disks.
  8336. */
  8337. if exists (select *
  8338.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8339.         where v.type = "V" and v.number > -1
  8340.             and d.status & v.number = 32
  8341.             and #spdevtab.name = d.name)
  8342. begin
  8343.     update #spdevtab
  8344.         set statusdesc = statusdesc + ", " + v.name
  8345.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8346.             where v.type = "V" and v.number > -1
  8347.                 and d.status & v.number = 32
  8348.                 and #spdevtab.name = d.name
  8349. end
  8350.  
  8351.  
  8352.     update #spdevtab
  8353.         set statusdesc = statusdesc + ", nonserial writes"
  8354.         from master.dbo.sysdevices d, #spdevtab
  8355.             where   d.status > 32
  8356.                 and d.status & 32 != 32
  8357.                 and #spdevtab.name = d.name
  8358.  
  8359.  
  8360. /*
  8361. **  Status of 0x80 is "reads mirrored" for mirrored disks.
  8362. */
  8363. if exists (select *
  8364.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8365.         where v.type = "V" and v.number > -1
  8366.             and d.status & v.number = 128
  8367.             and #spdevtab.name = d.name)
  8368. begin
  8369.     update #spdevtab
  8370.         set statusdesc = statusdesc + ", " + v.name
  8371.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8372.             where v.type = "V" and v.number > -1
  8373.                 and d.status & v.number = 128
  8374.                 and #spdevtab.name = d.name
  8375. end
  8376.  
  8377. /*
  8378. **  Now check out the status bits and turn them into english.
  8379. **  Status of 0x10 is a dump device.
  8380. */
  8381. if exists (select *
  8382.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8383.         where v.type = "V" and v.number > -1
  8384.             and d.status & v.number = 16
  8385.             and #spdevtab.name = d.name)
  8386. begin
  8387.     update #spdevtab
  8388.         set statusdesc = statusdesc + ", " + v.name
  8389.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8390.             where v.type = "V" and v.number > -1
  8391.                 and d.status & v.number = 16
  8392.                 and #spdevtab.name = d.name
  8393. end
  8394.  
  8395. /*
  8396. **  Now check out the status bits and turn them into english.
  8397. **  Status of 0x01 is a default disk.
  8398. */
  8399. if exists (select *
  8400.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8401.         where v.type = "V" and v.number > -1
  8402.             and d.status & v.number = 1
  8403.             and #spdevtab.name = d.name)
  8404. begin
  8405.     update #spdevtab
  8406.         set statusdesc = statusdesc + ", " + v.name
  8407.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8408.             where v.type = "V" and v.number > -1
  8409.                 and d.status & v.number = 1
  8410.                 and #spdevtab.name = d.name
  8411. end
  8412.  
  8413. /*
  8414. **  Now check out the status bits and turn them into english.
  8415. **  Status of 0x02 is a physical disk.
  8416. */
  8417. if exists (select *
  8418.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8419.         where v.type = "V" and v.number > -1
  8420.             and d.status & v.number = 2
  8421.             and #spdevtab.name = d.name)
  8422. begin
  8423.     update #spdevtab
  8424.         set statusdesc = substring(statusdesc, 1, 225) + ", " + v.name
  8425.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8426.             where v.type = "V" and v.number > -1
  8427.                 and d.status & v.number = 2
  8428.                 and #spdevtab.name = d.name
  8429.     /*
  8430.     **  Add in it's size in MB.
  8431.     */
  8432.     update #spdevtab
  8433.         set statusdesc = statusdesc + ", " + convert(varchar(10),
  8434.             round(((d.high - d.low)
  8435.                 * convert(float, v.low)) / 1048576, 0)) + " MB"
  8436.         from master.dbo.sysdevices d, #spdevtab, master.dbo.spt_values v
  8437.             where d.status & 2 = 2
  8438.                 and #spdevtab.name = d.name
  8439.                 and v.number = 1
  8440.                 and v.type = "E"
  8441. end
  8442.  
  8443. /*
  8444. **  Now check out the status bits and turn them into english.
  8445. **  Status of 0x04 is a logical disk.
  8446. */
  8447. if exists (select *
  8448.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8449.         where v.type = "V" and v.number > -1
  8450.             and d.status & v.number = 4
  8451.             and #spdevtab.name = d.name)
  8452. begin
  8453.     update #spdevtab
  8454.         set statusdesc = substring(statusdesc, 1, 225) + ", " + v.name
  8455.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8456.             where v.type = "V" and v.number > -1
  8457.                 and d.status & v.number = 4
  8458.                 and #spdevtab.name = d.name
  8459. end
  8460.  
  8461. /*
  8462. **  Now check out the status bits and turn them into english.
  8463. **  Status of 0x08 is a skip tape header.
  8464. */
  8465. if exists (select *
  8466.     from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8467.         where v.type = "V" and v.number > -1
  8468.             and d.status & v.number = 8
  8469.             and #spdevtab.name = d.name)
  8470. begin
  8471.     update #spdevtab
  8472.         set statusdesc = substring(statusdesc, 1, 225) + ", " + v.name
  8473.         from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  8474.             where v.type = "V" and v.number > -1
  8475.                 and d.status & v.number = 8
  8476.                 and #spdevtab.name = d.name
  8477. end
  8478.  
  8479. set nocount off
  8480.  
  8481. /*
  8482. **  The device number is in the high byte of sysdevices.low so
  8483. **  spt_values tells us which byte to pick out.
  8484. */
  8485. select device_name = d.name, physical_name = substring(d.phyname, 1, 46),
  8486.     description = #spdevtab.statusdesc,
  8487.     d.status, d.cntrltype,
  8488.     device_number = convert(tinyint, substring(convert(binary(4), d.low),
  8489.         v.low, 1)),
  8490.     d.low, d.high
  8491.         from master.dbo.sysdevices d, #spdevtab, master.dbo.spt_values v
  8492.     where d.name = #spdevtab.name
  8493.         and v.type = "E"
  8494.         and v.number = 3
  8495.  
  8496. return (0)
  8497. go
  8498. grant execute on sp_helpdevice to public
  8499. go
  8500. dump transaction master with truncate_only
  8501. go
  8502. /* helpgroup 27.1 3/5/90 */
  8503. create procedure sp_helpgroup
  8504. @grpname varchar(30) = NULL        /* group name of interest */
  8505. as
  8506.  
  8507. /*
  8508. **  If no group name given, list all the groups.
  8509. **  User ids are <= 16383 and group ids are > 16383 or = 0.
  8510. */
  8511. if @grpname = NULL
  8512. begin
  8513.     select Group_name = name, Group_id = gid
  8514.         from sysusers
  8515.             where (uid > 16383 or uid = 0)
  8516.         order by name
  8517.  
  8518.     return (0)
  8519. end
  8520.  
  8521. /*
  8522. **  Check to see if group exists.
  8523. */
  8524. if not exists (select *
  8525.             from sysusers
  8526.         where name = @grpname
  8527.             and (uid > 16383 or uid = 0))
  8528. begin
  8529.     print "No such group exists in the current database."
  8530.     return (1)
  8531. end
  8532.  
  8533. /*
  8534. **  List the particulars for the group.
  8535. */
  8536. select Group_name = substring(g.name, 1, 25), Group_id = g.gid,
  8537.     Users_in_group = substring(u.name, 1, 25), Userid = u.uid
  8538.         from sysusers u, sysusers g
  8539.             where g.name like @grpname
  8540.             and g.uid = u.gid
  8541.             and (g.uid > 16383 or g.uid = 0)
  8542.             and u.uid != u.gid
  8543.         order by g.name, u.name
  8544.  
  8545. return (0)
  8546. go
  8547. grant execute on sp_helpgroup to public
  8548. go
  8549. /*  helplog 27.1 3/5/90 */
  8550. create procedure sp_helplog
  8551. as
  8552. declare @firstpage int,
  8553.     @devname varchar(255),
  8554.     @msg varchar(255)
  8555.  
  8556.  
  8557. /* Get the first page of the log from sysindexes */
  8558. select @firstpage = first
  8559.     from sysindexes
  8560.         where id = 8
  8561.  
  8562. /*
  8563. **  Get the name of the device which contains the first page of the log.
  8564. **  The device number is in the high byte of d.low and u.vstart and which is the
  8565. **  high byte is defined in spt_values.
  8566. */
  8567. select @devname = d.name
  8568.     from master.dbo.sysdevices d, master.dbo.sysusages u,
  8569.         master.dbo.spt_values v
  8570.     where (d.status & 2 = 2 or d.status & 4 = 4)
  8571.         and @firstpage >= u.lstart
  8572.         and @firstpage < u.lstart + u.size - 1
  8573.         and convert(tinyint, substring(convert(binary(4), d.low),
  8574.             v.low, 1))
  8575.           = convert(tinyint, substring(convert(binary(4), u.vstart),
  8576.                v.low, 1))
  8577.         and v.type = "E"
  8578.         and v.number = 3
  8579.                 and u.dbid = db_id()
  8580.  
  8581. /* Print out the name of the device which contains the first page of the log */
  8582. select @msg = "In database """ + (select db_name())
  8583.     + """, the log starts on device """ + @devname + """."
  8584.  
  8585. print @msg
  8586.  
  8587. return (0)
  8588. go
  8589. grant execute on sp_helplog to public
  8590. go
  8591. dump transaction master with truncate_only
  8592. go
  8593. /* helpindex 27.1 3/5/90 */
  8594. create procedure sp_helpindex
  8595. @objname varchar(92)            /* the table to check for indexes */
  8596. as
  8597.  
  8598. declare @objid int            /* the object id of the table */
  8599. declare @indid int            /* the index id of an index */
  8600. declare @keys varchar(200)        /* string to build up index key in */
  8601. declare @inddesc varchar(68)        /* string to build up index desc in */
  8602.  
  8603.  
  8604. /*
  8605. **  Check to see that the object names are local to the current database.
  8606. */
  8607. if @objname like "%.%.%" and
  8608.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  8609. begin
  8610.     print "Object name must be in 'current' database."
  8611.     return (1)
  8612. end
  8613.  
  8614. /*
  8615. **  Check to see the the table exists and initialize @objid.
  8616. */
  8617. select @objid = object_id(@objname)
  8618.  
  8619. /*
  8620. **  Table doesn't exist so return.
  8621. */
  8622. if @objid is NULL
  8623. begin
  8624.     print "Object does not exist."
  8625.     return (1)
  8626. end
  8627.  
  8628. /*
  8629. **  See if the object has any indexes.
  8630. **  Since there may be more than one entry in sysindexes for the object,
  8631. **  this select will set @indid to the index id of the first index.
  8632. */
  8633. select @indid = min(indid)
  8634.     from sysindexes
  8635.         where id = @objid
  8636.             and indid > 0
  8637.             and indid < 255
  8638.  
  8639. /*
  8640. **  If no indexes, return.
  8641. */
  8642. if @indid is NULL
  8643. begin
  8644.     print "Object does not have any indexes."
  8645.     return (1)
  8646. end
  8647.  
  8648. /*
  8649. **  Now check out each index, figure out it's type and keys and
  8650. **  save the info in a temporary table that we'll print out at the end.
  8651. */
  8652. create table #spindtab
  8653. (
  8654.     index_name    varchar(30),
  8655.     index_keys    varchar(255),
  8656.     index_description    varchar(68)
  8657. )
  8658.  
  8659. while @indid != NULL
  8660. begin
  8661.     
  8662.     /*
  8663.     **  First we'll figure out what the keys are.
  8664.     */
  8665.     declare @i int
  8666.     declare @thiskey varchar(30)
  8667.     declare @lastindid int
  8668.  
  8669.     select @keys = "", @i = 1
  8670.  
  8671.     set nocount on
  8672.  
  8673.     while @i <= 16
  8674.     begin
  8675.         select @thiskey = index_col(@objname, @indid, @i)
  8676.  
  8677.         if @thiskey = NULL
  8678.         begin
  8679.             goto keysdone
  8680.         end
  8681.  
  8682.         if @i > 1
  8683.         begin
  8684.             select @keys = @keys + ", "
  8685.         end
  8686.  
  8687.         select @keys = @keys + index_col(@objname, @indid, @i)
  8688.  
  8689.         /*
  8690.         **  Increment @i so it will check for the next key.
  8691.         */
  8692.         select @i = @i + 1
  8693.  
  8694.     end
  8695.  
  8696.  
  8697.     /*
  8698.     **  When we get here we now have all the keys.
  8699.     */
  8700.     keysdone:
  8701.         set nocount off
  8702.  
  8703.     /*
  8704.     **  Initialize the index description by figuring out if it's a
  8705.     **  clustered or nonclustered index.
  8706.     */
  8707.     if @indid = 1
  8708.     begin
  8709.         select @inddesc = "clustered"
  8710.     end
  8711.     if @indid > 1
  8712.     begin
  8713.         select @inddesc = "nonclustered"
  8714.     end
  8715.  
  8716.     /*
  8717.     **  Now we'll check out the status bits for this index and
  8718.     **  build an english description from them.
  8719.     */
  8720.  
  8721.     /*
  8722.     **  See if the index is unique (0x02).
  8723.     */
  8724.     if exists (select *
  8725.         from master.dbo.spt_values v, sysindexes i
  8726.             where i.status & v.number = v.number
  8727.                 and v.type = "I"
  8728.                 and v.number = 2
  8729.                 and i.id = @objid
  8730.                 and i.indid = @indid)
  8731.     begin
  8732.     select @inddesc = @inddesc + ", " + v.name
  8733.         from master.dbo.spt_values v, sysindexes i
  8734.             where i.status & v.number = v.number
  8735.                 and v.type = "I"
  8736.                 and v.number = 2
  8737.                 and i.id = @objid
  8738.                 and i.indid = @indid
  8739.     end
  8740.  
  8741.     /*
  8742.     **  See if the index is ignore_dupkey (0x01).
  8743.     */
  8744.     if exists (select *
  8745.         from master.dbo.spt_values v, sysindexes i
  8746.             where i.status & v.number = v.number
  8747.                 and v.type = "I"
  8748.                 and v.number = 1
  8749.                 and i.id = @objid
  8750.                 and i.indid = @indid)
  8751.     begin
  8752.     select @inddesc = @inddesc + ", " + v.name
  8753.         from master.dbo.spt_values v, sysindexes i
  8754.             where i.status & v.number = v.number
  8755.                 and v.type = "I"
  8756.                 and v.number = 1
  8757.                 and i.id = @objid
  8758.                 and i.indid = @indid
  8759.     end
  8760.  
  8761.     /*
  8762.     **  See if the index is ignore_dup_row (0x04).
  8763.     */
  8764.     if exists (select *
  8765.         from master.dbo.spt_values v, sysindexes i
  8766.             where i.status & v.number = v.number
  8767.                 and v.type = "I"
  8768.                 and v.number = 4
  8769.                 and i.id = @objid
  8770.                 and i.indid = @indid)
  8771.     begin
  8772.     select @inddesc = @inddesc + ", " + v.name
  8773.         from master.dbo.spt_values v, sysindexes i
  8774.             where i.status & v.number = v.number
  8775.                 and v.type = "I"
  8776.                 and v.number = 4
  8777.                 and i.id = @objid
  8778.                 and i.indid = @indid
  8779.     end
  8780.  
  8781.     /*
  8782.     **  See if the index is allow_dup_row (0x40).
  8783.     */
  8784.     if exists (select *
  8785.         from master.dbo.spt_values v, sysindexes i
  8786.             where i.status & v.number = v.number
  8787.                 and v.type = "I"
  8788.                 and v.number = 64
  8789.                 and i.id = @objid
  8790.                 and i.indid = @indid)
  8791.     begin
  8792.     select @inddesc = @inddesc + ", " + v.name
  8793.         from master.dbo.spt_values v, sysindexes i
  8794.             where i.status & v.number = v.number
  8795.                 and v.type = "I"
  8796.                 and v.number = 64
  8797.                 and i.id = @objid
  8798.                 and i.indid = @indid
  8799.     end
  8800.  
  8801.     /*
  8802.     **  Add the location of the data.
  8803.     */
  8804.     select @inddesc = @inddesc + " located on " + s.name
  8805.         from syssegments s, sysindexes i
  8806.             where s.segment = i.segment
  8807.                 and i.id = @objid
  8808.                 and i.indid = @indid
  8809.  
  8810.     /*
  8811.     **  Now we have the whole description for the index so we'll add
  8812.     **  the goods to the temporary table.
  8813.     */
  8814.     insert into #spindtab
  8815.         select name, @keys, @inddesc
  8816.             from sysindexes
  8817.                 where id = @objid
  8818.                     and indid = @indid
  8819.     /*
  8820.     **  Now move @indid to the next index.
  8821.     */
  8822.     select @lastindid = @indid
  8823.     select @indid = NULL
  8824.     select @indid = min(indid)
  8825.         from sysindexes
  8826.             where id = @objid
  8827.                 and indid > @lastindid
  8828.                 and indid < 255
  8829. end
  8830.  
  8831. /*
  8832. **  Now print out the contents of the temporary index table.
  8833. */
  8834. select index_name = substring(index_name, 1, 20),
  8835.     index_description = substring(index_description, 1, 56),
  8836.     index_keys = index_keys from #spindtab
  8837.  
  8838. drop table #spindtab
  8839.  
  8840. return (0)
  8841. go
  8842. grant execute on sp_helpindex to public
  8843. go
  8844. /* helpjoins 27.1 3/5/90 */
  8845. create procedure sp_helpjoins
  8846. @lefttab varchar(92),            /* name of first table to join */
  8847. @righttab varchar(92)            /* name of first table to join */
  8848. as
  8849.  
  8850. declare @leftid int            /* id of first table */
  8851. declare @rightid int            /* id of second table */
  8852. declare @foundit bit            /* flag to indicate joins found */
  8853.  
  8854. /*
  8855. **  This procedure returns any foreign or common joins.
  8856. **  If none exists, then it returns any joins that have the same usertype.
  8857. **  If none exists, it returns any joins that have the same physical type.
  8858. */
  8859.  
  8860. /*
  8861. **  Make sure the @objname is local to the current database.
  8862. */
  8863. if @lefttab like "%.%.%" and
  8864.     substring(@lefttab, 1, charindex(".", @lefttab) - 1) != db_name()
  8865. begin
  8866.     print "Object must be in your current database."
  8867.     return (1)
  8868. end
  8869. if @righttab like "%.%.%" and
  8870.     substring(@righttab, 1, charindex(".", @righttab) - 1) != db_name()
  8871. begin
  8872.     print "Object must be in your current database."
  8873.     return (1)
  8874. end
  8875.  
  8876. /*
  8877. **  See if we can find the objects.  They must be a system table, user table,
  8878. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the
  8879. **  object type is -- it's more reliable than using sysobjects.type which
  8880. **  could change.
  8881. */
  8882. select @leftid = id
  8883.     from sysobjects
  8884.         where id = object_id(@lefttab)
  8885.             and (sysstat & 7 = 1        /* system table */
  8886.                 or sysstat & 7 = 2    /* view */
  8887.                 or sysstat & 7 = 3)    /* user table */
  8888. select @rightid = id
  8889.     from sysobjects
  8890.         where id = object_id(@righttab)
  8891.             and (sysstat & 7 = 1        /* system table */
  8892.                 or sysstat & 7 = 2    /* view */
  8893.                 or sysstat & 7 = 3)    /* user table */
  8894.  
  8895. /*
  8896. **  If either of the tables don't exist, quit.
  8897. */
  8898. if @leftid is NULL
  8899. begin
  8900.     print "First table doesn't exist."
  8901.     return (1)
  8902. end
  8903. if @rightid is NULL
  8904. begin
  8905.     print "Second table doesn't exist."
  8906.     return (1)
  8907. end
  8908.  
  8909. create table #hjtab
  8910. (
  8911.     a1    varchar(30),
  8912.     a2    varchar(30),
  8913.     b1    varchar(30) null,
  8914.     b2    varchar(30) null,
  8915.     c1    varchar(30) null,
  8916.     c2    varchar(30) null,
  8917.     d1    varchar(30) null,
  8918.     d2    varchar(30) null,
  8919.     e1    varchar(30) null,
  8920.     e2    varchar(30) null,
  8921.     f1    varchar(30) null,
  8922.     f2    varchar(30) null,
  8923.     g1    varchar(30) null,
  8924.     g2    varchar(30) null,
  8925.     h1    varchar(30) null,
  8926.     h2    varchar(30) null
  8927. )
  8928.  
  8929. /*
  8930. **  Look for foreign key joins.
  8931. */
  8932. select @foundit = 0
  8933. if exists (select *
  8934.         from syskeys
  8935.             where type = 2
  8936.                 and id = @leftid
  8937.                 and depid = @rightid)
  8938. begin
  8939.     insert into #hjtab
  8940.     select distinct first_pair = col_name(id, key1), col_name(depid, depkey1),
  8941.         second_pair = col_name(id, key2), col_name(depid, depkey2),
  8942.         third_pair = col_name(id, key3), col_name(depid, depkey3),
  8943.         fourth_pair = col_name(id, key4), col_name(depid, depkey4),
  8944.         fifth_pair = col_name(id, key5), col_name(depid, depkey5),
  8945.         sixth_pair = col_name(id, key6), col_name(depid, depkey6),
  8946.         seventh_pair = col_name(id, key7), col_name(depid, depkey7),
  8947.         eighth_pair = col_name(id, key8), col_name(depid, depkey8)
  8948.     from syskeys
  8949.         where type = 2
  8950.             and id = @leftid
  8951.             and depid = @rightid
  8952.  
  8953.     select @foundit = 1
  8954. end
  8955.  
  8956. if exists (select *
  8957.         from syskeys
  8958.             where type = 2
  8959.                 and id = @rightid
  8960.                 and depid = @leftid)
  8961. begin
  8962.     insert into #hjtab
  8963.     select distinct first_pair = col_name(depid, depkey1), col_name(id, key1),
  8964.         second_pair = col_name(depid, depkey2), col_name(id, key2),
  8965.         third_pair = col_name(depid, depkey3), col_name(id, key3),
  8966.         fourth_pair = col_name(depid, depkey4), col_name(id, key4),
  8967.         fifth_pair = col_name(depid, depkey5), col_name(id, key5),
  8968.         sixth_pair = col_name(depid, depkey6), col_name(id, key6),
  8969.         seventh_pair = col_name(depid, depkey7), col_name(id, key7),
  8970.         eighth_pair = col_name(depid, depkey8), col_name(id, key8)
  8971.     from syskeys
  8972.         where type = 2
  8973.             and id = @rightid
  8974.             and depid = @leftid
  8975.  
  8976.     select @foundit = 1
  8977. end
  8978.  
  8979. /*
  8980. **  Look for common key joins.
  8981. */
  8982. if exists (select *
  8983.         from syskeys
  8984.             where type = 3
  8985.                 and id = @leftid
  8986.                 and depid = @rightid)
  8987. begin
  8988.     insert into #hjtab
  8989.     select distinct first_pair = col_name(id, key1), col_name(depid, depkey1),
  8990.         second_pair = col_name(id, key2), col_name(depid, depkey2),
  8991.         third_pair = col_name(id, key3), col_name(depid, depkey3),
  8992.         fourth_pair = col_name(id, key4), col_name(depid, depkey4),
  8993.         fifth_pair = col_name(id, key5), col_name(depid, depkey5),
  8994.         sixth_pair = col_name(id, key6), col_name(depid, depkey6),
  8995.         seventh_pair = col_name(id, key7), col_name(depid, depkey7),
  8996.         eighth_pair = col_name(id, key8), col_name(depid, depkey8)
  8997.     from syskeys
  8998.         where type = 3
  8999.             and id = @leftid
  9000.             and depid = @rightid
  9001.  
  9002.     select @foundit = 1
  9003. end
  9004.  
  9005. if exists (select *
  9006.         from syskeys
  9007.             where type = 3
  9008.                 and id = @rightid
  9009.                 and depid = @leftid)
  9010. begin
  9011.     insert into #hjtab
  9012.     select distinct first_pair = col_name(depid, depkey1), col_name(id, key1),
  9013.         second_pair = col_name(depid, depkey2), col_name(id, key2),
  9014.         third_pair = col_name(depid, depkey3), col_name(id, key3),
  9015.         fourth_pair = col_name(depid, depkey4), col_name(id, key4),
  9016.         fifth_pair = col_name(depid, depkey5), col_name(id, key5),
  9017.         sixth_pair = col_name(depid, depkey6), col_name(id, key6),
  9018.         seventh_pair = col_name(depid, depkey7), col_name(id, key7),
  9019.         eighth_pair = col_name(depid, depkey8), col_name(id, key8)
  9020.     from syskeys
  9021.         where type = 3
  9022.             and id = @rightid
  9023.             and depid = @leftid
  9024.  
  9025.     select @foundit = 1
  9026. end
  9027.  
  9028. /*
  9029. **  We got a foreignkey and we didn't get a common key.
  9030. */
  9031. if @foundit = 1
  9032. begin
  9033.     select distinct *
  9034.         from #hjtab
  9035.  
  9036.     drop table #hjtab
  9037.  
  9038.     return (0)
  9039. end
  9040.  
  9041. /*
  9042. **  We didn't find any defined joins so we'll look for common user types.
  9043. */
  9044. if exists (select *
  9045.         from syscolumns a, syscolumns b
  9046.             where a.id = @leftid
  9047.                 and b.id = @rightid
  9048.                 and a.usertype = b.usertype
  9049.                 and a.usertype > 99)
  9050. begin
  9051.     select distinct first_pair = col_name(a.id, a.colid),
  9052.         col_name(b.id, b.colid)
  9053.     from syscolumns a, syscolumns b
  9054.         where a.id = @leftid
  9055.             and b.id = @rightid
  9056.             and a.usertype = b.usertype
  9057.             and a.usertype > 99
  9058.     order by first_pair
  9059.  
  9060.     /*
  9061.     **  If we found something, we can stop.
  9062.     */
  9063.     if @@rowcount > 0
  9064.         return (0)
  9065. end
  9066.  
  9067. /*
  9068. **  We couldn't find anything so far so we'll see if we have anything
  9069. **  that just shares the same physical type.
  9070. */
  9071. if exists (select *
  9072.         from syscolumns a, syscolumns b, master.dbo.spt_values y,
  9073.             master.dbo.spt_values z
  9074.         where a.id = @leftid
  9075.             and b.id = @rightid
  9076.             and a.id != b.id
  9077.             and a.name = b.name
  9078.             and y.type = "J"
  9079.             and a.type = y.low
  9080.             and z.type = "J"
  9081.             and b.type = z.low
  9082.             and y.number = z.number)
  9083. begin
  9084.     select distinct first_pair = col_name(a.id, a.colid),
  9085.         col_name(b.id, b.colid)
  9086.     from syscolumns a, syscolumns b, master.dbo.spt_values y,
  9087.         master.dbo.spt_values z
  9088.     where a.id = @leftid
  9089.         and b.id = @rightid
  9090.         and a.id != b.id
  9091.         and a.name = b.name
  9092.         and y.type = "J"
  9093.         and a.type = y.low
  9094.         and z.type = "J"
  9095.         and b.type = z.low
  9096.         and y.number = z.number
  9097.     order by first_pair
  9098.  
  9099. end
  9100.  
  9101. return (0)
  9102. go
  9103. grant execute on sp_helpjoins to public
  9104. go
  9105. dump transaction master with truncate_only
  9106. go
  9107. /* helpkey 27.1 3/5/90 */
  9108. create procedure sp_helpkey
  9109. @tabname varchar(92) = null    /* table or view we want to check */
  9110. as
  9111.  
  9112. declare    @tabid    int        /* id of table or view we want to check */
  9113.  
  9114. /*
  9115. **  Check to see that the object names are local to the current database.
  9116. */
  9117. if @tabname like "%.%.%" and
  9118.     substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  9119. begin
  9120.     print "Table or view name must be in 'current' database."
  9121.     return (1)
  9122. end
  9123.  
  9124. /*
  9125. **  If @tabname is null, show all the keys.
  9126. */
  9127. if @tabname = null
  9128. begin
  9129.     select keytype = substring(v.name, 1, 10), object = object_name(k.id),
  9130.         related_object = isnull(object_name(k.depid), " -- none --"),
  9131.         object_keys = substring(col_name(k.id, key1) + ", "
  9132.             + isnull(col_name(k.id, key2), "*") + ", "
  9133.             + isnull(col_name(k.id, key3), "*") + ", "
  9134.             + isnull(col_name(k.id, key4), "*") + ", "
  9135.             + isnull(col_name(k.id, key5), "*") + ", "
  9136.             + isnull(col_name(k.id, key6), "*") + ", "
  9137.             + isnull(col_name(k.id, key7), "*") + ", "
  9138.             + isnull(col_name(k.id, key8), "*"), 1, 70),
  9139.         related_keys = substring(isnull(col_name(k.depid, depkey1), "*")
  9140.             + ", "
  9141.             + isnull(col_name(k.depid, depkey2), "*") + ", "
  9142.             + isnull(col_name(k.depid, depkey3), "*") + ", "
  9143.             + isnull(col_name(k.depid, depkey4), "*") + ", "
  9144.             + isnull(col_name(k.depid, depkey5), "*") + ", "
  9145.             + isnull(col_name(k.depid, depkey6), "*") + ", "
  9146.             + isnull(col_name(k.depid, depkey7), "*") + ", "
  9147.             + isnull(col_name(k.depid, depkey8), "*"), 1, 70)
  9148.     from syskeys k, master.dbo.spt_values v
  9149.         where k.type = v.number and v.type = 'K'
  9150.     order by object, keytype, related_object
  9151.  
  9152.     return (0)
  9153. end
  9154.  
  9155. /*
  9156. **  We're looking for a particular table or view.
  9157. */
  9158. else
  9159. begin
  9160.     /*
  9161.     **  Make sure the table or view exists.
  9162.     */
  9163.     select @tabid = object_id(@tabname)
  9164.  
  9165.     if @tabid = null
  9166.     begin
  9167.         print "No such table or view in the current database."
  9168.         return (1)
  9169.     end
  9170.  
  9171.     /*
  9172.     **  See if any keys exist.
  9173.     */
  9174.     if not exists (select *
  9175.             from syskeys
  9176.             where id = @tabid or depid = @tabid)
  9177.     begin
  9178.         print "No defined keys for this object."
  9179.         return (1)
  9180.     end
  9181.  
  9182.     select keytype = substring(v.name, 1, 10), object = object_name(k.id),
  9183.         related_object = isnull(object_name(k.depid), " -- none --"),
  9184.         object_keys = substring(col_name(k.id, key1) + ", "
  9185.             + isnull(col_name(k.id, key2), "*") + ", "
  9186.             + isnull(col_name(k.id, key3), "*") + ", "
  9187.             + isnull(col_name(k.id, key4), "*") + ", "
  9188.             + isnull(col_name(k.id, key5), "*") + ", "
  9189.             + isnull(col_name(k.id, key6), "*") + ", "
  9190.             + isnull(col_name(k.id, key7), "*") + ", "
  9191.             + isnull(col_name(k.id, key8), "*"), 1, 70),
  9192.         related_keys = substring(isnull(col_name(k.depid, depkey1), "*")
  9193.             + ", "
  9194.             + isnull(col_name(k.depid, depkey2), "*") + ", "
  9195.             + isnull(col_name(k.depid, depkey3), "*") + ", "
  9196.             + isnull(col_name(k.depid, depkey4), "*") + ", "
  9197.             + isnull(col_name(k.depid, depkey5), "*") + ", "
  9198.             + isnull(col_name(k.depid, depkey6), "*") + ", "
  9199.             + isnull(col_name(k.depid, depkey7), "*") + ", "
  9200.             + isnull(col_name(k.depid, depkey8), "*"), 1, 70)
  9201.     from syskeys k, master.dbo.spt_values v
  9202.         where k.type = v.number and v.type = 'K'
  9203.             and (k.id = @tabid or k.depid = @tabid)
  9204.     order by object, keytype, related_object
  9205.  
  9206.     return (0)
  9207. end
  9208. go
  9209. grant execute on sp_helpkey to public
  9210. go
  9211. /* objectsegment 27.1 3/5/90 */
  9212. create procedure sp_objectsegment
  9213. @objid    int
  9214. as
  9215.     /*
  9216.     ** Print out the create date for the object and it's
  9217.     ** data segment, if applicable.
  9218.     */
  9219.  
  9220.     /*
  9221.     **  Objects have the following value for sysstat & 7:
  9222.     **    0 - trigger
  9223.     **    1 - system table
  9224.     **    2 - view
  9225.     **    3 - user table
  9226.     **    4 - sproc
  9227.     **    5 - NOT UNUSED
  9228.     **    6 - default
  9229.     **    7 - rule
  9230.     */
  9231.     if exists (select *
  9232.             from sysobjects
  9233.                 where id = @objid
  9234.                     and sysstat & 7 in (1, 3))
  9235.     begin
  9236.         select Data_located_on_segment = s.name, When_created = crdate
  9237.             from sysobjects o, syssegments s, sysindexes i
  9238.                 where o.id = @objid
  9239.                     and i.id = o.id
  9240.                     and i.indid < 2
  9241.                     and i.segment = s.segment
  9242.     end
  9243.  
  9244.     /*
  9245.     **  It's not a table so segment is not applicable.
  9246.     */
  9247.     else
  9248.     begin
  9249.         select Data_located_on_segment = "not applicable",
  9250.             When_created = crdate
  9251.         from sysobjects
  9252.             where id = @objid
  9253.     end
  9254.  
  9255. return (0)
  9256.  
  9257. go
  9258. grant execute on sp_objectsegment to public
  9259. go
  9260. dump transaction master with truncate_only
  9261. go
  9262. /* help 27.1 3/5/90 */
  9263. create procedure sp_help
  9264. @objname varchar(92) = NULL            /* object name we're after */
  9265. as
  9266.  
  9267. declare @objid int                /* id of the object */
  9268. declare @sysstat smallint            /* the type of the object */
  9269.  
  9270. set nocount on
  9271.  
  9272. /*
  9273. **  If no @objname given, give a little info about all objects.
  9274. */
  9275. if @objname = NULL
  9276. begin
  9277.     select Name = substring(o.name, 1, 15),
  9278.         Owner = substring(user_name(uid), 1, 15),
  9279.         Object_type = substring(v.name + x.name, 1, 22)
  9280.     from sysobjects o, master.dbo.spt_values v, master.dbo.spt_values x
  9281.         where o.sysstat & 7 = v.number
  9282.             and v.type = "O"
  9283.             and x.type = "R"
  9284.             and o.userstat & -32768 = x.number
  9285.     order by Object_type desc, Name asc
  9286.  
  9287.     select User_type = substring(s.name, 1, 15),
  9288.            Storage_type = substring(st.name, 1, 15),
  9289.            Length = s.length,
  9290.            Nulls = s.allownulls,
  9291.            Default_name=substring(object_name(s.tdefault), 1, 15),
  9292.            Rule_name= substring(object_name(s.domain), 1, 15)
  9293.     from systypes s, systypes st
  9294.         where s.type = st.type
  9295.             and s.usertype > 99
  9296.             and st.usertype != 18 /* ignore sysname */
  9297.             and st.usertype < 100
  9298.     order by s.name
  9299.  
  9300.     return (0)
  9301. end
  9302.  
  9303. /*
  9304. **  Make sure the @objname is local to the current database.
  9305. */
  9306. if @objname like "%.%.%" and
  9307.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  9308. begin
  9309.     print "Object must be in your current database."
  9310.     return  (1)
  9311. end
  9312.  
  9313. /*
  9314. **  Now check to see if the @objname is in sysobjects.  It has to be either
  9315. **  in sysobjects or systypes.
  9316. */
  9317. select @objid = id, @sysstat = sysstat
  9318.     from sysobjects
  9319.         where id = object_id(@objname)
  9320.  
  9321. /*
  9322. **  It wasn't in sysobjects so we'll check in systypes.
  9323. */
  9324. if @objid = NULL
  9325. begin
  9326.     select @objid = usertype
  9327.         from systypes
  9328.             where name = @objname
  9329.  
  9330.     /*
  9331.     **  Time to give up -- @objname is not in sysobjects or systypes.
  9332.     */
  9333.     if @objid = NULL
  9334.     begin
  9335.         print "Object does not exist in this database."
  9336.         return  (1)
  9337.     end
  9338.  
  9339.     /*
  9340.     ** Print help about a data type
  9341.     */
  9342.     select Type_name = substring(s.name, 1, 15),
  9343.            Storage_type = substring(st.name, 1, 15),
  9344.            Length = s.length,
  9345.            Nulls = s.allownulls,
  9346.            Default_name=substring(object_name(s.tdefault), 1, 15),
  9347.            Rule_name= substring(object_name(s.domain), 1, 15)
  9348.     from systypes s, systypes st
  9349.         where s.usertype = @objid
  9350.             and s.type = st.type
  9351.             and st.usertype != 18 /* ignore sysname */
  9352.             and st.usertype < 100
  9353.  
  9354.     return (0)
  9355. end
  9356.  
  9357. /*
  9358. **  It's in sysobjects so print out the information.
  9359. */
  9360. select Name = o.name, Owner = user_name(uid),
  9361.     Type = substring(v.name + x.name, 1, 16)
  9362.     from sysobjects o, master.dbo.spt_values v,
  9363.         master.dbo.spt_values x
  9364.     where o.id = @objid
  9365.         and o.sysstat & 7 = v.number
  9366.         and v.type = "O"
  9367.         and x.type = "R"
  9368.         and o.userstat & -32768 = x.number
  9369.  
  9370. execute sp_objectsegment @objid
  9371.  
  9372. /*
  9373. **  Objects have the following value for sysstat & 7:
  9374. **    0 - trigger
  9375. **    1 - system table
  9376. **    2 - view
  9377. **    3 - user table
  9378. **    4 - sproc
  9379. **    5 - NOT UNUSED
  9380. **    6 - default
  9381. **    7 - rule
  9382. **
  9383. **  If the object is a system table, view, or user table, we want to check
  9384. **  out the objects columns here. Since the moneyn, intn and floatn are
  9385. **  internal representations, created a temporary table to save the values
  9386. **  and update those types to money, int and float when printing out.
  9387. */
  9388.  
  9389. if @sysstat & 7 in (1, 2, 3)
  9390. begin
  9391.  
  9392.     create table #sphelptab
  9393.     (
  9394.         col_name    char (30),
  9395.         col_type    char (30),    
  9396.         col_len        tinyint,    
  9397.         col_status    tinyint,
  9398.         col_def        char (30)  null,
  9399.         col_dom        char (30)  null
  9400.     )
  9401.  
  9402.     insert into #sphelptab
  9403.         select c.name, t.name, c.length, c.status,
  9404.             object_name(c.cdefault),object_name(c.domain)
  9405.         from syscolumns c, systypes t
  9406.         where c.id = @objid
  9407.             and c.usertype *= t.usertype
  9408.  
  9409.     update #sphelptab
  9410.         set col_type = "money"
  9411.         where col_type = "moneyn"
  9412.  
  9413.     update #sphelptab
  9414.         set col_type = "float"
  9415.         where col_type = "floatn"
  9416.  
  9417.     update #sphelptab
  9418.         set col_type = "int"
  9419.         where col_type = "intn"
  9420.  
  9421.     update #sphelptab
  9422.         set col_type = "datetime"
  9423.         where col_type = "datetimn"
  9424.  
  9425.     select Column_name = substring(col_name, 1, 15),
  9426.            Type = substring(col_type, 1, 15),
  9427.            Length = col_len,
  9428.            Nulls = convert(bit, (col_status & 8)),
  9429.            Default_name=substring(col_def, 1, 15),
  9430.            Rule_name = substring(col_dom, 1, 15)
  9431.     from #sphelptab
  9432.  
  9433. end
  9434.  
  9435. /*
  9436. **  For procedures, the parameters of the procedures are stored in syscolumns.
  9437. */
  9438. if @sysstat & 7 = 4
  9439. begin
  9440.     /*
  9441.     **  Does the sproc have any parameters?
  9442.     */
  9443.     if exists (select *
  9444.             from syscolumns c, systypes t
  9445.                 where c.id = @objid
  9446.                     and c.usertype *= t.usertype)
  9447.     begin
  9448.         select Parameter_name = substring(c.name,1,15),
  9449.                Type = substring(t.name,1,15),
  9450.                Length = c.length,
  9451.                Param_order=c.colid
  9452.         from syscolumns c, systypes t
  9453.             where c.id = @objid
  9454.                 and c.usertype *= t.usertype
  9455.     end
  9456. end
  9457.  
  9458. /*
  9459. **  If the object is a table, check out the indexes.
  9460. */
  9461. if @sysstat & 7 in (1, 3)
  9462.     execute dbo.sp_helpindex @objname
  9463.  
  9464. /*
  9465. **  If the object is a table or view, check out the keys.
  9466. */
  9467. if @sysstat & 7 in (1, 2, 3)
  9468.     execute dbo.sp_helpkey @objname
  9469.  
  9470. /*
  9471. ** Drop the table if we created it
  9472. */
  9473. if @sysstat & 7 in (1, 2, 3)
  9474.     drop table #sphelptab
  9475.  
  9476. return (0)
  9477. go
  9478. grant execute on sp_help to public
  9479. go
  9480. /* helprotect 27.1 3/5/90 */
  9481. create procedure sp_helprotect
  9482. @name varchar(92),            /* name of object or user to check */
  9483. @username varchar(30) = NULL        /* name of user to restrict check */
  9484. as
  9485.  
  9486. declare @low int            /* range of userids to check */
  9487. declare @high int
  9488. declare @objid int            /* id of @name if object */
  9489.  
  9490. /*
  9491. **  Check to see that the object names are local to the current database.
  9492. */
  9493. if @name like "%.%.%" and
  9494.     substring(@name, 1, charindex(".", @name) - 1) != db_name()
  9495. begin
  9496.     print "Name must be in 'current' database."
  9497.     return (1)
  9498. end
  9499.  
  9500. /*
  9501. **  Initialize the range of user ids to check.  We will either check all
  9502. **  users (the initial values) or just one in particular (@low = @high)
  9503. */
  9504. select @low = 0, @high = 32767
  9505.  
  9506. if @username is not NULL
  9507. begin
  9508.     /*
  9509.     **  Set @low and @high so the search will be restricted to
  9510.     **  a particular user.
  9511.     */
  9512.     select @low = uid, @high = uid
  9513.         from sysusers
  9514.             where name = @username
  9515.  
  9516.     /*
  9517.     **  @username isn't valid.
  9518.     */
  9519.     if @@rowcount = 0
  9520.     begin
  9521.         print "No such user exists in the database."
  9522.         return (1)
  9523.     end
  9524. end
  9525.  
  9526. /*
  9527. **  There are two cases handled by this procedure.  If the first parameter
  9528. **  is an object (table, view, procedure) then @name is taken as an object
  9529. **  name and the procedure will figure out permissions for the object.
  9530. **
  9531. **  If the first parameter is not one of the objects mentioned it will be
  9532. **  taken as user name and all the permissions for the user or group name
  9533. **  will be shown.
  9534. */
  9535.  
  9536. /*
  9537. **  Check to see if it's an object.
  9538. */
  9539. if exists (select *
  9540.         from sysobjects
  9541.             where id = object_id(@name)
  9542.                 and (sysstat & 7 = 1    /* system table */
  9543.                 or sysstat & 7 = 2    /* view */
  9544.                 or sysstat & 7 = 3    /* user table */
  9545.                 or sysstat & 7 = 4))    /* procedure */
  9546. begin
  9547.     /*
  9548.     **  This is the case where we will show the various permissions for
  9549.     **  an object, possibly restricted by a particular user.
  9550.     */
  9551.     select type = substring(b.name, 1, 6), action = a.name,
  9552.         user = substring(user_name(p.uid), 1, 15),
  9553.         column = substring(isnull(col_name(id, c.number), "All"), 1, 10)
  9554.             from sysprotects p, master.dbo.spt_values c,
  9555.                 master.dbo.spt_values a, master.dbo.spt_values b
  9556.             where convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  9557.                 & c.high != 0
  9558.                 and c.number <=
  9559.                     (select count(*)
  9560.                         from syscolumns
  9561.                         where id = object_id(@name))
  9562.                 and a.type = "T"
  9563.                 and a.number = p.action
  9564.                 and b.type = "T"
  9565.                 and b.number = p.protecttype
  9566.                 and p.id = object_id(@name)
  9567.                 and p.uid between @low and @high
  9568.             order by type, action, user, column
  9569.     return (0)
  9570. end
  9571.  
  9572. /*
  9573. **  Since @name is not an object let's try it as a user.
  9574. */
  9575. select @low = uid, @high = uid
  9576.     from sysusers
  9577.         where name = @name
  9578.  
  9579. /*
  9580. **  @name isn't a user either so return.
  9581. */
  9582. if @@rowcount = 0
  9583. begin
  9584.     print "No such object or user exists in the database."
  9585.     return (1)
  9586. end
  9587.  
  9588. /*
  9589. **  Now we have the user so run the same protection query as before but
  9590. **  restrict on user and not on object.
  9591. */
  9592. select distinct type = substring(b.name, 1, 6), action = a.name,
  9593.     object = substring(isnull(object_name(p.id), ""), 1, 15),
  9594.     column = substring(isnull(col_name(id, c.number), "All"), 1, 10)
  9595.         from sysprotects p, master.dbo.spt_values c,
  9596.             master.dbo.spt_values a, master.dbo.spt_values b
  9597.         where convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  9598.             & c.high != 0
  9599.             and c.number <= 255
  9600.             and a.type = "T"
  9601.             and a.number = p.action
  9602.             and b.type = "T"
  9603.             and b.number = p.protecttype
  9604.             and p.uid = @low
  9605.         order by type, object, column, action
  9606.  
  9607. return (0)
  9608. go
  9609. grant execute on sp_helprotect to public
  9610. go
  9611. dump transaction master with truncate_only
  9612. go
  9613. /* helptext 27.1 3/5/90 */
  9614. create procedure sp_helptext
  9615. @objname varchar(92)
  9616. as
  9617.  
  9618. /*
  9619. **  Make sure the @objname is local to the current database.
  9620. */
  9621. if (@objname like "%.%.%" and
  9622.     substring(@objname, 1, charindex(".", @objname) - 1) != db_name() )
  9623. begin
  9624.     print "Object must be in your current database."
  9625.     return (1)
  9626. end
  9627.  
  9628. /*
  9629. **  See if @objname exists.
  9630. */
  9631. if (object_id(@objname) = NULL)
  9632. begin
  9633.     print "No such object in the current database."
  9634.     return (1)
  9635. end
  9636.  
  9637. /*
  9638. **  Find out howmany lines of text are coming back,
  9639. **    and return if there are none.
  9640. */
  9641. select count(*)
  9642.     from syscomments
  9643. where id = object_id(@objname)
  9644.  
  9645. /*
  9646. **  Now get the text.
  9647. */
  9648. select text
  9649.     from syscomments
  9650. where id = object_id(@objname)
  9651.  
  9652. return (0)
  9653. go
  9654. grant execute on sp_helptext to public
  9655. go
  9656. /* helpuser 27.1 3/5/90 */
  9657. create procedure sp_helpuser
  9658. @name_in_db varchar(30) = NULL
  9659. as
  9660.  
  9661. set nocount on
  9662.  
  9663. /*
  9664. **  If no @name_in_db, list all the users.
  9665. **  User ids are <= 16383 and group ids are > 16383.
  9666. */
  9667. if @name_in_db = NULL
  9668. begin
  9669.     select Users_name = substring(u.name, 1, 17), ID_in_db = u.uid,
  9670.         Group_name = substring(g.name, 1, 17),
  9671.         Login_name = substring(m.name, 1, 17),
  9672.         Default_db = substring(m.dbname, 1, 15)
  9673.             from sysusers u, sysusers g,
  9674.                 master.dbo.syslogins m
  9675.             where u.suid *= m.suid
  9676.                 and u.gid *= g.uid
  9677.                 and u.uid <= 16383 and u.uid > 0
  9678.             order by Users_name
  9679.     return (0)
  9680. end
  9681.  
  9682. /*
  9683. **  See if the @name_in_db is a user.  If so, print out the info for
  9684. **  the user, including users pretending to be the user (sysaliases).
  9685. */
  9686. if exists (select *
  9687.         from sysusers
  9688.             where name = @name_in_db
  9689.                 and uid <= 16383 and uid > 0)
  9690. begin
  9691.     select Users_name = substring(u.name, 1, 17), ID_in_db = u.uid,
  9692.         Group_name = substring(g.name, 1, 17),
  9693.         Login_name = substring(m.name, 1, 17),
  9694.         Default_db = substring(m.dbname, 1, 15)
  9695.             from sysusers u, sysusers g,
  9696.                 master.dbo.syslogins m
  9697.             where u.suid *= m.suid
  9698.                 and u.gid *= g.uid
  9699.                 and u.name = @name_in_db
  9700.                 and u.uid <= 16383 and u.uid > 0
  9701.  
  9702.     declare @suid    smallint
  9703.  
  9704.     select @suid = suid
  9705.         from sysusers
  9706.             where name = @name_in_db
  9707.  
  9708.     /*
  9709.     ** If anyone is using @name_in_db as an alias print it out.
  9710.     */
  9711.     if exists (select *
  9712.             from sysalternates
  9713.         where altsuid = @suid)
  9714.     begin
  9715.         print "Users aliased to user."
  9716.         select Login_name = suser_name(suid)
  9717.             from sysalternates
  9718.                 where altsuid = @suid
  9719.             order by Login_name
  9720.     end
  9721.  
  9722.     return (0)
  9723. end
  9724.  
  9725. /*
  9726. **  Maybe the @name_in_db is a group.  Is so print out the info.
  9727. */
  9728. if exists (select *
  9729.         from sysusers
  9730.             where name = @name_in_db
  9731.                 and (uid > 16383 or uid = 0))
  9732. begin
  9733.     print "The name supplied is a group name."
  9734.     execute sp_helpgroup @name_in_db
  9735.     return (0)
  9736. end
  9737.  
  9738. /*
  9739. **  Last thing to try is that the @name_in_db might be aliased.
  9740. */
  9741. if exists (select *
  9742.         from sysalternates
  9743.             where suid = suser_id(@name_in_db))
  9744. begin
  9745.     print "The name supplied is aliased to another user."
  9746.     select Alias_name = substring(u.name, 1, 17), ID_in_db = u.uid,
  9747.         Group_name = substring(g.name, 1, 17),
  9748.         Login_name = substring(m.name, 1, 17),
  9749.         Default_db = substring(m.dbname, 1, 15)
  9750.             from sysusers u, sysusers g, sysalternates a,
  9751.                 master.dbo.syslogins m
  9752.             where u.suid *= m.suid
  9753.                 and u.gid *= g.uid
  9754.                 and u.uid <= 16383 and u.uid > 0
  9755.                 and a.suid = suser_id(@name_in_db)
  9756.                 and a.altsuid = u.suid
  9757.     return (0)
  9758. end
  9759.  
  9760. print "The name supplied is not a user, group, or aliased."
  9761.  
  9762. return (1)
  9763. go
  9764. grant execute on sp_helpuser to public
  9765. go
  9766. dump transaction master with truncate_only
  9767. go
  9768. /* lock 27.1 3/5/90 */
  9769. create procedure sp_lock
  9770. @spid1 int = NULL,        /* server process id to check for locks */
  9771. @spid2 int = NULL        /* other process id to check for locks */
  9772. as
  9773.  
  9774. /*
  9775. **  Show the locks for both parameters.
  9776. */
  9777. if @spid1 is not NULL
  9778. begin
  9779.     select spid, locktype = name, table_id = id, page,
  9780.         dbname = substring(db_name(dbid), 1, 15)
  9781.     from syslocks, spt_values
  9782.         where syslocks.type = spt_values.number
  9783.             and spt_values.type = "L"
  9784.             and spid in (@spid1, @spid2)
  9785. end
  9786.  
  9787. /*
  9788. **  No parameters, so show all the locks.
  9789. */
  9790. else
  9791. begin
  9792.     select spid, locktype = name, table_id = id, page,
  9793.         dbname = substring(db_name(dbid), 1, 15)
  9794.     from syslocks, spt_values
  9795.         where syslocks.type = spt_values.number
  9796.             and spt_values.type = "L"
  9797.     order by spid, dbname, table_id, locktype, page
  9798. end
  9799.  
  9800. return (0)
  9801. go
  9802. grant execute on sp_lock to public
  9803. go
  9804. /* logdevice    27.1    3/5/90 */
  9805. create procedure sp_logdevice
  9806. @dbname    varchar(30),        /* database name that has the syslogs */
  9807. @devname    varchar(30)    /* device name to put syslogs on */
  9808. as
  9809.  
  9810. declare @dbid    smallint    /* dbid of the database to be changed */
  9811. declare @dbuid    smallint    /* id of the owner of the database */
  9812. declare @logbit    int        /* this is the bit to turn on in sysusages */
  9813.  
  9814. select @logbit = 4        /* bit 3 is the one to turn on */
  9815.  
  9816. /*
  9817. **  Verify the database name and get the @dbid and @dbuid
  9818. */
  9819. select @dbid = dbid, @dbuid = suid
  9820.     from sysdatabases
  9821.         where name = @dbname
  9822.  
  9823. /*
  9824. **  If @dbname not found, say so and list the databases.
  9825. */
  9826. if @dbid = NULL
  9827. begin
  9828.     print "No such database -- run sp_helpdb to list databases."
  9829.     return (1)
  9830. end
  9831.  
  9832. /*
  9833. **  See if the device exists.
  9834. */
  9835. if not exists (select *
  9836.         from master.dbo.sysdevices
  9837.             where name like @devname)
  9838. begin
  9839.     print "No such device exists -- run sp_helpdevice to list the SQLServer devices."
  9840.     return (1)
  9841. end
  9842.  
  9843. /*
  9844. **  You must be SA or the dbo to execute this sproc.
  9845. */
  9846. if suser_id() != 1 and suser_id() != @dbuid
  9847. begin
  9848.     print "Only the System Administrator (SA) or the Database Owner (dbo) may move the syslogs table."
  9849.     return (1)
  9850. end
  9851.  
  9852. /*
  9853. **  Now see if the @dbname uses the @devname
  9854. */
  9855. if not exists (select *
  9856.         from sysusages u, sysdevices d
  9857.             where d.name = @devname
  9858.                 and u.vstart between d.low and d.high
  9859.                 and u.dbid = @dbid)
  9860. begin
  9861.     print "The specified device is not used by the database."
  9862.     return (1)
  9863. end
  9864.  
  9865. /*
  9866. **  Check if there are any additional segments on original device to be made
  9867. **  into log only (don't want to make the only segment not usable for data!)
  9868. */
  9869. if (select count(*)
  9870.     from master.dbo.sysusages u, master.dbo.sysdevices d
  9871.         where d.name = @devname
  9872.             and u.vstart between d.low and d.high
  9873.             and u.dbid = @dbid
  9874.             and lstart != 0) = 0
  9875. begin
  9876.     print "Can't make the only segment on original device log-only"
  9877.     return (1)
  9878. end
  9879.  
  9880. /*
  9881. **  clear the bit from any database segments that aren't already log only
  9882. */
  9883. update master.dbo.sysusages
  9884.     set segmap = segmap & ~@logbit
  9885.         from master.dbo.sysusages
  9886.             where dbid = @dbid
  9887.             and segmap != @logbit
  9888.  
  9889. /*
  9890. **  Now set the segments on @devname as log-only.
  9891. */
  9892. update master.dbo.sysusages
  9893.     set segmap = @logbit
  9894.         from master.dbo.sysusages u, master.dbo.sysdevices d
  9895.             where d.name = @devname
  9896.                 and u.vstart between d.low and d.high
  9897.                 and u.dbid = @dbid
  9898.                 and lstart != 0
  9899.  
  9900. /*
  9901. **  Now we need to activate the new segment map.
  9902. */
  9903. dbcc dbrepair (@dbname, remap)
  9904. print "Syslogs moved."
  9905.  
  9906. return (0)
  9907. go
  9908. grant execute on sp_logdevice to public
  9909. go
  9910. dump transaction master with truncate_only
  9911. go
  9912. /* lookup 27.1 3/5/90 */
  9913. create procedure sp_lookup
  9914. @typearg int,
  9915. @owname    varchar(30) = "%", /* owner of the desired objects (except indexes) */
  9916. @obname    varchar(30) = "%", /* name of the desired objects (including indexes) */
  9917. @indtab    varchar(30) = "%", /* table associated with the desired index */
  9918. @indown varchar(30) = "%"  /* owner of the desired index */
  9919. as
  9920.     create table #ddtab
  9921.         (name    varchar(30),
  9922.          owner    varchar(30),
  9923.          type    int,
  9924.          indtab    varchar(30))
  9925.  
  9926. declare @table    int
  9927. declare @view    int
  9928. declare @index    int
  9929. declare @sproc    int
  9930. declare @trgr    int
  9931. declare @dflt    int
  9932. declare @rule    int
  9933. declare @dtype    int
  9934. declare @db    int
  9935. declare @report int
  9936.  
  9937. select @table = 1, @view = 2, @index = 4, @sproc = 8, @trgr = 16, @dflt = 32,
  9938.     @rule = 64, @dtype = 128, @db = 256, @report = 512
  9939.  
  9940. if (@typearg & @table = @table)
  9941.     insert into #ddtab
  9942.         select name, user_name(uid), @table, '' from dbo.sysobjects
  9943.             where name like @obname
  9944.                 and user_name(uid) like @owname
  9945.                 and (sysstat & 7 = 1    /* system table */
  9946.                   or sysstat & 7 = 3)    /* user table */
  9947.  
  9948.  
  9949. if (@typearg & @view = @view)
  9950.     insert into #ddtab
  9951.         select name, user_name(uid), @view, '' from dbo.sysobjects
  9952.             where name like @obname
  9953.                 and user_name(uid) like @owname
  9954.                   and sysstat & 7 = 2        /* view */
  9955.  
  9956. if (@typearg & @index = @index)
  9957.     insert into #ddtab
  9958.         select i.name, user_name(o.uid), @index, o.name
  9959.             from dbo.sysindexes i, dbo.sysobjects o
  9960.                 where i.name like @obname
  9961.                     and i.indid > 0
  9962.                     and i.id = o.id
  9963.                     and user_name(o.uid) like @indown
  9964.                     and o.name = object_name(o.id)
  9965.                     and o.name like @indtab
  9966.  
  9967. if (@typearg & @report = @report)
  9968.     insert into #ddtab
  9969.         select name, user_name(uid), @report, '' from dbo.sysobjects
  9970.             where name like @obname
  9971.                 and user_name(uid) like @owname
  9972.                 and sysstat & 7 = 4        /* sproc */
  9973.                 and userstat & -32768 = -32768    /* report */
  9974.  
  9975. if (@typearg & @sproc = @sproc)
  9976. begin
  9977.     /*
  9978.     **  If we also wanted reports, don't pick up reports again as sprocs.
  9979.     */
  9980.     if (@typearg & @report = @report)
  9981.     begin
  9982.         insert into #ddtab
  9983.             select name, user_name(uid), @sproc, '' from dbo.sysobjects
  9984.              where name like @obname
  9985.                 and user_name(uid) like @owname
  9986.                 and sysstat & 7 = 4    /* stored procedure */
  9987.                 and userstat & -32768 != -32768    /* report */
  9988.     end
  9989.     else
  9990.     begin
  9991.         insert into #ddtab
  9992.             select name, user_name(uid), @sproc, '' from dbo.sysobjects
  9993.              where name like @obname
  9994.                 and user_name(uid) like @owname
  9995.                 and sysstat & 7 = 4    /* stored procedure */
  9996.     end
  9997. end
  9998.  
  9999. if (@typearg & @trgr = @trgr)
  10000.     insert into #ddtab
  10001.         select name, user_name(uid), @trgr, '' from dbo.sysobjects
  10002.             where name like @obname
  10003.                 and user_name(uid) like @owname
  10004.                   and sysstat & 7 = 0    /* trigger */
  10005.  
  10006. if (@typearg & @dflt = @dflt)
  10007.     insert into #ddtab
  10008.         select name, user_name(uid), @dflt, '' from dbo.sysobjects
  10009.              where name like @obname
  10010.                  and user_name(uid) like @owname
  10011.                  and sysstat & 7 = 6        /* default */
  10012.  
  10013. if (@typearg & @rule = @rule)
  10014.     insert into #ddtab
  10015.         select name, user_name(uid), @rule, '' from dbo.sysobjects
  10016.              where name like @obname
  10017.                  and user_name(uid) like @owname
  10018.                  and sysstat & 7 = 7        /* rule */
  10019.  
  10020. if (@typearg & @dtype = @dtype)
  10021.     insert into #ddtab
  10022.         select name, user_name(uid), @dtype, '' from dbo.systypes
  10023.             where name like @obname
  10024.                 and user_name(uid) like @owname
  10025.  
  10026. if (@typearg & @db = @db)
  10027.     insert into #ddtab
  10028.         select name, suser_name(suid), @db, ''
  10029.             from master.dbo.sysdatabases
  10030.                 where (name like @obname)
  10031.                     and suser_name(suid) like @owname
  10032.  
  10033. select * from #ddtab order by name
  10034.  
  10035. drop table #ddtab
  10036.  
  10037. return (0)
  10038. go
  10039. grant execute on sp_lookup to public
  10040. go
  10041. /* helpremotelogin 27.1 3/5/90 */
  10042. create procedure sp_helpremotelogin
  10043. @remoteserver varchar(30) = NULL,    /* remote server name */
  10044. @remotename varchar(30) = NULL        /* remote login name */
  10045. as
  10046.  
  10047. declare    @msg    varchar(100)
  10048.  
  10049. set nocount on
  10050.  
  10051. /*
  10052. **  If no server given, get 'em all.
  10053. */
  10054. if @remoteserver = null
  10055. begin
  10056.     select @remoteserver = "%"
  10057. end
  10058.  
  10059. else
  10060. begin
  10061.     if not exists (select *
  10062.             from master.dbo.sysservers s,
  10063.                 master.dbo.sysremotelogins r
  10064.             where s.srvid = r.remoteserverid
  10065.                 and s.srvname like @remoteserver
  10066.                 and s.srvid > 0)
  10067.     begin
  10068.         if @remoteserver = "%"
  10069.         begin
  10070.             select @msg = "There are no remote servers defined."
  10071.             print @msg
  10072.             return (1)
  10073.         end
  10074.  
  10075.         select @msg = "There are no remote logins for the remote server '"
  10076.             + @remoteserver + "'."
  10077.         print @msg
  10078.         return (1)
  10079.     end
  10080. end
  10081.  
  10082. /*
  10083. **  If no remotename given, get 'em all.
  10084. */
  10085. if @remotename = null
  10086. begin
  10087.     select @remotename = "%"
  10088. end
  10089.  
  10090. else
  10091. begin
  10092.     if not exists (select *
  10093.             from master.dbo.sysremotelogins
  10094.             where isnull(remoteusername, "") like @remotename)
  10095.     begin
  10096.         if @remotename = "%"
  10097.         begin
  10098.             select @msg = "There are no remotelogins defined."
  10099.             print @msg
  10100.             return (1)
  10101.         end
  10102.  
  10103.         select @msg = "There are no remote logins for '"
  10104.             + @remotename + "'."
  10105.         print @msg
  10106.         return (1)
  10107.     end
  10108. end
  10109.  
  10110. /*
  10111. **  Check for empty results.
  10112. */
  10113. if not exists (select *
  10114.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  10115.         where isnull(r.remoteusername, "") like @remotename
  10116.             and s.srvid = r.remoteserverid
  10117.                 and s.srvname like @remoteserver)
  10118. begin
  10119.     if ((@remoteserver = '%') and (@remotename = '%'))
  10120.         select @msg = "There are no remote logins."
  10121.     else select @msg = "There are no remote logins for '"
  10122.         + @remotename + "' on remote server '" + @remoteserver + "'."
  10123.     print @msg
  10124.     return (1)
  10125. end
  10126.  
  10127. /*
  10128. **  Select the information.
  10129. */
  10130. select server = substring(s.srvname, 1, 22),
  10131.     local_user_name =
  10132.         substring(isnull(suser_name(r.suid), "** use local name **"), 1, 22),
  10133.     remote_user_name =
  10134.         substring(isnull(r.remoteusername, "** mapped locally **"), 1, 22),
  10135.     options = substring(v.name, 1, 9)
  10136.         from master.dbo.sysservers s, master.dbo.sysremotelogins r,
  10137.             master.dbo.spt_values v
  10138.     where s.srvid = r.remoteserverid
  10139.         and s.srvname like @remoteserver
  10140.         and s.srvid > 0
  10141.         and isnull(r.remoteusername, "") like @remotename
  10142.         and v.type = "F"
  10143.         and v.number = r.status
  10144. order by server, remote_user_name
  10145.  
  10146. return (0)
  10147. go
  10148. grant execute on sp_helpremotelogin to public
  10149. go
  10150. dump transaction master with truncate_only
  10151. go
  10152. /* helpsegment 27.1 3/5/90 */
  10153. create procedure sp_helpsegment
  10154. @segname varchar(30) = NULL        /* segment name */
  10155. as
  10156.  
  10157. declare    @segbit    int        /* this is the bit version of the segment # */
  10158. declare    @segment    int    /* the segment number of the segment */
  10159.  
  10160. set nocount on
  10161.  
  10162. /*
  10163. **  If no segment name given, get 'em all.
  10164. */
  10165. if @segname = null
  10166. begin
  10167.     select *
  10168.         from syssegments
  10169.     return (0)
  10170. end
  10171.  
  10172. /*
  10173. **  Make sure the segment exists
  10174. */
  10175. if not exists (select *
  10176.     from syssegments
  10177.         where name = @segname)
  10178. begin
  10179.     print "No such segment exists."
  10180.     return (1)
  10181. end
  10182.  
  10183. /*
  10184. **  Show the syssegment entry, then the fragments and size it is on,
  10185. **  then any dependent objects in the database.
  10186. */
  10187. select *
  10188.     from syssegments
  10189.         where name = @segname
  10190.  
  10191. /*
  10192. **  Set the bit position for the segment.
  10193. */
  10194. select @segment = segment
  10195.     from syssegments
  10196.         where name = @segname
  10197.  
  10198. /*
  10199. **  Now set the segments on @devname sysusages.
  10200. */
  10201. if (@segment < 31)
  10202.     select @segbit = power(2, @segment)
  10203. else
  10204.     /*
  10205.     **  Since this is segment 31, power(2, 31) will overflow
  10206.     **  since segmap is an int.  We'll grab the machine-dependent
  10207.     **  bit mask from spt_values to set the right bit.
  10208.     */
  10209.     select @segbit = low
  10210.         from master.dbo.spt_values
  10211.             where type = "E"
  10212.                 and number = 2
  10213.  
  10214. select device = d.name, size = convert(varchar(20),
  10215.         round((u.size * convert(float, v.low)) / 1048576, 0)) + "MB"
  10216.     from master.dbo.sysusages u, master.dbo.sysdevices d,
  10217.         master.dbo.spt_values v
  10218.         where u.segmap & @segbit = @segbit
  10219.             and d.low <= u.size + u.vstart
  10220.             and d.high >= u.size + u.vstart - 1
  10221.             and u.dbid = db_id()
  10222.             and d.status & 2 = 2
  10223.             and v.number = 1
  10224.             and v.type = "E"
  10225.  
  10226. if exists (select *
  10227.         from sysindexes i, syssegments s
  10228.             where s.name = @segname
  10229.                 and s.segment = i.segment)
  10230. begin
  10231.     select table_name = object_name(i.id), index_name = i.name, i.indid
  10232.         from sysindexes i, syssegments s
  10233.             where s.name = @segname
  10234.                 and s.segment = i.segment
  10235.         order by table_name, indid
  10236. end
  10237.  
  10238. return (0)
  10239. go
  10240. grant execute on sp_helpsegment to public
  10241. go
  10242. /* @(#)helpsort    27.1    3/5/90 */
  10243. create procedure sp_helpsort
  10244. AS
  10245. set nocount on
  10246.  
  10247. declare @sortid tinyint, @charid tinyint, @status tinyint
  10248. declare @image binary(1)
  10249. declare @count int, @end_flag int
  10250.  
  10251.  
  10252. declare @string varchar(66)
  10253. create table #helpsort3 (string varchar(66))
  10254.  
  10255. /*
  10256. ** Get the sortorder ID, character set ID, and status
  10257. */
  10258. select @sortid = id, @charid = csid, @image = convert(binary(1), definition)
  10259.     from syscharsets, syscurconfigs
  10260.     where id = value and config = 123
  10261.  
  10262. /*
  10263. ** Get the name and description of the character set and sort order
  10264. */
  10265.  
  10266.  
  10267. if (@sortid = NULL)
  10268.  BEGIN
  10269.     select @sortid = 0, @charid = 0, @status = 0
  10270.     insert #helpsort3 values ("Character Set = 0, ascii_8")
  10271.     insert #helpsort3 values ("    8-bit ASCII Character Set - System Default")
  10272.     insert #helpsort3 values ("Sort Order = 0, bin_ascii_8")
  10273.     insert #helpsort3 values ("    Binary Ordering - System Default")
  10274.  END
  10275. else
  10276.  BEGIN
  10277.     select @status = convert(tinyint, @image)
  10278.  
  10279.     select @string = "Character Set = " + convert(varchar(4), id) + ", " + name
  10280.         from syscharsets
  10281.         where id = @charid and type = 1001
  10282.  
  10283.     insert into #helpsort3 values (@string)
  10284.  
  10285.     select @count = 1
  10286.     select @end_flag = 1
  10287.  
  10288.     while (@end_flag != 0)
  10289.     begin
  10290.         select @string = substring(description, @count, 62)
  10291.             from syscharsets
  10292.             where id = @charid and type = 1001
  10293.         select @end_flag = isnull(ascii(@string), 0)
  10294.  
  10295.         if (@end_flag != 0)
  10296.         begin
  10297.             select @string = "    " + @string
  10298.             insert #helpsort3 values (@string)
  10299.         end
  10300.         select @count = @count + 62
  10301.     end
  10302.  
  10303.     select @string = "Sort Order = " + convert(varchar(4), id) + ", " + name
  10304.         from syscharsets
  10305.         where id = @sortid and type = 2001
  10306.  
  10307.     insert into #helpsort3 values (@string)
  10308.  
  10309.     select @count = 1
  10310.     select @end_flag = 1
  10311.  
  10312.     while (@end_flag != 0)
  10313.     begin
  10314.         select @string = substring(description, @count, 62)
  10315.             from syscharsets
  10316.             where id = @sortid and type = 2001
  10317.         select @end_flag = isnull(ascii(@string), 0)
  10318.  
  10319.         if (@end_flag != 0)
  10320.         begin
  10321.             select @string = "    " + @string
  10322.             insert #helpsort3 values (@string)
  10323.         end
  10324.  
  10325.         select @count = @count + 62
  10326.     end
  10327. END
  10328.  
  10329. select "Sort Order Description" = string from #helpsort3
  10330.  
  10331. /*
  10332. ** Build the sorted list of characters for this sort order.
  10333. ** If it's not stored in spt_values yet, then put the results in there.
  10334. */
  10335. if not exists (select * from spt_values where type = 'Z' and high = @sortid)
  10336. BEGIN
  10337.  
  10338.     create table #helpsort1 (char_val char(1))
  10339.  
  10340.  
  10341.  
  10342.     if @charid = 0       /*    ascii_8 has valid values from 32 to 126 */
  10343.     begin
  10344.             select @count = 32
  10345.             while @count <= 126
  10346.             begin
  10347.                 insert #helpsort1 (char_val)
  10348.                     values (char(@count))
  10349.                 select @count = @count + 1
  10350.             end
  10351.  
  10352.     end
  10353.  
  10354.     if @charid = 1      /*     iso_1 has values from 32 to 126, 160 to 255 */
  10355.     begin
  10356.             select @count = 32
  10357.             while @count <= 126
  10358.             begin
  10359.                 insert #helpsort1 (char_val)
  10360.                     values (char(@count))
  10361.                 select @count = @count + 1
  10362.             end
  10363.  
  10364.             select @count = 160
  10365.             while @count <= 255
  10366.             begin
  10367.                    insert #helpsort1 (char_val)
  10368.                         values (char(@count))
  10369.                     select @count = @count + 1
  10370.             end
  10371.     end
  10372.  
  10373.  
  10374.     if @charid = 2      /*     cp850 has valid values from 0 to 255 */
  10375.     begin
  10376.  
  10377.             select @count = 32
  10378.             while @count <= 255
  10379.             begin
  10380.                 insert #helpsort1 (char_val)
  10381.                     values (char(@count))
  10382.                 select @count = @count + 1
  10383.             end
  10384.     end
  10385.  
  10386.     if @charid = 3      /*     cp437 has valid values from 0 to 255 */
  10387.     begin
  10388.             select @count =32
  10389.             while @count <= 255
  10390.             begin
  10391.                 insert #helpsort1 (char_val)
  10392.                     values (char(@count))
  10393.                 select @count = @count + 1
  10394.             end
  10395.     end
  10396.  
  10397.      if @charid > 3     /* User Character Set Not recognised by sp_helpsort */
  10398.     begin
  10399.         
  10400.             select "Unknown Character Set : " + name from syscharsets
  10401.                     where id = @sortid and type = 2001
  10402.         return (0)
  10403.  
  10404.     end
  10405.  
  10406.  
  10407.     select (char_val) into #helpsort2
  10408.         from #helpsort1
  10409.         order by char_val
  10410.  
  10411.     declare @char char(1)
  10412.     declare @char_str varchar(20)
  10413.     declare @incr int
  10414.     declare @seq_num int
  10415.     declare @rel char(1)
  10416.     declare @old_char char(1)
  10417.  
  10418.     select @char = char(0), @seq_num = 0, @count = 0, @incr = 8, @end_flag = 1
  10419.     select @rel = " ", @old_char = NULL
  10420.  
  10421.     set rowcount 1
  10422.  
  10423.     while @end_flag != 0
  10424.     begin
  10425.  
  10426.         select @char_str = ""
  10427.  
  10428.     /* If PST_NOTUNIQ is set, then possibly caseless */
  10429.     if (@status & 8 = 8)
  10430.             while (@count < @incr  and  @end_flag != 0)
  10431.             begin
  10432.                 select @char = char_val
  10433.                     from #helpsort2
  10434.                     where char_val >= @char
  10435.                 select @end_flag = @@rowcount
  10436.  
  10437.         if (@end_flag != 0)
  10438.         begin
  10439.             if (@char = @old_char)
  10440.             select @rel = "="
  10441.             else
  10442.             select @rel = " "
  10443.  
  10444.             select @old_char = @char
  10445.             select @char_str = @char_str + @rel + @char
  10446.             delete from #helpsort2 where ascii(@char) = ascii(char_val)
  10447.  
  10448.             select @count = @count + 1
  10449.         end
  10450.             end
  10451.     else
  10452.             while (@count < @incr  and  @end_flag != 0)
  10453.             begin
  10454.                 select @char = char_val
  10455.                     from #helpsort2
  10456.                     where char_val > @char
  10457.                 select @end_flag = @@rowcount
  10458.  
  10459.         if (@end_flag != 0)
  10460.         begin
  10461.             select @char_str = @char_str + @rel + @char
  10462.             delete from #helpsort2 where ascii(@char) = ascii(char_val)
  10463.  
  10464.             select @count = @count + 1
  10465.         end
  10466.             end
  10467.  
  10468.         select @incr = @incr + 8, @seq_num = @seq_num + 1
  10469.         insert spt_values (name, number, type, low, high)
  10470.         values (@char_str, @seq_num, 'Z', @charid, @sortid)
  10471.     end
  10472.  
  10473.     set rowcount 0
  10474.  
  10475. END
  10476.  
  10477. /*
  10478. ** Display the sorted list of characters.
  10479. */
  10480. select @count = 1
  10481. select @end_flag = 1
  10482. select @incr = 5
  10483.  
  10484. truncate table #helpsort3
  10485.  
  10486. while @end_flag != 0
  10487. BEGIN
  10488.     select @string = ""
  10489.  
  10490.     while @count < @incr
  10491.     begin
  10492.         select @string = @string + substring(name, 2, 19)
  10493.             from spt_values
  10494.             where type = 'Z' and number = @count and high = @sortid
  10495.         select @end_flag = @@rowcount
  10496.         select @count = @count + 1
  10497.     end
  10498.  
  10499.     select @incr = @incr + 4
  10500.  
  10501.     insert into #helpsort3 values (@string)
  10502. END
  10503.  
  10504. select "Characters, in Order" = string
  10505.     from #helpsort3
  10506. set nocount off
  10507. return(0)
  10508. go
  10509. grant execute on sp_helpsort to public
  10510. go
  10511. dump transaction master with truncate_only
  10512. go
  10513. /* helpserver 27.1 3/5/90 */
  10514. create procedure sp_helpserver
  10515. @server varchar(30) = NULL        /* server name */
  10516. as
  10517.  
  10518. declare    @msg    varchar(100)
  10519.  
  10520. set nocount on
  10521.  
  10522. /*
  10523. **  If no server name given, get 'em all.
  10524. */
  10525. if @server = null
  10526. begin
  10527.     select @server = "%"
  10528. end
  10529.  
  10530. /*
  10531. **  Does the server exist?
  10532. */
  10533. if not exists (select *
  10534.         from master.dbo.sysservers
  10535.             where srvname like @server
  10536.                 or srvnetname like @server)
  10537. begin
  10538.     if @server = "%"
  10539.         select @msg = "There are no servers defined."
  10540.     else select @msg = "There is no server named '" + @server + "'."
  10541.     print @msg
  10542.     return (1)
  10543. end
  10544.     
  10545. /*
  10546. **  Display server information.
  10547. **  First put it into a temp table so we can blot inappropriate status
  10548. **  for the local server.  It's ugly but I couldn't think of another way.
  10549. */
  10550. create table #spt_server
  10551. (
  10552.     name    varchar(30),
  10553.     network    varchar(30),
  10554.     status    varchar(20),
  10555.     id    smallint
  10556. )
  10557. insert into #spt_server
  10558.     select s.srvname, s.srvnetname, status = isnull(v.name, ""),
  10559.         id = s.srvid
  10560.     from master.dbo.sysservers s, master.dbo.spt_values v
  10561.         where s.srvstatus *= v.number
  10562.             and v.type = "A"
  10563.             and (s.srvname like @server
  10564.                 or s.srvnetname like @server)
  10565. update #spt_server
  10566.     set status = ""
  10567.         from master.dbo.sysservers s, #spt_server t
  10568.             where s.srvid = 0
  10569.                 and s.srvname = t.name
  10570.  
  10571. select name, network_name = substring(network, 1, 28),
  10572.     status = substring(status, 1, 11), id = convert(char(4), id)
  10573.         from #spt_server
  10574.     order by name
  10575.  
  10576. return (0)
  10577. go
  10578. grant execute on sp_helpserver to public
  10579. go
  10580. /* markreport 27.1 3/5/90 */
  10581. create procedure sp_markreport
  10582. @reportname varchar(92)        /* proc name we want to mark as report */
  10583. as
  10584.  
  10585. /*
  10586. **  Make sure the @reportname is local to the current database.
  10587. */
  10588. if @reportname like "%.%.%" and
  10589.     substring(@reportname, 1, charindex(".", @reportname) - 1) != db_name()
  10590. begin
  10591.     print "Report must be in your current database."
  10592.     return (1)
  10593. end
  10594.  
  10595. /*
  10596. **  Make sure that it actually exists as a sproc.
  10597. */
  10598. if not exists (select *
  10599.         from sysobjects
  10600.             where id = object_id(@reportname)
  10601.             and sysstat & 7 = 4    /* stored procedure */
  10602.             and uid = user_id())
  10603. begin
  10604.     print "No such report exists."
  10605.     return (1)
  10606. end
  10607.  
  10608. update sysobjects
  10609.     set userstat = userstat | -32768    /* high bit means report */
  10610.         from sysobjects
  10611.             where id = object_id(@reportname)
  10612.             and sysstat & 7 = 4        /* stored procedure */
  10613.             and uid = user_id()
  10614.  
  10615. return (0)
  10616. go
  10617. grant execute on sp_markreport to public
  10618. go
  10619. dump transaction master with truncate_only
  10620. go
  10621. /* monitor 27.1 3/5/90 */
  10622. create procedure sp_monitor
  10623. as
  10624.  
  10625. /*
  10626. **  Declare variables to be used to hold current monitor values.
  10627. */
  10628. declare @now         datetime
  10629. declare @cpu_busy     int
  10630. declare @io_busy    int
  10631. declare @idle        int
  10632. declare @pack_received    int
  10633. declare @pack_sent    int
  10634. declare @pack_errors    int
  10635. declare @connections    int
  10636. declare @total_read    int
  10637. declare @total_write    int
  10638. declare @total_errors    int
  10639.  
  10640. declare @oldcpu_busy     int    /* used to see if DataServer has been rebooted */
  10641. declare @interval    int
  10642. declare @mspertick    int    /* milliseconds per tick */
  10643.  
  10644. /*
  10645. **  If we're in a transaction, disallow this since it might make recovery
  10646. **  impossible.
  10647. */
  10648. if @@trancount > 0
  10649. begin
  10650.     print "Can't run sp_monitor from within a transaction."
  10651.     return (1)
  10652. end
  10653.  
  10654. /*
  10655. **  Set @mspertick.  This is just used to make the numbers easier to handle
  10656. **  and avoid overflow.
  10657. */
  10658. select @mspertick = convert(int, @@timeticks / 1000.0)
  10659.  
  10660. /*
  10661. **  Get current monitor values.
  10662. */
  10663. select
  10664.     @now = getdate(),
  10665.     @cpu_busy = @@cpu_busy,
  10666.     @io_busy = @@io_busy,
  10667.     @idle = @@idle,
  10668.     @pack_received = @@pack_received,
  10669.     @pack_sent = @@pack_sent,
  10670.     @connections = @@connections,
  10671.     @pack_errors = @@packet_errors,
  10672.     @total_read = @@total_read,
  10673.     @total_write = @@total_write,
  10674.     @total_errors = @@total_errors
  10675.  
  10676. /*
  10677. **  Check to see if DataServer has been rebooted.  If it has then the
  10678. **  value of @@cpu_busy will be less than the value of spt_monitor.cpu_busy.
  10679. **  If it has update spt_monitor.
  10680. */
  10681. select @oldcpu_busy = cpu_busy
  10682.     from spt_monitor
  10683. if @oldcpu_busy > @cpu_busy
  10684. begin
  10685.     update spt_monitor
  10686.         set
  10687.             lastrun = @now,
  10688.             cpu_busy = @cpu_busy,
  10689.             io_busy = @io_busy,
  10690.             idle = @idle,
  10691.             pack_received = @pack_received,
  10692.             pack_sent = @pack_sent,
  10693.             connections = @connections,
  10694.             pack_errors = @pack_errors,
  10695.             total_read = @total_read,
  10696.             total_write = @total_write,
  10697.             total_errors = @total_errors
  10698. end
  10699.  
  10700. /*
  10701. **  Now print out old and new monitor values.
  10702. */
  10703. set nocount on
  10704. select @interval = datediff(ss, lastrun, @now)
  10705.     from spt_monitor
  10706. /* To prevent a divide by zero error when run for the first
  10707. ** time after boot up
  10708. */
  10709. if @interval = 0
  10710.     select @interval = 1
  10711. select last_run = lastrun, current_run = @now, seconds = @interval
  10712.     from spt_monitor
  10713.  
  10714. select
  10715.     cpu_busy = substring(convert(varchar(11),
  10716.         convert(int, ((@cpu_busy * @mspertick) / 1000)))
  10717.         + "("
  10718.         + convert(varchar(11), convert(int, (((@cpu_busy - cpu_busy)
  10719.         * @mspertick) / 1000)))
  10720.         + ")"
  10721.         + "-"
  10722.         + convert(varchar(11), convert(int, ((((@cpu_busy - cpu_busy)
  10723.         * @mspertick) / 1000) * 100) / @interval))
  10724.         + "%",
  10725.         1, 25),
  10726.     io_busy = substring(convert(varchar(11),
  10727.         convert(int, ((@io_busy * @mspertick) / 1000)))
  10728.         + "("
  10729.         + convert(varchar(11), convert(int, (((@io_busy - io_busy)
  10730.         * @mspertick) / 1000)))
  10731.         + ")"
  10732.         + "-"
  10733.         + convert(varchar(11), convert(int, ((((@io_busy - io_busy)
  10734.         * @mspertick) / 1000) * 100) / @interval))
  10735.         + "%",
  10736.         1, 25),
  10737.     idle = substring(convert(varchar(11),
  10738.         convert(int, ((@idle * @mspertick) / 1000)))
  10739.         + "("
  10740.         + convert(varchar(11), convert(int, (((@idle - idle)
  10741.         * @mspertick) / 1000)))
  10742.         + ")"
  10743.         + "-"
  10744.         + convert(varchar(11), convert(int, ((((@idle - idle)
  10745.         * @mspertick) / 1000) * 100) / @interval))
  10746.         + "%",
  10747.         1, 25)
  10748. from spt_monitor
  10749.  
  10750. select
  10751.     packets_received = substring(convert(varchar(11), @pack_received) + "(" +
  10752.         convert(varchar(11), @pack_received - pack_received) + ")", 1, 25),
  10753.     packets_sent = substring(convert(varchar(11), @pack_sent) + "(" +
  10754.         convert(varchar(11), @pack_sent - pack_sent) + ")", 1, 25),
  10755.     packet_errors = substring(convert(varchar(11), @pack_errors) + "(" +
  10756.         convert(varchar(11), @pack_errors - pack_errors) + ")", 1, 25)
  10757. from spt_monitor
  10758.  
  10759. select
  10760.     total_read = substring(convert(varchar(11), @total_read) + "(" +
  10761.         convert(varchar(11), @total_read - total_read) + ")", 1, 19),
  10762.     total_write = substring(convert(varchar(11), @total_write) + "(" +
  10763.         convert(varchar(11), @total_write - total_write) + ")", 1, 19),
  10764.     total_errors = substring(convert(varchar(11), @total_errors) + "(" +
  10765.         convert(varchar(11), @total_errors - total_errors) + ")", 1, 19),
  10766.     connections = substring(convert(varchar(11), @connections) + "(" +
  10767.         convert(varchar(11), @connections - connections) + ")", 1, 18)
  10768. from spt_monitor
  10769.  
  10770. /*
  10771. **  Now update spt_monitor
  10772. */
  10773. update spt_monitor
  10774.     set
  10775.         lastrun = @now,
  10776.         cpu_busy = @cpu_busy,
  10777.         io_busy = @io_busy,
  10778.         idle = @idle,
  10779.         pack_received = @pack_received,
  10780.         pack_sent = @pack_sent,
  10781.         connections = @connections,
  10782.         pack_errors = @pack_errors,
  10783.         total_read = @total_read,
  10784.         total_write = @total_write,
  10785.         total_errors = @total_errors
  10786.  
  10787. return (0)
  10788. go
  10789. /* password 27.1 3/5/90 */
  10790. create procedure sp_password
  10791. @old varchar(30) = NULL,        /* the old (current) password */
  10792. @new varchar(30),            /* the new password */
  10793. @loginame varchar(30) = NULL        /* user to change password on */
  10794. as
  10795.  
  10796. declare @suid int            /* suid of person to change pw on */
  10797.  
  10798. /*
  10799. **  If we're in a transaction, disallow this since it might make recovery
  10800. **  impossible.
  10801. */
  10802. if @@trancount > 0
  10803. begin
  10804.     print "Can't run sp_password from within a transaction."
  10805.     return (1)
  10806. end
  10807.  
  10808. /*
  10809. **  If the system administrator (sa) is running this command, the sa can
  10810. **  change anyone's password and doesn't have to know the old password.
  10811. **  We have this case is suser_id() = 1 and @loginame is NOT NULL.
  10812. **  In this case, we'll fill in the old password and set the suid.
  10813. */
  10814. if suser_id() != 1 and @loginame != NULL
  10815. begin
  10816.     /*
  10817.     **  Only the sa can use the @loginame.
  10818.     */
  10819.     print "Only System Administrator (SA) can use loginame option -- password not changed."
  10820.     return (1)
  10821. end
  10822.  
  10823. /*
  10824. **  Does the SA want to change someone's password?
  10825. */
  10826. if suser_id() = 1 and @loginame != NULL
  10827. begin
  10828.     /*  Check to see if the user exists. */
  10829.     select @suid = suid
  10830.         from syslogins
  10831.     where name = @loginame
  10832.  
  10833.     if @suid = NULL
  10834.     begin
  10835.         print "No such login -- no password changed."
  10836.         return (1)
  10837.     end
  10838.  
  10839.     /*
  10840.     **  Get the old password for the user.  If @old not given, don't
  10841.     **  bother checking against the old password.
  10842.     */
  10843.     if @old != null
  10844.     begin
  10845.         select @suid = suid
  10846.             from syslogins
  10847.         where name = @loginame and password = @old
  10848.  
  10849.         if @suid = NULL
  10850.         begin
  10851.             print "Old (current) password incorrect for user -- password not changed."
  10852.             return (1)
  10853.         end
  10854.     end
  10855. end
  10856.  
  10857. /*
  10858. **  This is just the normal case when someone wants to change their own
  10859. **  password.  Set the @suid.
  10860. */
  10861. else if @loginame = NULL
  10862. begin
  10863.     select @suid = suid
  10864.         from syslogins
  10865.     where password = @old
  10866.         and suid = suser_id()
  10867. end
  10868. if @suid = NULL
  10869. begin
  10870.     print "Old (current) password incorrect for user -- password not changed."
  10871.     return (1)
  10872. end
  10873.  
  10874. /*
  10875. **  This is the final case.  The sa is running the command to change
  10876. **  someone else's password and is supplying the old password.  Confirm
  10877. **  that the old password is correct.
  10878. */
  10879. if suser_id() = 1
  10880. begin
  10881.     select @suid = suid
  10882.         from syslogins
  10883.     where password = @old
  10884.         and suid = suser_id(@loginame)
  10885.  
  10886.     if @suid = NULL
  10887.     begin
  10888.         print "Old (current) password incorrect for user -- password not changed."
  10889.         return (1)
  10890.     end
  10891. end
  10892.  
  10893. /*
  10894. **  If @suid still hasn't been set then the user running the procedure
  10895. **  doesn't have the right current password.
  10896. */
  10897. if @suid = NULL
  10898. begin
  10899.     print "Old (current) password incorrect for user -- password not changed."
  10900.     return (1)
  10901. end
  10902.  
  10903. /*
  10904. **  Everything is consistent so change the password.
  10905. */
  10906. update syslogins
  10907.     set password = @new
  10908.         where suid = @suid
  10909.  
  10910. print 'Password changed.'
  10911.  
  10912. return (0)
  10913. go
  10914. grant execute on sp_password to public
  10915. go
  10916. dump transaction master with truncate_only
  10917. go
  10918. /* placeobject    27.1    3/5/90 */
  10919. create procedure sp_placeobject
  10920. @segname    varchar(30),    /* segment name */
  10921. @objname    varchar(62)    /* object name */
  10922. as
  10923.  
  10924. declare    @msg    char(100)
  10925. declare @objid    int
  10926. declare @tabname    varchar(30)
  10927. declare @indexname    varchar(30)
  10928. declare    @indexid    smallint
  10929. declare    @segment    smallint
  10930.  
  10931. /*
  10932. **  Check to see if the segment exists.
  10933. */
  10934. select @segment = segment
  10935.         from syssegments
  10936.             where name = @segname
  10937. if @segment = null
  10938. begin
  10939.     select @msg = "There is no such segment as '" + @segname + "'."
  10940.     print @msg
  10941.     return (1)
  10942. end
  10943.  
  10944. /*
  10945. **  Are we dealing with a table or it's index?
  10946. */
  10947. if @objname like "%.%"
  10948. begin
  10949.  
  10950.     /*
  10951.     **  Get the table name out.
  10952.     */
  10953.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  10954.     select @indexname = substring(@objname, charindex(".", @objname) + 1, 61)
  10955. end
  10956. else
  10957. begin
  10958.     select @tabname = @objname, @indexname = null
  10959. end
  10960.  
  10961. /*
  10962. **  Can't use placeobject to move syslogs.
  10963. */
  10964. if (object_id(@tabname) = 8)
  10965. begin
  10966.     print "Use sp_logdevice to move syslogs table."
  10967.     return (1)
  10968. end
  10969.  
  10970. /*
  10971. **  Can't move system tables.
  10972. */
  10973. if (object_id(@tabname) < 100)
  10974. begin
  10975.     print "You can't move system tables."
  10976.     return (1)
  10977. end
  10978.  
  10979. /*
  10980. **  If the user is not the dbo or the SA, make sure he/she owns it.
  10981. */
  10982. if suser_id() != 1 and user_id() != 1
  10983. begin
  10984.     if not exists (select *
  10985.             from sysobjects
  10986.                 where name = @tabname
  10987.                     and uid = user_id())
  10988.     begin
  10989.         select @msg = "You do not own a table named '" + @tabname
  10990.             + "'."
  10991.         print @msg
  10992.         return (1)
  10993.     end
  10994. end
  10995. else
  10996. begin
  10997.     /*
  10998.     **  If dbo or SA, make sure the table exists.
  10999.     */
  11000.     if not exists (select *
  11001.             from sysobjects
  11002.                 where name = @tabname)
  11003.     begin
  11004.         select @msg = "There is no table named '" + @tabname + "'."
  11005.         print @msg
  11006.         return (1)
  11007.     end
  11008. end
  11009.  
  11010. /*
  11011. **  If we are dealing with an index, make sure it exists.
  11012. */
  11013. if @indexname != null and not exists (select *
  11014.                     from sysindexes
  11015.                         where id = object_id(@tabname)
  11016.                             and name = @indexname)
  11017. begin
  11018.     select @msg = "There is no index named '" + @indexname
  11019.         + "' for table '" + @tabname + "'."
  11020.     print @msg
  11021.     return (1)
  11022. end
  11023.  
  11024. /*
  11025. **  Everything's ok so change the segment for the object.
  11026. */
  11027. if @indexname = null
  11028. begin
  11029.     select @indexid = indid
  11030.         from sysindexes
  11031.             where id = object_id(@tabname)
  11032.                 and indid < 2
  11033. end
  11034. else
  11035. begin
  11036.     select @indexid = indid
  11037.         from sysindexes
  11038.             where id = object_id(@tabname)
  11039.                 and name = @indexname
  11040. end
  11041.  
  11042. update sysindexes
  11043. set segment = @segment
  11044.     from sysindexes
  11045.         where id = object_id(@tabname)
  11046.             and indid = @indexid
  11047.  
  11048. declare @dbname    varchar(30)
  11049. select @dbname = db_name()
  11050. dbcc dbrepair (@dbname, remap)
  11051. select @msg = "'" + @objname + "' is now on segment '" + @segname + "'."
  11052. print @msg
  11053.  
  11054. return (0)
  11055. go
  11056. grant execute on sp_placeobject to public
  11057. go
  11058. /* primarykey 27.1 3/5/90 */
  11059. create procedure sp_primarykey
  11060. @tabname  varchar(92),            /* table name that has the key */
  11061. @col1     varchar(30),             /* columns that make up the key */
  11062. @col2     varchar(30) = NULL,
  11063. @col3     varchar(30) = NULL,
  11064. @col4     varchar(30) = NULL,
  11065. @col5     varchar(30) = NULL,
  11066. @col6     varchar(30) = NULL,
  11067. @col7     varchar(30) = NULL,
  11068. @col8     varchar(30) = NULL
  11069. as
  11070.  
  11071. declare @objid int            /* object id of the table */
  11072. declare @uid smallint            /* owner id of the object */
  11073. declare @cnt smallint            /* howmany columns in key */
  11074. declare @key1 tinyint            /* colids of the columns in the key */
  11075. declare @key2 tinyint
  11076. declare @key3 tinyint
  11077. declare @key4 tinyint
  11078. declare @key5 tinyint
  11079. declare @key6 tinyint
  11080. declare @key7 tinyint
  11081. declare @key8 tinyint
  11082.  
  11083. /*
  11084. **  Check to see that the tabname is local.
  11085. */
  11086. if @tabname like "%.%.%" and
  11087.     substring(@tabname, 1, charindex(".", @tabname) - 1) != db_name()
  11088. begin
  11089.     print "Table or view name must be in 'current' database."
  11090.     return (1)
  11091. end
  11092.  
  11093. /*
  11094. **  See if we can find the object.  It must be a system table, user table,
  11095. **  or view.  The low 3 bits of sysobjects.sysstat indicate what the
  11096. **  object type is -- it's more reliable than using sysobjects.type which
  11097. **  could change.
  11098. */
  11099. select @objid = id, @uid = uid
  11100.     from sysobjects
  11101.         where id = object_id(@tabname)
  11102.             and (sysstat & 7 = 1        /* system table */
  11103.                 or sysstat & 7 = 2    /* view */
  11104.                 or sysstat & 7 = 3)    /* user table */
  11105.  
  11106. if @objid is NULL
  11107. begin
  11108.     print "No table or view exists with the supplied name."
  11109.     return (1)
  11110. end
  11111.  
  11112. /*
  11113. **  Check to see that object owner is adding the key.
  11114. */
  11115. if @uid != user_id()
  11116. begin
  11117.     print "Only the owner of the table may define a primary key."
  11118.     return (1)
  11119. end
  11120.  
  11121. /*
  11122. **  See if the primary key exists already.  Syskeys.type = 1 indicates
  11123. **  a primary key.
  11124. */
  11125. if exists (select *
  11126.         from syskeys
  11127.             where id = @objid
  11128.                 and type = 1)
  11129. begin
  11130.     print "Primary key already exists on table -- drop key first."
  11131.     return (1)
  11132. end
  11133.  
  11134. /*
  11135. **  Now check out each column argument to verify it's existence.
  11136. */
  11137. select @cnt = 1, @key1 = colid
  11138.     from syscolumns
  11139.         where name = @col1
  11140.             and id = @objid
  11141. if @key1 is NULL
  11142. begin
  11143.     print "The table has no such first column."
  11144.     return (1)
  11145. end
  11146.  
  11147. if @col2 != NULL
  11148. begin
  11149.     select @cnt = @cnt + 1, @key2 = colid
  11150.         from syscolumns
  11151.             where name = @col2
  11152.                 and id = @objid
  11153.     if @key2 is NULL
  11154.     begin
  11155.         print "The table has no such second column."
  11156.         return (1)
  11157.     end
  11158. end
  11159. else goto doinsert
  11160.  
  11161. if @col3 != NULL
  11162. begin
  11163.     select @cnt = @cnt + 1, @key3 = colid
  11164.         from syscolumns
  11165.             where name = @col3
  11166.                 and id = @objid
  11167.     if @key3 is NULL
  11168.     begin
  11169.         print "The table has no such third column."
  11170.         return (1)
  11171.     end
  11172. end
  11173. else goto doinsert
  11174.  
  11175. if @col4 != NULL
  11176. begin
  11177.     select @cnt = @cnt + 1, @key4 = colid
  11178.         from syscolumns
  11179.             where name = @col4
  11180.                 and id = @objid
  11181.     if @key4 is NULL
  11182.     begin
  11183.         print "The table has no such fourth column."
  11184.         return (1)
  11185.     end
  11186. end
  11187. else goto doinsert
  11188.  
  11189. if @col5 != NULL
  11190. begin
  11191.     select @cnt = @cnt + 1, @key5 = colid
  11192.         from syscolumns
  11193.             where name = @col5
  11194.                 and id = @objid
  11195.     if @key5 is NULL
  11196.     begin
  11197.         print "The table has no such fifth column."
  11198.         return (1)
  11199.     end
  11200. end
  11201. else goto doinsert
  11202.  
  11203. if @col6 != NULL
  11204. begin
  11205.     select @cnt = @cnt + 1, @key6 = colid
  11206.         from syscolumns
  11207.             where name = @col6
  11208.                 and id = @objid
  11209.     if @key6 is NULL
  11210.     begin
  11211.         print "The table has no such sixth column."
  11212.         return (1)
  11213.     end
  11214. end
  11215. else goto doinsert
  11216.  
  11217. if @col7 != NULL
  11218. begin
  11219.     select @cnt = @cnt + 1, @key7 = colid
  11220.         from syscolumns
  11221.             where name = @col7
  11222.                 and id = @objid
  11223.     if @key7 is NULL
  11224.     begin
  11225.         print "The table has no such seventh column."
  11226.         return (1)
  11227.     end
  11228. end
  11229. else goto doinsert
  11230.  
  11231. if @col8 != NULL
  11232. begin
  11233.     select @cnt = @cnt + 1, @key8 = colid
  11234.         from syscolumns
  11235.             where name = @col8
  11236.                 and id = @objid
  11237.     if @key8 is NULL
  11238.     begin
  11239.         print "The table has no such eighth column."
  11240.         return (1)
  11241.     end
  11242. end
  11243.  
  11244. /*
  11245. **  Type 1 is a primary key, 2 is a foreignkey, and 3 is a commonjoin.
  11246. */
  11247. doinsert:
  11248.  
  11249. insert into syskeys
  11250.     (id, type, depid, keycnt, size, key1, key2, key3, key4, key5,
  11251.         key6, key7, key8)
  11252. values (@objid, 1, NULL, @cnt, 0, @key1, @key2, @key3, @key4, @key5,
  11253.         @key6, @key7, @key8)
  11254.  
  11255. print "New primary key added."
  11256.  
  11257. return (0)
  11258. go
  11259. grant execute on sp_primarykey to public
  11260. go
  11261. dump transaction master with truncate_only
  11262. go
  11263. /* recompile 27.1 3/5/90 */
  11264. create procedure sp_recompile
  11265. @objname    varchar(92)
  11266. as
  11267.  
  11268. declare @id        int
  11269. declare @type    smallint
  11270. declare @uid        smallint
  11271. declare @errmsg    varchar(250)
  11272.  
  11273. if @objname like '%%.%%.%%'
  11274. begin
  11275.     select @errmsg = 'Table ' + @objname + ' must be in current database (' + db_name() + ')'
  11276.     print @errmsg
  11277.     return
  11278. end
  11279.  
  11280. select @id = id, @type = sysstat & 7, @uid = uid
  11281. from sysobjects
  11282. where id = object_id(@objname)
  11283.  
  11284. if @id = null
  11285. begin
  11286.     select @errmsg = 'Object ' + @objname + ' is not a table in database ' + db_name() + '.'
  11287.     print @errmsg
  11288.     return
  11289. end
  11290.  
  11291. if @type = 1
  11292. begin
  11293.     select @errmsg = @objname + ' is a system table.'
  11294.     print @errmsg
  11295.     print 'sp_recompile cannot be used on system tables.'
  11296.     return
  11297. end
  11298. if @type != 3
  11299. begin
  11300.     select @errmsg = 'Object ' + @objname + ' is not a table.'
  11301.     print @errmsg
  11302.     return
  11303. end
  11304.  
  11305. if user_id() != 1 and @uid != user_id()
  11306. begin
  11307.     select @errmsg = 'You do not own table ' + @objname + '.'
  11308.     print @errmsg
  11309.     return
  11310. end
  11311.  
  11312. set nocount on
  11313.  
  11314. update sysobjects
  11315. set schema = schema + 1
  11316. where id = @id
  11317.  
  11318. select @errmsg = 'Each stored procedure and trigger that uses table ' + @objname
  11319. print @errmsg
  11320. print 'will be recompiled the next time it is executed.'
  11321. go
  11322. grant execute on sp_recompile to public
  11323. go
  11324. /* remoteoption 27.1 3/5/90 */
  11325. create procedure sp_remoteoption
  11326. @remoteserver varchar(30) = NULL,    /* server name to change */
  11327. @loginame varchar(30) = NULL,        /* user's remote name */
  11328. @remotename varchar(30) = NULL,        /* user's local user name */
  11329. @optname varchar(20) = NULL,        /* option name to turn on/off */
  11330. @optvalue varchar(10) = NULL        /* true or false */
  11331. as
  11332.  
  11333. declare @statvalue smallint        /* number of option */
  11334. declare @optcount int            /* number of options like @optname */
  11335. declare    @msg    varchar(100)
  11336. declare @suid    smallint
  11337.  
  11338. /*
  11339. **  If no @server given, just list the possible remote login options.
  11340. **  Only certain status bits may be set or cleared.
  11341. **       settable                        not settable
  11342. **      ------------------------------  --------------------------
  11343. **    trusted (1)
  11344. */
  11345. if @remoteserver = null
  11346. begin
  11347.     print "Settable remote login options."
  11348.     select remotelogin_option = name
  11349.         from spt_values
  11350.             where type = "F"
  11351.                 and number in (1, 1)
  11352.                 and number > 0
  11353.         order by name
  11354.     return (0)
  11355. end
  11356.  
  11357. /*
  11358. **  If @loginame is NULL then we want to set @suid = -1. Otherwise get
  11359. **  it real value.
  11360. */
  11361. if @loginame = null
  11362.     select @suid = -1
  11363. else select @suid = suser_id(@loginame)
  11364.  
  11365. /*
  11366. **  Verify the server name, local, and remote names.
  11367. */
  11368. if not exists (select *
  11369.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  11370.             where r.remoteserverid = s.srvid
  11371.                 and s.srvname = @remoteserver
  11372.                 and r.remoteusername = @remotename
  11373.                 and r.suid = @suid)
  11374. begin
  11375.     select @msg = "There is no remote user '" + isnull(@remotename, "NULL")
  11376.         + "' mapped to local user '" + isnull(@loginame, "NULL")
  11377.         + "' on remote server '" + @remoteserver + "'."
  11378.     print @msg
  11379.     return (1)
  11380. end
  11381.  
  11382. /*
  11383. **  Check remaining parameters.
  11384. */
  11385. if @optname is NULL or @optvalue not in ("true", "false") or @optvalue = null
  11386. begin
  11387.     print "Usage: sp_remoteoption [remoteserver, loginame, remotename, optname, {true | false}]"
  11388.     return (1)
  11389. end
  11390.  
  11391. /*
  11392. **  Use @optname and try to find the right option.
  11393. **  If there isn't just one, print appropriate diagnostics and return.
  11394. */
  11395. select @optcount = count(*)
  11396.         from spt_values
  11397.         where name like "%" + @optname + "%" and type = "F"
  11398.             and number in (1, 1)
  11399.             and number > 0
  11400.  
  11401. /*
  11402. **  If no option, show the user what the options are.
  11403. */
  11404. if @optcount = 0
  11405. begin
  11406.     print "Remote login option doesn't exist or can't be set by user."
  11407.     print "Run sp_remoteoption with no parameters to see options."
  11408.     return (1)
  11409. end
  11410.  
  11411. /*
  11412. **  If more than one option like @optname, show the duplicates and return.
  11413. */
  11414. if @optcount > 1
  11415. begin
  11416.     print "Remote login option is not unique."
  11417.  
  11418.     select duplicate_option = name
  11419.         from spt_values
  11420.             where name like "%" + @optname + "%"
  11421.                 and type = "F"
  11422.                 and number in (1, 1)
  11423.                 and number > 0
  11424.  
  11425.     return (1)
  11426. end
  11427.  
  11428. /*
  11429. **  Only the SA or the dbo of @dbnbame can execute the update part
  11430. **  of this procedure so check.  Also the sproc must be executed in the master
  11431. **  database.
  11432. */
  11433. if suser_id() != 1
  11434. begin
  11435.     print "Only the System Administrator (SA) may set remote login options."
  11436.     return (1)
  11437. end
  11438.  
  11439. /*
  11440. **  If we're in a transaction, disallow this since it might make recovery
  11441. **  impossible.
  11442. */
  11443. if @@trancount > 0
  11444. begin
  11445.     print "Can't run sp_remoteoption from within a transaction."
  11446.     return (1)
  11447. end
  11448.  
  11449. /*
  11450. **  Get the number which is the bit value to set
  11451. */
  11452. select @statvalue = number
  11453.         from spt_values
  11454.         where name like "%" + @optname + "%" and type = "F"
  11455.             and number in (1, 1)
  11456.             and number > 0
  11457.  
  11458.  
  11459. /*
  11460. **  Now update sysremotelogins.
  11461. */
  11462. if @optvalue = "true"
  11463. begin
  11464.     update master.dbo.sysremotelogins
  11465.         set status = status | @statvalue
  11466.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  11467.             where r.remoteserverid = s.srvid
  11468.                 and s.srvname = @remoteserver
  11469.                 and r.remoteusername = @remotename
  11470.                 and r.suid = @suid
  11471.     select @msg = "Option '" + @optname + "' turned on."
  11472.     print @msg
  11473.     return (0)
  11474.  
  11475. end
  11476.  
  11477. /*
  11478. **  We want to turn it off.
  11479. */
  11480. else
  11481. begin
  11482.     update master.dbo.sysremotelogins
  11483.         set status = status & ~@statvalue
  11484.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  11485.             where r.remoteserverid = s.srvid
  11486.                 and s.srvname = @remoteserver
  11487.                 and r.remoteusername = @remotename
  11488.                 and r.suid = @suid
  11489.     select @msg = "Option '" + @optname + "' turned off."
  11490.     print @msg
  11491.     return (0)
  11492. end
  11493.  
  11494. return (0)
  11495. go
  11496. grant execute on sp_remoteoption to public
  11497. go
  11498. dump transaction master with truncate_only
  11499. go
  11500. /* rename 27.1 3/5/90 */
  11501. create procedure sp_rename
  11502. @objname varchar(61),            /* old (current) object name */
  11503. @newname sysname(30)            /* new name we want to call it */
  11504. as
  11505.  
  11506. declare @objid int            /* object id of the thing to rename */
  11507. declare @msg varchar(250)
  11508. declare @returncode    int
  11509.  
  11510. if @objname like "%.%.%"
  11511. begin
  11512.     print "Object must be in the current database."
  11513.     return (1)
  11514. end
  11515.  
  11516. /*
  11517. **  Check to see that the @newname is valid.
  11518. */
  11519. execute @returncode = sp_validname @newname
  11520. if @returncode != 0
  11521. begin
  11522.     select @msg = "'" + @newname + "' is not a valid name."
  11523.     print @msg
  11524.     return @returncode
  11525. end
  11526.  
  11527. /*
  11528. **  Check to see if we are renaming a table/column (or table/index)
  11529. **  or a table or usertype.
  11530. **  If @objname is of the form table.column then it's a column.
  11531. **  In the column case, we need to extract and verify the table and
  11532. **  column names and make sure the user owns the table that is getting
  11533. **  the rule bound.
  11534. */
  11535. if @objname like "%.%"
  11536. begin
  11537.     declare @tabname varchar(30)        /* name of table */
  11538.     declare @colname varchar(30)        /* name of column */
  11539.     declare @colid tinyint            /* colid of the column */
  11540.  
  11541.     /*
  11542.     **  Get the table name out.
  11543.     */
  11544.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  11545.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  11546.  
  11547.     /*
  11548.     **  Now check to see that the column exists in a table owned
  11549.     **  by the user.
  11550.     */
  11551.     select @objid = syscolumns.id, @colid = syscolumns.colid
  11552.         from sysobjects, syscolumns
  11553.     where syscolumns.id = sysobjects.id
  11554.         and sysobjects.name = @tabname
  11555.         and syscolumns.name = @colname
  11556.         and (sysobjects.sysstat & 7 = 2        /* user table */
  11557.         or sysobjects.sysstat & 7 = 3)        /* view */
  11558.         and sysobjects.uid = user_id()
  11559.  
  11560.     /*
  11561.     **  Did we find it?  If not, it might be an index name so we'll
  11562.     **  check in the next case.
  11563.     */
  11564.     if @objid != null
  11565.     begin
  11566.         /*
  11567.         **  Check to make sure that there is no already a column
  11568.         **  that has the new name.
  11569.         */
  11570.         if exists (select *
  11571.                 from syscolumns
  11572.                     where id = @objid
  11573.                         and name = @newname)
  11574.         begin
  11575.             select @msg = "There is already a column named '"
  11576.                 + @newname + "'"
  11577.                 + " in table '"
  11578.                 + @tabname
  11579.                 + "'."
  11580.             print @msg
  11581.             return (1)
  11582.         end
  11583.  
  11584.         /*
  11585.         **  Go ahead and change the column name.
  11586.         */
  11587.         update syscolumns
  11588.             set name = @newname
  11589.                 from syscolumns
  11590.             where id = @objid
  11591.                 and colid = @colid
  11592.  
  11593.         print "Column name has been changed."
  11594.         return (0)
  11595.     end
  11596.  
  11597.     /*
  11598.     **  Might be an index name.
  11599.     */
  11600.     else
  11601.     begin
  11602.         select @objid = sysindexes.id, @colid = sysindexes.indid
  11603.             from sysobjects, sysindexes
  11604.         where sysindexes.id = sysobjects.id
  11605.             and sysobjects.name = @tabname
  11606.             and sysindexes.name = @colname
  11607.             and sysindexes.indid != 0
  11608.             and sysobjects.sysstat & 7 = 3    /* user table */
  11609.             and sysobjects.uid = user_id()
  11610.  
  11611.         /*
  11612.         **  If @objid is still NULL then that means it isn't an
  11613.         **  index name.  We checked above to see if it was a column
  11614.         **  name so now it's time to give up.
  11615.         */
  11616.         if @objid = null
  11617.         begin
  11618.             print "You do not own a table or column (or index) of that name in the current database."
  11619.             return (1)
  11620.         end
  11621.  
  11622.         /*
  11623.         **  Check to make sure that there is no already an index
  11624.         **  that has the new name.
  11625.         */
  11626.         if exists (select *
  11627.                 from sysindexes
  11628.                     where id = @objid
  11629.                         and name = @newname)
  11630.         begin
  11631.             select @msg = "There is already an index named '"
  11632.                 + @newname + "'"
  11633.                 + " for table '"
  11634.                 + @tabname
  11635.                 + "'."
  11636.             print @msg
  11637.             return (1)
  11638.         end
  11639.  
  11640.         /*
  11641.         **  Go ahead and change the index name.
  11642.         */
  11643.         update sysindexes
  11644.             set name = @newname
  11645.                 from sysindexes
  11646.             where id = @objid
  11647.                 and indid = @colid
  11648.  
  11649.         print "Index name has been changed."
  11650.         return (0)
  11651.     end
  11652. end
  11653.  
  11654. /*
  11655. **  Check to see if the object exists and is owned by the user.
  11656. **  It will either be in the sysobjects table or the systypes table.
  11657. **  Check sysobjects first.
  11658. **  System objects have ids < 100 and we don't allow their names to change.
  11659. */
  11660. select @objid = id
  11661.     from sysobjects
  11662.         where id = object_id(@objname)
  11663.             and uid = user_id()
  11664.             and id > 99
  11665.  
  11666. /*
  11667. **  Don't allow the newname to begin with #.
  11668. */
  11669. if @objid != null
  11670. begin
  11671.     if substring(@newname, 1, 1) = "#"
  11672.     begin
  11673.         print "Object name beginning with '#' is not allowed."
  11674.         return (1)
  11675.     end
  11676. end
  11677.  
  11678. /*
  11679. **  It's not in sysobjects so check systypes.
  11680. */
  11681. if @objid is NULL
  11682. begin
  11683.     select @objid = usertype
  11684.         from systypes
  11685.             where name = @objname
  11686.                 and uid = user_id()
  11687.                 and usertype > 99
  11688.  
  11689.     /*
  11690.     **  It's nowhere to be found so quit.
  11691.     */
  11692.     if @objid is NULL
  11693.     begin
  11694.         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."
  11695.         return (1)
  11696.     end
  11697.  
  11698.     /* Check that newname doesn't already exist. */
  11699.     if exists (select *
  11700.             from systypes
  11701.                 where name = @newname)
  11702.     begin
  11703.         print "Newname already exists in systypes."
  11704.         return (1)
  11705.     end
  11706.  
  11707.     /*
  11708.     ** Change the name of a datatype
  11709.     */
  11710.     update systypes
  11711.         set name = @newname
  11712.             where usertype = @objid
  11713.  
  11714.     print "User datatype name has been changed."
  11715.     return (0)
  11716. end
  11717.  
  11718. /*
  11719. **  It might be a table so update the index entry for the table IF it has
  11720. **  no clustered index.  No clustered index means that there is an entry
  11721. **  for the table under the @objid with indid = 0.
  11722. */
  11723. begin transaction
  11724.  
  11725.     /* Check that newname doesn't already exist. */
  11726.     if exists (select *
  11727.             from sysobjects
  11728.                 where name = @newname)
  11729.     begin
  11730.         print "Newname already exists in sysobjects."
  11731.         rollback transaction
  11732.         return (1)
  11733.     end
  11734.  
  11735.     update sysobjects
  11736.         set name = @newname
  11737.             where id = @objid
  11738.  
  11739.      /*
  11740.      **  This might not do anything -- only if we are dealing with a
  11741.      **  table that has no clustered index.
  11742.      **  Also change the name of the text entry, if any.
  11743.      */
  11744.      update sysindexes
  11745.         set name = @newname
  11746.             where id = @objid
  11747.                 and indid = 0
  11748.      update sysindexes
  11749.         set name = "t" + @newname
  11750.             where id = @objid
  11751.                 and indid = 255
  11752. commit transaction
  11753.  
  11754. print "Object name has been changed."
  11755.  
  11756. return (0)
  11757. go
  11758. grant execute on sp_rename to public
  11759. go
  11760. /* renamedb 27.1 3/5/90 */
  11761. create procedure sp_renamedb
  11762. @dbname sysname(30),            /* old (current) db name */
  11763. @newname sysname(30)            /* new name we want to call it */
  11764. as
  11765.  
  11766. declare @objid int            /* object id of the thing to rename */
  11767. declare @msg varchar(250)
  11768. declare @bitdesc varchar(30)        /* bit description for the db */
  11769. declare @curdbid int            /* id of database to be changed */
  11770.  
  11771. /*
  11772. **  If we're in a transaction, disallow this since it might make recovery
  11773. **  impossible.
  11774. */
  11775. if @@trancount > 0
  11776. begin
  11777.     print "Can't run sp_renamedb from within a transaction."
  11778.     return (1)
  11779. end
  11780.  
  11781. /*
  11782. **  Only the SA can do this.
  11783. */
  11784. if suser_id() != 1
  11785. begin
  11786.     print "Only the System Administrator (SA) can change the name of a database."
  11787.     return (1)
  11788. end
  11789.  
  11790. /*
  11791. **  Make sure the database exists.
  11792. */
  11793. if not exists (select *
  11794.         from master.dbo.sysdatabases
  11795.             where name = @dbname)
  11796. begin
  11797.     print "No specified database currently exists."
  11798.     return (1)
  11799. end
  11800.  
  11801. /*
  11802. **  Make sure that the @newname db doesn't already exist.
  11803. */
  11804. if exists (select *
  11805.         from master.dbo.sysdatabases
  11806.             where name = @newname)
  11807. begin
  11808.     print "A database with the new name already exists."
  11809.     return (1)
  11810. end
  11811.  
  11812. /*
  11813. **  Check to see that the @newname is valid.
  11814. */
  11815. declare @returncode    int
  11816. execute @returncode = sp_validname @newname
  11817. if @returncode != 0
  11818. begin
  11819.     select @msg = "'" + @newname + "' is not a valid name."
  11820.     print @msg
  11821.     return @returncode
  11822. end
  11823.  
  11824. /*
  11825. **  Don't allow the names of master, tempdb, and model to be changed.
  11826. */
  11827. if @dbname in ("master", "model", "tempdb")
  11828. begin
  11829.     print "The databases 'master', 'model', and 'tempdb' cannot be renamed."
  11830.     return (1)
  11831. end
  11832.  
  11833. /* Get current database id */
  11834. select @curdbid = db_id(@dbname)
  11835.  
  11836. /*
  11837. ** Check single user bit (4096)
  11838. ** Database must be in single user mode to necessitate the rid update in the
  11839. ** database's DBTABLE
  11840. */
  11841. select @bitdesc = null
  11842. select @bitdesc = v.name
  11843.         from spt_values v, master.dbo.sysdatabases d
  11844.                 where d.dbid = @curdbid
  11845.                         and v.type = "D"
  11846.                         and d.status & v.number = 4096
  11847. if @bitdesc = null
  11848. begin
  11849.         select @msg = "System Administrator (SA) must set database '" + @dbname + "'"
  11850.         print @msg
  11851.         print "to single-user mode with sp_dboption before using sp_renamedb."
  11852.         return(1)
  11853. end
  11854.  
  11855. /*
  11856. **  Everything is ok so do it.
  11857. */
  11858. update master.dbo.sysdatabases
  11859.     set name = @newname
  11860.         where name = @dbname
  11861.  
  11862. print "Database is renamed and in single-user mode."
  11863. print "System Administrator (SA) must reset it to multi-user mode with sp_dboption."
  11864.  
  11865. return (0)
  11866. go
  11867. dump transaction master with truncate_only
  11868. go
  11869. /* serveroption 27.1 3/5/90 */
  11870. create procedure sp_serveroption
  11871. @server varchar(30) = NULL,        /* server name to change */
  11872. @optname varchar(20) = NULL,        /* option name to turn on/off */
  11873. @optvalue varchar(10) = NULL        /* true or false */
  11874. as
  11875.  
  11876. declare @srvid int            /* id of the server */
  11877. declare @statvalue smallint        /* number of option */
  11878. declare @optcount int            /* number of options like @optname */
  11879.  
  11880. /*
  11881. **  If no @server given, just list the possible server options.
  11882. **  Only certain status bits may be set or cleared.
  11883. **  The default is to allow timeouts (bit 0 is clear).
  11884. **       settable                        not settable
  11885. **      ------------------------------  --------------------------
  11886. **      timeouts (0)
  11887. */
  11888. if @server = null
  11889. begin
  11890.     print "Settable server options."
  11891.     select server_option = name
  11892.         from spt_values
  11893.             where type = "A"
  11894.                 and number not in (-1, 1)
  11895.         order by name
  11896.     return (0)
  11897. end
  11898.  
  11899. /*
  11900. **  Verify the server name and get the @srvid
  11901. */
  11902. select @srvid = srvid
  11903.     from master.dbo.sysservers
  11904.         where srvname = @server
  11905.  
  11906. /*
  11907. **  If @server not found, say so.
  11908. */
  11909. if @srvid = NULL
  11910. begin
  11911.     print "No such server -- run sp_helpserver to list servers."
  11912.     return (1)
  11913. end
  11914.  
  11915. /*
  11916. **  Check remaining parameters.
  11917. */
  11918. if @optname is NULL or @optvalue not in ("true", "false") or @optvalue = null
  11919. begin
  11920.     print "usage: sp_serveroption [server, optname, {true | false}]"
  11921.     return (1)
  11922. end
  11923.  
  11924. /*
  11925. **  Use @optname and try to find the right option.
  11926. **  If there isn't just one, print appropriate diagnostics and return.
  11927. */
  11928. select @optcount = count(*)
  11929.         from spt_values
  11930.         where name like "%" + @optname + "%" and type = "A"
  11931.             and number not in (-1, 1)
  11932.  
  11933. /*
  11934. **  If no option, show the user what the options are.
  11935. */
  11936. if @optcount = 0
  11937. begin
  11938.     print "Server option doesn't exist or can't be set by user."
  11939.     print "Run sp_serveroption with no parameters to see options."
  11940.     return (1)
  11941. end
  11942.  
  11943. /*
  11944. **  If more than one option like @optname, show the duplicates and return.
  11945. */
  11946. if @optcount > 1
  11947. begin
  11948.     print "Server option is not unique."
  11949.  
  11950.     select duplicate_option = name
  11951.         from spt_values
  11952.             where name like "%" + @optname + "%"
  11953.                 and type = "A"
  11954.                 and number not in (-1, 1)
  11955.  
  11956.     return (1)
  11957. end
  11958.  
  11959. /*
  11960. **  Only the SA or the dbo of @dbnbame can execute the update part
  11961. **  of this procedure so check.  Also the sproc must be executed in the master
  11962. **  database.
  11963. */
  11964. if suser_id() != 1
  11965. begin
  11966.     print "Only the System Administrator (SA) may set server options."
  11967.     return (1)
  11968. end
  11969.  
  11970. /*
  11971. **  Currently there is only one option that can be set -- no timeouts --
  11972. **  and it only applies to remote servers.  If the current server is
  11973. **  local then reject it.
  11974. */
  11975. if @srvid = 0
  11976. begin
  11977.     print "Option can be set for remote servers only -- not the local server."
  11978.     return (1)
  11979. end
  11980.  
  11981.  
  11982. /*
  11983. **  If we're in a transaction, disallow this since it might make recovery
  11984. **  impossible.
  11985. */
  11986. if @@trancount > 0
  11987. begin
  11988.     print "Can't run sp_serveroption from within a transaction."
  11989.     return (1)
  11990. end
  11991. /*
  11992. **  Get the number which is the bit value to set
  11993. */
  11994. select @statvalue = number
  11995.         from spt_values
  11996.         where name like "%" + @optname + "%" and type = "A"
  11997.             and number > 0
  11998.  
  11999.  
  12000. /*
  12001. **  Now update sysservers.
  12002. **  The timeouts option is handled a little strangely since the default
  12003. **  is timeouts.  Therefore timeouts = true means to clear the bit
  12004. **  and timeouts = false means to set the bit.
  12005. */
  12006. if @optvalue = "true"
  12007. begin
  12008.     update sysservers
  12009.         set srvstatus = srvstatus & ~@statvalue
  12010.             where srvid = @srvid
  12011. end
  12012.  
  12013. /*
  12014. **  We want to turn it off.
  12015. */
  12016. else
  12017. begin
  12018.     update sysservers
  12019.         set srvstatus = srvstatus | @statvalue
  12020.             where srvid = @srvid
  12021. end
  12022.  
  12023. return (0)
  12024. go
  12025. grant execute on sp_serveroption to public
  12026. go
  12027. /* spaceused 27.1 3/5/90 */
  12028. create procedure sp_spaceused
  12029. @objname varchar(92) = null        /* the object we want size on */
  12030. as
  12031.  
  12032. declare @id    int            /* the object id of @objname */
  12033. declare @type    smallint        /* the object type */
  12034. declare    @pages    int            /* working variable for size calc. */
  12035.  
  12036. /*
  12037. **  Check to see that the objname is local.
  12038. */
  12039. if @objname != null
  12040. begin
  12041.     if @objname like "%.%.%" and
  12042.         substring(@objname, 1, charindex(".", @objname) - 1) != db_name()
  12043.     begin
  12044.         print "Object must be in 'current' database."
  12045.         return (1)
  12046.     end
  12047.  
  12048.     /*
  12049.     **  Try to find the object.
  12050.     */
  12051.     select @id = id, @type = sysstat & 7
  12052.         from sysobjects
  12053.             where id = object_id(@objname)
  12054.  
  12055.     /*
  12056.     **  Does the object exist?
  12057.     */
  12058.     if @id = null
  12059.     begin
  12060.         print "Object is not a table in the database."
  12061.         return (1)
  12062.     end
  12063.  
  12064.     /*
  12065.     **  See if it's a space object.
  12066.     **  types are:
  12067.     **    0 - trigger
  12068.     **    1 - system table
  12069.     **    2 - view
  12070.     **    3 - user table
  12071.     **    4 - sproc
  12072.     **    6 - default
  12073.     **    7 - rule
  12074.     */
  12075.     if not exists (select *
  12076.             from sysindexes
  12077.                 where @id = id
  12078.                     and indid < 2)
  12079.     begin
  12080.         if @type in (0, 4, 6, 7)
  12081.         begin
  12082.             print "Object is stored in 'sysprocedures' and has no space allocated directly."
  12083.             return (1)
  12084.         end
  12085.  
  12086.         if @type = 2
  12087.         begin
  12088.             print "Views don't have space allocated."
  12089.             return (1)
  12090.         end
  12091.     end
  12092.  
  12093. end
  12094.  
  12095. /*
  12096. **  We need to create a temp table to do the calculation.
  12097. **  reserved: sum(reserved) where indid in (0, 1, 255)
  12098. **  data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text)
  12099. **  index: sum(used) where indid in (0, 1, 255) - data
  12100. **  unused: sum(reserved) - sum(used) where indid in (0, 1, 255)
  12101. */
  12102. create table #spt_space
  12103. (
  12104.     rows        int null,
  12105.     reserved    int null,
  12106.     data        int null,
  12107.     indexp        int null,
  12108.     unused        int null
  12109. )
  12110. set nocount on
  12111.  
  12112. /*
  12113. **  If @id is null, then we want summary data.
  12114. */
  12115. if @id = null
  12116. begin
  12117.     select distinct database_name = db_name(), database_size =
  12118.         substring(convert(varchar(10),
  12119.             round( (d.low * convert(float, sum(size)))
  12120.             / 1048576, 0)) + " " + "MB", 1, 20)
  12121.         from master.dbo.sysusages, master.dbo.spt_values d
  12122.             where dbid = db_id()
  12123.                 and d.number = 1
  12124.                 and d.type = "E"
  12125.             having dbid = db_id()
  12126.                 and d.number = 1
  12127.                 and d.type = "E"
  12128.  
  12129.     /*
  12130.     **  Now calculate the summary data.
  12131.     **  reserved: sum(reserved) where indid in (0, 1, 255)
  12132.     */
  12133.     insert into #spt_space (reserved)
  12134.         select sum(reserved)
  12135.             from sysindexes
  12136.                 where indid in (0, 1, 255)
  12137.  
  12138.     /*
  12139.     ** data: sum(dpages) where indid < 2
  12140.     **    + sum(used) where indid = 255 (text)
  12141.     */
  12142.     select @pages = sum(dpages)
  12143.             from sysindexes
  12144.                 where indid < 2
  12145.     select @pages = @pages + isnull(sum(used), 0)
  12146.         from sysindexes
  12147.             where indid = 255
  12148.     update #spt_space
  12149.         set data = @pages
  12150.  
  12151.     
  12152.     /* index: sum(used) where indid in (0, 1, 255) - data */
  12153.     update #spt_space
  12154.         set indexp = (select sum(used)
  12155.                 from sysindexes
  12156.                     where indid in (0, 1, 255))
  12157.                 - data
  12158.  
  12159.     /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  12160.     update #spt_space
  12161.         set unused = reserved
  12162.                 - (select sum(used)
  12163.                     from sysindexes
  12164.                         where indid in (0, 1, 255))
  12165.  
  12166.     select reserved = substring(convert(varchar(15),
  12167.         round((reserved * convert(float, d.low)) / 1024, 0))
  12168.             + " " + "KB", 1, 15),
  12169.         data = substring(convert(varchar(15),
  12170.         round((data * convert(float, d.low)) / 1024, 0))
  12171.             + " " + "KB", 1, 15),
  12172.         index_size = substring(convert(varchar(15),
  12173.         round((indexp * convert(float, d.low)) / 1024, 0))
  12174.             + " " + "KB", 1, 15),
  12175.         unused = substring(convert(varchar(15),
  12176.         round((unused * convert(float, d.low)) / 1024, 0))
  12177.             + " " + "KB", 1, 15)
  12178.             from #spt_space, master.dbo.spt_values d
  12179.         where d.number = 1
  12180.             and d.type = "E"
  12181. end
  12182.  
  12183. /*
  12184. **  We want a particular object.
  12185. */
  12186. else
  12187. begin
  12188.     /*
  12189.     **  Now calculate the summary data.
  12190.     **  reserved: sum(reserved) where indid in (0, 1, 255)
  12191.     */
  12192.     insert into #spt_space (reserved)
  12193.         select sum(reserved)
  12194.             from sysindexes
  12195.                 where indid in (0, 1, 255)
  12196.                     and id = @id
  12197.  
  12198.     /*
  12199.     ** data: sum(dpages) where indid < 2
  12200.     **    + sum(used) where indid = 255 (text)
  12201.     */
  12202.     select @pages = sum(dpages)
  12203.             from sysindexes
  12204.                 where indid < 2
  12205.                     and id = @id
  12206.     select @pages = @pages + isnull(sum(used), 0)
  12207.         from sysindexes
  12208.             where indid = 255
  12209.                 and id = @id
  12210.     update #spt_space
  12211.         set data = @pages
  12212.  
  12213.     
  12214.     /* index: sum(used) where indid in (0, 1, 255) - data */
  12215.     update #spt_space
  12216.         set indexp = (select sum(used)
  12217.                 from sysindexes
  12218.                     where indid in (0, 1, 255)
  12219.                         and id = @id)
  12220.                 - data
  12221.  
  12222.     /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  12223.     update #spt_space
  12224.         set unused = reserved
  12225.                 - (select sum(used)
  12226.                     from sysindexes
  12227.                         where indid in (0, 1, 255)
  12228.                             and id = @id)
  12229.     update #spt_space
  12230.         set rows = i.rows
  12231.             from sysindexes i
  12232.                 where i.indid < 2
  12233.                     and i.id = @id
  12234.  
  12235.     select name = substring(object_name(@id), 1, 20),
  12236.         rows = convert(char(11), rows),
  12237.         reserved = substring(convert(varchar(15),
  12238.             round((reserved * convert(float, d.low)) / 1024, 0))
  12239.                 + " " + "KB", 1, 10),
  12240.         data = substring(convert(varchar(15),
  12241.             round((data * convert(float, d.low)) / 1024, 0))
  12242.                 + " " + "KB", 1, 10),
  12243.         index_size = substring(convert(varchar(15),
  12244.             round((indexp * convert(float, d.low)) / 1024, 0))
  12245.                 + " " + "KB", 1, 10),
  12246.         unused = substring(convert(varchar(15),
  12247.             round((unused * convert(float, d.low)) / 1024, 0))
  12248.                 + " " + "KB", 1, 10)
  12249.     from #spt_space, master.dbo.spt_values d
  12250.         where d.number = 1
  12251.             and d.type = "E"
  12252. end
  12253.  
  12254. return (0)
  12255. go
  12256. grant execute on sp_spaceused to public
  12257. go
  12258. dump transaction master with truncate_only
  12259. go
  12260. /* unbindefault 27.1 3/5/90 */
  12261. create procedure sp_unbindefault
  12262. @objname varchar(61),            /* table/column or datatype name */
  12263. @futureonly varchar(15) = NULL        /* flag to indicate extent of binding */
  12264. as
  12265.  
  12266. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  12267.                     ** the binding to be limited */
  12268.  
  12269. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  12270.  
  12271. /*
  12272. **  When a default or rule is bound to a user-defined datatype, it is also
  12273. **  bound, by default, to any columns of the user datatype that are currently
  12274. **  using the existing default or rule as their default or rule.  This default
  12275. **  action may be overridden by setting @futureonly = @futurevalue when the
  12276. **  procedure is invoked.  In this case existing columns with the user
  12277. **  datatype won't have their existing default or rule changed.
  12278. */
  12279.  
  12280. /*
  12281. **  Check to see that the object names are local to the current database.
  12282. */
  12283. if @objname like "%.%.%"
  12284. begin
  12285.     print "Column or usertype must be in 'current' database."
  12286.     return (1)
  12287. end
  12288.  
  12289. /*
  12290. **  If @objname is of the form tab.col then we are unbinding a column.
  12291. **  Otherwise its a datatype.  In the column case, we need to extract
  12292. **  and verify the table and column names and make sure the user owns
  12293. **  the table that is getting the default unbound.
  12294. */
  12295. if @objname like "%.%"
  12296. begin
  12297.     declare @tabname varchar(30)        /* name of table */
  12298.     declare @colname varchar(30)        /* name of column */
  12299.  
  12300.     /*
  12301.     **  Get the table name out.
  12302.     */
  12303.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  12304.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  12305.  
  12306.     /*
  12307.     **  Find it and unbind it.
  12308.     */
  12309.     if exists (select *
  12310.             from syscolumns, sysobjects
  12311.         where syscolumns.id = object_id(@tabname)
  12312.             and syscolumns.name = @colname
  12313.             and sysobjects.id = object_id(@tabname)
  12314.             and uid = user_id()
  12315.             and sysobjects.sysstat & 7 = 3)      /* user table */
  12316.     begin
  12317.         /*
  12318.         **  Is there something bound to it?
  12319.         */
  12320.         if exists (select *
  12321.             from syscolumns, sysobjects
  12322.         where syscolumns.id = object_id(@tabname)
  12323.             and syscolumns.name = @colname
  12324.             and sysobjects.id = object_id(@tabname)
  12325.             and uid = user_id()
  12326.             and sysobjects.sysstat & 7 = 3      /* user table */
  12327.             and cdefault != 0)
  12328.         begin
  12329.             update syscolumns
  12330.                 set cdefault = 0
  12331.                     from syscolumns, sysobjects
  12332.                 where syscolumns.id = object_id(@tabname)
  12333.                     and syscolumns.name = @colname
  12334.                     and sysobjects.id = object_id(@tabname)
  12335.                     and uid = user_id()
  12336.                     and sysobjects.sysstat & 7 = 3
  12337.  
  12338.             /*
  12339.             **  Since binding a default is a schema change,
  12340.             **  update schema count
  12341.             **  for the object in the sysobjects table.
  12342.             */
  12343.             update sysobjects
  12344.                 set schema = schema + 1
  12345.                     from sysobjects
  12346.                         where id = object_id(@tabname)
  12347.                             and uid = user_id()
  12348.  
  12349.             print "Default unbound from table column."
  12350.             return (0)
  12351.         end
  12352.  
  12353.         else
  12354.         begin
  12355.             print "The specified column has no default."
  12356.             return (1)
  12357.         end
  12358.     end
  12359.  
  12360.     /*
  12361.     **  If we got here, there was no such object.
  12362.     */
  12363.     print "You do not own a table with a column of that name."
  12364.     return (1)
  12365.  
  12366. end
  12367. else
  12368. begin
  12369.     /*
  12370.     **  We're unbinding to a user type.  In this case, the @objname
  12371.     **  is really the name of the user datatype.
  12372.     **  When we unbind to a user type, any existing columns get changed
  12373.     **  to the new binding unless their current binding is not equal
  12374.     **  to the current binding for the usertype or if they set the
  12375.     **  @futureonly parameter to @futurevalue.
  12376.     */
  12377.     declare @olddefault int            /* current default for type */
  12378.  
  12379.     /*
  12380.     **  Get the current default for the datatype.
  12381.     */
  12382.     select @olddefault = tdefault
  12383.         from systypes
  12384.     where name = @objname
  12385.         and uid = user_id()
  12386.         and usertype > 100
  12387.  
  12388.     if @olddefault = null
  12389.     begin
  12390.         print "You do not own a user datatype of that name."
  12391.         return (1)
  12392.     end
  12393.  
  12394.     if @olddefault = 0
  12395.     begin
  12396.         print "The specified user datatype has no default."
  12397.         return (1)
  12398.     end
  12399.  
  12400.     update systypes
  12401.         set tdefault = 0
  12402.             from systypes
  12403.         where name = @objname
  12404.             and uid = user_id()
  12405.             and usertype > 100
  12406.  
  12407.     print "Default unbound from datatype."
  12408.  
  12409.     /*
  12410.     **  Now see if there are any columns with the usertype that
  12411.     **  need the new binding.
  12412.     */
  12413.     if isnull(@futureonly, "") != @futurevalue
  12414.         and exists (select *
  12415.             from syscolumns, systypes
  12416.                 where syscolumns.usertype = systypes.usertype
  12417.                     and systypes.name = @objname
  12418.                     and systypes.usertype > 100
  12419.                     and systypes.uid = user_id()
  12420.                     and syscolumns.cdefault = @olddefault)
  12421.     begin
  12422.         /*
  12423.         **  Update syscolumns with new binding.
  12424.         */
  12425.         update syscolumns
  12426.             set cdefault = systypes.tdefault
  12427.                 from syscolumns, systypes
  12428.             where syscolumns.usertype = systypes.usertype
  12429.                 and systypes.name = @objname
  12430.                 and systypes.usertype > 100
  12431.                 and systypes.uid = user_id()
  12432.                 and syscolumns.cdefault = @olddefault
  12433.  
  12434.         /*
  12435.         **  Update the table schema to indicate that something
  12436.         **  has changed in the table's schema.
  12437.         */
  12438.         update sysobjects
  12439.             set schema = schema + 1
  12440.                 from sysobjects, syscolumns, systypes
  12441.             where sysobjects.id = syscolumns.id
  12442.                 and syscolumns.usertype = systypes.usertype
  12443.                 and systypes.name = @objname
  12444.                 and systypes.usertype > 100
  12445.                 and systypes.uid = user_id()
  12446.                 and syscolumns.cdefault = systypes.tdefault
  12447.  
  12448.         print "Columns of the user datatype specified had their defaults unbound."
  12449.     end
  12450. end
  12451.  
  12452. return (0)
  12453. go
  12454. grant execute on sp_unbindefault to public
  12455. go
  12456. /* unbindrule 27.1 3/5/90 */
  12457. create procedure sp_unbindrule
  12458. @objname varchar(61),            /* table/column or datatype name */
  12459. @futureonly varchar(15) = NULL        /* flag to indicate extent of binding */
  12460. as
  12461.  
  12462. declare @futurevalue varchar(15)    /* the value of @futureonly that causes
  12463.                     ** the binding to be limited */
  12464.  
  12465. select @futurevalue = "futureonly"    /* initialize @futurevalue */
  12466.  
  12467. /*
  12468. **  When a default or rule is bound to a user-defined datatype, it is also
  12469. **  bound, by default, to any columns of the user datatype that are currently
  12470. **  using the existing default or rule as their default or rule.  This default
  12471. **  action may be overridden by setting @futureonly = @futurevalue when the
  12472. **  procedure is invoked.  In this case existing columns with the user
  12473. **  datatype won't have their existing default or rule changed.
  12474. */
  12475.  
  12476. /*
  12477. **  Check to see that the object names are local to the current database.
  12478. */
  12479. if @objname like "%.%.%"
  12480. begin
  12481.     print "Column or user datatype must be in 'current' database."
  12482.     return (1)
  12483. end
  12484.  
  12485. /*
  12486. **  If @objname is of the form tab.col then we are unbinding a column.
  12487. **  Otherwise its a datatype.  In the column case, we need to extract
  12488. **  and verify the table and column names and make sure the user owns
  12489. **  the table that is getting the default unbound.
  12490. */
  12491. if @objname like "%.%"
  12492. begin
  12493.     declare @tabname varchar(30)        /* name of table */
  12494.     declare @colname varchar(30)        /* name of column */
  12495.  
  12496.     /*
  12497.     **  Get the table name out.
  12498.     */
  12499.     select @tabname = substring(@objname, 1, charindex(".", @objname) - 1)
  12500.     select @colname = substring(@objname, charindex(".", @objname) + 1, 61)
  12501.  
  12502.     /*
  12503.     **  Find it and unbind it.
  12504.     */
  12505.     if exists (select *
  12506.             from syscolumns, sysobjects
  12507.         where syscolumns.id = object_id(@tabname)
  12508.             and syscolumns.name = @colname
  12509.             and sysobjects.id = object_id(@tabname)
  12510.             and uid = user_id()
  12511.             and sysobjects.sysstat & 7 = 3)      /* user table */
  12512.     begin
  12513.         /*
  12514.         **  Is there something bound to it?
  12515.         */
  12516.         if exists (select *
  12517.             from syscolumns, sysobjects
  12518.         where syscolumns.id = object_id(@tabname)
  12519.             and syscolumns.name = @colname
  12520.             and sysobjects.id = object_id(@tabname)
  12521.             and uid = user_id()
  12522.             and sysobjects.sysstat & 7 = 3      /* user table */
  12523.             and domain != 0)
  12524.         begin
  12525.             update syscolumns
  12526.                 set domain = 0
  12527.                     from syscolumns, sysobjects
  12528.                 where syscolumns.id = object_id(@tabname)
  12529.                     and syscolumns.name = @colname
  12530.                     and sysobjects.id = object_id(@tabname)
  12531.                     and uid = user_id()
  12532.                     and sysobjects.sysstat & 7 = 3
  12533.  
  12534.             /*
  12535.             **  Since binding a rule is a schema change,
  12536.             **  update schema count
  12537.             **  for the object in the sysobjects table.
  12538.             */
  12539.             update sysobjects
  12540.                 set schema = schema + 1
  12541.                     from sysobjects
  12542.                         where id = object_id(@tabname)
  12543.                             and uid = user_id()
  12544.  
  12545.             print "Rule unbound from table column."
  12546.             return (0)
  12547.         end
  12548.  
  12549.         else
  12550.         begin
  12551.             print "The specified column has no rule."
  12552.             return (1)
  12553.         end
  12554.     end
  12555.  
  12556.     /*
  12557.     **  If we got here, there was no such object.
  12558.     */
  12559.     print "You do not own a table with a column of that name."
  12560.     return (1)
  12561.  
  12562. end
  12563. else
  12564. begin
  12565.     /*
  12566.     **  We're unbinding to a user type.  In this case, the @objname
  12567.     **  is really the name of the user datatype.
  12568.     **  When we unbind to a user type, any existing columns get changed
  12569.     **  to the new binding unless their current binding is not equal
  12570.     **  to the current binding for the usertype or if they set the
  12571.     **  @futureonly parameter to @futurevalue.
  12572.     */
  12573.     declare @oldrule int            /* current rule for type */
  12574.  
  12575.     /*
  12576.     **  Get the current rule for the datatype.
  12577.     */
  12578.     select @oldrule = domain
  12579.         from systypes
  12580.     where name = @objname
  12581.         and uid = user_id()
  12582.         and usertype > 100
  12583.  
  12584.     if @oldrule = null
  12585.     begin
  12586.         print "You do not own a user datatype of that name."
  12587.         return (1)
  12588.     end
  12589.  
  12590.     if @oldrule = 0
  12591.     begin
  12592.         print "The specified user datatype has no rule."
  12593.         return (1)
  12594.     end
  12595.  
  12596.     update systypes
  12597.         set domain = 0
  12598.             from systypes
  12599.         where name = @objname
  12600.             and uid = user_id()
  12601.             and usertype > 100
  12602.  
  12603.     print "Rule unbound from datatype."
  12604.  
  12605.     /*
  12606.     **  Now see if there are any columns with the usertype that
  12607.     **  need the new binding.
  12608.     */
  12609.     if isnull(@futureonly, "") != @futurevalue
  12610.         and exists (select *
  12611.             from syscolumns, systypes
  12612.                 where syscolumns.usertype = systypes.usertype
  12613.                     and systypes.name = @objname
  12614.                     and systypes.usertype > 100
  12615.                     and systypes.uid = user_id()
  12616.                     and syscolumns.domain = @oldrule)
  12617.     begin
  12618.         /*
  12619.         **  Update syscolumns with new binding.
  12620.         */
  12621.         update syscolumns
  12622.             set domain = systypes.domain
  12623.                 from syscolumns, systypes
  12624.             where syscolumns.usertype = systypes.usertype
  12625.                 and systypes.name = @objname
  12626.                 and systypes.usertype > 100
  12627.                 and systypes.uid = user_id()
  12628.                 and syscolumns.domain = @oldrule
  12629.  
  12630.         /*
  12631.         **  Update the table schema to indicate that something
  12632.         **  has changed in the table's schema.
  12633.         */
  12634.         update sysobjects
  12635.             set schema = schema + 1
  12636.                 from sysobjects, syscolumns, systypes
  12637.             where sysobjects.id = syscolumns.id
  12638.                 and syscolumns.usertype = systypes.usertype
  12639.                 and systypes.name = @objname
  12640.                 and systypes.usertype > 100
  12641.                 and systypes.uid = user_id()
  12642.                 and syscolumns.domain = systypes.domain
  12643.  
  12644.         print "Columns of the user datatype specified had their rules unbound."
  12645.     end
  12646. end
  12647.  
  12648. return (0)
  12649. go
  12650. grant execute on sp_unbindrule to public
  12651. go
  12652. dump transaction master with truncate_only
  12653. go
  12654. /* who 27.1 3/5/90 */
  12655. create procedure sp_who @loginame varchar(30) = NULL as
  12656.  
  12657. declare @low int
  12658. declare @high int
  12659. declare @spidlow int
  12660. declare @spidhigh int
  12661.  
  12662. select @low = 0, @high = 32767, @spidlow = 0, @spidhigh = 32767
  12663.  
  12664. if @loginame is not NULL
  12665. begin
  12666.     select @low = suser_id(@loginame), @high = suser_id(@loginame)
  12667.     if @low is NULL
  12668.     begin
  12669.         if @loginame like "[0-9]%"
  12670.         begin
  12671.             select @spidlow = convert(int, @loginame),
  12672.                  @spidhigh = convert(int, @loginame),
  12673.                  @low = 0, @high = 32767
  12674.         end
  12675.         else
  12676.         begin
  12677.             print "No login exists with the supplied name."
  12678.             return (1)
  12679.         end
  12680.     end
  12681. end
  12682. select spid,status,loginame=substring(suser_name(suid),1,12),
  12683.    hostname,blk=convert(char(3),blocked),
  12684.    dbname=substring(db_name(dbid),1,10),cmd
  12685. from sysprocesses
  12686. where suid >= @low and suid <= @high
  12687. and spid >= @spidlow and spid <= @spidhigh
  12688.  
  12689. return (0)
  12690. go
  12691. grant execute on sp_who to public
  12692. go
  12693. /* addprobe    27.1    3/5/90    */
  12694. /*
  12695. **  This file add the account and user named "probe".  This account is
  12696. **  used the 2-phased commit service.
  12697. */
  12698. use master
  12699. go
  12700.  
  12701. if not exists (select *
  12702.         from syslogins
  12703.             where name = "probe")
  12704. begin
  12705.     execute sp_addlogin probe
  12706. end
  12707. go
  12708.  
  12709. if not exists (select *
  12710.         from sysusers
  12711.             where name = "probe")
  12712. begin
  12713.     execute sp_adduser probe
  12714. end
  12715. go
  12716. go
  12717. dump transaction master with truncate_only
  12718. go
  12719. /* definekey 27.1 3/5/90 */
  12720.  
  12721. /*
  12722. **  This file defines the primary, foreign, and common keys for the
  12723. **  system tables.
  12724. */
  12725.  
  12726. /*
  12727. **  First do model, then do master.
  12728. */
  12729. use model
  12730. go
  12731.  
  12732. /*
  12733. **  Do the primary keys for the system tables.
  12734. */
  12735. if exists (select *
  12736.         from syskeys
  12737.     where type = 1 and id = object_id("sysalternates"))
  12738.         execute sp_dropkey primary, sysalternates
  12739. go
  12740. sp_primarykey sysalternates, suid
  12741. go
  12742.  
  12743. if exists (select *
  12744.         from syskeys
  12745.     where type = 1 and id = object_id("syscolumns"))
  12746.         execute sp_dropkey primary, syscolumns
  12747. go
  12748. sp_primarykey syscolumns, id, number, colid
  12749. go
  12750.  
  12751. if exists (select *
  12752.         from syskeys
  12753.     where type = 1 and id = object_id("syscomments"))
  12754.         execute sp_dropkey primary, syscomments
  12755. go
  12756. sp_primarykey syscomments, id, number, colid, texttype
  12757. go
  12758.  
  12759. if exists (select *
  12760.         from syskeys
  12761.     where type = 1 and id = object_id("sysdepends"))
  12762.         execute sp_dropkey primary, sysdepends
  12763. go
  12764. sp_primarykey sysdepends, id, number, depid, depnumber
  12765. go
  12766.  
  12767. if exists (select *
  12768.         from syskeys
  12769.     where type = 1 and id = object_id("sysindexes"))
  12770.         execute sp_dropkey primary, sysindexes
  12771. go
  12772. sp_primarykey sysindexes, id, indid
  12773. go
  12774.  
  12775. if exists (select *
  12776.         from syskeys
  12777.     where type = 1 and id = object_id("syskeys"))
  12778.         execute sp_dropkey primary, syskeys
  12779. go
  12780. sp_primarykey syskeys, id
  12781. go
  12782.  
  12783. if exists (select *
  12784.         from syskeys
  12785.     where type = 1 and id = object_id("sysobjects"))
  12786.         execute sp_dropkey primary, sysobjects
  12787. go
  12788. sp_primarykey sysobjects, id
  12789. go
  12790.  
  12791. if exists (select *
  12792.         from syskeys
  12793.     where type = 1 and id = object_id("sysprocedures"))
  12794.         execute sp_dropkey primary, sysprocedures
  12795. go
  12796. sp_primarykey sysprocedures, id, number, type, sequence
  12797. go
  12798.  
  12799. if exists (select *
  12800.         from syskeys
  12801.     where type = 1 and id = object_id("sysprotects"))
  12802.         execute sp_dropkey primary, sysprotects
  12803. go
  12804. sp_primarykey sysprotects, id, uid, action
  12805. go
  12806.  
  12807. if exists (select *
  12808.         from syskeys
  12809.     where type = 1 and id = object_id("systypes"))
  12810.         execute sp_dropkey primary, systypes
  12811. go
  12812. sp_primarykey systypes, name
  12813. go
  12814.  
  12815. if exists (select *
  12816.         from syskeys
  12817.     where type = 1 and id = object_id("sysusers"))
  12818.         execute sp_dropkey primary, sysusers
  12819. go
  12820. sp_primarykey sysusers, suid
  12821. go
  12822.  
  12823. if exists (select *
  12824.         from syskeys
  12825.     where type = 1 and id = object_id("syssegments"))
  12826.         execute sp_dropkey primary, syssegments
  12827. go
  12828. sp_primarykey syssegments, segment
  12829. go
  12830.  
  12831. /*
  12832. **  Now do the foreign keys and common keys.
  12833. */
  12834. if exists (select *
  12835.         from syskeys
  12836.     where type = 2 and id = object_id("sysalternates"))
  12837.         execute sp_dropkey foreign, sysalternates, sysusers
  12838. go
  12839. sp_foreignkey sysalternates, sysusers, suid
  12840. go
  12841.  
  12842. if exists (select *
  12843.         from syskeys
  12844.     where type = 3 and id = object_id("syscolumns"))
  12845.         execute sp_dropkey common, syscolumns, syscomments
  12846. go
  12847. sp_commonkey syscolumns, syscomments, id, id
  12848. go
  12849.  
  12850. sp_dropkey common, syscolumns, syskeys
  12851. go
  12852. sp_commonkey syscolumns, syskeys, id, key1
  12853. go
  12854. sp_commonkey syscolumns, syskeys, id, depkey1
  12855. go
  12856.  
  12857. sp_dropkey common, syscolumns, sysobjects
  12858. go
  12859. sp_commonkey syscolumns, sysobjects, id, id
  12860. go
  12861.  
  12862. sp_dropkey common, syscolumns, systypes
  12863. go
  12864. sp_commonkey syscolumns, systypes, type, type
  12865. go
  12866. sp_commonkey syscolumns, systypes, usertype, usertype
  12867. go
  12868. sp_commonkey syscolumns, systypes, domain, domain
  12869. go
  12870. sp_commonkey syscolumns, systypes, cdefault, tdefault
  12871. go
  12872.  
  12873. if exists (select *
  12874.         from syskeys
  12875.     where type = 3 and id = object_id("syscomments"))
  12876.         execute sp_dropkey common, syscomments, sysobjects
  12877. go
  12878. sp_commonkey syscomments, sysobjects, id, id
  12879. go
  12880.  
  12881. if exists (select *
  12882.         from syskeys
  12883.     where type = 3 and id = object_id("sysdepends"))
  12884.         execute sp_dropkey common, sysdepends, sysobjects
  12885. go
  12886. sp_commonkey sysdepends, sysobjects, id, id
  12887. go
  12888.  
  12889. if exists (select *
  12890.         from syskeys
  12891.     where type = 3 and id = object_id("sysindexes"))
  12892.         execute sp_dropkey common, sysindexes, sysobjects
  12893. go
  12894. sp_commonkey sysindexes, sysobjects, id, id
  12895. go
  12896.  
  12897. sp_dropkey common, sysindexes, syssegments
  12898. go
  12899. sp_commonkey sysindexes, syssegments, segment, segment
  12900. go
  12901.  
  12902. if exists (select *
  12903.         from syskeys
  12904.     where type = 2 and id = object_id("syskeys"))
  12905.         execute sp_dropkey foreign, syskeys, sysobjects
  12906. go
  12907. sp_foreignkey syskeys, sysobjects, id
  12908. go
  12909.  
  12910. if exists (select *
  12911.         from syskeys
  12912.     where type = 3 and id = object_id("syskeys"))
  12913.         execute sp_dropkey common, syskeys, sysobjects
  12914. go
  12915. sp_commonkey syskeys, sysobjects, depid, id
  12916. go
  12917.  
  12918. if exists (select *
  12919.         from syskeys
  12920.     where type = 3 and id = object_id("sysobjects"))
  12921.         execute sp_dropkey common, sysobjects, sysprocedures
  12922. go
  12923. sp_commonkey sysobjects, sysprocedures, id, id
  12924. go
  12925.  
  12926. sp_dropkey common, sysobjects, sysprotects
  12927. go
  12928. sp_commonkey sysobjects, sysprotects, id, id
  12929. go
  12930.  
  12931. sp_dropkey common, sysobjects, sysusers
  12932. go
  12933. sp_commonkey sysobjects, sysusers, uid, uid
  12934. go
  12935.  
  12936. if exists (select *
  12937.         from syskeys
  12938.     where type = 3 and id = object_id("sysprotects"))
  12939.         execute sp_dropkey common, sysprotects, sysusers
  12940. go
  12941. sp_commonkey sysprotects, sysusers, uid, uid
  12942. go
  12943.  
  12944. if exists (select *
  12945.         from syskeys
  12946.     where type = 3 and id = object_id("systypes"))
  12947.         execute sp_dropkey common, systypes, sysusers
  12948. go
  12949. sp_commonkey systypes, sysusers, uid, uid
  12950. go
  12951.  
  12952. if exists (select *
  12953.         from syskeys
  12954.     where type = 3 and id = object_id("sysindexes"))
  12955.         execute sp_dropkey common, sysindexes, syssegments
  12956. go
  12957. sp_commonkey sysindexes, syssegments, segment, segment
  12958. go
  12959.  
  12960. /*
  12961. **  Now do the standard systems tables in master.
  12962. */
  12963. use master
  12964. go
  12965.  
  12966. /*
  12967. **  Do the primary keys for the system tables.
  12968. */
  12969. if exists (select *
  12970.         from syskeys
  12971.     where type = 1 and id = object_id("sysalternates"))
  12972.         execute sp_dropkey primary, sysalternates
  12973. go
  12974. sp_primarykey sysalternates, suid
  12975. go
  12976.  
  12977. if exists (select *
  12978.         from syskeys
  12979.     where type = 1 and id = object_id("syscolumns"))
  12980.         execute sp_dropkey primary, syscolumns
  12981. go
  12982. sp_primarykey syscolumns, id, number, colid
  12983. go
  12984.  
  12985. if exists (select *
  12986.         from syskeys
  12987.     where type = 1 and id = object_id("syscomments"))
  12988.         execute sp_dropkey primary, syscomments
  12989. go
  12990. sp_primarykey syscomments, id, number, colid, texttype
  12991. go
  12992.  
  12993. if exists (select *
  12994.         from syskeys
  12995.     where type = 1 and id = object_id("sysdepends"))
  12996.         execute sp_dropkey primary, sysdepends
  12997. go
  12998. sp_primarykey sysdepends, id, number, depid, depnumber
  12999. go
  13000.  
  13001. if exists (select *
  13002.         from syskeys
  13003.     where type = 1 and id = object_id("sysindexes"))
  13004.         execute sp_dropkey primary, sysindexes
  13005. go
  13006. sp_primarykey sysindexes, id, indid
  13007. go
  13008.  
  13009. if exists (select *
  13010.         from syskeys
  13011.     where type = 1 and id = object_id("syskeys"))
  13012.         execute sp_dropkey primary, syskeys
  13013. go
  13014. sp_primarykey syskeys, id
  13015. go
  13016.  
  13017. if exists (select *
  13018.         from syskeys
  13019.     where type = 1 and id = object_id("sysobjects"))
  13020.         execute sp_dropkey primary, sysobjects
  13021. go
  13022. sp_primarykey sysobjects, id
  13023. go
  13024.  
  13025. if exists (select *
  13026.         from syskeys
  13027.     where type = 1 and id = object_id("sysprocedures"))
  13028.         execute sp_dropkey primary, sysprocedures
  13029. go
  13030. sp_primarykey sysprocedures, id, number, type, sequence
  13031. go
  13032.  
  13033. if exists (select *
  13034.         from syskeys
  13035.     where type = 1 and id = object_id("sysprotects"))
  13036.         execute sp_dropkey primary, sysprotects
  13037. go
  13038. sp_primarykey sysprotects, id, uid, action
  13039. go
  13040.  
  13041. if exists (select *
  13042.         from syskeys
  13043.     where type = 1 and id = object_id("systypes"))
  13044.         execute sp_dropkey primary, systypes
  13045. go
  13046. sp_primarykey systypes, name
  13047. go
  13048.  
  13049. if exists (select *
  13050.         from syskeys
  13051.     where type = 1 and id = object_id("sysusers"))
  13052.         execute sp_dropkey primary, sysusers
  13053. go
  13054. sp_primarykey sysusers, suid
  13055. go
  13056.  
  13057. if exists (select *
  13058.         from syskeys
  13059.     where type = 1 and id = object_id("sysusages"))
  13060.         execute sp_dropkey primary, sysusages
  13061. go
  13062. sp_primarykey sysusages, dbid, lstart
  13063. go
  13064.  
  13065. if exists (select *
  13066.         from syskeys
  13067.     where type = 1 and id = object_id("sysservers"))
  13068.         execute sp_dropkey primary, sysservers
  13069. go
  13070. sp_primarykey sysservers, srvid
  13071. go
  13072.  
  13073. if exists (select *
  13074.         from syskeys
  13075.     where type = 1 and id = object_id("sysremotelogins"))
  13076.         execute sp_dropkey primary, sysremotelogins
  13077. go
  13078. sp_primarykey sysremotelogins, remoteserverid, remoteusername
  13079. go
  13080.  
  13081. /*
  13082. **  Now do the foreign keys and common keys.
  13083. */
  13084. if exists (select *
  13085.         from syskeys
  13086.     where type = 2 and id = object_id("sysalternates"))
  13087.         execute sp_dropkey foreign, sysalternates, sysusers
  13088. go
  13089. sp_foreignkey sysalternates, sysusers, suid
  13090. go
  13091.  
  13092. if exists (select *
  13093.         from syskeys
  13094.     where type = 3 and id = object_id("syscolumns"))
  13095.         execute sp_dropkey common, syscolumns, syscomments
  13096. go
  13097. sp_commonkey syscolumns, syscomments, id, id
  13098. go
  13099.  
  13100. sp_dropkey common, syscolumns, syskeys
  13101. go
  13102. sp_commonkey syscolumns, syskeys, id, key1
  13103. go
  13104. sp_commonkey syscolumns, syskeys, id, depkey1
  13105. go
  13106.  
  13107. sp_dropkey common, syscolumns, sysobjects
  13108. go
  13109. sp_commonkey syscolumns, sysobjects, id, id
  13110. go
  13111.  
  13112. sp_dropkey common, syscolumns, systypes
  13113. go
  13114. sp_commonkey syscolumns, systypes, type, type
  13115. go
  13116. sp_commonkey syscolumns, systypes, usertype, usertype
  13117. go
  13118. sp_commonkey syscolumns, systypes, domain, domain
  13119. go
  13120. sp_commonkey syscolumns, systypes, cdefault, tdefault
  13121. go
  13122.  
  13123. if exists (select *
  13124.         from syskeys
  13125.     where type = 3 and id = object_id("syscomments"))
  13126.         execute sp_dropkey common, syscomments, sysobjects
  13127. go
  13128. sp_commonkey syscomments, sysobjects, id, id
  13129. go
  13130.  
  13131. if exists (select *
  13132.         from syskeys
  13133.     where type = 3 and id = object_id("sysdepends"))
  13134.         execute sp_dropkey common, sysdepends, sysobjects
  13135. go
  13136. sp_commonkey sysdepends, sysobjects, id, id
  13137. go
  13138.  
  13139. if exists (select *
  13140.         from syskeys
  13141.     where type = 3 and id = object_id("sysindexes"))
  13142.         execute sp_dropkey common, sysindexes, sysobjects
  13143. go
  13144. sp_commonkey sysindexes, sysobjects, id, id
  13145. go
  13146.  
  13147. sp_dropkey common, sysindexes, syssegments
  13148. go
  13149. sp_commonkey sysindexes, syssegments, segment, segment
  13150. go
  13151.  
  13152. if exists (select *
  13153.         from syskeys
  13154.     where type = 2 and id = object_id("syskeys"))
  13155.         execute sp_dropkey foreign, syskeys, sysobjects
  13156. go
  13157. sp_foreignkey syskeys, sysobjects, id
  13158. go
  13159.  
  13160. if exists (select *
  13161.         from syskeys
  13162.     where type = 3 and id = object_id("syskeys"))
  13163.         execute sp_dropkey common, syskeys, sysobjects
  13164. go
  13165. sp_commonkey syskeys, sysobjects, depid, id
  13166. go
  13167.  
  13168. if exists (select *
  13169.         from syskeys
  13170.     where type = 3 and id = object_id("sysobjects"))
  13171.         execute sp_dropkey common, sysobjects, sysprocedures
  13172. go
  13173. sp_commonkey sysobjects, sysprocedures, id, id
  13174. go
  13175.  
  13176. sp_dropkey common, sysobjects, sysprotects
  13177. go
  13178. sp_commonkey sysobjects, sysprotects, id, id
  13179. go
  13180.  
  13181. sp_dropkey common, sysobjects, sysusers
  13182. go
  13183. sp_commonkey sysobjects, sysusers, uid, uid
  13184. go
  13185.  
  13186. if exists (select *
  13187.         from syskeys
  13188.     where type = 3 and id = object_id("sysprotects"))
  13189.         execute sp_dropkey common, sysprotects, sysusers
  13190. go
  13191. sp_commonkey sysprotects, sysusers, uid, uid
  13192. go
  13193.  
  13194. if exists (select *
  13195.         from syskeys
  13196.     where type = 3 and id = object_id("systypes"))
  13197.         execute sp_dropkey common, systypes, sysusers
  13198. go
  13199. sp_commonkey systypes, sysusers, uid, uid
  13200. go
  13201.  
  13202. /*
  13203. **  These are in the master database only.
  13204. */
  13205. /*  Define primary keys first */
  13206. if exists (select *
  13207.         from syskeys
  13208.     where type = 1 and id = object_id("sysconfigures"))
  13209.         execute sp_dropkey primary, sysconfigures
  13210. go
  13211. sp_primarykey sysconfigures, config
  13212. go
  13213.  
  13214. if exists (select *
  13215.         from syskeys
  13216.     where type = 1 and id = object_id("sysdatabases"))
  13217.         execute sp_dropkey primary, sysdatabases
  13218. go
  13219. sp_primarykey sysdatabases, name
  13220. go
  13221.  
  13222. if exists (select *
  13223.         from syskeys
  13224.     where type = 1 and id = object_id("sysdevices"))
  13225.         execute sp_dropkey primary, sysdevices
  13226. go
  13227. sp_primarykey sysdevices, name
  13228. go
  13229.  
  13230. if exists (select *
  13231.         from syskeys
  13232.     where type = 1 and id = object_id("syslogins"))
  13233.         execute sp_dropkey primary, syslogins
  13234. go
  13235. sp_primarykey syslogins, suid
  13236. go
  13237.  
  13238. if exists (select *
  13239.         from syskeys
  13240.     where type = 1 and id = object_id("sysmessages"))
  13241.         execute sp_dropkey primary, sysmessages
  13242. go
  13243. sp_primarykey sysmessages, error, dlevel
  13244. go
  13245.  
  13246. if exists (select *
  13247.         from syskeys
  13248.     where type = 1 and id = object_id("sysusages"))
  13249.         execute sp_dropkey primary, sysusages
  13250. go
  13251. sp_primarykey sysusages, dbid, lstart
  13252. go
  13253.  
  13254. /*
  13255. **  Now the foreign and common keys.
  13256. */
  13257. if exists (select *
  13258.         from syskeys
  13259.     where type = 3 and id = object_id("syscurconfigs"))
  13260.         execute sp_dropkey common, syscurconfigs, sysconfigures
  13261. go
  13262. sp_commonkey syscurconfigs, sysconfigures, config, config
  13263. go
  13264.  
  13265. if exists (select *
  13266.         from syskeys
  13267.     where type = 3 and id = object_id("sysdatabases"))
  13268.         execute sp_dropkey common, sysdatabases, syslocks
  13269. go
  13270. sp_commonkey sysdatabases, syslocks, dbid, dbid
  13271. go
  13272.  
  13273. sp_dropkey common, sysdatabases, syslogins
  13274. go
  13275. sp_commonkey sysdatabases, syslogins, suid, suid
  13276. go
  13277.  
  13278. sp_dropkey common, sysdatabases, sysprocesses
  13279. go
  13280. sp_commonkey sysdatabases, sysprocesses, dbid, dbid
  13281. go
  13282.  
  13283. sp_dropkey common, sysdatabases, sysusages
  13284. go
  13285. sp_commonkey sysdatabases, sysusages, dbid, dbid
  13286. go
  13287.  
  13288. if exists (select *
  13289.         from syskeys
  13290.     where type = 3 and id = object_id("syslocks"))
  13291.         execute sp_dropkey common, syslocks, sysprocesses
  13292. go
  13293. sp_commonkey syslocks, sysprocesses, spid, spid
  13294. go
  13295.  
  13296. if exists (select *
  13297.         from syskeys
  13298.     where type = 3 and id = object_id("syslogins"))
  13299.         execute sp_dropkey common, syslogins, sysprocesses
  13300. go
  13301. sp_commonkey syslogins, sysprocesses, suid, suid
  13302. go
  13303.  
  13304. if exists (select *
  13305.         from syskeys
  13306.     where type = 3 and id = object_id("sysservers"))
  13307.         execute sp_dropkey common, sysservers, sysremotelogins
  13308. go
  13309. sp_commonkey sysservers, sysremotelogins, srvid, remoteserverid
  13310. go
  13311.  
  13312. if exists (select *
  13313.         from syskeys
  13314.     where type = 3 and id = object_id("sysremotelogins"))
  13315.         execute sp_dropkey common, sysremotelogins, sysusers
  13316. go
  13317. sp_commonkey sysremotelogins, sysusers, suid, suid
  13318. go
  13319.  
  13320. dump transaction master to diskdump with truncate_only
  13321. go
  13322.  
  13323. dump transaction model to diskdump with truncate_only
  13324. go
  13325.  
  13326. go
  13327. dump transaction master to diskdump with truncate_only
  13328. go
  13329. use master
  13330. go
  13331. sp_configure "allow updates",0
  13332. go
  13333. reconfigure with override
  13334. go
  13335. print 'Loading of master database is complete.'
  13336. go
  13337.