home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a061 / 8.img / ADMIN2.SQ@ / ADMIN2.bin
Encoding:
Text File  |  1992-02-26  |  89.6 KB  |  2,643 lines

  1. /*******************************************************************************/
  2. /*  SQL Administrator INSTALL SCRIPT FOR SQL SERVER VERSION 4.2            */
  3. /*******************************************************************************/
  4.  
  5. /************* DUMP THE TRANSACTION LOG **************************************/
  6. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  7. /* script periodically, you will run out of transaction log space.           */
  8. dump tran master with truncate_only
  9. go
  10. /************* END DUMP THE TRANSACTION LOG **********************************/
  11.  
  12. use master
  13. go
  14.  
  15. /*******************************************************************************/
  16. /*        DROP ALL EXISTING PROCEDURE AND TABLES FIRST               */
  17. /*******************************************************************************/
  18. if exists (select * from sysobjects where name = 'sp_MScheck_admin' and sysstat & 7 = 4)
  19. begin
  20.   drop procedure sp_MScheck_admin
  21. end
  22. if exists (select * from sysobjects where name = 'sp_MSdevice_list' and sysstat & 7 = 4)
  23. begin
  24.   drop procedure sp_MSdevice_list
  25. end
  26. if exists (select * from sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  27. begin
  28.   drop procedure sp_MSnext_devnumber
  29. end
  30. if exists (select * from sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  31. begin
  32.   drop procedure sp_MSnext_devnumber
  33. end
  34. if exists (select * from sysobjects where name = "sp_MSdev_mirror" and sysstat & 7 = 4)
  35. begin
  36.   drop procedure sp_MSdev_mirror
  37. end
  38. if exists (select * from sysobjects where name = "sp_MSdev_description" and sysstat & 7 = 4)
  39. begin
  40.     drop procedure sp_MSdev_description
  41. end
  42. if exists (select * from sysobjects where name = "sp_MSdev_databases" and sysstat & 7 = 4)
  43. begin
  44.   drop procedure sp_MSdev_databases
  45. end
  46. if exists (select * from sysobjects where name = "MSscheduled_backups")
  47. begin
  48.   drop table MSscheduled_backups
  49. end
  50. if exists (select * from sysobjects where name = "MSscheduled_backups_log")
  51. begin
  52.     drop table MSscheduled_backups_log
  53. end
  54. if exists (select * from sysobjects where name = "MSsystem_monitor")
  55. begin
  56.     drop table MSsystem_monitor
  57. end
  58. if exists (select * from sysobjects where name = "MSlast_monitor")
  59. begin
  60.     drop table MSlast_monitor
  61. end
  62. if exists (select * from sysobjects where name = "sp_MSsys_monitor" and sysstat & 7 = 4)
  63. begin
  64.     drop procedure sp_MSsys_monitor
  65. end
  66. if exists (select * from sysobjects where name = "sp_MSdatabase_list" and sysstat & 7 = 4)
  67. begin
  68.     drop procedure sp_MSdatabase_list
  69. end
  70. if exists (select * from sysobjects where name = "sp_MSdatabase_avail" and sysstat & 7 = 4)
  71. begin
  72.     drop procedure sp_MSdatabase_avail
  73. end
  74. if exists (select * from sysobjects where name = "sp_MSbackup_now" and sysstat & 7 = 4)
  75. begin
  76.     drop procedure sp_MSbackup_now
  77. end
  78. if exists (select * from sysobjects where name = "sp_MSbackup_schedule" and sysstat & 7 = 4)
  79. begin
  80.     drop procedure sp_MSbackup_schedule
  81. end
  82. if exists (select * from sysobjects where name = "sp_MSuser_list" and sysstat & 7 = 4)
  83. begin
  84.     drop procedure sp_MSuser_list
  85. end
  86. if exists (select * from sysobjects where name = 'sp_MSmonitor' and sysstat & 7 = 4)
  87. begin
  88.     drop procedure sp_MSmonitor
  89. end
  90. if exists (select * from sysobjects where name = 'sp_MScmd_permissions' and sysstat & 7 = 4)
  91. begin
  92.   drop procedure sp_MScmd_permissions
  93. end
  94. if exists (select * from sysobjects where name = 'sp_MSuser_cmd_permissions' and sysstat & 7 = 4)
  95. begin
  96.   drop procedure sp_MSuser_cmd_permissions
  97. end
  98. if exists (select * from sysobjects where name = 'sp_MSsegment_list' and sysstat & 7 = 4)
  99. begin
  100.   drop procedure sp_MSsegment_list
  101. end
  102. if exists (select * from sysobjects where name = 'sp_MSsegment_devices' and sysstat & 7 = 4)
  103. begin
  104.   drop procedure sp_MSsegment_devices
  105. end
  106. if exists (select * from sysobjects where name = 'sp_MSseg_properties' and sysstat & 7 = 4)
  107. begin
  108.   drop procedure sp_MSseg_properties
  109. end
  110. if exists (select * from sysobjects where name = 'sp_MSseg_tables' and sysstat & 7 = 4)
  111. begin
  112.   drop procedure sp_MSseg_tables
  113. end
  114. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_options_bits')
  115. begin
  116.   drop procedure sp_MSdb_options_bits
  117. end
  118. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_devices_segments')
  119. begin
  120.   drop procedure sp_MSdb_devices_segments
  121. end
  122. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_space')
  123. begin
  124.   drop procedure sp_MSdb_space
  125. end
  126. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_options')
  127. begin
  128.   drop procedure sp_MSdb_options
  129. end
  130. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_devices')
  131. begin
  132.   drop procedure sp_MSdb_devices
  133. end
  134. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_properties')
  135. begin
  136.   drop procedure sp_MSdb_properties
  137. end
  138. if exists (select * from sysobjects where name = 'sp_MSthread_list' and sysstat & 7 = 4)
  139. begin
  140.   drop procedure sp_MSthread_list
  141. end
  142. if exists (select * from sysobjects where name = 'sp_MSlogdevice' and sysstat & 7 = 4)
  143. begin
  144.   drop procedure sp_MSlogdevice
  145. end
  146. go
  147.  
  148. /***************************************************************************/
  149. /* This stored procedure checks to make sure all the stored procedures and
  150.    tables that the SQL Server Admin tool uses are actually on the server   */
  151. /***************************************************************************/
  152.  
  153. print ""
  154. print "Creating sp_MScheck_admin"
  155. print ""
  156. go
  157. create procedure sp_MScheck_admin as
  158.  
  159. /* Create a temporary table to store the missing objects */
  160.  
  161. set nocount on
  162.  
  163. create table #spmissing
  164. (name varchar(30),
  165.  type varchar(10)
  166. )
  167.  
  168. /* */
  169. /* Check for the stored procedures first */
  170. /* */
  171.  
  172. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdevice_list' and sysstat & 7 = 4)
  173. begin
  174.   insert into #spmissing values('sp_MSdevice_list', 'procedure')
  175. end
  176. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_description' and sysstat & 7 = 4)
  177. begin
  178.   insert into #spmissing values('sp_MSdev_description', 'procedure')
  179. end
  180. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_databases' and sysstat & 7 = 4)
  181. begin
  182.   insert into #spmissing values('sp_MSdev_databases', 'procedure')
  183. end
  184. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsys_monitor' and sysstat & 7 = 4)
  185. begin
  186.   insert into #spmissing values('sp_MSsys_monitor', 'procedure')
  187. end
  188. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  189. begin
  190.   insert into #spmissing values('sp_MSnext_devnumber', 'procedure')
  191. end
  192. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_list' and sysstat & 7 = 4)
  193. begin
  194.   insert into #spmissing values('sp_MSdatabase_list', 'procedure')
  195. end
  196. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_avail' and sysstat & 7 = 4)
  197. begin
  198.   insert into #spmissing values('sp_MSdatabase_avail', 'procedure')
  199. end
  200. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_now' and sysstat & 7 = 4)
  201. begin
  202.   insert into #spmissing values('sp_MSbackup_now', 'procedure')
  203. end
  204. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_list' and sysstat & 7 = 4)
  205. begin
  206.   insert into #spmissing values('sp_MSuser_list', 'procedure')
  207. end
  208. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSmonitor' and sysstat & 7 = 4)
  209. begin
  210.   insert into #spmissing values('sp_MSmonitor', 'procedure')
  211. end
  212. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices' and sysstat & 7 = 4)
  213. begin
  214.   insert into #spmissing values('sp_MSdb_devices', 'procedure')
  215. end
  216. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options' and sysstat & 7 = 4)
  217. begin
  218.   insert into #spmissing values('sp_MSdb_options', 'procedure')
  219. end
  220. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_properties' and sysstat & 7 = 4)
  221. begin
  222.   insert into #spmissing values('sp_MSdb_properties', 'procedure')
  223. end
  224. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_space' and sysstat & 7 = 4)
  225. begin
  226.   insert into #spmissing values('sp_MSdb_space', 'procedure')
  227. end
  228. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_schedule' and sysstat & 7 = 4)
  229. begin
  230.   insert into #spmissing values('sp_MSbackup_schedule', 'procedure')
  231. end
  232. if not exists (select * from master.dbo.sysobjects where name = 'sp_MScmd_permissions' and sysstat & 7 = 4)
  233. begin
  234.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  235. end
  236. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_cmd_permissions' and sysstat & 7 = 4)
  237. begin
  238.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  239. end
  240. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options_bits' and sysstat & 7 = 4)
  241. begin
  242.   insert into #spmissing values('sp_MSdb_options_bits', 'procedure')
  243. end
  244.  
  245. /* */
  246. /* these are 4.2 only */
  247. /* */
  248.  
  249. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_mirror' and sysstat & 7 = 4)
  250. begin
  251.   insert into #spmissing values('sp_MSdev_mirror', 'procedure')
  252. end
  253. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices_segments' and sysstat & 7 = 4)
  254. begin
  255.   insert into #spmissing values('sp_MSdb_devices_segments', 'procedure')
  256. end
  257. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_list' and sysstat & 7 = 4)
  258. begin
  259.   insert into #spmissing values('sp_MSsegment_list', 'procedure')
  260. end
  261. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_devices' and sysstat & 7 = 4)
  262. begin
  263.   insert into #spmissing values('sp_MSsegment_devices', 'procedure')
  264. end
  265. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_properties' and sysstat & 7 = 4)
  266. begin
  267.   insert into #spmissing values('sp_MSseg_properties', 'procedure')
  268. end
  269. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_tables' and sysstat & 7 = 4)
  270. begin
  271.   insert into #spmissing values('sp_MSseg_tables', 'procedure')
  272. end
  273. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSthread_list' and sysstat & 7 = 4)
  274. begin
  275.   insert into #spmissing values('sp_MSthread_list', 'procedure')
  276. end
  277.  
  278. /* */
  279. /* Check for the tables next */
  280. /* */
  281.  
  282. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups')
  283. begin
  284.   insert into #spmissing values('MSschedule_backups','table')
  285. end
  286. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups_log')
  287. begin
  288.   insert into #spmissing values('MSschedule_backups_log','table')
  289. end
  290. if not exists (select * from master.dbo.sysobjects where name = 'MSsystem_monitor')
  291. begin
  292.   insert into #spmissing values('MSsystem_monitor','table')
  293. end
  294. if not exists (select * from master.dbo.sysobjects where name = 'MSlast_monitor')
  295. begin
  296.   insert into #spmissing values('MSlast_monitor','table')
  297. end
  298.  
  299. set nocount off
  300.  
  301. select * from #spmissing
  302. go
  303.  
  304.  
  305. /****************************************************************************/
  306. /* This stored procedure will get the name, type, size, location, device number, space
  307.    available, and mirror status for all the devices in the system        */
  308. /****************************************************************************/
  309.  
  310. print ""
  311. print "Creating sp_MSdevice_list"
  312. print ""
  313. go
  314. create procedure sp_MSdevice_list as
  315.  
  316. /* Create a temporary table where we can store all the necessary information */
  317.  
  318. set nocount on
  319.  
  320. create table #spdevtab
  321. (name varchar(30) null,
  322.  cntrltype varchar(10) null,
  323.  devtype varchar(20) null,
  324.  devsize float null,
  325.  devsizeout varchar(20) null,
  326.  spaceused int null,
  327.  freespaceout varchar(20) null,
  328.  mirror varchar(10) null
  329. )
  330.  
  331. declare @alloc int
  332. select @alloc = low from master.dbo.spt_values where type = "E" and number = 1
  333.  
  334. /* Insert the name, size, and space used for the database devices that have databases on them */
  335.  
  336. insert into #spdevtab (name, devsize, devsizeout, spaceused, mirror)
  337. select d.name, ((d.high - d.low) * @alloc + 500000) / 1048576,
  338. ltrim(str((((d.high - d.low) * @alloc + 500000)/1048576), 8,0)) + " MB", sum((usg.size * @alloc + 500000) / 1048576), "none"
  339. from sysdevices d, sysusages usg
  340. where d.low <= usg.size + usg.vstart - 1
  341. and d.high >= usg.size + usg.vstart - 1
  342. and d.cntrltype = 0
  343. group by d.name
  344.  
  345. /* Insert the name, size for the database devices that are clean */
  346.  
  347. insert into #spdevtab (name, devsize, devsizeout, spaceused, mirror)
  348. select d.name, ((d.high - d.low) * @alloc + 500000) / 1048576,
  349. ltrim(str((((d.high - d.low) * @alloc + 500000)/1048576), 8,0)) + " MB", 0, "none"
  350. from sysdevices d, sysusages usg
  351. where d.cntrltype = 0 and
  352. d.name not in (select name from #spdevtab)
  353. group by d.name
  354.  
  355. /* Insert the name for the database dump devices */
  356.  
  357. insert #spdevtab (name, devsizeout, freespaceout, mirror)
  358. select d.name, "*******","*****", "*******"
  359. from sysdevices d, #spdevtab s where
  360. d.name not in (select name from #spdevtab)
  361.  
  362. /* Calculate the free space each disk device */
  363.  
  364. update #spdevtab set #spdevtab.freespaceout = convert(varchar(10),#spdevtab.devsize - #spdevtab.spaceused) + " MB"
  365.          from master.dbo.sysdevices d, #spdevtab where d.cntrltype = 0 and #spdevtab.name = d.name
  366.  
  367. /* set tape's to 0 length, this is a temporary fix to account for server requiring
  368.    tape length, will change later */
  369. update #spdevtab set #spdevtab.devsize = 0.0
  370.          from master.dbo.sysdevices d, #spdevtab where d.cntrltype = 5 and #spdevtab.name = d.name
  371.  
  372. /*  Now figure out what kind of controller type it is.
  373.  
  374. **  cntrltype = 0    special
  375. **            2    disk
  376. **          3-4    floppy
  377. **          5        tape
  378. */
  379.  
  380. update #spdevtab set #spdevtab.cntrltype = "d" from master.dbo.sysdevices d, #spdevtab
  381.          where d.cntrltype = 2 and #spdevtab.name = d.name
  382.  
  383. update #spdevtab set #spdevtab.cntrltype = "f" from master.dbo.sysdevices d, #spdevtab
  384.          where d.cntrltype between 3 and 4 and #spdevtab.name = d.name
  385.  
  386. update #spdevtab set #spdevtab.cntrltype = "t" from master.dbo.sysdevices d, #spdevtab
  387.          where d.cntrltype = 5 and #spdevtab.name = d.name
  388.  
  389. update #spdevtab set #spdevtab.cntrltype = "UNKNOWN" from master.dbo.sysdevices d, #spdevtab
  390.          where d.cntrltype > 8 and #spdevtab.name = d.name
  391.  
  392. /* Get floppy device capacities which is listed in sysdevices.high in number of 62k blocks. */
  393.  
  394. if exists (select * from master.dbo.sysdevices d, #spdevtab
  395.        where d.cntrltype between 3 and 4 and d.high > 0 and #spdevtab.name = d.name)
  396. begin
  397.   update #spdevtab set devsizeout = ltrim(str(((d.high * 63488.0) / 1000000.0), 5,1)) + " MB" from master.dbo.sysdevices d, #spdevtab
  398.            where d.cntrltype between 3 and 4 and d.high > 0 and #spdevtab.name = d.name
  399. end
  400.  
  401. /* Get tape device capacities which is listed in sysdevices.high in number of 62k blocks. (skip decimal places) */
  402.  
  403. if exists (select * from master.dbo.sysdevices d, #spdevtab
  404.        where d.cntrltype = 5 and d.high > 0 and #spdevtab.name = d.name)
  405. begin
  406. /* Tempoary fix until server doesn't care about tape size */
  407. /*  update #spdevtab set devsizeout =  ltrim(str(((d.high * 63488) / 1000000), 5,1)) + " MB" from master.dbo.sysdevices d, #spdevtab
  408.            where d.cntrltype = 5 and d.high > 0 and #spdevtab.name = d.name */
  409.   update #spdevtab set devsizeout =  "*******" from master.dbo.sysdevices d, #spdevtab
  410.            where d.cntrltype = 5 and d.high > 0 and #spdevtab.name = d.name
  411. end
  412.  
  413. /* Now get the dump devices by turning the status bits into english. 0x10 = dump device. */
  414.  
  415. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  416.        where v.type = "V" and v.number > -1 and d.status & v.number = 16 and #spdevtab.name = d.name)
  417. begin
  418.   update #spdevtab set devtype = substring(v.name,1,4)
  419.      from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  420.      where v.type = "V" and v.number > -1 and d.status & v.number = 16 and #spdevtab.name = d.name
  421. end
  422.  
  423. /* Now get the physical disks by turning the status bits into english. 0x02 is a physical disk. */
  424.  
  425. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  426.        where v.type = "V" and v.number > -1 and d.status & v.number = 2 and #spdevtab.name = d.name)
  427. begin
  428.   update #spdevtab set devtype = substring(v.name,charindex(" ",v.name)+ 1,4) from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  429.      where v.type = "V" and v.number > - 1 and d.status & v.number = 2 and #spdevtab.name = d.name
  430. end
  431.  
  432. /* Now get the default disk by turning the status bits into english. 0x01 is a default disk. */
  433.  
  434. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  435.        where v.type = "V" and v.number > -1 and d.status & v.number = 1 and #spdevtab.name = d.name)
  436. begin
  437.    update #spdevtab set devtype = "dflt" from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  438.       where v.type = "V" and v.number > -1 and d.status & v.number = 1 and #spdevtab.name = d.name
  439. end
  440.  
  441. /* Now get the logical disks by turning the status bits into english. 0x04 is a logical disk. */
  442.  
  443. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  444.        where v.type = "V" and v.number > -1 and d.status & v.number = 4 and #spdevtab.name = d.name)
  445. begin
  446.   update #spdevtab set devtype = v.name from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  447.      where v.type = "V" and v.number > -1 and d.status & v.number = 4 and #spdevtab.name = d.name
  448. end
  449.  
  450. /* Now get the devices that are mirrored. The mirror status bits are >= 32. */
  451.  
  452. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  453.        where v.type = "V" and v.number > -1 and d.status >= 32 and #spdevtab.name = d.name)
  454. begin
  455.    if exists (select * from master.dbo.sysdevices d, #spdevtab
  456.           where d.status & 512 = 512 and #spdevtab.name = d.name)
  457.    begin
  458.      update #spdevtab set mirror = "enabled" from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  459.         where d.status & 512 = 512 and #spdevtab.name = d.name
  460.    end
  461.  
  462.    if exists (select * from master.dbo.sysdevices d, #spdevtab
  463.           where d.status & 512 != 512 and d.status >= 32 and #spdevtab.name = d.name)
  464.    begin
  465.      update #spdevtab set mirror = "disabled" from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  466.         where d.status & 512 != 512 and d.status >= 32 and #spdevtab.name = d.name
  467.    end
  468. end
  469.  
  470. /* */
  471. /* Final select statement to output the information */
  472. /* */
  473.  
  474. set nocount off
  475.  
  476. select device_name = d.name, device_type = #spdevtab.cntrltype + #spdevtab.devtype,
  477.        device_size = #spdevtab.devsizeout, location = substring(d.phyname, 1, 46),
  478.        device_number = d.low / 16777216, freespace = #spdevtab.freespaceout, mirror_stat = #spdevtab.mirror
  479. from master.dbo.sysdevices d, #spdevtab
  480. where d.name = #spdevtab.name
  481. order by device_name, device_type
  482. go
  483.  
  484. /************* DUMP THE TRANSACTION LOG **************************************/
  485. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  486. /* script periodically, you will run out of transaction log space.           */
  487. dump tran master with truncate_only
  488. go
  489. /************* END DUMP THE TRANSACTION LOG **********************************/
  490.  
  491. /****************************************************************************/
  492. /* This stored procedure will get the next available device number          */
  493. /****************************************************************************/
  494.  
  495. print ""
  496. print "Creating sp_MSnext_devnumber"
  497. print ""
  498. go
  499. create procedure sp_MSnext_devnumber as
  500. declare @nextdev tinyint, @maxdev tinyint
  501. select @nextdev = 1
  502. select @maxdev= value from master..sysconfigures where config=116
  503.  
  504. while @nextdev != @maxdev /* This is one max device number allowed */
  505. begin
  506.   if exists(select name from sysdevices where (low / 16777216) = @nextdev)
  507.     select @nextdev = @nextdev + 1
  508.   else
  509.   begin
  510.     select @nextdev
  511.     return
  512.   end
  513. end
  514. raiserror 70000 "No available device numbers"
  515. go
  516.  
  517. /****************************************************************************/
  518. /* This stored procedure gets the mirror status of a particular device.  This */
  519. /* is only valid on the 4.2.1 server.  It is called by sp_MSdev_description */
  520. /****************************************************************************/
  521.  
  522.  
  523. print ""
  524. print "Creating sp_MSdev_mirror"
  525. print ""
  526. go
  527. create procedure sp_MSdev_mirror
  528. @devname varchar(30), @description varchar(255)  /* device and current description */
  529. as
  530.  
  531. /*  TCD 8/2/91 See if the device exists. */
  532.  
  533. if not exists (select * from master.dbo.sysdevices
  534.              where name = @devname)
  535. begin
  536.     raiserror 70001 "Device doesn't exist. The 'sa' must have dropped the device since you last displayed the listing."
  537.     return
  538. end
  539.  
  540.  
  541. /* */
  542. /* See if disk is mirrored.  Status bits >= 0x20 (32) */
  543. /* */
  544.  
  545. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  546.        where v.type = "V" and v.number > -1 and d.status >= 32 and d.name = @devname)
  547. begin
  548.  
  549. /*  Check to see if mirror in enabled. Status bits 0x200 (512). */
  550.  
  551.    if exists (select * from master.dbo.sysdevices d where d.status & 512 = 512 and d.name = @devname)
  552.    begin
  553.        select @description = @description + ", mirror enabled"
  554.    end
  555.  
  556. /* Check to see if mirror is disabled.    */
  557.  
  558.    if exists (select * from master.dbo.sysdevices d
  559.        where d.status & 512 != 512 and d.status >= 32 and d.name = @devname)
  560.    begin
  561.        select @description = @description + ", mirror disabled"
  562.    end
  563.  
  564. /* Get the name of the device it's mirrored on */
  565.  
  566.    select @description = @description + ", mirror on: " + (select d.mirrorname
  567.              from master.dbo.sysdevices d where d.name = @devname)
  568.  
  569. end
  570.  
  571. /*
  572. **  Check to see if there is a mirrorname entry but mirroring not enabled.
  573. **  If so, then one side of the mirror is off-line.
  574. */
  575.  
  576. else if exists (select * from master.dbo.sysdevices d
  577.         where d.name = @devname and d.mirrorname != null)
  578. begin
  579.  
  580.     /* Figure out which side of the mirror is disabled. */
  581.     /* If 0x100 is on, then phyname is disabled and mirrorname is enabled. */
  582.  
  583.   if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  584.          where v.type = "V" and v.number > -1 and d.status & v.number = 256
  585.          and d.name = @devname)
  586.   begin
  587.        select @description = @description + ", only device: " +
  588.                   (select d.mirrorname + "of mirror is enabled -- device: "
  589.                   + d.phyname + " is disabled"
  590.                   from master.dbo.sysdevices d, master.dbo.spt_values v
  591.                   where v.type = "V" and v.number > -1 and d.status & v.number = 256
  592.                   and d.name = @devname)
  593.   end
  594.   else if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  595.          where v.type = "V" and v.number > -1 and d.status & v.number = 64
  596.            and d.name = @devname)
  597.   begin
  598.        select @description = @description + ", only device: " +
  599.                  (select d.mirrorname + "of mirror is enabled -- device: "
  600.                   + d.phyname + " is disabled"
  601.                   from master.dbo.sysdevices d, master.dbo.sysdevices e, master.dbo.spt_values v
  602.                   where v.type = "V" and v.number > -1 and d.status & v.number = 64 and
  603.                   d.name = @devname and e.status & 256 != 256
  604.                   and e.name = @devname)
  605.   end
  606. end
  607.  
  608. /* Check for "serial writes" for mirrored disks. Status bits = 0x20 */
  609.  
  610. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  611.        where v.type = "V" and v.number > -1 and d.status & v.number = 32
  612.        and d.name = @devname)
  613. begin
  614.        select @description = @description + ", serial mirror writes"
  615. end
  616.  
  617. /* Check for "reads mirrored" for mirrored disks. Status bits = 0x80 */
  618.  
  619. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  620.         where v.type = "V" and v.number > -1 and d.status & v.number = 128
  621.        and d.name = @devname)
  622. begin
  623.        select @description = @description + ", reads mirrored"
  624. end
  625.  
  626. /* Output the final description */
  627.  
  628. set nocount off
  629. select @description
  630. go
  631.  
  632. /************* DUMP THE TRANSACTION LOG **************************************/
  633. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  634. /* script periodically, you will run out of transaction log space.           */
  635. dump tran master with truncate_only
  636. go
  637. /************* END DUMP THE TRANSACTION LOG **********************************/
  638.  
  639.  
  640. /****************************************************************************/
  641. /* This stored procedure will get the description of characteristics for
  642.    a particular device                                */
  643. /****************************************************************************/
  644.  
  645. print ""
  646. print "Creating sp_MSdev_description"
  647. print ""
  648. go
  649. create procedure sp_MSdev_description
  650. @devname varchar(30)          /* device to check out */
  651. as
  652.  
  653. /*  See if the device exists. */
  654.  
  655. if not exists (select * from master.dbo.sysdevices
  656.              where name = @devname)
  657. begin
  658.     raiserror 70001 "Device doesn't exist. The 'sa' must have dropped the device since you last displayed the listing."
  659.     return
  660. end
  661.  
  662. /* */
  663. /* Create a temporary table where we can build up a translation of the device status bits. */
  664. /* */
  665.  
  666. declare @description varchar(250)
  667.  
  668. set nocount on
  669.  
  670. /*
  671. **  Now figure out what kind of controller type it is.    The type are
  672. **  COMPLETELY platform dependent.
  673. **  cntrltype = 0    special
  674. **            2    disk
  675. **          3-4    floppy
  676. **          5        tape
  677. */
  678.  
  679. if exists (select * from master.dbo.sysdevices d
  680.        where d.cntrltype = 0 and d.name = @devname)
  681. begin
  682.        select @description = "database/log device"
  683. end
  684.  
  685. /* */
  686. /* See if disk is a default disk. Status of 0x01 is a default disk. */
  687. /* */
  688.  
  689. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  690.        where v.type = "V" and v.number > -1 and d.status & v.number = 1
  691.        and d.name = @devname)
  692. begin
  693.     select @description = @description + ", default disk"
  694. end
  695.  
  696. if exists (select * from master.dbo.sysdevices d
  697.         where d.cntrltype between 3 and 4 and d.name = @devname)
  698. begin
  699.        select @description = "floppy dump"
  700. end
  701.  
  702. if exists (select * from master.dbo.sysdevices d
  703.        where d.cntrltype = 5 and d.name = @devname and d.status & 8 = 0)
  704. begin
  705.        select @description = "tape dump-no skip label"
  706. end
  707.  
  708. if exists (select * from master.dbo.sysdevices d
  709.        where d.cntrltype = 5 and d.name = @devname and d.status & 8 = 8)
  710. begin
  711.        select @description = "tape dump-skip label"
  712. end
  713.  
  714. exec sp_MSdev_mirror @devname, @description
  715. go
  716.  
  717. /****************************************************************************/
  718. /* This stored procedure will get the name, size, and owner of all the databases
  719.    that use a particular device for data only, log only, data and log        */
  720. /****************************************************************************/
  721.  
  722. print ""
  723. print "Creating sp_MSdev_databases"
  724. print ""
  725. go
  726. create procedure sp_MSdev_databases
  727. @devname varchar(30)          /* device to check out */
  728. as
  729.  
  730.  
  731. /*  TCD 8/2/91 See if the device exists. */
  732.  
  733. if not exists (select * from master.dbo.sysdevices
  734.              where name = @devname)
  735. begin
  736.     raiserror 70001 "Device doesn't exist. The 'sa' must have dropped the device since you last displayed the listing."
  737.     return
  738. end
  739.  
  740. /* Databases that use the device for data only */
  741. declare @low int
  742. select @low = a.low from spt_values a where a.type = 'E' and a.number = 1
  743. select name = db_name(usg.dbid),
  744. size = ltrim(convert(varchar(10),round((sum(usg.size) * @low) / 1048576,1))) + " MB",
  745. usage = b.name, owner = suser_name(suid)
  746. from sysdatabases d, sysusages usg, sysdevices v, spt_values b
  747. where d.dbid = usg.dbid and v.low <= size + vstart
  748.       and v.high >= size + vstart - 1
  749.       and v.status & 2 = 2
  750.       and v.name = @devname
  751.       and b.type = "S"
  752.       and usg.segmap & 7 = b.number
  753. group by usg.dbid, suser_name(suid), b.name
  754. order by 1
  755. go
  756.  
  757. /************* DUMP THE TRANSACTION LOG **************************************/
  758. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  759. /* script periodically, you will run out of transaction log space.           */
  760. dump tran master with truncate_only
  761. go
  762. /************* END DUMP THE TRANSACTION LOG **********************************/
  763.  
  764. /************************************************************************/
  765. /* This section to create all the tables necessary for scheduled
  766.    backup and system monitoring                                         */
  767. /************************************************************************/
  768.  
  769. /* */
  770. /* Create the scheduled backup table */
  771. /* */
  772.  
  773.  
  774. print ""
  775. print "Creating MSscheduled_backups"
  776. print ""
  777. go
  778. create table MSscheduled_backups
  779. (
  780. Event_id        int        Not Null,    /* Unique identifier */
  781. Database_name        varchar(30)    Not Null,    /* Name of database to be dumped */
  782. Database_owner        varchar(30)    Not Null,    /* Name of the database owner */
  783. Database_dump        varchar(30)    Null,        /* Database dump device */
  784. Log_dump        varchar(30)    Null,        /* Log dump device */
  785. Day            tinyint     Not Null,    /* Day data is to be dumped */
  786. Frequency        tinyint     Not Null,    /* How often data is to be dumped */
  787. Start_time        char(5)     Not Null,    /* Time data should be dumped */
  788. Enabled         bit        Not Null,    /* 0 = disabled 1 = enabled */
  789. Last_dump        datetime    Null,        /* Last dump that took place */
  790. In_progress        bit        Not Null    /* 1 = backup in progress */
  791. )
  792. go
  793. create unique index sched_idx on MSscheduled_backups (Event_id)
  794. go
  795.  
  796. /* */
  797. /* Create the scheduled backup log table */
  798. /* */
  799.  
  800. print ""
  801. print "Creating MSscheduled_backups_log"
  802. print ""
  803. go
  804. create table MSscheduled_backups_log
  805. (
  806. Event_ID        int        Not Null,    /* Event ID from schedule table */
  807. Database_name        varchar(30)    Not Null,    /* Name of database to be dumped */
  808. Actual_start_time    datetime    Not Null,    /* Actual time the dump started */
  809. Actual_end_time     datetime    Null,        /* Actual time the dump completed */
  810. Status            bit        Not Null,    /* 0 = SUCCESS 1 = FAILED */
  811. Message         varchar(255)    Null        /* Error message upon failure. */
  812. )
  813. go
  814.  
  815. /* */
  816. /* Create the MSsystem_monitor table */
  817. /* */
  818.  
  819. print ""
  820. print "Creating MSsystem_monitor"
  821. print ""
  822. go
  823. create table MSsystem_monitor
  824. (
  825. Monitor_time        datetime    Not Null,    /* Time the monitor data was taken */
  826. CPU_busy_start        int        Not Null,    /* # secs CPU busy since server started */
  827. CPU_busy_last        int        Not Null,    /* # seconds CPU was busy since last monitor */
  828. CPU_busy_pct        tinyint     Not Null,    /* Percent CPU was busy since server started */
  829. IO_busy_start        int        Not Null,    /* # seconds IO was busy since server started */
  830. IO_busy_last        int        Not Null,    /* # seconds IO was busy since last monitor */
  831. IO_busy_pct        tinyint     Not Null,    /* Percent IO was busy since server started */
  832. Idle_start        int        Not Null,    /* # secs server idle since server started */
  833. Idle_last        int        Not Null,    /* # seconds server was idle since last monitor */
  834. Idle_pct        tinyint     Not Null,    /* Percent server was idle since server started */
  835. Pkts_sent_start     int        Not Null,    /* # packets sent since server started */
  836. Pkts_sent_last        int        Not Null,    /* # packets sent since last monitor */
  837. Pkts_received_start    int        Not Null,    /* # packets received since server started */
  838. Pkts_received_last    int        Not Null,    /* # packets received since last monitor */
  839. Pkt_errors_start    int        Not Null,    /* # packet errors since server started */
  840. Pkt_errors_last     int        Not Null,    /* # packet errors since last montor */
  841. Total_reads_start    int        Not Null,    /* # reads since server started */
  842. Total_reads_last    int        Not Null,    /* # reads since last monitor */
  843. Total_writes_start    int        Not Null,    /* # writes since server started */
  844. Total_writes_last    int        Not Null,    /* # write since last  monitor */
  845. Total_errors_start    int        Not Null,    /* # errors ince server started */
  846. Total_errors_last    int        Not Null,    /* # errors since last monitor */
  847. Connections_start    int        Not Null,    /* # connections since server started */
  848. Connections_last    int        Not Null,    /* # connections since last monitor */
  849. )
  850. go
  851.  
  852. /* */
  853. /* Create the MSlast_monitor table */
  854. /* */
  855.  
  856. print ""
  857. print "Creating MSlast_montior"
  858. print ""
  859. go
  860. create table MSlast_monitor
  861. (
  862. Last_time       datetime       Not Null,       /* Time of last monitor */
  863. CPU_busy       int           Not Null,       /* # seconds CPU was busy since last monitor */
  864. IO_busy        int           Not Null,       /* # seconds IO was busy since last monitor */
  865. Idle           int           Not Null,       /* # seconds server was idle since last monitor */
  866. Pkts_sent       int           Not Null,       /* # packets sent since last monitor */
  867. Pkts_received       int           Not Null,       /* # packets received since last monitor */
  868. Pkt_errors       int           Not Null,       /* # packet errors since last montor */
  869. Total_reads       int           Not Null,       /* # reads since last monitor */
  870. Total_writes       int           Not Null,       /* # write since last  monitor */
  871. Total_errors       int           Not Null,       /* # errors since last monitor */
  872. Connections       int           Not Null,       /* # connections since last monitor */
  873. )
  874. go
  875.  
  876. insert into MSlast_monitor
  877. select getdate(), @@cpu_busy, @@io_busy, @@idle, @@pack_sent, @@pack_received,
  878.        @@packet_errors, @@total_read, @@total_write, @@total_errors, @@connections
  879. go
  880.  
  881. /************* DUMP THE TRANSACTION LOG **************************************/
  882. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  883. /* script periodically, you will run out of transaction log space.           */
  884. dump tran master with truncate_only
  885. go
  886. /************* END DUMP THE TRANSACTION LOG **********************************/
  887.  
  888.  
  889.  
  890. /***************************************************************************/
  891. /* This stored procedure performs the monitoring of the system.  It is a
  892.    variation of the sp_monitor stored procedure on in the output and storage
  893.    of data                                   */
  894. /***************************************************************************/
  895.  
  896.  
  897. print ""
  898. print "Creating sp_MSsys_monitor"
  899. print ""
  900. go
  901. create procedure sp_MSsys_monitor as
  902.  
  903. /* */
  904. /*  Declare variables to be used to hold current monitor values. */
  905. /* */
  906.  
  907. declare @now         datetime
  908. declare @cpu_busy     int
  909. declare @io_busy    int
  910. declare @idle        int
  911. declare @pack_received    int
  912. declare @pack_sent    int
  913. declare @pack_errors    int
  914. declare @connections    int
  915. declare @total_read    int
  916. declare @total_write    int
  917. declare @total_errors    int
  918.  
  919. declare @oldcpu_busy     int    /* used to see if SQL Server has been rebooted */
  920. declare @interval    int
  921. declare @mspertick    int    /* milliseconds per tick */
  922.  
  923. /* */
  924. /*  Set @mspertick.  This is just used to make the numbers easier to handle
  925.     and avoid overflow. */
  926. /* */
  927.  
  928. select @mspertick = convert(int, @@timeticks / 1000.0)
  929.  
  930. /*  Get current monitor values. */
  931.  
  932. begin transaction
  933. select
  934.     @now = getdate(),
  935.     @cpu_busy = @@cpu_busy,
  936.     @io_busy = @@io_busy,
  937.     @idle = @@idle,
  938.     @pack_received = @@pack_received,
  939.     @pack_sent = @@pack_sent,
  940.     @connections = @@connections,
  941.     @pack_errors = @@packet_errors,
  942.     @total_read = @@total_read,
  943.     @total_write = @@total_write,
  944.     @total_errors = @@total_errors
  945.  
  946. /*
  947. **  Check to see if SQL Server has been rebooted.  If it has then the
  948. **  value of @@cpu_busy will be less than the value of spt_monitor.cpu_busy.
  949. **  If it has update spt_monitor.
  950. */
  951.  
  952. select @oldcpu_busy = CPU_busy from MSlast_monitor
  953. if @oldcpu_busy > @cpu_busy
  954. begin
  955.    update MSlast_monitor set
  956.       Last_time = @now,
  957.       CPU_busy = @cpu_busy,
  958.       IO_busy = @io_busy,
  959.       Idle = @idle,
  960.       Pkts_received = @pack_received,
  961.       Pkts_sent = @pack_sent,
  962.       Connections = @connections,
  963.       Pkt_errors = @pack_errors,
  964.       Total_reads = @total_read,
  965.       Total_writes = @total_write,
  966.       Total_errors = @total_errors
  967. end
  968.  
  969. /* */
  970. /*  Insert a row in the monitor table to reflect the current values since the
  971.     server was started. */
  972. /* */
  973.  
  974. set nocount on
  975.  
  976. insert into MSsystem_monitor
  977. select @now, (@cpu_busy * @mspertick) / 1000, 0, 0, (@io_busy * @mspertick) / 1000,
  978. 0, 0, (@idle * @mspertick) / 1000, 0,0, @pack_sent, 0, @pack_received, 0,
  979. @pack_errors,0, @total_read, 0, @total_write, 0, @total_errors, 0, @connections, 0
  980.  
  981. /* */
  982. /*  Now update the system monitor table with the difference between this monitor
  983.     and the last monitor taken */
  984. /* */
  985.  
  986. select @interval = datediff(ss, Last_time, @now)
  987. from MSlast_monitor
  988.  
  989. update MSsystem_monitor
  990.     set CPU_busy_last = ((@cpu_busy - CPU_busy) * @mspertick) / 1000,
  991.     CPU_busy_pct = ((((@cpu_busy - CPU_busy) * @mspertick) / 1000) * 100) / @interval,
  992.     IO_busy_last = ((@io_busy - IO_busy) * @mspertick) / 1000,
  993.     IO_busy_pct =  ((((@io_busy - IO_busy) * @mspertick) / 1000) * 100) / @interval,
  994.     Idle_last =  ((@idle - Idle) * @mspertick) / 1000,
  995.     Idle_pct = ((((@idle - Idle) * @mspertick) / 1000) * 100) / @interval,
  996.     Pkts_received_last = @pack_received - Pkts_received,
  997.     Pkts_sent_last = @pack_sent - Pkts_sent,
  998.     Pkt_errors_last = @pack_errors - Pkt_errors,
  999.     Total_reads_last = @total_read - Total_reads,
  1000.     Total_writes_last = @total_write - Total_writes,
  1001.     Total_errors_last = @total_errors - Total_errors,
  1002.     Connections_last = @connections - Connections
  1003. from MSlast_monitor
  1004. where Monitor_time = @now
  1005.  
  1006. /* */
  1007. /* Now update the MSlast_monitor table for next round */
  1008. /* */
  1009.  
  1010. update MSlast_monitor set
  1011.     Last_time = @now,
  1012.     CPU_busy = @cpu_busy,
  1013.     IO_busy = @io_busy,
  1014.     Idle = @idle,
  1015.     Pkts_received = @pack_received,
  1016.     Pkts_sent = @pack_sent,
  1017.     Connections = @connections,
  1018.     Pkt_errors = @pack_errors,
  1019.     Total_reads = @total_read,
  1020.     Total_writes = @total_write,
  1021.     Total_errors = @total_errors
  1022.  
  1023. set nocount off
  1024.  
  1025.  
  1026. commit transaction
  1027.  
  1028. return
  1029. go
  1030.  
  1031.  
  1032. /************* DUMP THE TRANSACTION LOG **************************************/
  1033. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1034. /* script periodically, you will run out of transaction log space.           */
  1035. dump tran master with truncate_only
  1036. go
  1037. /************* END DUMP THE TRANSACTION LOG **********************************/
  1038.  
  1039.  
  1040. /****************************************************************************/
  1041. /* This stored procedure will get the name, size, owner, space available,
  1042.    and create date of all the databases in the system */
  1043. /****************************************************************************/
  1044.  
  1045. print ""
  1046. print "Creating sp_MSdatabase_list"
  1047. print ""
  1048. go
  1049. create procedure sp_MSdatabase_list
  1050. as
  1051.  
  1052. create table #spdbdesc        /* Create temporary table for storage. */
  1053. (
  1054.  dbid     smallint null
  1055. )
  1056.  
  1057. set nocount on
  1058.  
  1059. insert into #spdbdesc (dbid)
  1060.        select dbid from sysdatabases
  1061.  
  1062. set nocount off
  1063.  
  1064. select distinct name = sysdatabases.name,
  1065.         db_size = substring(convert(varchar(10),
  1066.             (spt_values.low * sum(sysusages.size)) / 1048576)
  1067.             + "MB", 1, 8),
  1068.         owner = syslogins.name,
  1069.                 spaceavail = "         ",
  1070.         created = convert(char(11), sysdatabases.crdate)
  1071. from sysdatabases, syslogins, sysusages, spt_values, #spdbdesc
  1072. where sysdatabases.dbid = #spdbdesc.dbid
  1073.       and sysdatabases.suid = syslogins.suid
  1074.       and #spdbdesc.dbid = sysusages.dbid
  1075.       and spt_values.type = "E"
  1076.       and spt_values.number = 1
  1077. group by #spdbdesc.dbid
  1078. having sysdatabases.dbid = #spdbdesc.dbid
  1079.        and sysdatabases.suid = syslogins.suid
  1080.        and #spdbdesc.dbid = sysusages.dbid
  1081.        and spt_values.type = "E"
  1082.        and spt_values.number = 1
  1083. order by sysdatabases.name
  1084. go
  1085.  
  1086. /****************************************************************************/
  1087. /* This stored procedure will get the space available for all the databases
  1088.    in the system */
  1089. /****************************************************************************/
  1090. print ""
  1091. print "Creating sp_MSdatabase_avail"
  1092. print ""
  1093. go
  1094. create procedure sp_MSdatabase_avail
  1095. as
  1096.  
  1097. declare @db_size int, @reserved int, @unused int, @low int
  1098.  
  1099. set nocount on
  1100.  
  1101. select @db_size =  ((sum(size) * d.low) / 1048576) * 1024
  1102.            from master.dbo.sysusages, master.dbo.spt_values d
  1103.            where dbid = db_id() and d.number = 1 and d.type = "E"
  1104.            having dbid = db_id() and d.number = 1 and d.type = "E"
  1105.  
  1106. /* */
  1107. /* reserved: sum(reserved) where indid in (0, 1, 255) */
  1108. /* */
  1109.  
  1110. select @low = d.low from master.dbo.spt_values d where d.number = 1 and d.type = "E"
  1111.  
  1112. select @reserved = sum(reserved) from sysindexes where indid in (0, 1, 255)
  1113. select @reserved = (@reserved * @low)/1024
  1114.  
  1115. /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  1116.  
  1117. select @unused = sum(reserved) - sum(used) from sysindexes where indid in (0, 1, 255)
  1118. select @unused = (@unused * @low)/1024
  1119.  
  1120. set nocount off
  1121.  
  1122. select space_avail = convert(varchar(10),@db_size - (@reserved - @unused)) + "KB"
  1123. go
  1124.  
  1125. /****************************************************************************/
  1126. /* This stored procedure will get all the databases that need to be dumped
  1127.    at the time of inquiry */
  1128. /****************************************************************************/
  1129.  
  1130.  
  1131. print ""
  1132. print "Creating sp_MSbackup_now"
  1133. print ""
  1134. go
  1135. create procedure sp_MSbackup_now as
  1136. declare @now datetime, @dayofweek tinyint, @hour tinyint, @minute tinyint,
  1137. @elapsed int, @monthyear varchar(30)
  1138.  
  1139. set nocount on
  1140.  
  1141. /* */
  1142. /* Get the current date and time. */
  1143. /* Parse out the relevant parts of the date */
  1144. /* */
  1145.  
  1146. select @now = getdate()
  1147. select @dayofweek = datepart(weekday,@now)
  1148. select @monthyear = substring(convert(varchar(12),getdate()),1,12)
  1149.  
  1150. /* */
  1151. /* Create a temporary table that holds data on what needs to be dumped */
  1152. /* */
  1153.  
  1154. create table #spdumptab
  1155. (
  1156. id           int           Not Null,       /* Unique identifier */
  1157. name           varchar(30)     Not Null,       /* Name of database to be dumped */
  1158. owner           varchar(30)     Not Null,       /* Name of the database owner */
  1159. ddump           varchar(30)     Null,           /* Database dump device */
  1160. ldump           varchar(30)     Null,           /* Log dump device */
  1161. datacntrltype  smallint        Null,           /* Control type */
  1162. logcntrltype   smallint        Null            /* Control type */
  1163. )
  1164.  
  1165. /* */
  1166. /* Check all the databases that are dumped daily, weekly, and biweekly */
  1167. /* Note: The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1168. /* */
  1169.  
  1170. insert into #spdumptab
  1171. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL
  1172. from MSscheduled_backups
  1173. where Enabled = 1                         /* Dump turned on */
  1174.       and (Day = @dayofweek or Frequency = 1)             /* Dump today or Daily */
  1175.       and Frequency <= 14                     /* Freq daily, weekly, or biweekly */
  1176.       and datediff(day, Last_dump, @now) >= Frequency         /* Freq time has elapsed */
  1177.       and @now >= convert(datetime, @monthyear + Start_time)
  1178.       and datediff(hour, Last_dump, @now) >= Frequency*24    /* Freq time has elapsed */
  1179.       and datediff(minute, Last_dump, @now) >= Frequency*24*60    /* Freq time has elapsed */
  1180.  
  1181. /* */
  1182. /* Check all the databases that are dumped monthly */
  1183. /* Note: First we get this week number, then do the same criteria as the
  1184.      Daily, weekly, bi-weekly dump.
  1185.      The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1186. /* */
  1187.  
  1188. declare @rundate datetime, @weekno tinyint     /* Get this week number */
  1189. select @rundate = @now
  1190. select @weekno = 1
  1191. while datepart(month,dateadd(day,-7,@rundate)) = datepart(month,@now)
  1192. begin
  1193.     select @weekno = @weekno + 1
  1194.     select @rundate = dateadd(day,-7,@rundate)
  1195. end
  1196.  
  1197. insert into #spdumptab
  1198. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL
  1199. from MSscheduled_backups
  1200. where Enabled = 1                         /* Dump turned on */
  1201.       and (Day = @dayofweek)                     /* Dump today */
  1202.       and Frequency >= 31                     /* Freq monthly */
  1203.       and Frequency - 30 = @weekno                 /* Week of month */
  1204.       and datediff(day, Last_dump, @now) >= 30             /* Freq time has elapsed */
  1205.       and @now >= convert(datetime, @monthyear + Start_time)
  1206.       and datediff(hour, Last_dump, @now) >= Frequency*24    /* Freq time has elapsed */
  1207.       and datediff(minute, Last_dump, @now) >= Frequency*24*60    /* Freq time has elapsed */
  1208.  
  1209.  
  1210. update #spdumptab set datacntrltype = (select cntrltype from master..sysdevices s where
  1211. #spdumptab.ddump = s.name)
  1212.  
  1213. update #spdumptab set logcntrltype = (select cntrltype from master..sysdevices s where
  1214. #spdumptab.ldump = s.name)
  1215.  
  1216. set nocount off
  1217.  
  1218. /* */
  1219. /* Output the values to the daemon */
  1220. /* */
  1221.  
  1222. select id = id, name = name, owner = owner, ddump = ddump, ldump = ldump,
  1223. dcntrl = datacntrltype, lcntrl = logcntrltype from #spdumptab
  1224. go
  1225.  
  1226. /************* DUMP THE TRANSACTION LOG **************************************/
  1227. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1228. /* script periodically, you will run out of transaction log space.           */
  1229. dump tran master with truncate_only
  1230. go
  1231. /************* END DUMP THE TRANSACTION LOG **********************************/
  1232.  
  1233. /*
  1234. ** This stored procedure returns the current scheduling for backups, it will
  1235. ** get the database name, day (in chars), frequency (in chars), start time,
  1236. ** and enabled status (in chars) from MSscheduled_backups.
  1237. */
  1238.  
  1239.  
  1240. print ""
  1241. print "Creating sp_MSbackup_schedule"
  1242. print ""
  1243. go
  1244. create procedure sp_MSbackup_schedule as
  1245.  
  1246. /* Create a tempory table to hold the results */
  1247.  
  1248. create table #spbacksch
  1249. (id int,
  1250.  dataname char (30) not null,
  1251.  day char (9) null,
  1252.  frequency char (9) null,
  1253.  start_time char(10) not null,
  1254.  enabled char (8) null )
  1255.  
  1256. set nocount on
  1257.  
  1258. /* Fill the temporary table with the id, database name, and start time in the schedule table */
  1259.  
  1260. insert into #spbacksch select d.Event_id,d.Database_name,NULL,NULL,convert(char,d.Start_time),NULL from MSscheduled_backups d
  1261.  
  1262. /* Now set the day of week to it's character equivalent from a numeric value */
  1263.  
  1264. update #spbacksch set day = "*********" from MSscheduled_backups d
  1265.                 where d.Day = 0 and d.Event_id = id
  1266. update #spbacksch set day = "Sunday" from MSscheduled_backups d
  1267.         where d.Day = 1 and d.Event_id = id
  1268. update #spbacksch set day = "Monday" from MSscheduled_backups d
  1269.         where d.Day = 2 and d.Event_id = id
  1270. update #spbacksch set day = "Tuesday" from MSscheduled_backups d
  1271.         where d.Day = 3 and d.Event_id = id
  1272. update #spbacksch set day = "Wednesday" from MSscheduled_backups d
  1273.         where d.Day = 4 and d.Event_id = id
  1274. update #spbacksch set day = "Thursday" from MSscheduled_backups d
  1275.         where d.Day = 5 and d.Event_id = id
  1276. update #spbacksch set day = "Friday" from MSscheduled_backups d
  1277.         where d.Day = 6 and d.Event_id = id
  1278. update #spbacksch set day = "Saturday" from MSscheduled_backups d
  1279.         where d.Day = 7 and d.Event_id = id
  1280.  
  1281. /* Now set the frequency to it's character equivalent from a numeric value */
  1282.  
  1283. update #spbacksch set frequency = "Daily" from MSscheduled_backups d
  1284.                 where d.Frequency = 1 and d.Event_id = id
  1285. update #spbacksch set frequency = "Weekly" from MSscheduled_backups d
  1286.                 where d.Frequency = 7 and d.Event_id = id
  1287. update #spbacksch set frequency = "Bi-Weekly" from MSscheduled_backups d
  1288.                 where d.Frequency = 14 and d.Event_id = id
  1289. update #spbacksch set frequency = "Monthly - Week 1" from MSscheduled_backups d
  1290.                 where d.Frequency = 31 and d.Event_id = id
  1291. update #spbacksch set frequency = "Monthly - Week 2" from MSscheduled_backups d
  1292.                 where d.Frequency = 32 and d.Event_id = id
  1293. update #spbacksch set frequency = "Monthly - Week 3" from MSscheduled_backups d
  1294.                 where d.Frequency = 33 and d.Event_id = id
  1295. update #spbacksch set frequency = "Monthly - Week 4" from MSscheduled_backups d
  1296.                 where d.Frequency = 34 and d.Event_id = id
  1297.  
  1298. /* Now set the enabled status to it's character equivalent from a numeric value */
  1299.  
  1300. update #spbacksch set enabled = "No" from MSscheduled_backups d
  1301.                 where d.Enabled = 0 and d.Event_id = id
  1302. update #spbacksch set enabled = "Yes" from MSscheduled_backups d
  1303.                 where d.Enabled = 1 and d.Event_id = id
  1304.  
  1305. set nocount off
  1306.  
  1307. /* select the data from the temporary table to give the schedule in a human understandable form */
  1308.  
  1309. select dataname, day, frequency, start_time, enabled, convert(char,id) from #spbacksch
  1310.                order by dataname,id
  1311.  
  1312. go
  1313.  
  1314. /************* DUMP THE TRANSACTION LOG **************************************/
  1315. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1316. /* script periodically, you will run out of transaction log space.           */
  1317. dump tran master with truncate_only
  1318. go
  1319. /************* END DUMP THE TRANSACTION LOG **********************************/
  1320.  
  1321.  
  1322.  
  1323. /****************************************************************************/
  1324. /* This stored proc will get the login id, user name, alias, and group for
  1325.     all the users in a database for the MDI list box            */
  1326. /****************************************************************************/
  1327. print ""    /* print this or script hangs with some servers */
  1328.  
  1329. print ""
  1330. print "Creating sp_MSuser_list"
  1331. print ""
  1332. go
  1333. create procedure sp_MSuser_list
  1334. as
  1335.  
  1336. create table #spusers       /* Create temporary table for storage. */
  1337. (
  1338.  login_id varchar(30) null,
  1339.  user_name varchar(30) null,
  1340.  alias_name varchar(30) null,
  1341.  group_name varchar(30) null
  1342. )
  1343.  
  1344. set nocount on
  1345.  
  1346. /* */
  1347. /* Fill the temp table with system and user names */
  1348. /* */
  1349.  
  1350. insert into #spusers
  1351. select suser_name(suid), user_name(uid), "", "" from sysusers
  1352. where uid < 16383 and uid > 0
  1353.  
  1354. /* */
  1355. /* Get the aliases first */
  1356. /* */
  1357.  
  1358. insert into #spusers
  1359. select suser_name(sysalternates.suid),"", sysusers.name, ""
  1360. from sysusers, sysalternates, #spusers
  1361. where sysalternates.altsuid = suser_id(login_id)
  1362. and sysusers.suid = suser_id(login_id)
  1363.  
  1364. /* */
  1365. /* Get the group names */
  1366. /* */
  1367.  
  1368. update #spusers set group_name = g.name from sysusers u, sysusers g, #spusers
  1369. where u.suid = suser_id(#spusers.login_id) and
  1370.       u.uid = user_id(#spusers.user_name) and
  1371.       u.gid *= g.uid and
  1372.       u.uid <= 16383 and u.uid > 0
  1373.  
  1374. set nocount off
  1375.  
  1376. select * from #spusers
  1377. go
  1378.  
  1379. /****************************************************************************/
  1380. /* This stored procedure will get the needed values for the statistics
  1381. ** monitoring dialog.  This also updates the system monitor in the same
  1382. ** fashion as sp_monitor, thereby mimicing what sp_monitor does.
  1383. */
  1384. /****************************************************************************/
  1385.  
  1386. print ""
  1387. print "Creating sp_MSmonitor"
  1388. print ""
  1389. go
  1390. create procedure sp_MSmonitor
  1391. as
  1392.  
  1393. set nocount on
  1394.  
  1395. /*
  1396. **  Declare variables to be used to hold current monitor values.
  1397. */
  1398. declare @now         datetime
  1399. declare @cpu_busy     int
  1400. declare @io_busy    int
  1401. declare @idle        int
  1402. declare @pack_received    int
  1403. declare @pack_sent    int
  1404. declare @pack_errors    int
  1405. declare @connections    int
  1406. declare @total_read    int
  1407. declare @total_write    int
  1408. declare @total_errors    int
  1409.  
  1410. declare @oldcpu_busy     int    /* used to see if SQL Server has been rebooted */
  1411. declare @interval    int
  1412. declare @mspertick    int    /* milliseconds per tick */
  1413.  
  1414. /*
  1415. **  Set @mspertick.  This is just used to make the numbers easier to handle
  1416. **  and avoid overflow.
  1417. */
  1418. select @mspertick = convert(int, @@timeticks / 1000.0)
  1419.  
  1420. /*
  1421. **  Get current monitor values.
  1422. */
  1423. begin transaction
  1424. select
  1425.     @now = getdate(),
  1426.     @cpu_busy = @@cpu_busy,
  1427.     @io_busy = @@io_busy,
  1428.     @idle = @@idle,
  1429.     @pack_received = @@pack_received,
  1430.     @pack_sent = @@pack_sent,
  1431.     @connections = @@connections,
  1432.     @pack_errors = @@packet_errors,
  1433.     @total_read = @@total_read,
  1434.     @total_write = @@total_write,
  1435.     @total_errors = @@total_errors
  1436.  
  1437. /*
  1438. **  Check to see if SQL Server has been rebooted.  If it has then the
  1439. **  value of @@cpu_busy will be less than the value of master..spt_monitor.cpu_busy.
  1440. **  If it has update master..spt_monitor.
  1441. */
  1442. select @oldcpu_busy = cpu_busy
  1443.     from master..spt_monitor
  1444. if @oldcpu_busy > @cpu_busy
  1445. begin
  1446.     update master..spt_monitor
  1447.         set
  1448.             lastrun = @now,
  1449.             cpu_busy = @cpu_busy,
  1450.             io_busy = @io_busy,
  1451.             idle = @idle,
  1452.             pack_received = @pack_received,
  1453.             pack_sent = @pack_sent,
  1454.             connections = @connections,
  1455.             pack_errors = @pack_errors,
  1456.             total_read = @total_read,
  1457.             total_write = @total_write,
  1458.             total_errors = @total_errors
  1459. end
  1460.  
  1461. /*
  1462. **  Now print out old and new monitor values.
  1463. */
  1464. select @interval = datediff(ss, lastrun, @now) from master..spt_monitor s
  1465.  
  1466. select last_run = convert(char(30),lastrun),seconds = convert(char(30),@interval)
  1467.     from master..spt_monitor s
  1468.  
  1469. select
  1470.     CpuBusy = convert(varchar(30),"CPU Busy") + ","
  1471.                 + convert(varchar(18),((@cpu_busy * @mspertick) / 1000)) + ","
  1472.         + convert(varchar(18),((@cpu_busy - cpu_busy)* @mspertick) / 1000) + ","
  1473.         + convert(varchar(18), ((((@cpu_busy - cpu_busy)* @mspertick) / 1000) * 100) / @interval),
  1474.     IOBusy = convert(varchar(30),"IO Busy") + ","
  1475.                 + convert(varchar(18),((@io_busy * @mspertick) / 1000)) + ","
  1476.         + convert(varchar(18),(((@io_busy - io_busy)* @mspertick) / 1000)) + ","
  1477.         + convert(varchar(18), ((((@io_busy - io_busy)* @mspertick) / 1000) * 100) / @interval),
  1478.     Idle =   convert(varchar(30),"Idle Time") + ","
  1479.                 + convert(varchar(18),((@idle * @mspertick) / 1000)) + ","
  1480.         + convert(varchar(18),(((@idle - idle)* @mspertick) / 1000)) + ","
  1481.         + convert(varchar(18),((((@idle - idle)* @mspertick) / 1000) * 100) / @interval),
  1482.     PacketsReceived = convert(varchar(30),"Packets Received") + ","
  1483.                 + convert(varchar(18), @pack_received) + ","
  1484.         + convert(varchar(18), @pack_received - pack_received),
  1485.         PacketsSent = convert(varchar(30),"Packets Sent") + ","
  1486.         + convert(varchar(18), @pack_sent) + ","
  1487.                 + convert(varchar(18), @pack_sent - pack_sent),
  1488.     PacketErrors = convert(varchar(30),"Packet Errors") + ","
  1489.                 + convert(varchar(18), @pack_errors) + ","
  1490.                 + convert(varchar(18), @pack_errors - pack_errors),
  1491.     TotalRead = convert(varchar(30),"Total Read") + ","
  1492.                 + convert(varchar(18), @total_read) + ","
  1493.         + convert(varchar(18), @total_read - total_read),
  1494.         TotalWrite = convert(varchar(30),"Total Write") + ","
  1495.                 + convert(varchar(18), @total_write) + ","
  1496.         + convert(varchar(18), @total_write - total_write),
  1497.     TotalErrors = convert(varchar(30),"Total Errors") + ","
  1498.                 + convert(varchar(18), @total_errors) + ","
  1499.         + convert(varchar(18), @total_errors - total_errors),
  1500.     Connections = convert(varchar(30),"Connections") + ","
  1501.                 + convert(varchar(18), @connections) + ","
  1502.         + convert(varchar(18), @connections - connections)
  1503.        from master..spt_monitor
  1504.  
  1505. /*
  1506. **  Now update master..spt_monitor
  1507. */
  1508. update master..spt_monitor
  1509.     set
  1510.         lastrun = @now,
  1511.         cpu_busy = @cpu_busy,
  1512.         io_busy = @io_busy,
  1513.         idle = @idle,
  1514.         pack_received = @pack_received,
  1515.         pack_sent = @pack_sent,
  1516.         connections = @connections,
  1517.         pack_errors = @pack_errors,
  1518.         total_read = @total_read,
  1519.         total_write = @total_write,
  1520.         total_errors = @total_errors
  1521.  
  1522. commit transaction
  1523. set nocount off
  1524. return
  1525. go
  1526.  
  1527. /************* DUMP THE TRANSACTION LOG **************************************/
  1528. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1529. /* script periodically, you will run out of transaction log space.           */
  1530. dump tran master with truncate_only
  1531. go
  1532. /************* END DUMP THE TRANSACTION LOG **********************************/
  1533.  
  1534.  
  1535. /*****************************************************************************/
  1536. /* This stored procedure gets all the users that have particular permissions */
  1537. /*****************************************************************************/
  1538.  
  1539. print ""
  1540. print "Creating sp_MScmd_permissions"
  1541. print ""
  1542. go
  1543. create procedure sp_MScmd_permissions
  1544. @cre_dflt bit, @cre_proc bit, @cre_rule bit, @cre_table bit, @cre_db bit,
  1545. @cre_view bit, @dmp_db bit, @dmp_tran bit
  1546. as
  1547.  
  1548. set nocount on
  1549.  
  1550. create table #tmp_permiss
  1551. (name     varchar(30),
  1552.  dflt     bit,
  1553.  creproc bit,
  1554.  crerule bit,
  1555.  cretble bit,
  1556.  credb     bit,
  1557.  creview bit,
  1558.  dmpdb     bit,
  1559.  dmptran bit
  1560. )
  1561.  
  1562. insert into #tmp_permiss
  1563. select user_name(uid),0,0,0,0,0,0,0,0 from sysusers
  1564.  
  1565. update #tmp_permiss set dflt = 1 from sysprotects where
  1566. name = user_name(uid) and
  1567. protecttype = (select number from master..spt_values where name = 'Grant')
  1568. and action = (select number from master..spt_values where name = 'Create Default')
  1569.  
  1570. update #tmp_permiss set creproc = 1 from sysprotects where
  1571. name = user_name(uid) and
  1572. protecttype = (select number from master..spt_values where name = 'Grant')
  1573. and action = (select number from master..spt_values where name = 'Create Procedure')
  1574.  
  1575. update #tmp_permiss set crerule = 1 from sysprotects where
  1576. name = user_name(uid) and
  1577. protecttype = (select number from master..spt_values where name = 'Grant')
  1578. and action = (select number from master..spt_values where name = 'Create Rule')
  1579.  
  1580. update #tmp_permiss set cretble = 1 from sysprotects where
  1581. name = user_name(uid) and
  1582. protecttype = (select number from master..spt_values where name = 'Grant')
  1583. and action = (select number from master..spt_values where name = 'Create Table')
  1584.  
  1585. update #tmp_permiss set credb = 1 from sysprotects where
  1586. name = user_name(uid) and
  1587. protecttype = (select number from master..spt_values where name = 'Grant')
  1588. and action = (select number from master..spt_values where name = 'Create Database')
  1589.  
  1590. update #tmp_permiss set creview = 1 from sysprotects where
  1591. name = user_name(uid) and
  1592. protecttype = (select number from master..spt_values where name = 'Grant')
  1593. and action = (select number from master..spt_values where name = 'Create View')
  1594.  
  1595. update #tmp_permiss set dmpdb = 1 from sysprotects where
  1596. name = user_name(uid) and
  1597. protecttype = (select number from master..spt_values where name = 'Grant')
  1598. and action = (select number from master..spt_values where name = 'Dump Database')
  1599.  
  1600. update #tmp_permiss set dmptran = 1 from sysprotects where
  1601. name = user_name(uid) and
  1602. protecttype = (select number from master..spt_values where name = 'Grant')
  1603. and action = (select number from master..spt_values where name = 'Dump Transaction')
  1604.  
  1605. set nocount off
  1606.  
  1607. select name from #tmp_permiss
  1608. where dflt = @cre_dflt and creproc = @cre_proc and crerule = @cre_rule
  1609. and cretble = @cre_table and credb = @cre_db and creview = @cre_view
  1610. and dmpdb = @dmp_db and dmptran = @dmp_tran
  1611. go
  1612.  
  1613. /*****************************************************************************/
  1614. /* This stored procedure gets all the users that have particular permissions */
  1615. /*****************************************************************************/
  1616.  
  1617. print ""
  1618. print "Creating sp_MSuser_cmd_permissions"
  1619. print ""
  1620. go
  1621. create procedure sp_MSuser_cmd_permissions @username varchar(30)
  1622. as
  1623.  
  1624. /*  TCD 8/2/91 See if the user exists. */
  1625.  
  1626. if not exists (select * from dbo.sysusers
  1627.              where name = @username)
  1628. begin
  1629.     raiserror 70003 "User doesn't exist. The 'sa' or 'dbo' must have dropped the user since you last displayed the listing."
  1630.     return
  1631. end
  1632.  
  1633. set nocount on
  1634.  
  1635. create table #tmp_permiss
  1636. (dflt     bit,
  1637.  creproc bit,
  1638.  crerule bit,
  1639.  cretble bit,
  1640.  credb     bit,
  1641.  creview bit,
  1642.  dmpdb     bit,
  1643.  dmptran bit
  1644. )
  1645.  
  1646. if user_id(@username) = 1     /* dbo has all permissions */
  1647.   insert into #tmp_permiss
  1648.   values (1,1,1,1,1,1,1,1)
  1649. else
  1650. begin
  1651.   insert into #tmp_permiss
  1652.   values (0,0,0,0,0,0,0,0)
  1653.  
  1654.   update #tmp_permiss set dflt = 1 from sysprotects where
  1655.   user_name(uid) = @username and
  1656.   protecttype = (select number from master..spt_values where name = 'Grant')
  1657.   and action = (select number from master..spt_values where name = 'Create Default')
  1658.  
  1659.   update #tmp_permiss set creproc = 1 from sysprotects where
  1660.   user_name(uid) = @username and
  1661.   protecttype = (select number from master..spt_values where name = 'Grant')
  1662.   and action = (select number from master..spt_values where name = 'Create Procedure')
  1663.  
  1664.   update #tmp_permiss set crerule = 1 from sysprotects where
  1665.   user_name(uid) = @username and
  1666.   protecttype = (select number from master..spt_values where name = 'Grant')
  1667.   and action = (select number from master..spt_values where name = 'Create Rule')
  1668.  
  1669.   update #tmp_permiss set cretble = 1 from sysprotects where
  1670.   user_name(uid) = @username and
  1671.   protecttype = (select number from master..spt_values where name = 'Grant')
  1672.   and action = (select number from master..spt_values where name = 'Create Table')
  1673.  
  1674.   update #tmp_permiss set credb = 1 from sysprotects where
  1675.   user_name(uid) = @username and
  1676.   protecttype = (select number from master..spt_values where name = 'Grant')
  1677.   and action = (select number from master..spt_values where name = 'Create Database')
  1678.  
  1679.   update #tmp_permiss set creview = 1 from sysprotects where
  1680.   user_name(uid) = @username and
  1681.   protecttype = (select number from master..spt_values where name = 'Grant')
  1682.   and action = (select number from master..spt_values where name = 'Create View')
  1683.  
  1684.   update #tmp_permiss set dmpdb = 1 from sysprotects where
  1685.   user_name(uid) = @username and
  1686.   protecttype = (select number from master..spt_values where name = 'Grant')
  1687.   and action = (select number from master..spt_values where name = 'Dump Database')
  1688.  
  1689.   update #tmp_permiss set dmptran = 1 from sysprotects where
  1690.   user_name(uid) = @username and
  1691.   protecttype = (select number from master..spt_values where name = 'Grant')
  1692.   and action = (select number from master..spt_values where name = 'Dump Transaction')
  1693. end
  1694.  
  1695. set nocount off
  1696.  
  1697. select * from #tmp_permiss
  1698. go
  1699.  
  1700.  
  1701. /************* DUMP THE TRANSACTION LOG **************************************/
  1702. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1703. /* script periodically, you will run out of transaction log space.           */
  1704. dump tran master with truncate_only
  1705. go
  1706. /************* END DUMP THE TRANSACTION LOG **********************************/
  1707.  
  1708. /****************************************************************************/
  1709. /* This stored procedure will get the name, device, size, and location of
  1710.    each segment for a particular database. You must be in the db to exec    */
  1711. /****************************************************************************/
  1712.  
  1713. print ""
  1714. print "Creating sp_MSsegment_list"
  1715. print ""
  1716. go
  1717. create procedure sp_MSsegment_list as
  1718.  
  1719. declare @segbit int
  1720.  
  1721. /* Create a temporary table where we can store all the necessary information */
  1722.  
  1723. set nocount on
  1724.  
  1725. create table #spsegtab1
  1726. (name varchar(30) null,
  1727.  segnumber smallint,
  1728.  segbit int null
  1729. )
  1730.  
  1731. create table #spsegtab2
  1732. (
  1733.  name varchar(30) null,
  1734.  device varchar(30) null,
  1735.  size varchar(10) null,
  1736. )
  1737.  
  1738. /* Get all the segments in the system and their status */
  1739. /* Set the segments on @devname sysusages. */
  1740. /*    If segment 31, power(2, 31) will overflow
  1741.       since segmap is an int.  We'll grab the machine-dependent
  1742.       bit mask from spt_values to set the right bit. */
  1743.  
  1744. insert into #spsegtab1 (name, segnumber) select name, segment from syssegments
  1745. update #spsegtab1 set segbit = power(2, segnumber) where segnumber < 31
  1746. update #spsegtab1 set segbit = low from master.dbo.spt_values
  1747.           where type = "E" and number = 2
  1748.             and segnumber >= 31
  1749.  
  1750.  
  1751. /* Populate the #spsegtab2 with the devices for each segment */
  1752.  
  1753.  
  1754. insert into #spsegtab2
  1755. select distinct #spsegtab1.name, d.name, convert(varchar(20), round((u.size * convert(float, v.low)) / 1048576, 0)) + "MB"
  1756. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v, #spsegtab1
  1757.         where u.segmap & #spsegtab1.segbit = #spsegtab1.segbit
  1758.             and d.low <= u.size + u.vstart
  1759.             and d.high >= u.size + u.vstart - 1
  1760.             and u.dbid = db_id()
  1761.             and d.status & 2 = 2
  1762.             and v.number = 1
  1763.             and v.type = "E"
  1764.  
  1765.  
  1766. set nocount off
  1767.  
  1768. select * from #spsegtab2
  1769. order by name
  1770. go
  1771. /****************************************************************************/
  1772. /* This stored procedure will return a list of the devices that contain
  1773.    a segment of a given name.                            */
  1774. /****************************************************************************/
  1775.  
  1776. print ""
  1777. print "Creating sp_MSsegment_devices"
  1778. print ""
  1779. go
  1780. create procedure sp_MSsegment_devices @segname varchar(30)        /* segment name */
  1781. as
  1782. declare    @segbit    int        /* this is the bit version of the segment # */
  1783. declare    @segment    int    /* the segment number of the segment */
  1784. set nocount on
  1785. /*
  1786. **  Set the bit position for the segment.
  1787. */
  1788. select @segment = segment
  1789.     from syssegments
  1790.         where name = @segname
  1791.  
  1792. /*
  1793. **  Now set the segments on @devname sysusages.
  1794. */
  1795. if (@segment < 31)
  1796.     select @segbit = power(2, @segment)
  1797. else
  1798.     /*
  1799.     **  Since this is segment 31, power(2, 31) will overflow
  1800.     **  since segmap is an int.  We'll grab the machine-dependent
  1801.     **  bit mask from spt_values to set the right bit.
  1802.     */
  1803.     select @segbit = low
  1804.         from master.dbo.spt_values
  1805.             where type = "E"
  1806.                 and number = 2
  1807.  
  1808. select distinct device = d.name
  1809.     from master.dbo.sysusages u, master.dbo.sysdevices d,
  1810.         master.dbo.spt_values v
  1811.         where u.segmap & @segbit = @segbit
  1812.             and d.low <= u.size + u.vstart
  1813.             and d.high >= u.size + u.vstart - 1
  1814.             and u.dbid = db_id()
  1815.             and d.status & 2 = 2
  1816.             and v.number = 1
  1817.             and v.type = "E"
  1818. set nocount off
  1819. go
  1820.  
  1821. /****************************************************************************/
  1822. /* This stored procedure will get the properties of a particular segment in
  1823.    in the system                                */
  1824. /****************************************************************************/
  1825.  
  1826. print ""
  1827. print "Creating sp_MSseg_properties"
  1828. print ""
  1829. go
  1830. create procedure sp_MSseg_properties @segname varchar(30) as
  1831.  
  1832. declare @segbit int, @segsize varchar(10), @segment int, @segdevs varchar(255)
  1833.  
  1834. /*  TCD 8/2/91 See if the segment exists. */
  1835.  
  1836. if not exists (select * from syssegments
  1837.              where name = @segname)
  1838. begin
  1839.     raiserror 70005 "Segment doesn't exist. The 'sa' or 'dbo' must have dropped the segment since you last displayed the listing."
  1840.     return
  1841. end
  1842.  
  1843. set nocount on
  1844.  
  1845. /* Set the bit position for the segment */
  1846.  
  1847. select @segment = segment from syssegments where name = @segname
  1848. if (@segment < 31)
  1849.     select @segbit = power(2, @segment)
  1850. else
  1851.  
  1852.     /*  Since this is segment 31, power(2, 31) will overflow
  1853.     **  since segmap is an int.  We'll grab the machine-dependent
  1854.     **  bit mask from spt_values to set the right bit. */
  1855.  
  1856.      select @segbit = low from master.dbo.spt_values where type = "E" and number = 2
  1857.  
  1858. /* Get the total size of the segment */
  1859.  
  1860. select @segsize = convert(varchar(20), sum(round((u.size * convert(float, v.low)) / 1048576, 0))) + "MB"
  1861. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v
  1862. where u.segmap & @segbit = @segbit
  1863.       and d.low <= u.size + u.vstart
  1864.       and d.high >= u.size + u.vstart - 1
  1865.       and u.dbid = db_id()
  1866.       and d.status & 2 = 2
  1867.       and v.number = 1
  1868.       and v.type = "E"
  1869.  
  1870. set nocount off
  1871.  
  1872. /* output the segment number and the segment size */
  1873.  
  1874. select segment_number = @segment, segment_size = @segsize
  1875.  
  1876. /* output the device names */
  1877.  
  1878. select distinct d.name
  1879. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v
  1880. where u.segmap & @segbit = @segbit
  1881.       and d.low <= u.size + u.vstart
  1882.       and d.high >= u.size + u.vstart - 1
  1883.       and u.dbid = db_id()
  1884.       and d.status & 2 = 2
  1885.       and v.number = 1
  1886.       and v.type = "E"
  1887.  
  1888. go
  1889.  
  1890. /****************************************************************************/
  1891. /* This stored procedure will get the tables/indexes for a particular
  1892.    segment in the system                            */
  1893. /****************************************************************************/
  1894.  
  1895. print ""
  1896. print "Creating sp_MSseg_tables"
  1897. print ""
  1898. go
  1899. create procedure sp_MSseg_tables @segname varchar(30) as
  1900. select table_name = object_name(i.id), index_name = i.name, i.indid
  1901. from sysindexes i, syssegments s
  1902. where s.name = @segname and s.segment = i.segment
  1903. order by table_name, indid
  1904. go
  1905. /************* DUMP THE TRANSACTION LOG **************************************/
  1906. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1907. /* script periodically, you will run out of transaction log space.           */
  1908. dump tran master with truncate_only
  1909. go
  1910. /************* END DUMP THE TRANSACTION LOG **********************************/
  1911.  
  1912. /*****************************************************************************/
  1913. /* This stored procedure gets all the options set for a particular database  */
  1914. /* It applies to the current database.                         */
  1915. /* It is generally called by sp_MSdb_properties                  */
  1916. /*****************************************************************************/
  1917.  
  1918.  
  1919. print ""
  1920. print "Creating sp_MSdb_options_bits"
  1921. print ""
  1922. go
  1923. create procedure sp_MSdb_options_bits as
  1924. declare @bitdesc varchar(255), @curdbid int, @allopts int
  1925. select @curdbid = db_id()
  1926. select @allopts = 7196
  1927.  
  1928. set nocount on
  1929.  
  1930. create table #spdbbits
  1931. (allopts bit,
  1932.  dbuse bit,
  1933.  chkpt bit,
  1934.  readonly bit,
  1935.  bulkload bit,
  1936.  single bit,
  1937.  trunc bit)
  1938.  
  1939. insert into #spdbbits values(0,0,0,0,0,0,0)
  1940.  
  1941. /*  Check all settable options (7196) */
  1942.  
  1943. select @bitdesc = null
  1944. select @bitdesc = v.name from spt_values v, sysdatabases d
  1945.        where d.dbid = @curdbid and v.type = "D"
  1946.        and d.status & v.number = 7196 and v.number = @allopts        /* all options */
  1947.  
  1948. if @bitdesc != null
  1949. begin
  1950.   update #spdbbits set allopts = 1
  1951. end
  1952.  
  1953. /* Check select into/bulk copy bit (4)    */
  1954.  
  1955. select @bitdesc = null
  1956. select @bitdesc = v.name from spt_values v, sysdatabases d
  1957.        where d.dbid = @curdbid and v.type = "D"
  1958.        and d.status & v.number = 4 and v.number != @allopts      /* all options */
  1959.  
  1960. if @bitdesc != null
  1961. begin
  1962.    update #spdbbits set bulkload = 1
  1963. end
  1964.  
  1965. /*  Check no checkpoint on recovery bit (16)  */
  1966.  
  1967. select @bitdesc = null
  1968. select @bitdesc = v.name from spt_values v, sysdatabases d
  1969.        where d.dbid = @curdbid and v.type = "D"
  1970.        and d.status & v.number = 16 and v.number != @allopts /* all options */
  1971.  
  1972. if @bitdesc != null
  1973. begin
  1974.   update #spdbbits set chkpt = 1
  1975. end
  1976.  
  1977. /* Check single user bit (4096) */
  1978.  
  1979. select @bitdesc = null
  1980. select @bitdesc = v.name from spt_values v, sysdatabases d
  1981.        where d.dbid = @curdbid and v.type = "D"
  1982.        and d.status & v.number = 4096 and v.number != @allopts         /* all options */
  1983.  
  1984. if @bitdesc != null
  1985. begin
  1986.   update #spdbbits set single = 1
  1987. end
  1988.  
  1989. /*  Check dbo only bit (2048) */
  1990.  
  1991. select @bitdesc = null
  1992. select @bitdesc = v.name from spt_values v, sysdatabases d
  1993.        where d.dbid = @curdbid and v.type = "D"
  1994.        and d.status & v.number = 2048 and v.number != @allopts         /* all options */
  1995.  
  1996. if @bitdesc != null
  1997. begin
  1998.   update #spdbbits set dbuse = 1
  1999. end
  2000.  
  2001. /*  Check read only bit (1024) */
  2002.  
  2003. select @bitdesc = null
  2004. select @bitdesc = v.name from spt_values v, sysdatabases d
  2005.        where d.dbid = @curdbid and v.type = "D"
  2006.        and d.status & v.number = 1024 and v.number != @allopts         /* all options */
  2007.  
  2008. if @bitdesc != null
  2009. begin
  2010.   update #spdbbits set readonly = 1
  2011. end
  2012.  
  2013. /*  Check truncate log on checkpoint bit (8) */
  2014.  
  2015. select @bitdesc = null
  2016. select @bitdesc = v.name from spt_values v, sysdatabases d
  2017.        where d.dbid = @curdbid and v.type = "D"
  2018.        and d.status & v.number = 8 and v.number != @allopts      /* all options */
  2019.  
  2020. if @bitdesc != null
  2021. begin
  2022.   update #spdbbits set trunc = 1
  2023. end
  2024.  
  2025. set nocount off
  2026.  
  2027. select * from #spdbbits
  2028. go
  2029.  
  2030.  
  2031. /*****************************************************************************/
  2032. /* This procedure gets the devices that a database resides on for segment use */
  2033. /*****************************************************************************/
  2034.  
  2035. print ""
  2036. print "Creating sp_MSdb_devices_segments"
  2037. print ""
  2038. go
  2039. create procedure sp_MSdb_devices_segments @dbname varchar(30) as
  2040.  
  2041. /*  TCD 8/2/91 See if the database exists. */
  2042.  
  2043. if not exists (select * from sysdatabases
  2044.              where name = @dbname)
  2045. begin
  2046.     raiserror 70006 "Database doesn't exist. The 'sa' or 'dbo' must have dropped the database since you last displayed the listing."
  2047.     return
  2048. end
  2049.  
  2050. select distinct device = sysdevices.name
  2051. from sysdatabases, sysusages, sysdevices, spt_values a, spt_values b
  2052. where sysdatabases.dbid = sysusages.dbid
  2053.         and sysdevices.low <= size + vstart
  2054.         and sysdevices.high >= size + vstart - 1
  2055.         and sysdevices.status & 2 = 2
  2056.         and sysdatabases.name = @dbname
  2057.         and a.type = "E" and a.number = 1 and b.type = "S"
  2058.         and sysusages.segmap & 7 = b.number
  2059.         order by 1
  2060. go
  2061.  
  2062. /************* DUMP THE TRANSACTION LOG **************************************/
  2063. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2064. /* script periodically, you will run out of transaction log space.           */
  2065. dump tran master with truncate_only
  2066. go
  2067. /************* END DUMP THE TRANSACTION LOG **********************************/
  2068.  
  2069. /*****************************************************************************/
  2070. /* This procedure gets the space used by a particular database.          */
  2071. /* You must be in the database to access this information             */
  2072. /*****************************************************************************/
  2073.  
  2074.  
  2075. print ""
  2076. print "Creating sp_MSdb_space"
  2077. print ""
  2078. go
  2079. create procedure sp_MSdb_space as
  2080. declare @options varchar(255)
  2081.  
  2082. declare @pages    int               /* working variable for size calc. */
  2083. declare @dbsize int               /* Size of the database */
  2084.  
  2085. /* Create a temp table to do the calculation. */
  2086. /*  reserved: sum(reserved) where indid in (0, 1, 255)
  2087.     data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text)
  2088.     index: sum(used) where indid in (0, 1, 255) - data
  2089.     unused: sum(reserved) - sum(used) where indid in (0, 1, 255)
  2090. */
  2091.  
  2092. set nocount on
  2093.  
  2094. create table #spt_space
  2095. (
  2096.  db_size     int null,
  2097.  rows         int null,
  2098.  reserved     int null,
  2099.  data         int null,
  2100.  indexp      int null,
  2101.  unused      int null,
  2102.  log_size     int null,
  2103.  log_used     int null
  2104. )
  2105.  
  2106. /* Get the total size of the database */
  2107.  
  2108. insert into #spt_space (db_size)
  2109. select distinct (sum(size) * d.low) / 1048576
  2110. from master.dbo.sysusages, master.dbo.spt_values d
  2111. where dbid = db_id() and d.number = 1 and d.type = "E"
  2112. having dbid = db_id() and d.number = 1 and d.type = "E"
  2113.  
  2114. /* */
  2115. /*  Now calculate the summary data.
  2116.     reserved: sum(reserved) where indid in (0, 1, 255) */
  2117. /* */
  2118.  
  2119. update #spt_space
  2120. set reserved = (select sum(sysindexes.reserved) from sysindexes where indid in (0, 1, 255))
  2121.  
  2122. /* */
  2123. /* data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text) */
  2124. /* */
  2125.  
  2126. select @pages = sum(dpages) from sysindexes
  2127. where indid < 2
  2128.  
  2129. select @pages = @pages + isnull(sum(used), 0) from sysindexes
  2130. where indid = 255
  2131.  
  2132. update #spt_space set data = @pages
  2133.  
  2134.     
  2135. /* index: sum(used) where indid in (0, 1, 255) - data */
  2136.  
  2137. update #spt_space
  2138. set indexp = (select sum(used) from sysindexes where indid in (0, 1, 255)) - data
  2139.  
  2140. /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  2141.  
  2142. update #spt_space
  2143. set unused = reserved - (select sum(used) from sysindexes where indid in (0, 1, 255))
  2144.  
  2145. /* log_size: sum(reserved) where indid in (0, 1, 255) and object is 'syslogs' */
  2146.  
  2147. update #spt_space
  2148. set log_size = (select sum(sysindexes.reserved) from sysindexes where indid in (0, 1, 255) and id = object_id('syslogs'))
  2149.  
  2150. /* log_used: sum(used) where indid in (0, 1, 255) and id = object_id('syslogs') */
  2151.  
  2152. update #spt_space set log_used = (select sum(used)
  2153. from sysindexes
  2154. where indid in (0, 1, 255) and id = object_id('syslogs'))
  2155.  
  2156. set nocount off
  2157.  
  2158. select reserved = substring(convert(varchar(11), (reserved * d.low ) / 1024) + " KB", 1, 15),
  2159.        data = substring(convert(varchar(11),(data * d.low) / 1024) + " KB", 1, 15),
  2160.        log_space = substring (convert(varchar(11), (round((log_size * convert(float, d.low)) / 1024, 0))) + " KB", 1, 15),
  2161.        index_size = substring(convert(varchar(11), (indexp * d.low) / 1024) + " KB", 1, 15),
  2162.        unused_reserve = substring(convert(varchar(11),(unused * d.low) / 1024) + " KB", 1, 15),
  2163.        unused_log = substring (convert(varchar(11), (round(((log_size - log_used) * convert(float, d.low)) / 1024, 0))) + " KB", 1, 15)
  2164.        from #spt_space, master.dbo.spt_values d
  2165.        where d.number = 1 and d.type = "E"
  2166. go
  2167.  
  2168. /*****************************************************************************/
  2169. /* This stored procedure gets all the options set for a particular database  */
  2170. /* It applies to the current database.                         */
  2171. /* It is generally called by sp_MSdb_properties                  */
  2172. /*****************************************************************************/
  2173.  
  2174.  
  2175. print ""
  2176. print "Creating sp_MSdb_options"
  2177. print ""
  2178. go
  2179. create procedure sp_MSdb_options as
  2180. declare @options varchar(255), @bitdesc varchar(255), @curdbid int, @allopts int
  2181. select @curdbid = db_id()
  2182. select @allopts = 7196
  2183.  
  2184. set nocount on
  2185.  
  2186. select @options = ""
  2187.  
  2188. /* Check select into/bulk copy bit (4)    */
  2189.  
  2190. select @bitdesc = null
  2191. select @bitdesc = v.name from spt_values v, sysdatabases d
  2192.        where d.dbid = @curdbid and v.type = "D"
  2193.        and d.status & v.number = 4 and v.number != @allopts      /* all options */
  2194.  
  2195. if @bitdesc != null
  2196. begin
  2197.    if @options != ""
  2198.      select @options = @options + ", " +  @bitdesc
  2199.    else select @options = @bitdesc
  2200. end
  2201.  
  2202. /*  Check no checkpoint on recovery bit (16)  */
  2203.  
  2204. select @bitdesc = null
  2205. select @bitdesc = v.name from spt_values v, sysdatabases d
  2206.        where d.dbid = @curdbid and v.type = "D"
  2207.        and d.status & v.number = 16 and v.number != @allopts /* all options */
  2208.  
  2209. if @bitdesc != null
  2210. begin
  2211.   if @options != ""
  2212.      select @options = @options + ", " +  @bitdesc
  2213.   else select @options = @bitdesc
  2214. end
  2215.  
  2216. /* Check single user bit (4096) */
  2217.  
  2218. select @bitdesc = null
  2219. select @bitdesc = v.name from spt_values v, sysdatabases d
  2220.        where d.dbid = @curdbid and v.type = "D"
  2221.        and d.status & v.number = 4096 and v.number != @allopts         /* all options */
  2222.  
  2223. if @bitdesc != null
  2224. begin
  2225.   if @options != ""
  2226.      select @options = @options + ", " +  @bitdesc
  2227.   else select @options = @bitdesc
  2228. end
  2229.  
  2230. /*  Check dbo only bit (2048) */
  2231.  
  2232. select @bitdesc = null
  2233. select @bitdesc = v.name from spt_values v, sysdatabases d
  2234.        where d.dbid = @curdbid and v.type = "D"
  2235.        and d.status & v.number = 2048 and v.number != @allopts         /* all options */
  2236.  
  2237. if @bitdesc != null
  2238. begin
  2239.   if @options != ""
  2240.      select @options = @options + ", " +  @bitdesc
  2241.   else select @options = @bitdesc
  2242. end
  2243.  
  2244. /*  Check read only bit (1024) */
  2245.  
  2246. select @bitdesc = null
  2247. select @bitdesc = v.name from spt_values v, sysdatabases d
  2248.        where d.dbid = @curdbid and v.type = "D"
  2249.        and d.status & v.number = 1024 and v.number != @allopts         /* all options */
  2250.  
  2251. if @bitdesc != null
  2252. begin
  2253.   if @options != ""
  2254.      select @options = @options + ", " +  @bitdesc
  2255.   else select @options = @bitdesc
  2256. end
  2257.  
  2258. /*  Check load only bit (512) */
  2259.  
  2260. select @bitdesc = null
  2261. select @bitdesc = v.name from spt_values v, sysdatabases d
  2262.        where d.dbid = @curdbid and v.type = "D"
  2263.        and d.status & v.number = 512 and v.number != @allopts        /* all options */
  2264.  
  2265. if @bitdesc != null
  2266. begin
  2267.   if @options != ""
  2268.      select @options = @options + ", " +  @bitdesc
  2269.   else select @options = @bitdesc
  2270. end
  2271.  
  2272. /* Check not recovered only bit (256)  */
  2273.  
  2274. select @bitdesc = null
  2275. select @bitdesc = v.name from spt_values v, sysdatabases d
  2276.        where d.dbid = @curdbid and v.type = "D"
  2277.        and d.status & v.number = 256 and v.number != @allopts  /* all options */
  2278.  
  2279. if @bitdesc != null
  2280. begin
  2281.   if @options != ""
  2282.      select @options = @options + ", " +  @bitdesc
  2283.   else select @options = @bitdesc
  2284. end
  2285.  
  2286. /* Check don't recover bit (32)  */
  2287.  
  2288. select @bitdesc = null
  2289. select @bitdesc = v.name from spt_values v, sysdatabases d
  2290.        where d.dbid = @curdbid and v.type = "D"
  2291.        and d.status & v.number = 32 and v.number != @allopts       /* all options */
  2292.  
  2293. if @bitdesc != null
  2294. begin
  2295.   if @options != ""
  2296.      select @options = @options + ", " +  @bitdesc
  2297.   else select @options = @bitdesc
  2298. end
  2299.  
  2300. /*  Check truncate log on checkpoint bit (8) */
  2301.  
  2302. select @bitdesc = null
  2303. select @bitdesc = v.name from spt_values v, sysdatabases d
  2304.        where d.dbid = @curdbid and v.type = "D"
  2305.        and d.status & v.number = 8 and v.number != @allopts      /* all options */
  2306.  
  2307. if @bitdesc != null
  2308. begin
  2309.   if @options != ""
  2310.      select @options = @options + ", " +  @bitdesc
  2311.   else select @options = @bitdesc
  2312. end
  2313.  
  2314. /* If no flags are set, say so. */
  2315.  
  2316. if @options = ""
  2317. begin
  2318.   select @options = "no options set"
  2319. end
  2320.  
  2321. set nocount off
  2322.  
  2323. select @options
  2324. go
  2325.  
  2326. /************* DUMP THE TRANSACTION LOG **************************************/
  2327. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2328. /* script periodically, you will run out of transaction log space.           */
  2329. dump tran master with truncate_only
  2330. go
  2331. /************* END DUMP THE TRANSACTION LOG **********************************/
  2332.  
  2333. /*****************************************************************************/
  2334. /* This procedure gets the devices that a database resides on.             */
  2335. /*****************************************************************************/
  2336.  
  2337. print ""
  2338. print "Creating sp_MSdb_devices"
  2339. print ""
  2340. go
  2341. create procedure sp_MSdb_devices @dbname varchar(30) as
  2342.  
  2343. select device = sysdevices.name,
  2344.        size = convert(varchar(10), round((spt_values.low * convert(float, size))/ 1048576, 0)) + " " + "MB",
  2345.        usage = b.name
  2346. from sysdatabases, sysusages, sysdevices, spt_values a, spt_values b
  2347. where sysdatabases.dbid = sysusages.dbid
  2348.       and sysdevices.low <= size + vstart
  2349.       and sysdevices.high >= size + vstart - 1
  2350.       and sysdevices.status & 2 = 2
  2351.       and sysdatabases.name = @dbname
  2352.       and a.type = "E" and a.number = 1 and b.type = "S"
  2353.       and sysusages.segmap & 7 = b.number
  2354.       order by 1
  2355. go
  2356.  
  2357. /*****************************************************************************/
  2358. /* These routines will get all the necessary data for the Database Properties */
  2359. /* dialog box.    You must be in the database to access this information         */
  2360. /*****************************************************************************/
  2361.  
  2362. print ""
  2363. print "Creating sp_MSdb_properties"
  2364. print ""
  2365. go
  2366. create procedure sp_MSdb_properties as
  2367. declare @dbname varchar(255), @users int, @groups int
  2368.  
  2369. /* Get number of users and number of groups */
  2370.  
  2371. set nocount on
  2372. select @users = count(*) from sysusers where uid < 16383 and uid > 0
  2373. select @groups = count(*) from sysusers where uid >= 16383 or uid = 0
  2374. set nocount off
  2375.  
  2376. select Number_users = @users, Number_groups = @groups
  2377.  
  2378. /* Get the object counts and db options */
  2379.  
  2380. exec sp_MSdb_options
  2381.  
  2382. /* Get the space used by the database */
  2383.  
  2384. exec sp_MSdb_space
  2385. go
  2386.  
  2387. /*****************************************************************/
  2388. /* This stored procedure will calculate thread usage             */
  2389. /****************************************************************/
  2390.  
  2391.  
  2392. print ""
  2393. print "Creating sp_MSthread_list"
  2394. print ""
  2395. go
  2396. create procedure sp_MSthread_list as
  2397.  
  2398. set nocount on
  2399.  
  2400. declare @devs int, @servs int, @total int, @avail_threads int,
  2401. @avail_servs int, @mirrors int, @outline varchar(50)
  2402.  
  2403. select @devs = count(*) from sysdevices where cntrltype = 0
  2404. select @mirrors = count(*) from sysdevices where status & 512 = 512
  2405. select @servs = 2 * count(*) from sysservers where srvid != 0
  2406. select @total = 4 + @devs + @mirrors+ @servs
  2407. select @avail_threads = 53-4-@devs-@mirrors-@servs
  2408. if @avail_threads % 2 = 1
  2409.     select @avail_servs = (@avail_threads - 1)/2
  2410. else
  2411.     select @avail_servs = @avail_threads/2
  2412.  
  2413.  
  2414. set nocount off
  2415. select '4 SQL Server Threads'
  2416. select @outline = convert(varchar(5), @devs+@mirrors)+ ' Database Device(s) (including mirrors)'
  2417. select @outline
  2418. select @outline = convert(varchar(5), @servs/2)+ ' Remote Server(s)'
  2419. select @outline
  2420. select @outline = convert(varchar(5),  @total) + ' Threads Currently Used'
  2421. select @outline
  2422. select @outline = 'You May Create Up To ' + convert(varchar(5), (53 - @total)) + ' More Devices'
  2423. select @outline
  2424. select @outline = 'Or You May Create Up To ' + convert(varchar(5), @avail_servs) + ' Remote Servers'
  2425. select @outline
  2426. go
  2427.  
  2428.  
  2429. /***************************************************************************/
  2430. /* This section grants execute permission on the stored procedure and
  2431.    select permissions on all the tables                    */
  2432. /***************************************************************************/
  2433.  
  2434. /* */
  2435. /* Grant permission on the procedures */
  2436. /* */
  2437.  
  2438. print ""
  2439. print "Granting execute permissions on procedures"
  2440. print ""
  2441. go
  2442. grant execute on sp_MScheck_admin to public
  2443. grant execute on sp_MSdevice_list to public
  2444. grant execute on sp_MSdev_description to public
  2445. grant execute on sp_MSdev_databases to public
  2446. grant execute on sp_MSsys_monitor to public
  2447. grant execute on sp_MSnext_devnumber to public
  2448. grant execute on sp_MSdatabase_list to public
  2449. grant execute on sp_MSdatabase_avail to public
  2450. grant execute on sp_MSbackup_now to public
  2451. grant execute on sp_MSuser_list to public
  2452. grant execute on sp_MSmonitor to public
  2453. grant execute on sp_MSdb_devices to public
  2454. grant execute on sp_MSdb_options to public
  2455. grant execute on sp_MSdb_properties to public
  2456. grant execute on sp_MSdb_space to public
  2457. grant execute on sp_MSbackup_schedule to public
  2458. grant execute on sp_MScmd_permissions to public
  2459. grant execute on sp_MSuser_cmd_permissions to public
  2460. grant execute on sp_MSdb_options_bits to public
  2461.  
  2462. /* */
  2463. /* these are 4.2 only */
  2464. /* */
  2465.  
  2466. grant execute on sp_MSdev_mirror to public
  2467. grant execute on sp_MSdb_devices_segments to public
  2468. grant execute on sp_MSsegment_list to public
  2469. grant execute on sp_MSsegment_devices to public
  2470. grant execute on sp_MSseg_properties to public
  2471. grant execute on sp_MSseg_tables to public
  2472. grant execute on sp_MSthread_list to public
  2473. go
  2474.  
  2475. /* */
  2476. /* Grant permissions on tables */
  2477. /* */
  2478.  
  2479. print ""
  2480. print "Creating permissions on tables"
  2481. print ""
  2482. go
  2483. grant select, update, delete, insert on MSscheduled_backups to dbo
  2484. grant select, update, delete, insert on MSscheduled_backups_log to dbo
  2485. go
  2486.  
  2487.  
  2488. /* logdevice    27.1    3/5/90 */
  2489. /* rv, 2/4/92, bug 1265 */
  2490.  
  2491. sp_configure "allow updates",1
  2492. go
  2493. reconfigure with override
  2494. go
  2495.  
  2496. create procedure sp_MSlogdevice
  2497. @dbname varchar(30),            /* database name that has the syslogs */
  2498. @devname        varchar(30),    /* device name to put syslogs on */
  2499. @fragflag       varchar(3)=' '  /* if ='*', change all fragments */
  2500. as
  2501.  
  2502. declare @dbid   smallint        /* dbid of the database to be changed */
  2503. declare @dbuid  smallint        /* id of the owner of the database */
  2504. declare @logbit int             /* this is the bit to turn on in sysusages */
  2505. declare @fragcnt smallint       /* count of allocation fragments */
  2506. declare @fragstart int          /* lowest lstart of fragments to change */
  2507.  
  2508. select @logbit = 4              /* bit 3 is the one to turn on */
  2509. select @fragstart = 0           /* lowest lstart of fragments to change */
  2510.  
  2511. /*
  2512. **  Verify the database name and get the @dbid and @dbuid
  2513. */
  2514. select @dbid = dbid, @dbuid = suid
  2515.         from sysdatabases
  2516.                 where name = @dbname
  2517.  
  2518. /*
  2519. **  If @dbname not found, say so and list the databases.
  2520. */
  2521. if @dbid = NULL
  2522. begin
  2523.         print "No such database -- run sp_helpdb to list databases."
  2524.         return (1)
  2525. end
  2526.  
  2527. /*
  2528. **  See if the device exists.
  2529. */
  2530. if not exists (select *
  2531.                 from master.dbo.sysdevices
  2532.                         where name like @devname)
  2533. begin
  2534.         print "No such device exists -- run sp_helpdevice to list the SQLServer devices."
  2535.         return (1)
  2536. end
  2537.  
  2538. /*
  2539. **  You must be SA or the dbo to execute this sproc.
  2540. */
  2541. if suser_id() != 1 and suser_id() != @dbuid
  2542. begin
  2543.         print "Only the System Administrator (SA) or the Database Owner (dbo) may move the syslogs table."
  2544.         return (1)
  2545. end
  2546.  
  2547. /*
  2548. **  Now see if the @dbname uses the @devname
  2549. */
  2550. if not exists (select *
  2551.                 from sysusages u, sysdevices d
  2552.                         where d.name = @devname
  2553.                                 and u.vstart between d.low and d.high
  2554.                                 and u.dbid = @dbid)
  2555. begin
  2556.         print "The specified device is not used by the database."
  2557.         return (1)
  2558. end
  2559.  
  2560. /*
  2561. **  Count the allocation fragments on the specified device, excluding
  2562. **  the one containing the system tables (lstart=0).
  2563. */
  2564. select @fragcnt=count(*)
  2565.         from master.dbo.sysusages u, master.dbo.sysdevices d
  2566.                 where d.name = @devname
  2567.                         and u.vstart between d.low and d.high
  2568.                         and u.dbid = @dbid
  2569.                         and lstart != 0
  2570. /*
  2571. **  If there are none, it cannot be changed to log-only status
  2572. */
  2573. if @fragcnt = 0
  2574. begin
  2575.      print "Can't make the only segment on original device log-only"
  2576.      return (1)
  2577. end
  2578.  
  2579. /*
  2580. **  If more than one allocation fragment and '*' option not specified,
  2581. **  change only the one with the highest lstart.
  2582. */
  2583. if @fragcnt > 1 and @fragflag!='*'
  2584. begin
  2585.      print "Warning, there are multiple allocation fragments on this device."
  2586.      print "Only the last fragment will be changed to log-only status.  If you"
  2587.      print "wish to change them all, run sp_MSlogdevice database,device,'*' "
  2588.  
  2589.      select @fragstart=max(lstart)
  2590.         from master.dbo.sysusages u, master.dbo.sysdevices d
  2591.                 where d.name = @devname
  2592.                         and u.vstart between d.low and d.high
  2593.                         and u.dbid = @dbid
  2594. end
  2595.  
  2596.  
  2597. /*
  2598. **  clear the bit from any database segments that aren't already log only
  2599. */
  2600. update master.dbo.sysusages
  2601.         set segmap = segmap & ~@logbit
  2602.                 from master.dbo.sysusages
  2603.                         where dbid = @dbid
  2604.                         and segmap != @logbit
  2605. /*
  2606. **  Now set the segments on @devname as log-only.
  2607. */
  2608. update master.dbo.sysusages
  2609.         set segmap = @logbit
  2610.                 from master.dbo.sysusages u, master.dbo.sysdevices d
  2611.                         where d.name = @devname
  2612.                                 and u.vstart between d.low and d.high
  2613.                                 and u.dbid = @dbid
  2614.                                 and lstart != 0
  2615.                                 and lstart>=@fragstart
  2616.  
  2617. /*
  2618. **  Now we need to activate the new segment map.
  2619. */
  2620. dbcc dbrepair (@dbname, remap)
  2621. print "Syslogs moved."
  2622.  
  2623. return (0)
  2624. go
  2625. sp_configure "allow updates",0
  2626. go
  2627. reconfigure with override
  2628. go
  2629.  
  2630.  
  2631. grant execute on sp_MSlogdevice to public
  2632. go
  2633. /************* DUMP THE TRANSACTION LOG **************************************/
  2634. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2635. /* script periodically, you will run out of transaction log space.           */
  2636. dump tran master with truncate_only
  2637. go
  2638. /************* END DUMP THE TRANSACTION LOG **********************************/
  2639. checkpoint
  2640. go
  2641. 
  2642. 
  2643.