home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / ppc / admin60.sql < prev    next >
Encoding:
Text File  |  1995-09-29  |  124.2 KB  |  3,721 lines

  1. /***************************************************************************/
  2. /*   ADMIN2.SQL - SQL Administrator INSTALL SCRIPT             */
  3. /*                              FOR SQL SERVER VERSION 4.2x          */
  4. /***************************************************************************/
  5. /* latest revision - 3/16/93                                               */
  6. /* Copyright (C) 1993 - Microsoft Corporation                              */
  7. /***************************************************************************/
  8. print ''
  9. print 'Install SQL Administrator Scripts (SQL Server version 4.2x)'
  10. print ''
  11. go
  12. /************* DUMP THE TRANSACTION LOG **************************************/
  13. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  14. /* script periodically, you will run out of transaction log space.           */
  15. dump tran master with truncate_only
  16. go
  17. /************* END DUMP THE TRANSACTION LOG **********************************/
  18.  
  19. use master
  20. go
  21.  
  22. /*******************************************************************************/
  23. /*        DROP ALL EXISTING PROCEDURE AND TABLES FIRST               */
  24. /*******************************************************************************/
  25. if exists (select * from sysobjects where name = 'sp_MScheck_admin' and sysstat & 7 = 4)
  26. begin
  27.   drop procedure sp_MScheck_admin
  28. end
  29. if exists (select * from sysobjects where name = 'sp_MSdevice_list' and sysstat & 7 = 4)
  30. begin
  31.   drop procedure sp_MSdevice_list
  32. end
  33. if exists (select * from sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  34. begin
  35.   drop procedure sp_MSnext_devnumber
  36. end
  37. if exists (select * from sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  38. begin
  39.   drop procedure sp_MSnext_devnumber
  40. end
  41. if exists (select * from sysobjects where name = 'sp_MSdev_mirror' and sysstat & 7 = 4)
  42. begin
  43.   drop procedure sp_MSdev_mirror
  44. end
  45. if exists (select * from sysobjects where name = 'sp_MSdev_description' and sysstat & 7 = 4)
  46. begin
  47.     drop procedure sp_MSdev_description
  48. end
  49. if exists (select * from sysobjects where name = 'sp_MSdev_databases' and sysstat & 7 = 4)
  50. begin
  51.   drop procedure sp_MSdev_databases
  52. end
  53. if exists (select * from sysobjects where name = 'MSsystem_monitor')
  54. begin
  55.     drop table MSsystem_monitor
  56. end
  57. if exists (select * from sysobjects where name = 'MSlast_monitor')
  58. begin
  59.     drop table MSlast_monitor
  60. end
  61. if exists (select * from sysobjects where name = 'sp_MSsys_monitor' and sysstat & 7 = 4)
  62. begin
  63.     drop procedure sp_MSsys_monitor
  64. end
  65. if exists (select * from sysobjects where name = 'sp_MSdatabase_list' and sysstat & 7 = 4)
  66. begin
  67.     drop procedure sp_MSdatabase_list
  68. end
  69. if exists (select * from sysobjects where name = 'sp_MSdatabase_avail' and sysstat & 7 = 4)
  70. begin
  71.     drop procedure sp_MSdatabase_avail
  72. end
  73. if exists (select * from sysobjects where name = 'sp_MSbackup_now' and sysstat & 7 = 4)
  74. begin
  75.     drop procedure sp_MSbackup_now
  76. end
  77. if exists (select * from sysobjects where name = 'sp_MSbackup_schedule' and sysstat & 7 = 4)
  78. begin
  79.     drop procedure sp_MSbackup_schedule
  80. end
  81. if exists (select * from sysobjects where name = 'sp_MSuser_list' and sysstat & 7 = 4)
  82. begin
  83.     drop procedure sp_MSuser_list
  84. end
  85. if exists (select * from sysobjects where name = 'sp_MSmonitor' and sysstat & 7 = 4)
  86. begin
  87.     drop procedure sp_MSmonitor
  88. end
  89. if exists (select * from sysobjects where name = 'sp_MScmd_permissions' and sysstat & 7 = 4)
  90. begin
  91.   drop procedure sp_MScmd_permissions
  92. end
  93. if exists (select * from sysobjects where name = 'sp_MSuser_cmd_permissions' and sysstat & 7 = 4)
  94. begin
  95.   drop procedure sp_MSuser_cmd_permissions
  96. end
  97. if exists (select * from sysobjects where name = 'sp_MSsegment_list' and sysstat & 7 = 4)
  98. begin
  99.   drop procedure sp_MSsegment_list
  100. end
  101. if exists (select * from sysobjects where name = 'sp_MSsegment_devices' and sysstat & 7 = 4)
  102. begin
  103.   drop procedure sp_MSsegment_devices
  104. end
  105. if exists (select * from sysobjects where name = 'sp_MSseg_properties' and sysstat & 7 = 4)
  106. begin
  107.   drop procedure sp_MSseg_properties
  108. end
  109. if exists (select * from sysobjects where name = 'sp_MSseg_tables' and sysstat & 7 = 4)
  110. begin
  111.   drop procedure sp_MSseg_tables
  112. end
  113. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_options_bits')
  114. begin
  115.   drop procedure sp_MSdb_options_bits
  116. end
  117. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_devices_segments')
  118. begin
  119.   drop procedure sp_MSdb_devices_segments
  120. end
  121. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_space')
  122. begin
  123.   drop procedure sp_MSdb_space
  124. end
  125. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_options')
  126. begin
  127.   drop procedure sp_MSdb_options
  128. end
  129. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_devices')
  130. begin
  131.   drop procedure sp_MSdb_devices
  132. end
  133. if exists (select * from sysobjects where sysstat & 7 = 4 and name = 'sp_MSdb_properties')
  134. begin
  135.   drop procedure sp_MSdb_properties
  136. end
  137. if exists (select * from sysobjects where name = 'sp_MSthread_list' and sysstat & 7 = 4)
  138. begin
  139.   drop procedure sp_MSthread_list
  140. end
  141. if exists (select * from sysobjects where name = 'sp_MShelpdb' and sysstat & 7 = 4)
  142. begin
  143.   drop procedure sp_MShelpdb
  144. end
  145. if exists (select * from sysobjects where name = 'sp_MShelpremotelogin' and sysstat & 7 = 4)
  146. begin
  147.   drop procedure sp_MShelpremotelogin
  148. end
  149. if exists (select * from sysobjects where name = 'sp_MShelpserver' and sysstat & 7 = 4)
  150. begin
  151.   drop procedure sp_MShelpserver
  152. end
  153. if exists (select * from sysobjects where name = 'sp_MSAdmin_version' and sysstat & 7 = 4)
  154. begin
  155.   drop procedure sp_MSAdmin_version
  156. end
  157. go
  158.  
  159. if exists (select * from sysobjects where name = 'sp_MSlogdevice' and sysstat & 7 = 4)
  160. begin
  161.   drop procedure sp_MSlogdevice
  162. end
  163. go
  164.  
  165. /****************************************************************************/
  166. /* This stored procedure is an updated version designed to tell users with  */
  167. /* old .exe's to upgrade to the newer ones.                                 */
  168. /****************************************************************************/
  169.  
  170.  
  171. print ''
  172. print 'Creating sp_MScheck_admin'
  173. print ''
  174. go
  175. create procedure sp_MScheck_admin as
  176.  
  177. /* Create a temporary table to store the missing objects */
  178.  
  179. set nocount on
  180.  
  181. create table #spmissing
  182. (name varchar(30),
  183.  type varchar(10)
  184. )
  185.  
  186. /* */
  187. /* Check for the stored procedures first */
  188. /* */
  189.  
  190. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdevice_list' and sysstat & 7 = 4)
  191. begin
  192.   insert into #spmissing values('sp_MSdevice_list', 'procedure')
  193. end
  194. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_description' and sysstat & 7 = 4)
  195. begin
  196.   insert into #spmissing values('sp_MSdev_description', 'procedure')
  197. end
  198. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_databases' and sysstat & 7 = 4)
  199. begin
  200.   insert into #spmissing values('sp_MSdev_databases', 'procedure')
  201. end
  202. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsys_monitor' and sysstat & 7 = 4)
  203. begin
  204.   insert into #spmissing values('sp_MSsys_monitor', 'procedure')
  205. end
  206. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  207. begin
  208.   insert into #spmissing values('sp_MSnext_devnumber', 'procedure')
  209. end
  210. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_list' and sysstat & 7 = 4)
  211. begin
  212.   insert into #spmissing values('sp_MSdatabase_list', 'procedure')
  213. end
  214. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_avail' and sysstat & 7 = 4)
  215. begin
  216.   insert into #spmissing values('sp_MSdatabase_avail', 'procedure')
  217. end
  218. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_now' and sysstat & 7 = 4)
  219. begin
  220.   insert into #spmissing values('sp_MSbackup_now', 'procedure')
  221. end
  222. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_list' and sysstat & 7 = 4)
  223. begin
  224.   insert into #spmissing values('sp_MSuser_list', 'procedure')
  225. end
  226. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSmonitor' and sysstat & 7 = 4)
  227. begin
  228.   insert into #spmissing values('sp_MSmonitor', 'procedure')
  229. end
  230. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices' and sysstat & 7 = 4)
  231. begin
  232.   insert into #spmissing values('sp_MSdb_devices', 'procedure')
  233. end
  234. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options' and sysstat & 7 = 4)
  235. begin
  236.   insert into #spmissing values('sp_MSdb_options', 'procedure')
  237. end
  238. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_properties' and sysstat & 7 = 4)
  239. begin
  240.   insert into #spmissing values('sp_MSdb_properties', 'procedure')
  241. end
  242. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_space' and sysstat & 7 = 4)
  243. begin
  244.   insert into #spmissing values('sp_MSdb_space', 'procedure')
  245. end
  246. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_schedule' and sysstat & 7 = 4)
  247. begin
  248.   insert into #spmissing values('sp_MSbackup_schedule', 'procedure')
  249. end
  250. if not exists (select * from master.dbo.sysobjects where name = 'sp_MScmd_permissions' and sysstat & 7 = 4)
  251. begin
  252.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  253. end
  254. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_cmd_permissions' and sysstat & 7 = 4)
  255. begin
  256.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  257. end
  258. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options_bits' and sysstat & 7 = 4)
  259. begin
  260.   insert into #spmissing values('sp_MSdb_options_bits', 'procedure')
  261. end
  262.  
  263. /* */
  264. /* these are 4.2 only */
  265. /* */
  266.  
  267. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_mirror' and sysstat & 7 = 4)
  268. begin
  269.   insert into #spmissing values('sp_MSdev_mirror', 'procedure')
  270. end
  271. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices_segments' and sysstat & 7 = 4)
  272. begin
  273.   insert into #spmissing values('sp_MSdb_devices_segments', 'procedure')
  274. end
  275. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_list' and sysstat & 7 = 4)
  276. begin
  277.   insert into #spmissing values('sp_MSsegment_list', 'procedure')
  278. end
  279. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_devices' and sysstat & 7 = 4)
  280. begin
  281.   insert into #spmissing values('sp_MSsegment_devices', 'procedure')
  282. end
  283. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_properties' and sysstat & 7 = 4)
  284. begin
  285.   insert into #spmissing values('sp_MSseg_properties', 'procedure')
  286. end
  287. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_tables' and sysstat & 7 = 4)
  288. begin
  289.   insert into #spmissing values('sp_MSseg_tables', 'procedure')
  290. end
  291. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSthread_list' and sysstat & 7 = 4)
  292. begin
  293.   insert into #spmissing values('sp_MSthread_list', 'procedure')
  294. end
  295.  
  296. /* */
  297. /* Check for the tables next */
  298. /* */
  299.  
  300. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups')
  301. begin
  302.   insert into #spmissing values('MSschedule_backups','table')
  303. end
  304. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups_log')
  305. begin
  306.   insert into #spmissing values('MSschedule_backups_log','table')
  307. end
  308. if not exists (select * from master.dbo.sysobjects where name = 'MSsystem_monitor')
  309. begin
  310.   insert into #spmissing values('MSsystem_monitor','table')
  311. end
  312. if not exists (select * from master.dbo.sysobjects where name = 'MSlast_monitor')
  313. begin
  314.   insert into #spmissing values('MSlast_monitor','table')
  315. end
  316.  
  317. set nocount off
  318.  
  319. print 'The SQL Administrator scripts on this server are for a later executable than you are running.  It is recommended that you upgrade your executable.'
  320. select * from #spmissing
  321. go
  322.  
  323.  
  324. /****************************************************************************/
  325. /* This stored procedure will get the name, type, size, location, device number, space
  326.    available, and mirror status for all the devices in the system        */
  327. /****************************************************************************/
  328. go
  329. print ''
  330. print 'Creating sp_MSdevice_list'
  331. print ''
  332. go
  333. create procedure sp_MSdevice_list as
  334.  
  335. /* Create a temporary table where we can store all the necessary information */
  336.  
  337. set nocount on
  338.  
  339. create table #spdevtab
  340. (name varchar(30) null,
  341.  cntrltype varchar(10) null,
  342.  devtype varchar(20) null,
  343.  devsize float null,
  344.  devsizeout varchar(20) null,
  345.  spaceused float null,
  346.  freespaceout varchar(20) null,
  347.  mirror varchar(10) null,
  348.  cntrlnum int null
  349. )
  350.  
  351. declare @alloc float
  352. select @alloc = convert(float,low) from master.dbo.spt_values where type = 'E' and number = 1
  353.  
  354. /* Insert the name, size, and space used for the database devices that have databases on them */
  355.  
  356. insert into #spdevtab (name, devsize, devsizeout, spaceused, mirror,cntrlnum)
  357. select d.name,round((((convert(float,d.high) - convert(float,d.low)) * @alloc) / 1048576.0),0),
  358. ltrim(str(round((((convert(float,d.high) - convert(float,d.low)) * @alloc)/1048576.0),0), 8,0)) + ' MB',
  359. spaceused = (select round(sum((convert(float,usg.size) * @alloc) / 1048576.0),0) from master..sysusages usg
  360.    where d.low <= usg.size + usg.vstart - 1 and d.high >= usg.size + usg.vstart - 1),
  361.  'none',d.low/16777216
  362. from master.dbo.sysdevices d
  363. where d.cntrltype = 0 and exists (select * from master..sysusages usg
  364.    where d.low <= usg.size + usg.vstart - 1 and d.high >= usg.size + usg.vstart - 1)
  365.  
  366.  
  367. /* Insert the name, size for the database devices that are clean */
  368.  
  369. insert into #spdevtab (name, devsize, devsizeout, spaceused, mirror,cntrlnum)
  370. select d.name,round((((convert(float,d.high) - convert(float,d.low)) * @alloc) / 1048576.0),0),
  371. ltrim(str(round((((convert(float,d.high) - convert(float,d.low)) * @alloc)/1048576.0),0), 8,0)) + ' MB', 0, 'none',
  372. d.low/16777216
  373. from master.dbo.sysdevices d
  374. where d.cntrltype = 0 and
  375. d.name not in (select name from #spdevtab)
  376.  
  377. /* Insert the name for the database dump devices */
  378.  
  379. insert #spdevtab (name, devsizeout, freespaceout, mirror)
  380. select d.name, '*******','*****', '*******'
  381. from master.dbo.sysdevices d where
  382. d.name not in (select name from #spdevtab)
  383.  
  384. /* Calculate the free space each disk device */
  385.  
  386. update #spdevtab set #spdevtab.freespaceout = convert(varchar(10),(convert(int,(#spdevtab.devsize - #spdevtab.spaceused)))) + ' MB'
  387.          from master.dbo.sysdevices d, #spdevtab where d.cntrltype = 0 and #spdevtab.name = d.name
  388.  
  389. /* set tape's to 0 length, this is a temporary fix to account for server requiring
  390.    tape length, will change later */
  391. update #spdevtab set #spdevtab.devsize = 0.0
  392.          from master.dbo.sysdevices d, #spdevtab where d.cntrltype = 5 and #spdevtab.name = d.name
  393.  
  394. /*  Now figure out what kind of controller type it is.
  395.  
  396. **  cntrltype = 0    special
  397. **            2    disk
  398. **          3-4    floppy
  399. **          5        tape
  400. */
  401.  
  402. update #spdevtab set #spdevtab.cntrltype = 'd' from master.dbo.sysdevices d, #spdevtab
  403.          where d.cntrltype = 2 and #spdevtab.name = d.name
  404.  
  405. update #spdevtab set #spdevtab.cntrltype = 'f' from master.dbo.sysdevices d, #spdevtab
  406.          where d.cntrltype between 3 and 4 and #spdevtab.name = d.name
  407.  
  408. update #spdevtab set #spdevtab.cntrltype = 't' from master.dbo.sysdevices d, #spdevtab
  409.          where d.cntrltype = 5 and #spdevtab.name = d.name
  410.  
  411. update #spdevtab set #spdevtab.cntrltype = 'UNKNOWN' from master.dbo.sysdevices d, #spdevtab
  412.          where d.cntrltype > 8 and #spdevtab.name = d.name
  413.  
  414. /* Get floppy device capacities which is listed in master.dbo.sysdevices.high in number of 62k blocks. */
  415.  
  416. if exists (select * from master.dbo.sysdevices d, #spdevtab
  417.        where d.cntrltype between 3 and 4 and d.high > 0 and #spdevtab.name = d.name)
  418. begin
  419.   update #spdevtab set devsizeout = ltrim(str(((d.high * 63488.0) / 1000000.0), 5,1)) + ' MB' from master.dbo.sysdevices d, #spdevtab
  420.            where d.cntrltype between 3 and 4 and d.high > 0 and #spdevtab.name = d.name
  421. end
  422.  
  423. /* Get tape device capacities which is listed in master.dbo.sysdevices.high in number of 62k blocks. (skip decimal places) */
  424.  
  425. if exists (select * from master.dbo.sysdevices d, #spdevtab
  426.        where d.cntrltype = 5 and d.high > 0 and #spdevtab.name = d.name)
  427. begin
  428. /* Tempoary fix until server doesn't care about tape size */
  429. /*  update #spdevtab set devsizeout =  ltrim(str(((d.high * 63488) / 1000000), 5,1)) + ' MB' from master.dbo.sysdevices d, #spdevtab
  430.            where d.cntrltype = 5 and d.high > 0 and #spdevtab.name = d.name */
  431.   update #spdevtab set devsizeout =  '*******' from master.dbo.sysdevices d, #spdevtab
  432.            where d.cntrltype = 5 and d.high > 0 and #spdevtab.name = d.name
  433. end
  434.  
  435. /* Now get the dump devices by turning the status bits into english. 0x10 = dump device. */
  436.  
  437. if exists (select * 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 = 16 and #spdevtab.name = d.name)
  439. begin
  440.   update #spdevtab set devtype = substring(v.name,1,4)
  441.      from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  442.      where v.type = 'V' and v.number > -1 and d.status & v.number = 16 and #spdevtab.name = d.name
  443. end
  444.  
  445. /* Now get the physical disks by turning the status bits into english. 0x02 is a physical disk. */
  446.  
  447. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  448.        where v.type = 'V' and v.number > -1 and d.status & v.number = 2 and #spdevtab.name = d.name)
  449. begin
  450.   update #spdevtab set devtype = substring(v.name,charindex(' ',v.name)+ 1,4) from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  451.      where v.type = 'V' and v.number > - 1 and d.status & v.number = 2 and #spdevtab.name = d.name
  452. end
  453.  
  454. /* Now get the default disk by turning the status bits into english. 0x01 is a default disk. */
  455.  
  456. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  457.        where v.type = 'V' and v.number > -1 and d.status & v.number = 1 and #spdevtab.name = d.name)
  458. begin
  459.    update #spdevtab set devtype = 'dflt' from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  460.       where v.type = 'V' and v.number > -1 and d.status & v.number = 1 and #spdevtab.name = d.name
  461. end
  462.  
  463. /* Now get the logical disks by turning the status bits into english. 0x04 is a logical disk. */
  464.  
  465. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  466.        where v.type = 'V' and v.number > -1 and d.status & v.number = 4 and #spdevtab.name = d.name)
  467. begin
  468.   update #spdevtab set devtype = v.name from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  469.      where v.type = 'V' and v.number > -1 and d.status & v.number = 4 and #spdevtab.name = d.name
  470. end
  471.  
  472. /* Now get the devices that are mirrored. The mirror status bits are >= 32. */
  473.  
  474. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
  475.        where v.type = 'V' and v.number > -1 and d.status >= 32 and #spdevtab.name = d.name)
  476. begin
  477.    if exists (select * from master.dbo.sysdevices d, #spdevtab
  478.           where d.status & 512 = 512 and #spdevtab.name = d.name)
  479.    begin
  480.      update #spdevtab set mirror = 'enabled' from master.dbo.sysdevices d
  481.         where d.status & 512 = 512 and #spdevtab.name = d.name
  482.    end
  483.  
  484.    if exists (select * from master.dbo.sysdevices d, #spdevtab
  485.           where d.status & 512 != 512 and d.status >= 32 and #spdevtab.name = d.name)
  486.    begin
  487.      update #spdevtab set mirror = 'disabled' from master.dbo.sysdevices d
  488.         where d.status & 512 != 512 and d.status >= 32 and #spdevtab.name = d.name
  489.    end
  490. end
  491.  
  492. /* get control numbers > 127 to be positive */
  493. update #spdevtab set #spdevtab.cntrlnum = 256+cntrlnum
  494.   from master.dbo.sysdevices d where (#spdevtab.cntrlnum < 0 and d.cntrltype = 0) and (#spdevtab.name = d.name)
  495. /* set rest to 0 */
  496. update #spdevtab set #spdevtab.cntrlnum = 0
  497.   from master.dbo.sysdevices d where d.cntrltype != 0 and #spdevtab.name = d.name
  498.  
  499. /* */
  500. /* Final select statement to output the information */
  501. /* */
  502.  
  503. set nocount off
  504.  
  505. select device_name = d.name, device_type = #spdevtab.cntrltype + #spdevtab.devtype,
  506.        device_size = #spdevtab.devsizeout, location = substring(d.phyname, 1, 46),
  507.        device_number = #spdevtab.cntrlnum, freespace = #spdevtab.freespaceout, mirror_stat = #spdevtab.mirror
  508. from master.dbo.sysdevices d, #spdevtab
  509. where d.name = #spdevtab.name
  510. order by device_name, device_type
  511. go
  512.  
  513. /************* DUMP THE TRANSACTION LOG **************************************/
  514. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  515. /* script periodically, you will run out of transaction log space.           */
  516. dump tran master with truncate_only
  517. go
  518. /************* END DUMP THE TRANSACTION LOG **********************************/
  519.  
  520.  
  521. /****************************************************************************/
  522. /* This stored procedure will get the next available device number          */
  523. /****************************************************************************/
  524.  
  525. print ''
  526. print 'Creating sp_MSnext_devnumber'
  527. print ''
  528. go
  529. create procedure sp_MSnext_devnumber as
  530. declare @nextdev smallint, @maxdev smallint
  531. select @nextdev = 1
  532. select @maxdev = 255        /* devno is the high byte in sysdevices.low so should never change */
  533.  
  534. while @nextdev != @maxdev /* This is one max device number allowed */
  535. begin
  536.   if exists(select name from master.dbo.sysdevices where convert(smallint, convert(binary(1), (low / 16777216))) = @nextdev)
  537.     select @nextdev = @nextdev + 1
  538.   else
  539.   begin
  540.     select @nextdev
  541.     return
  542.   end
  543. end
  544. raiserror 70000 'No available device numbers'
  545. go
  546.  
  547. /****************************************************************************/
  548. /* This stored procedure gets the mirror status of a particular device.  This */
  549. /* is only valid on the 4.2.1 server.  It is called by sp_MSdev_description */
  550. /****************************************************************************/
  551.  
  552.  
  553. print ''
  554. print 'Creating sp_MSdev_mirror'
  555. print ''
  556. go
  557. create procedure sp_MSdev_mirror
  558. @devname varchar(30), @description varchar(255)  /* device and current description */
  559. as
  560.  
  561. /*  TCD 8/2/91 See if the device exists. */
  562.  
  563. if not exists (select * from master.dbo.sysdevices
  564.              where name = @devname)
  565. begin
  566.     raiserror 70001 'Device doesn''t exist. The ''sa'' must have dropped the device since you last displayed the listing.'
  567.     return
  568. end
  569.  
  570.  
  571. /* */
  572. /* See if disk is mirrored.  Status bits >= 0x20 (32) */
  573. /* */
  574.  
  575. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  576.        where v.type = 'V' and v.number > -1 and d.status >= 32 and d.name = @devname)
  577. begin
  578.  
  579. /*  Check to see if mirror in enabled. Status bits 0x200 (512). */
  580.  
  581.    if exists (select * from master.dbo.sysdevices d where d.status & 512 = 512 and d.name = @devname)
  582.    begin
  583.        select @description = @description + ', mirror enabled'
  584.    end
  585.  
  586. /* Check to see if mirror is disabled.    */
  587.  
  588.    if exists (select * from master.dbo.sysdevices d
  589.        where d.status & 512 != 512 and d.status >= 32 and d.name = @devname)
  590.    begin
  591.        select @description = @description + ', mirror disabled'
  592.    end
  593.  
  594. /* Get the name of the device it's mirrored on */
  595.  
  596.    select @description = @description + ', mirror on: ' + (select d.mirrorname
  597.              from master.dbo.sysdevices d where d.name = @devname)
  598.  
  599. end
  600.  
  601. /*
  602. **  Check to see if there is a mirrorname entry but mirroring not enabled.
  603. **  If so, then one side of the mirror is off-line.
  604. */
  605.  
  606. else if exists (select * from master.dbo.sysdevices d
  607.         where d.name = @devname and d.mirrorname != null)
  608. begin
  609.  
  610.     /* Figure out which side of the mirror is disabled. */
  611.     /* If 0x100 is on, then phyname is disabled and mirrorname is enabled. */
  612.  
  613.   if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  614.          where v.type = 'V' and v.number > -1 and d.status & v.number = 256
  615.          and d.name = @devname)
  616.   begin
  617.        select @description = @description + ', only device: ' +
  618.                   (select d.mirrorname + 'of mirror is enabled -- device: '
  619.                   + d.phyname + ' is disabled'
  620.                   from master.dbo.sysdevices d, master.dbo.spt_values v
  621.                   where v.type = 'V' and v.number > -1 and d.status & v.number = 256
  622.                   and d.name = @devname)
  623.   end
  624.   else if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  625.          where v.type = 'V' and v.number > -1 and d.status & v.number = 64
  626.            and d.name = @devname)
  627.   begin
  628.        select @description = @description + ', only device: ' +
  629.                  (select d.mirrorname + 'of mirror is enabled -- device: '
  630.                   + d.phyname + ' is disabled'
  631.                   from master.dbo.sysdevices d, master.dbo.sysdevices e, master.dbo.spt_values v
  632.                   where v.type = 'V' and v.number > -1 and d.status & v.number = 64 and
  633.                   d.name = @devname and e.status & 256 != 256
  634.                   and e.name = @devname)
  635.   end
  636. end
  637.  
  638. /* Check for 'serial writes' for mirrored disks. Status bits = 0x20 */
  639.  
  640. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  641.        where v.type = 'V' and v.number > -1 and d.status & v.number = 32
  642.        and d.name = @devname)
  643. begin
  644.        select @description = @description + ', serial mirror writes'
  645. end
  646.  
  647. /* Check for 'reads mirrored' for mirrored disks. Status bits = 0x80 */
  648.  
  649. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  650.         where v.type = 'V' and v.number > -1 and d.status & v.number = 128
  651.        and d.name = @devname)
  652. begin
  653.        select @description = @description + ', reads mirrored'
  654. end
  655.  
  656. /* Output the final description */
  657.  
  658. set nocount off
  659. select @description
  660. go
  661.  
  662. /************* DUMP THE TRANSACTION LOG **************************************/
  663. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  664. /* script periodically, you will run out of transaction log space.           */
  665. dump tran master with truncate_only
  666. go
  667. /************* END DUMP THE TRANSACTION LOG **********************************/
  668.  
  669.  
  670. /****************************************************************************/
  671. /* This stored procedure will get the description of characteristics for
  672.    a particular device                                */
  673. /****************************************************************************/
  674.  
  675. print ''
  676. print 'Creating sp_MSdev_description'
  677. print ''
  678. go
  679. create procedure sp_MSdev_description
  680. @devname varchar(30)          /* device to check out */
  681. as
  682.  
  683. /*  See if the device exists. */
  684.  
  685. if not exists (select * from master.dbo.sysdevices
  686.              where name = @devname)
  687. begin
  688.     raiserror 70001 'Device doesn''t exist. The ''sa'' must have dropped the device since you last displayed the listing.'
  689.     return
  690. end
  691.  
  692. /* */
  693. /* Create a temporary table where we can build up a translation of the device status bits. */
  694. /* */
  695.  
  696. declare @description varchar(250)
  697.  
  698. set nocount on
  699.  
  700. /*
  701. **  Now figure out what kind of controller type it is.    The type are
  702. **  COMPLETELY platform dependent.
  703. **  cntrltype = 0    special
  704. **            2    disk
  705. **          3-4    floppy
  706. **          5        tape
  707. */
  708.  
  709. if exists (select * from master.dbo.sysdevices d
  710.        where d.cntrltype = 0 and d.name = @devname)
  711. begin
  712.        select @description = 'database/log device'
  713. end
  714.  
  715. /* */
  716. /* See if disk is a default disk. Status of 0x01 is a default disk. */
  717. /* */
  718.  
  719. if exists (select * from master.dbo.sysdevices d, master.dbo.spt_values v
  720.        where v.type = 'V' and v.number > -1 and d.status & v.number = 1
  721.        and d.name = @devname)
  722. begin
  723.     select @description = @description + ', default disk'
  724. end
  725.  
  726. if exists (select * from master.dbo.sysdevices d
  727.         where d.cntrltype between 3 and 4 and d.name = @devname)
  728. begin
  729.        select @description = 'floppy dump'
  730. end
  731.  
  732. if exists (select * from master.dbo.sysdevices d
  733.        where d.cntrltype = 5 and d.name = @devname and d.status & 8 = 0)
  734. begin
  735.        select @description = 'tape dump-no skip label'
  736. end
  737.  
  738. if exists (select * from master.dbo.sysdevices d
  739.        where d.cntrltype = 5 and d.name = @devname and d.status & 8 = 8)
  740. begin
  741.        select @description = 'tape dump-skip label'
  742. end
  743.  
  744. exec sp_MSdev_mirror @devname, @description
  745. go
  746.  
  747. /****************************************************************************/
  748. /* This stored procedure will get the name, size, and owner of all the databases
  749.    that use a particular device for data only, log only, data and log        */
  750. /****************************************************************************/
  751. print ''
  752. print 'Creating sp_MSdev_databases'
  753. print ''
  754. go
  755. create procedure sp_MSdev_databases
  756. @devname varchar(30)          /* device to check out */
  757. as
  758.  
  759.  
  760. /*  TCD 8/2/91 See if the device exists. */
  761.  
  762. if not exists (select * from master.dbo.sysdevices
  763.              where name = @devname)
  764. begin
  765.     raiserror 70001 'Device doesn''t exist. The ''sa'' must have dropped the device since you last displayed the listing.'
  766.     return
  767. end
  768.  
  769. /* Databases that use the device for data only */
  770. declare @low float
  771. select @low = convert(float,a.low) from spt_values a where a.type = 'E' and a.number = 1
  772. select name = db_name(usg.dbid),
  773. size = ltrim(convert(varchar(10),round(sum((convert(float,usg.size)) * @low) / 1048576,0))) + ' MB',
  774. usage = b.name, owner = suser_name(suid)
  775. from sysdatabases d, sysusages usg, sysdevices v, spt_values b
  776. where d.dbid = usg.dbid and v.low <= size + vstart
  777.       and v.high >= size + vstart - 1
  778.       and v.status & 2 = 2
  779.       and v.name = @devname
  780.       and b.type = 'S'
  781.       and usg.segmap & 7 = b.number
  782. group by usg.dbid, suser_name(suid), b.name
  783. order by 1
  784. go
  785.  
  786. /************* DUMP THE TRANSACTION LOG **************************************/
  787. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  788. /* script periodically, you will run out of transaction log space.           */
  789. dump tran master with truncate_only
  790. go
  791. /************* END DUMP THE TRANSACTION LOG **********************************/
  792.  
  793. /************************************************************************/
  794. /* This section to create all the tables necessary for scheduled
  795.    backup and system monitoring                                         */
  796. /************************************************************************/
  797.  
  798. print ''
  799. print 'Creating MSscheduled_backups, if table already exists, ignore error message.'
  800. print ''
  801. go
  802. create table MSscheduled_backups
  803. (
  804. Event_id        int        Not Null,    /* Unique identifier */
  805. Database_name        varchar(30)    Not Null,    /* Name of database to be dumped */
  806. Database_owner        varchar(30)    Not Null,    /* Name of the database owner */
  807. Database_dump        varchar(30)    Null,        /* Database dump device */
  808. Log_dump        varchar(30)    Null,        /* Log dump device */
  809. Day            tinyint     Not Null,    /* Day data is to be dumped */
  810. Frequency        tinyint     Not Null,    /* How often data is to be dumped */
  811. Start_time        char(5)     Not Null,    /* Time data should be dumped */
  812. Enabled         bit        Not Null,    /* 0 = disabled 1 = enabled */
  813. Last_dump        datetime    Null,        /* Last dump that took place */
  814. In_progress        bit        Not Null    /* 1 = backup in progress */
  815. )
  816. go
  817. create unique index sched_idx on MSscheduled_backups (Event_id)
  818. go
  819.  
  820. /* alter existing table to add Stat column */
  821. if patindex('%Windows%',@@version) > 0
  822.    or @@microsoftversion & 0x00FFFFFF >= 0x00050000
  823. begin
  824.  
  825. if not exists (select name from syscolumns where name = 'Stat' and id = (select id from sysobjects where name = 'MSscheduled_backups'))
  826. begin
  827.    alter table MSscheduled_backups add Stat tinyint null
  828. end
  829.  
  830. if not exists (select name from syscolumns where name = 'email_name' and id = (select id from sysobjects where name = 'MSscheduled_backups'))
  831. begin
  832.    alter table MSscheduled_backups add attempts tinyint null, email_name varchar(60) null
  833. end
  834. end
  835. go
  836.  
  837. print 'Ignore errors on unknown column Stat if upgrading OS/2 SQL Server'
  838. go
  839.  
  840. if patindex('%Windows%',@@version) > 0
  841.    or @@microsoftversion & 0x00FFFFFF >= 0x00050000
  842. begin
  843. /* change existing table entries to indicate append of tape dump in stat column rather than */
  844. /* in dump device name */
  845. /* no append */
  846. update MSscheduled_backups set Stat = 1 where (Database_dump not like '%+' or Database_dump = NULL) and (Log_dump not like '%+' or Log_dump = NULL) and Stat = NULL
  847. /* append database dump */
  848. update MSscheduled_backups set Stat = 2 where Database_dump like '%+' and (Log_dump not like '%+' or Log_dump = null) and Stat = NULL
  849. /* append log dump */
  850. update MSscheduled_backups set Stat = 3 where Log_dump like '%+' and (Database_dump not like '%+' or Database_dump = null) and Stat = NULL
  851. /* append both */
  852. update MSscheduled_backups set Stat = 4 where Database_dump like '%+' and Log_dump like '%+' and Stat = NULL
  853. /* change '+'s to space */
  854. update MSscheduled_backups set Database_dump = stuff(Database_dump,charindex('+',Database_dump),1,' ') where Stat != 1 and Database_dump like '%+'
  855. /* trim out extra space */
  856. update MSscheduled_backups set Database_dump = rtrim(Database_dump) where Stat != 1
  857. /* change '+'s to space */
  858. update MSscheduled_backups set Log_dump = stuff(Log_dump,charindex('+',Log_dump),1,' ') where Stat != 1  and Log_dump like '%+'
  859. /* trim out extra space */
  860. update MSscheduled_backups set Log_dump = rtrim(Log_dump) where Stat != 1
  861. end
  862. go
  863.  
  864.  
  865. /* */
  866. /* Create the scheduled backup log table */
  867. /* */
  868.  
  869. print ''
  870. print 'Creating MSscheduled_backups_log, if table already exists ignore error message.'
  871. print ''
  872. go
  873. create table MSscheduled_backups_log
  874. (
  875. Event_ID        int        Not Null,    /* Event ID from schedule table */
  876. Database_name        varchar(30)    Not Null,    /* Name of database to be dumped */
  877. Actual_start_time    datetime    Not Null,    /* Actual time the dump started */
  878. Actual_end_time     datetime    Null,        /* Actual time the dump completed */
  879. Status            bit        Not Null,    /* 1 = SUCCESS 0 = FAILED */
  880. Message         varchar(255)    Null        /* Error message upon failure. */
  881. )
  882. go
  883.  
  884. /* */
  885. /* Create the MSsystem_monitor table */
  886. /* */
  887.  
  888. print ''
  889. print 'Creating MSsystem_monitor'
  890. print ''
  891. go
  892. create table MSsystem_monitor
  893. (
  894. Monitor_time        datetime    Not Null,    /* Time the monitor data was taken */
  895. CPU_busy_start        int        Not Null,    /* # secs CPU busy since server started */
  896. CPU_busy_last        int        Not Null,    /* # seconds CPU was busy since last monitor */
  897. CPU_busy_pct        tinyint     Not Null,    /* Percent CPU was busy since server started */
  898. IO_busy_start        int        Not Null,    /* # seconds IO was busy since server started */
  899. IO_busy_last        int        Not Null,    /* # seconds IO was busy since last monitor */
  900. IO_busy_pct        tinyint     Not Null,    /* Percent IO was busy since server started */
  901. Idle_start        int        Not Null,    /* # secs server idle since server started */
  902. Idle_last        int        Not Null,    /* # seconds server was idle since last monitor */
  903. Idle_pct        tinyint     Not Null,    /* Percent server was idle since server started */
  904. Pkts_sent_start     int        Not Null,    /* # packets sent since server started */
  905. Pkts_sent_last        int        Not Null,    /* # packets sent since last monitor */
  906. Pkts_received_start    int        Not Null,    /* # packets received since server started */
  907. Pkts_received_last    int        Not Null,    /* # packets received since last monitor */
  908. Pkt_errors_start    int        Not Null,    /* # packet errors since server started */
  909. Pkt_errors_last     int        Not Null,    /* # packet errors since last montor */
  910. Total_reads_start    int        Not Null,    /* # reads since server started */
  911. Total_reads_last    int        Not Null,    /* # reads since last monitor */
  912. Total_writes_start    int        Not Null,    /* # writes since server started */
  913. Total_writes_last    int        Not Null,    /* # write since last  monitor */
  914. Total_errors_start    int        Not Null,    /* # errors ince server started */
  915. Total_errors_last    int        Not Null,    /* # errors since last monitor */
  916. Connections_start    int        Not Null,    /* # connections since server started */
  917. Connections_last    int        Not Null,    /* # connections since last monitor */
  918. )
  919. go
  920.  
  921. /* */
  922. /* Create the MSlast_monitor table */
  923. /* */
  924.  
  925. print ''
  926. print 'Creating MSlast_montior'
  927. print ''
  928. go
  929. create table MSlast_monitor
  930. (
  931. Last_time       datetime       Not Null,       /* Time of last monitor */
  932. CPU_busy       int           Not Null,       /* # seconds CPU was busy since last monitor */
  933. IO_busy        int           Not Null,       /* # seconds IO was busy since last monitor */
  934. Idle           int           Not Null,       /* # seconds server was idle since last monitor */
  935. Pkts_sent       int           Not Null,       /* # packets sent since last monitor */
  936. Pkts_received       int           Not Null,       /* # packets received since last monitor */
  937. Pkt_errors       int           Not Null,       /* # packet errors since last montor */
  938. Total_reads       int           Not Null,       /* # reads since last monitor */
  939. Total_writes       int           Not Null,       /* # write since last  monitor */
  940. Total_errors       int           Not Null,       /* # errors since last monitor */
  941. Connections       int           Not Null,       /* # connections since last monitor */
  942. )
  943. go
  944.  
  945. insert into MSlast_monitor
  946. select getdate(), @@cpu_busy, @@io_busy, @@idle, @@pack_sent, @@pack_received,
  947.        @@packet_errors, @@total_read, @@total_write, @@total_errors, @@connections
  948. go
  949.  
  950. /************* DUMP THE TRANSACTION LOG **************************************/
  951. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  952. /* script periodically, you will run out of transaction log space.           */
  953. dump tran master with truncate_only
  954. go
  955. /************* END DUMP THE TRANSACTION LOG **********************************/
  956.  
  957.  
  958.  
  959. /***************************************************************************/
  960. /* This stored procedure performs the monitoring of the system.  It is a
  961.    variation of the sp_monitor stored procedure on in the output and storage
  962.    of data                                   */
  963. /***************************************************************************/
  964.  
  965.  
  966. print ''
  967. print 'Creating sp_MSsys_monitor'
  968. print ''
  969. go
  970. create procedure sp_MSsys_monitor as
  971.  
  972. /* */
  973. /*  Declare variables to be used to hold current monitor values. */
  974. /* */
  975.  
  976. declare @now         datetime
  977. declare @cpu_busy     int
  978. declare @io_busy    int
  979. declare @idle        int
  980. declare @pack_received    int
  981. declare @pack_sent    int
  982. declare @pack_errors    int
  983. declare @connections    int
  984. declare @total_read    int
  985. declare @total_write    int
  986. declare @total_errors    int
  987.  
  988. declare @oldcpu_busy     int    /* used to see if SQL Server has been rebooted */
  989. declare @interval    int
  990. declare @mspertick    int    /* milliseconds per tick */
  991.  
  992. /* */
  993. /*  Set @mspertick.  This is just used to make the numbers easier to handle
  994.     and avoid overflow. */
  995. /* */
  996.  
  997. select @mspertick = convert(int, @@timeticks / 1000.0)
  998.  
  999. /*  Get current monitor values. */
  1000.  
  1001. begin transaction
  1002. select
  1003.     @now = getdate(),
  1004.     @cpu_busy = @@cpu_busy,
  1005.     @io_busy = @@io_busy,
  1006.     @idle = @@idle,
  1007.     @pack_received = @@pack_received,
  1008.     @pack_sent = @@pack_sent,
  1009.     @connections = @@connections,
  1010.     @pack_errors = @@packet_errors,
  1011.     @total_read = @@total_read,
  1012.     @total_write = @@total_write,
  1013.     @total_errors = @@total_errors
  1014.  
  1015. /*
  1016. **  Check to see if SQL Server has been rebooted.  If it has then the
  1017. **  value of @@cpu_busy will be less than the value of master.dbo.spt_monitor.cpu_busy.
  1018. **  If it has update master.dbo.spt_monitor.
  1019. */
  1020.  
  1021. select @oldcpu_busy = CPU_busy from MSlast_monitor
  1022. if @oldcpu_busy > @cpu_busy
  1023. begin
  1024.    update MSlast_monitor set
  1025.       Last_time = @now,
  1026.       CPU_busy = @cpu_busy,
  1027.       IO_busy = @io_busy,
  1028.       Idle = @idle,
  1029.       Pkts_received = @pack_received,
  1030.       Pkts_sent = @pack_sent,
  1031.       Connections = @connections,
  1032.       Pkt_errors = @pack_errors,
  1033.       Total_reads = @total_read,
  1034.       Total_writes = @total_write,
  1035.       Total_errors = @total_errors
  1036. end
  1037.  
  1038. /* */
  1039. /*  Insert a row in the monitor table to reflect the current values since the
  1040.     server was started. */
  1041. /* */
  1042.  
  1043. set nocount on
  1044.  
  1045. insert into MSsystem_monitor
  1046. select @now, (@cpu_busy * @mspertick) / 1000, 0, 0, (@io_busy * @mspertick) / 1000,
  1047. 0, 0, (@idle * @mspertick) / 1000, 0,0, @pack_sent, 0, @pack_received, 0,
  1048. @pack_errors,0, @total_read, 0, @total_write, 0, @total_errors, 0, @connections, 0
  1049.  
  1050. /* */
  1051. /*  Now update the system monitor table with the difference between this monitor
  1052.     and the last monitor taken */
  1053. /* */
  1054.  
  1055. select @interval = datediff(ss, Last_time, @now)
  1056. from MSlast_monitor
  1057.  
  1058. update MSsystem_monitor
  1059.     set CPU_busy_last = ((@cpu_busy - CPU_busy) * @mspertick) / 1000,
  1060.     CPU_busy_pct = ((((@cpu_busy - CPU_busy) * @mspertick) / 1000) * 100) / @interval,
  1061.     IO_busy_last = ((@io_busy - IO_busy) * @mspertick) / 1000,
  1062.     IO_busy_pct =  ((((@io_busy - IO_busy) * @mspertick) / 1000) * 100) / @interval,
  1063.     Idle_last =  ((@idle - Idle) * @mspertick) / 1000,
  1064.     Idle_pct = ((((@idle - Idle) * @mspertick) / 1000) * 100) / @interval,
  1065.     Pkts_received_last = @pack_received - Pkts_received,
  1066.     Pkts_sent_last = @pack_sent - Pkts_sent,
  1067.     Pkt_errors_last = @pack_errors - Pkt_errors,
  1068.     Total_reads_last = @total_read - Total_reads,
  1069.     Total_writes_last = @total_write - Total_writes,
  1070.     Total_errors_last = @total_errors - Total_errors,
  1071.     Connections_last = @connections - Connections
  1072. from MSlast_monitor
  1073. where Monitor_time = @now
  1074.  
  1075. /* */
  1076. /* Now update the MSlast_monitor table for next round */
  1077. /* */
  1078.  
  1079. update MSlast_monitor set
  1080.     Last_time = @now,
  1081.     CPU_busy = @cpu_busy,
  1082.     IO_busy = @io_busy,
  1083.     Idle = @idle,
  1084.     Pkts_received = @pack_received,
  1085.     Pkts_sent = @pack_sent,
  1086.     Connections = @connections,
  1087.     Pkt_errors = @pack_errors,
  1088.     Total_reads = @total_read,
  1089.     Total_writes = @total_write,
  1090.     Total_errors = @total_errors
  1091.  
  1092. set nocount off
  1093.  
  1094.  
  1095. commit transaction
  1096.  
  1097. return
  1098. go
  1099.  
  1100.  
  1101. /************* DUMP THE TRANSACTION LOG **************************************/
  1102. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1103. /* script periodically, you will run out of transaction log space.           */
  1104. dump tran master with truncate_only
  1105. go
  1106. /************* END DUMP THE TRANSACTION LOG **********************************/
  1107.  
  1108.  
  1109. /****************************************************************************/
  1110. /* This stored procedure will get the name, size, owner, space available,
  1111.    and create date of all the databases in the system */
  1112. /****************************************************************************/
  1113.  
  1114. print ''
  1115. print 'Creating sp_MSdatabase_list'
  1116. print ''
  1117. go
  1118. create procedure sp_MSdatabase_list
  1119. as
  1120.  
  1121. select distinct name = d.name,
  1122.         db_size = substring( convert( varchar(10), round( convert(
  1123.                   float,v.low * (select sum(convert(float,u.size)) from master.dbo.sysusages u where dbid = d.dbid)
  1124.       ) / 1048576.0, 0 ) ) + 'MB', 1, 8 ),
  1125.         owner = suser_name(suid),
  1126.       spaceavail = '         ',
  1127.         created = convert(char(11), d.crdate)
  1128. from master.dbo.sysdatabases d, master.dbo.spt_values v
  1129.        where v.type = 'E' and v.number = 1
  1130. order by d.name
  1131. go
  1132.  
  1133.  
  1134. /****************************************************************************/
  1135. /* This stored procedure will get the space available for all the databases
  1136.    in the system */
  1137. /****************************************************************************/
  1138. print ''
  1139. print 'Creating sp_MSdatabase_avail'
  1140. print ''
  1141. go
  1142. create procedure sp_MSdatabase_avail
  1143. as
  1144.  
  1145. declare @db_size float, @reserved float, @unused float, @low float
  1146. declare @spaceavail float
  1147. set nocount on
  1148.  
  1149. declare @pagesize int
  1150. select @pagesize = low from master..spt_values where number = 1 and type = 'E'
  1151. select @db_size =  ((sum((convert(float,size)) * @pagesize) / 1048576.0) * 1024.0)
  1152.            from master.dbo.sysusages
  1153.            where dbid = db_id()
  1154.  
  1155. /* */
  1156. /* reserved: sum(reserved) where indid in (0, 1, 255) */
  1157. /* */
  1158.  
  1159. select @low = convert(float,d.low) from master.dbo.spt_values d where d.number = 1 and d.type = 'E'
  1160.  
  1161. select @reserved = sum(convert(float,reserved)) from sysindexes where indid in (0, 1, 255)
  1162. select @reserved = (@reserved * @low)/1024.0
  1163.  
  1164. /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  1165.  
  1166. select @unused = sum(convert(float,reserved)) - sum(convert(float,used)) from sysindexes where indid in (0, 1, 255)
  1167. select @unused = (@unused * @low)/1024.0
  1168.  
  1169. set nocount off
  1170. select @spaceavail = (convert(int,round(@db_size,0)) - convert(int,round((@reserved - @unused),0)))
  1171.  
  1172. if(@spaceavail < 0)
  1173.     select space_avail = '0KB'
  1174. else
  1175.     select space_avail = convert(varchar(10),convert(int,round(@spaceavail,0)))+'KB'
  1176.  
  1177. go
  1178. /************* DUMP THE TRANSACTION LOG **************************************/
  1179. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1180. /* script periodically, you will run out of transaction log space.           */
  1181. dump tran master with truncate_only
  1182. go
  1183. /************* END DUMP THE TRANSACTION LOG **********************************/
  1184.  
  1185.  
  1186. /****************************************************************************/
  1187. /* This stored procedure will get all the databases that need to be dumped
  1188.    at the time of inquiry for a Windows NT SQL Server */
  1189. /****************************************************************************/
  1190. if exists (select * from sysobjects where name = 'sp_MSbackup_now' and sysstat & 7 = 4)
  1191. begin
  1192.     drop procedure sp_MSbackup_now
  1193. end
  1194. go
  1195.  
  1196. print''
  1197. print 'Ignore error message if installing on a OS/2 SQL Server'
  1198. print''
  1199. print ''
  1200. print 'Creating sp_MSbackup_now'
  1201. print ''
  1202. go
  1203. create procedure sp_MSbackup_now as
  1204. declare @now datetime, @firstday smallint, @dayofweek smallint, @hour smallint, @minute smallint,
  1205. @elapsed int, @monthyear varchar(30)
  1206.  
  1207. set nocount on
  1208.  
  1209. /* */
  1210. /* Get the current date and time. */
  1211. /* Parse out the relevant parts of the date */
  1212. /* */
  1213.  
  1214. select @now = getdate()
  1215. if (@@langid=0)
  1216.     select @firstday=7
  1217. else
  1218.     select @firstday = datefirst from master..syslanguages where langid=@@langid
  1219.  
  1220. select @dayofweek = (datepart(weekday,@now)+@firstday)%7
  1221. select @monthyear = substring(convert(varchar(12),getdate()),1,12)
  1222.  
  1223. /* */
  1224. /* Create a temporary table that holds data on what needs to be dumped */
  1225. /* */
  1226.  
  1227. create table #spdumptab
  1228. (
  1229. id           int           Not Null,       /* Unique identifier */
  1230. name           varchar(30)     Not Null,       /* Name of database to be dumped */
  1231. owner           varchar(30)     Not Null,       /* Name of the database owner */
  1232. ddump           varchar(30)     Null,           /* Database dump device */
  1233. ldump           varchar(30)     Null,           /* Log dump device */
  1234. datacntrltype  smallint        Null,           /* Control type */
  1235. logcntrltype   smallint        Null,           /* Control type */
  1236. status         tinyint         Null,           /* extra dump parameters */
  1237. trys           tinyint         Null,           /* number of attempts to dump */
  1238. emailname      varchar(60)     Null,           /* email recipient(s) for notification */
  1239. dumptime       varchar(32)     Not Null,       /* scheduled event time */
  1240. day            tinyint         Not Null,       /* day of week for dump */
  1241. freq           tinyint         Not Null        /* frequency of dump */
  1242. )
  1243.  
  1244. /* */
  1245. /* Check all the databases that are dumped daily, weekly, and biweekly */
  1246. /* Note: The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1247. /* */
  1248.  
  1249. insert into #spdumptab
  1250. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL,Stat,attempts,email_name,
  1251.     @monthyear+Start_time,Day,Frequency
  1252. from MSscheduled_backups
  1253. where Enabled = 1                         /* Dump turned on */
  1254.       and ((convert(smallint,Day) = @dayofweek) or Frequency = 1)             /* Dump today or Daily */
  1255.       and Frequency <= 14                     /* Freq daily, weekly, or biweekly */
  1256.       and datediff(day, Last_dump, @now) >= convert(smallint,Frequency)         /* Freq time has elapsed */
  1257.       and @now >= convert(datetime, @monthyear + Start_time)
  1258.       and datediff(hour, Last_dump, @now) >= convert(smallint,Frequency)*24    /* Freq time has elapsed */
  1259.       and datediff(minute, Last_dump, @now) >= convert(smallint,Frequency)*24*60    /* Freq time has elapsed */
  1260.  
  1261. /* */
  1262. /* Check all the databases that are dumped monthly */
  1263. /* Note: First we get this week number, then do the same criteria as the
  1264.      Daily, weekly, bi-weekly dump.
  1265.      The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1266. /* */
  1267.  
  1268. declare @rundate datetime, @weekno smallint     /* Get this week number */
  1269. select @rundate = @now
  1270. select @weekno = 1
  1271. while datepart(month,dateadd(day,-7,@rundate)) = datepart(month,@now)
  1272. begin
  1273.     select @weekno = @weekno + 1
  1274.     select @rundate = dateadd(day,-7,@rundate)
  1275. end
  1276.  
  1277. insert into #spdumptab
  1278. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL,Stat,attempts,email_name,
  1279. @monthyear+Start_time,Day,Frequency from MSscheduled_backups
  1280. where Enabled = 1                         /* Dump turned on */
  1281.       and (convert(smallint,Day) = @dayofweek)                 /* Dump today */
  1282.       and Frequency >= 31                     /* Freq monthly */
  1283.       and (convert(smallint,Frequency) - 22) >= @weekno         /* Week of month */
  1284.       and datediff(day, Last_dump, @now) >= 22             /* Freq time has elapsed */
  1285.       and @now >= convert(datetime, @monthyear + Start_time)
  1286.       and datediff(hour, Last_dump, @now) >= 22*24    /* Freq time has elapsed */
  1287.       and datediff(minute, Last_dump, @now) >= 22*24*60    /* Freq time has elapsed */
  1288.  
  1289.  
  1290. update #spdumptab set datacntrltype = (select distinct cntrltype from master..sysdevices s where
  1291. #spdumptab.ddump = s.name)
  1292.  
  1293. update #spdumptab set logcntrltype = (select distinct cntrltype from master..sysdevices s where
  1294. #spdumptab.ldump = s.name)
  1295.  
  1296.  
  1297. set nocount off
  1298.  
  1299. /* */
  1300. /* Output the values to the daemon */
  1301. /* */
  1302.  
  1303. select id = id, name = name, owner = owner, ddump = ddump, ldump = ldump,
  1304. dcntrl = datacntrltype, lcntrl = logcntrltype, stat = status, attempt=trys,
  1305. email=emailname,dumptime=convert(varchar(32),convert(datetime,dumptime)),day,freq from #spdumptab
  1306. order by (convert(datetime,dumptime))
  1307. go
  1308.  
  1309.  
  1310. /****************************************************************************/
  1311. /* This stored procedure will get all the databases that need to be dumped
  1312.    at the time of inquiry for an OS/2 SQL Server*/
  1313. /****************************************************************************/
  1314. if (patindex('%Windows%',@@version) = 0
  1315.          and @@microsoftversion & 0x00FFFFFF < 0x00050000) and
  1316.    exists (select * from sysobjects where name = 'sp_MSbackup_now' and sysstat & 7 = 4)
  1317. begin
  1318.         print 'OS/2 server, dropping NT version of sp_MSbackup_now'
  1319.     drop procedure sp_MSbackup_now
  1320. end
  1321. go
  1322.  
  1323. print''
  1324. print 'Ignore error message if installing on a Windows NT SQL Server'
  1325. print''
  1326. print ''
  1327. print 'Creating sp_MSbackup_now'
  1328. print ''
  1329. go
  1330.  
  1331. create procedure sp_MSbackup_now as
  1332. declare @now datetime, @firstday smallint, @dayofweek smallint, @hour smallint, @minute smallint,
  1333. @elapsed int, @monthyear varchar(30)
  1334.  
  1335. set nocount on
  1336.  
  1337. /* */
  1338. /* Get the current date and time. */
  1339. /* Parse out the relevant parts of the date */
  1340. /* */
  1341.  
  1342. select @now = getdate()
  1343. if (@@langid=0)
  1344.     select @firstday=7
  1345. else
  1346.     select @firstday = datefirst from master..syslanguages where langid=@@langid
  1347.  
  1348. select @dayofweek = (datepart(weekday,@now)+@firstday)%7
  1349. select @monthyear = substring(convert(varchar(12),getdate()),1,12)
  1350.  
  1351. /* */
  1352. /* Create a temporary table that holds data on what needs to be dumped */
  1353. /* */
  1354.  
  1355. create table #spdumptab
  1356. (
  1357. id           int           Not Null,       /* Unique identifier */
  1358. name           varchar(30)     Not Null,       /* Name of database to be dumped */
  1359. owner           varchar(30)     Not Null,       /* Name of the database owner */
  1360. ddump           varchar(30)     Null,           /* Database dump device */
  1361. ldump           varchar(30)     Null,           /* Log dump device */
  1362. datacntrltype  smallint        Null,           /* Control type */
  1363. logcntrltype   smallint        Null,           /* Control type */
  1364. )
  1365.  
  1366. /* */
  1367. /* Check all the databases that are dumped daily, weekly, and biweekly */
  1368. /* Note: The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1369. /* */
  1370.  
  1371. insert into #spdumptab
  1372. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL
  1373. from MSscheduled_backups
  1374. where Enabled = 1                         /* Dump turned on */
  1375.       and ((convert(smallint,Day) = @dayofweek) or Frequency = 1)             /* Dump today or Daily */
  1376.       and Frequency <= 14                     /* Freq daily, weekly, or biweekly */
  1377.       and datediff(day, Last_dump, @now) >= convert(smallint,Frequency)         /* Freq time has elapsed */
  1378.       and @now >= convert(datetime, @monthyear + Start_time)
  1379.       and datediff(hour, Last_dump, @now) >= convert(smallint,Frequency)*24    /* Freq time has elapsed */
  1380.       and datediff(minute, Last_dump, @now) >= convert(smallint,Frequency)*24*60    /* Freq time has elapsed */
  1381.  
  1382. /* */
  1383. /* Check all the databases that are dumped monthly */
  1384. /* Note: First we get this week number, then do the same criteria as the
  1385.      Daily, weekly, bi-weekly dump.
  1386.      The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1387. /* */
  1388.  
  1389. declare @rundate datetime, @weekno smallint     /* Get this week number */
  1390. select @rundate = @now
  1391. select @weekno = 1
  1392. while datepart(month,dateadd(day,-7,@rundate)) = datepart(month,@now)
  1393. begin
  1394.     select @weekno = @weekno + 1
  1395.     select @rundate = dateadd(day,-7,@rundate)
  1396. end
  1397.  
  1398. insert into #spdumptab
  1399. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL
  1400. from MSscheduled_backups
  1401. where Enabled = 1                         /* Dump turned on */
  1402.       and (convert(smallint,Day) = @dayofweek)                     /* Dump today */
  1403.       and Frequency >= 31                     /* Freq monthly */
  1404.       and (convert(smallint,Frequency) - 22) >= @weekno                 /* Week of month */
  1405.       and datediff(day, Last_dump, @now) >= 22             /* Freq time has elapsed */
  1406.       and @now >= convert(datetime, @monthyear + Start_time)
  1407.       and datediff(hour, Last_dump, @now) >= 22*24    /* Freq time has elapsed */
  1408.       and datediff(minute, Last_dump, @now) >= 22*24*60    /* Freq time has elapsed */
  1409.  
  1410.  
  1411. update #spdumptab set datacntrltype = (select cntrltype from master..sysdevices s where
  1412. #spdumptab.ddump = s.name)
  1413.  
  1414. update #spdumptab set logcntrltype = (select cntrltype from master..sysdevices s where
  1415. #spdumptab.ldump = s.name)
  1416.  
  1417.  
  1418. set nocount off
  1419.  
  1420. /* */
  1421. /* Output the values to the daemon */
  1422. /* */
  1423.  
  1424. select id = id, name = name, owner = owner, ddump = ddump, ldump = ldump,
  1425. dcntrl = datacntrltype, lcntrl = logcntrltype from #spdumptab
  1426. go
  1427.  
  1428.  
  1429.  
  1430. /************* DUMP THE TRANSACTION LOG **************************************/
  1431. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1432. /* script periodically, you will run out of transaction log space.           */
  1433. dump tran master with truncate_only
  1434. go
  1435. /************* END DUMP THE TRANSACTION LOG **********************************/
  1436.  
  1437. /*
  1438. ** This stored procedure returns the current scheduling for backups, it will
  1439. ** get the database name, day (in chars), frequency (in chars), start time,
  1440. ** and enabled status (in chars) from MSscheduled_backups.
  1441. */
  1442.  
  1443.  
  1444. print ''
  1445. print 'Creating sp_MSbackup_schedule'
  1446. print ''
  1447. go
  1448. create procedure sp_MSbackup_schedule as
  1449.  
  1450. /* Create a tempory table to hold the results */
  1451.  
  1452. create table #spbacksch
  1453. (id int,
  1454.  dataname char (30) not null,
  1455.  day char (9) null,
  1456.  frequency char (9) null,
  1457.  start_time char(10) not null,
  1458.  enabled char (8) null )
  1459.  
  1460. set nocount on
  1461.  
  1462. /* Fill the temporary table with the id, database name, and start time in the schedule table */
  1463.  
  1464. insert into #spbacksch select d.Event_id,d.Database_name,NULL,NULL,convert(char,d.Start_time),NULL from MSscheduled_backups d
  1465.  
  1466. /* Now set the day of week to it's character equivalent from a numeric value */
  1467.  
  1468. update #spbacksch set day = '*********' from MSscheduled_backups d
  1469.                 where d.Day = 0 and d.Event_id = id
  1470. update #spbacksch set day = 'Sunday' from MSscheduled_backups d
  1471.         where d.Day = 1 and d.Event_id = id
  1472. update #spbacksch set day = 'Monday' from MSscheduled_backups d
  1473.         where d.Day = 2 and d.Event_id = id
  1474. update #spbacksch set day = 'Tuesday' from MSscheduled_backups d
  1475.         where d.Day = 3 and d.Event_id = id
  1476. update #spbacksch set day = 'Wednesday' from MSscheduled_backups d
  1477.         where d.Day = 4 and d.Event_id = id
  1478. update #spbacksch set day = 'Thursday' from MSscheduled_backups d
  1479.         where d.Day = 5 and d.Event_id = id
  1480. update #spbacksch set day = 'Friday' from MSscheduled_backups d
  1481.         where d.Day = 6 and d.Event_id = id
  1482. update #spbacksch set day = 'Saturday' from MSscheduled_backups d
  1483.         where d.Day = 7 and d.Event_id = id
  1484.  
  1485. /* Now set the frequency to it's character equivalent from a numeric value */
  1486.  
  1487. update #spbacksch set frequency = 'Daily' from MSscheduled_backups d
  1488.                 where d.Frequency = 1 and d.Event_id = id
  1489. update #spbacksch set frequency = 'Weekly' from MSscheduled_backups d
  1490.                 where d.Frequency = 7 and d.Event_id = id
  1491. update #spbacksch set frequency = 'Bi-Weekly' from MSscheduled_backups d
  1492.                 where d.Frequency = 14 and d.Event_id = id
  1493. update #spbacksch set frequency = 'Monthly - Week 1' from MSscheduled_backups d
  1494.                 where d.Frequency = 31 and d.Event_id = id
  1495. update #spbacksch set frequency = 'Monthly - Week 2' from MSscheduled_backups d
  1496.                 where d.Frequency = 32 and d.Event_id = id
  1497. update #spbacksch set frequency = 'Monthly - Week 3' from MSscheduled_backups d
  1498.                 where d.Frequency = 33 and d.Event_id = id
  1499. update #spbacksch set frequency = 'Monthly - Week 4' from MSscheduled_backups d
  1500.                 where d.Frequency = 34 and d.Event_id = id
  1501.  
  1502. /* Now set the enabled status to it's character equivalent from a numeric value */
  1503.  
  1504. update #spbacksch set enabled = 'No' from MSscheduled_backups d
  1505.                 where d.Enabled = 0 and d.Event_id = id
  1506. update #spbacksch set enabled = 'Yes' from MSscheduled_backups d
  1507.                 where d.Enabled = 1 and d.Event_id = id
  1508.  
  1509. set nocount off
  1510.  
  1511. /* select the data from the temporary table to give the schedule in a human understandable form */
  1512.  
  1513. select dataname, day, frequency, start_time, enabled, convert(char,id) from #spbacksch
  1514.                order by dataname,id
  1515.  
  1516. go
  1517.  
  1518. /************* DUMP THE TRANSACTION LOG **************************************/
  1519. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1520. /* script periodically, you will run out of transaction log space.           */
  1521. dump tran master with truncate_only
  1522. go
  1523. /************* END DUMP THE TRANSACTION LOG **********************************/
  1524.  
  1525.  
  1526.  
  1527. /****************************************************************************/
  1528. /* This stored proc will get the login id, user name, alias, and group for
  1529.     all the users in a database for the MDI list box            */
  1530. /****************************************************************************/
  1531. print ''    /* print this or script hangs with some servers */
  1532.  
  1533. print ''
  1534. print 'Creating sp_MSuser_list'
  1535. print ''
  1536. go
  1537. create procedure sp_MSuser_list
  1538. as
  1539.  
  1540. create table #spusers       /* Create temporary table for storage. */
  1541. (
  1542.  login_id varchar(30) null,
  1543.  user_name varchar(30) null,
  1544.  alias_name varchar(30) null,
  1545.  group_name varchar(30) null
  1546. )
  1547.  
  1548. set nocount on
  1549.  
  1550. /* */
  1551. /* Fill the temp table with system and user names */
  1552. /* */
  1553.  
  1554. insert into #spusers
  1555. select suser_name(suid), user_name(uid), '', '' from sysusers
  1556. where uid < 16383 and uid > 0
  1557.  
  1558. /* */
  1559. /* Get the aliases first */
  1560. /* */
  1561.  
  1562. insert into #spusers
  1563. select suser_name(sysalternates.suid),'', sysusers.name, ''
  1564. from sysusers, sysalternates, #spusers
  1565. where sysalternates.altsuid = suser_id(login_id)
  1566. and sysusers.suid = suser_id(login_id)
  1567.  
  1568. /* */
  1569. /* Get the group names */
  1570. /* */
  1571.  
  1572. update #spusers set group_name = g.name from sysusers u, sysusers g, #spusers
  1573. where u.suid = suser_id(#spusers.login_id) and
  1574.       u.uid = user_id(#spusers.user_name) and
  1575.       u.gid *= g.uid and
  1576.       u.uid <= 16383 and u.uid > 0
  1577.  
  1578. set nocount off
  1579.  
  1580. select * from #spusers
  1581. go
  1582.  
  1583. /****************************************************************************/
  1584. /* This stored procedure will get the needed values for the statistics
  1585. ** monitoring dialog.  This also updates the system monitor in the same
  1586. ** fashion as sp_monitor, thereby mimicing what sp_monitor does.
  1587. */
  1588. /****************************************************************************/
  1589.  
  1590. print ''
  1591. print 'Creating sp_MSmonitor'
  1592. print ''
  1593. go
  1594. create procedure sp_MSmonitor
  1595. as
  1596.  
  1597. set nocount on
  1598.  
  1599. /*
  1600. **  Declare variables to be used to hold current monitor values.
  1601. */
  1602. declare @now         datetime
  1603. declare @cpu_busy     int
  1604. declare @io_busy    int
  1605. declare @idle        int
  1606. declare @pack_received    int
  1607. declare @pack_sent    int
  1608. declare @pack_errors    int
  1609. declare @connections    int
  1610. declare @total_read    int
  1611. declare @total_write    int
  1612. declare @total_errors    int
  1613.  
  1614. declare @oldcpu_busy     int    /* used to see if SQL Server has been rebooted */
  1615. declare @interval    int
  1616. declare @mspertick    int    /* milliseconds per tick */
  1617.  
  1618. /*
  1619. **  Set @mspertick.  This is just used to make the numbers easier to handle
  1620. **  and avoid overflow.
  1621. */
  1622. select @mspertick = convert(int, @@timeticks / 1000.0)
  1623.  
  1624. /*
  1625. **  Get current monitor values.
  1626. */
  1627. begin transaction
  1628. select
  1629.     @now = getdate(),
  1630.     @cpu_busy = @@cpu_busy,
  1631.     @io_busy = @@io_busy,
  1632.     @idle = @@idle,
  1633.     @pack_received = @@pack_received,
  1634.     @pack_sent = @@pack_sent,
  1635.     @connections = @@connections,
  1636.     @pack_errors = @@packet_errors,
  1637.     @total_read = @@total_read,
  1638.     @total_write = @@total_write,
  1639.     @total_errors = @@total_errors
  1640.  
  1641. /*
  1642. **  Check to see if SQL Server has been rebooted.  If it has then the
  1643. **  value of @@cpu_busy will be less than the value of master..spt_monitor.cpu_busy.
  1644. **  If it has update master..spt_monitor.
  1645. */
  1646. select @oldcpu_busy = cpu_busy
  1647.     from master..spt_monitor
  1648. if @oldcpu_busy > @cpu_busy
  1649. begin
  1650.     update master..spt_monitor
  1651.         set
  1652.             lastrun = @now,
  1653.             cpu_busy = @cpu_busy,
  1654.             io_busy = @io_busy,
  1655.             idle = @idle,
  1656.             pack_received = @pack_received,
  1657.             pack_sent = @pack_sent,
  1658.             connections = @connections,
  1659.             pack_errors = @pack_errors,
  1660.             total_read = @total_read,
  1661.             total_write = @total_write,
  1662.             total_errors = @total_errors
  1663. end
  1664.  
  1665. /*
  1666. **  Now print out old and new monitor values.
  1667. */
  1668. select @interval = datediff(ss, lastrun, @now) from master..spt_monitor s
  1669.  
  1670. select last_run = convert(char(30),lastrun),seconds = convert(char(30),@interval)
  1671.     from master..spt_monitor s
  1672.  
  1673. select
  1674.     CpuBusy = convert(varchar(30),'CPU Busy (ms)') + ','
  1675.                 + convert(varchar(18),((@cpu_busy * @mspertick) / 1000)) + ','
  1676.         + convert(varchar(18),((@cpu_busy - cpu_busy)* @mspertick) / 1000) + ','
  1677.         + convert(varchar(18), ((((@cpu_busy - cpu_busy)* @mspertick) / 1000) * 100) / @interval),
  1678.     IOBusy = convert(varchar(30),'IO Busy (ms)') + ','
  1679.                 + convert(varchar(18),((@io_busy * @mspertick) / 1000)) + ','
  1680.         + convert(varchar(18),(((@io_busy - io_busy)* @mspertick) / 1000)) + ','
  1681.         + convert(varchar(18), ((((@io_busy - io_busy)* @mspertick) / 1000) * 100) / @interval),
  1682.     Idle =   convert(varchar(30),'Idle Time (ms)') + ','
  1683.                 + convert(varchar(18),((@idle * @mspertick) / 1000)) + ','
  1684.         + convert(varchar(18),(((@idle - idle)* @mspertick) / 1000)) + ','
  1685.         + convert(varchar(18),((((@idle - idle)* @mspertick) / 1000) * 100) / @interval),
  1686.     PacketsReceived = convert(varchar(30),'Packets Received') + ','
  1687.                 + convert(varchar(18), @pack_received) + ','
  1688.         + convert(varchar(18), @pack_received - pack_received),
  1689.         PacketsSent = convert(varchar(30),'Packets Sent') + ','
  1690.         + convert(varchar(18), @pack_sent) + ','
  1691.                 + convert(varchar(18), @pack_sent - pack_sent),
  1692.     PacketErrors = convert(varchar(30),'Packet Errors') + ','
  1693.                 + convert(varchar(18), @pack_errors) + ','
  1694.                 + convert(varchar(18), @pack_errors - pack_errors),
  1695.     TotalRead = convert(varchar(30),'Total Read') + ','
  1696.                 + convert(varchar(18), @total_read) + ','
  1697.         + convert(varchar(18), @total_read - total_read),
  1698.         TotalWrite = convert(varchar(30),'Total Write') + ','
  1699.                 + convert(varchar(18), @total_write) + ','
  1700.         + convert(varchar(18), @total_write - total_write),
  1701.     TotalErrors = convert(varchar(30),'Total Errors') + ','
  1702.                 + convert(varchar(18), @total_errors) + ','
  1703.         + convert(varchar(18), @total_errors - total_errors),
  1704.     Connections = convert(varchar(30),'Connections') + ','
  1705.                 + convert(varchar(18), @connections) + ','
  1706.         + convert(varchar(18), @connections - connections)
  1707.        from master..spt_monitor
  1708.  
  1709. /*
  1710. **  Now update master..spt_monitor
  1711. */
  1712. update master..spt_monitor
  1713.     set
  1714.         lastrun = @now,
  1715.         cpu_busy = @cpu_busy,
  1716.         io_busy = @io_busy,
  1717.         idle = @idle,
  1718.         pack_received = @pack_received,
  1719.         pack_sent = @pack_sent,
  1720.         connections = @connections,
  1721.         pack_errors = @pack_errors,
  1722.         total_read = @total_read,
  1723.         total_write = @total_write,
  1724.         total_errors = @total_errors
  1725.  
  1726. commit transaction
  1727. set nocount off
  1728. return
  1729. go
  1730.  
  1731. /************* DUMP THE TRANSACTION LOG **************************************/
  1732. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1733. /* script periodically, you will run out of transaction log space.           */
  1734. dump tran master with truncate_only
  1735. go
  1736. /************* END DUMP THE TRANSACTION LOG **********************************/
  1737.  
  1738.  
  1739. /*****************************************************************************/
  1740. /* This stored procedure gets all the users that have particular permissions */
  1741. /*****************************************************************************/
  1742.  
  1743. print ''
  1744. print 'Creating sp_MScmd_permissions'
  1745. print ''
  1746. go
  1747. create procedure sp_MScmd_permissions
  1748. @cre_dflt bit, @cre_proc bit, @cre_rule bit, @cre_table bit, @cre_db bit,
  1749. @cre_view bit, @dmp_db bit, @dmp_tran bit
  1750. as
  1751.  
  1752. set nocount on
  1753.  
  1754. create table #tmp_permiss
  1755. (name     varchar(30),
  1756.  dflt     bit,
  1757.  creproc bit,
  1758.  crerule bit,
  1759.  cretble bit,
  1760.  credb     bit,
  1761.  creview bit,
  1762.  dmpdb     bit,
  1763.  dmptran bit
  1764. )
  1765.  
  1766. insert into #tmp_permiss
  1767. select user_name(uid),0,0,0,0,0,0,0,0 from sysusers
  1768.  
  1769. update #tmp_permiss set dflt = 1 from sysprotects where
  1770. name = user_name(uid) and
  1771. protecttype = (select number from master..spt_values where name = 'Grant')
  1772. and action = (select number from master..spt_values where name = 'Create Default')
  1773.  
  1774. update #tmp_permiss set creproc = 1 from sysprotects where
  1775. name = user_name(uid) and
  1776. protecttype = (select number from master..spt_values where name = 'Grant')
  1777. and action = (select number from master..spt_values where name = 'Create Procedure')
  1778.  
  1779. update #tmp_permiss set crerule = 1 from sysprotects where
  1780. name = user_name(uid) and
  1781. protecttype = (select number from master..spt_values where name = 'Grant')
  1782. and action = (select number from master..spt_values where name = 'Create Rule')
  1783.  
  1784. update #tmp_permiss set cretble = 1 from sysprotects where
  1785. name = user_name(uid) and
  1786. protecttype = (select number from master..spt_values where name = 'Grant')
  1787. and action = (select number from master..spt_values where name = 'Create Table')
  1788.  
  1789. update #tmp_permiss set credb = 1 from sysprotects where
  1790. name = user_name(uid) and
  1791. protecttype = (select number from master..spt_values where name = 'Grant')
  1792. and action = (select number from master..spt_values where name = 'Create Database')
  1793.  
  1794. update #tmp_permiss set creview = 1 from sysprotects where
  1795. name = user_name(uid) and
  1796. protecttype = (select number from master..spt_values where name = 'Grant')
  1797. and action = (select number from master..spt_values where name = 'Create View')
  1798.  
  1799. update #tmp_permiss set dmpdb = 1 from sysprotects where
  1800. name = user_name(uid) and
  1801. protecttype = (select number from master..spt_values where name = 'Grant')
  1802. and action = (select number from master..spt_values where name = 'Dump Database')
  1803.  
  1804. update #tmp_permiss set dmptran = 1 from sysprotects where
  1805. name = user_name(uid) and
  1806. protecttype = (select number from master..spt_values where name = 'Grant')
  1807. and action = (select number from master..spt_values where name = 'Dump Transaction')
  1808.  
  1809. set nocount off
  1810.  
  1811. select name from #tmp_permiss
  1812. where dflt = @cre_dflt and creproc = @cre_proc and crerule = @cre_rule
  1813. and cretble = @cre_table and credb = @cre_db and creview = @cre_view
  1814. and dmpdb = @dmp_db and dmptran = @dmp_tran
  1815. go
  1816.  
  1817. /*****************************************************************************/
  1818. /* This stored procedure gets all the users that have particular permissions */
  1819. /*****************************************************************************/
  1820.  
  1821. print ''
  1822. print 'Creating sp_MSuser_cmd_permissions'
  1823. print ''
  1824. go
  1825. create procedure sp_MSuser_cmd_permissions @username varchar(30)
  1826. as
  1827.  
  1828. /*  TCD 8/2/91 See if the user exists. */
  1829.  
  1830. if not exists (select * from dbo.sysusers
  1831.              where name = @username)
  1832. begin
  1833.     raiserror 70003 'User doesn''t exist. The ''sa'' or ''dbo'' must have dropped the user since you last displayed the listing.'
  1834.     return
  1835. end
  1836.  
  1837. set nocount on
  1838.  
  1839. create table #tmp_permiss
  1840. (dflt     bit,
  1841.  creproc bit,
  1842.  crerule bit,
  1843.  cretble bit,
  1844.  credb     bit,
  1845.  creview bit,
  1846.  dmpdb     bit,
  1847.  dmptran bit
  1848. )
  1849.  
  1850. if user_id(@username) = 1     /* dbo has all permissions */
  1851.   insert into #tmp_permiss
  1852.   values (1,1,1,1,1,1,1,1)
  1853. else
  1854. begin
  1855.   insert into #tmp_permiss
  1856.   values (0,0,0,0,0,0,0,0)
  1857.  
  1858.   update #tmp_permiss set dflt = 1 from sysprotects where
  1859.   user_name(uid) = @username and
  1860.   protecttype = (select number from master..spt_values where name = 'Grant')
  1861.   and action = (select number from master..spt_values where name = 'Create Default')
  1862.  
  1863.   update #tmp_permiss set creproc = 1 from sysprotects where
  1864.   user_name(uid) = @username and
  1865.   protecttype = (select number from master..spt_values where name = 'Grant')
  1866.   and action = (select number from master..spt_values where name = 'Create Procedure')
  1867.  
  1868.   update #tmp_permiss set crerule = 1 from sysprotects where
  1869.   user_name(uid) = @username and
  1870.   protecttype = (select number from master..spt_values where name = 'Grant')
  1871.   and action = (select number from master..spt_values where name = 'Create Rule')
  1872.  
  1873.   update #tmp_permiss set cretble = 1 from sysprotects where
  1874.   user_name(uid) = @username and
  1875.   protecttype = (select number from master..spt_values where name = 'Grant')
  1876.   and action = (select number from master..spt_values where name = 'Create Table')
  1877.  
  1878.   update #tmp_permiss set credb = 1 from sysprotects where
  1879.   user_name(uid) = @username and
  1880.   protecttype = (select number from master..spt_values where name = 'Grant')
  1881.   and action = (select number from master..spt_values where name = 'Create Database')
  1882.  
  1883.   update #tmp_permiss set creview = 1 from sysprotects where
  1884.   user_name(uid) = @username and
  1885.   protecttype = (select number from master..spt_values where name = 'Grant')
  1886.   and action = (select number from master..spt_values where name = 'Create View')
  1887.  
  1888.   update #tmp_permiss set dmpdb = 1 from sysprotects where
  1889.   user_name(uid) = @username and
  1890.   protecttype = (select number from master..spt_values where name = 'Grant')
  1891.   and action = (select number from master..spt_values where name = 'Dump Database')
  1892.  
  1893.   update #tmp_permiss set dmptran = 1 from sysprotects where
  1894.   user_name(uid) = @username and
  1895.   protecttype = (select number from master..spt_values where name = 'Grant')
  1896.   and action = (select number from master..spt_values where name = 'Dump Transaction')
  1897. end
  1898.  
  1899. set nocount off
  1900.  
  1901. select * from #tmp_permiss
  1902. go
  1903.  
  1904.  
  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 will get the name, device, size, and location of
  1914.    each segment for a particular database. You must be in the db to exec    */
  1915. /****************************************************************************/
  1916.  
  1917. print ''
  1918. print 'Creating sp_MSsegment_list'
  1919. print ''
  1920. go
  1921. create procedure sp_MSsegment_list as
  1922.  
  1923. declare @segbit int
  1924.  
  1925. /* Create a temporary table where we can store all the necessary information */
  1926.  
  1927. set nocount on
  1928.  
  1929. create table #spsegtab1
  1930. (name varchar(30) null,
  1931.  segnumber smallint,
  1932.  segbit int null
  1933. )
  1934.  
  1935. create table #spsegtab2
  1936. (
  1937.  name varchar(30) null,
  1938.  device varchar(30) null,
  1939. )
  1940.  
  1941. /* Get all the segments in the system and their status */
  1942. /* Set the segments on @devname master.dbo.sysusages. */
  1943. /*    If segment 31, power(2, 31) will overflow
  1944.       since segmap is an int.  We'll grab the machine-dependent
  1945.       bit mask from master.dbo.spt_values to set the right bit. */
  1946.  
  1947. insert into #spsegtab1 (name, segnumber) select name, segment from syssegments
  1948. update #spsegtab1 set segbit = power(2, segnumber) where segnumber < 31
  1949. update #spsegtab1 set segbit = low from master.dbo.spt_values
  1950.           where type = 'E' and number = 2
  1951.             and segnumber >= 31
  1952.  
  1953.  
  1954. /* Populate the #spsegtab2 with the devices for each segment */
  1955.  
  1956.  
  1957. insert into #spsegtab2
  1958. select distinct #spsegtab1.name, d.name
  1959. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v, #spsegtab1
  1960.         where u.segmap & #spsegtab1.segbit = #spsegtab1.segbit
  1961.             and d.low <= u.size + u.vstart
  1962.             and d.high >= u.size + u.vstart - 1
  1963.             and u.dbid = db_id()
  1964.             and d.status & 2 = 2
  1965.             and v.number = 1
  1966.             and v.type = 'E'
  1967.  
  1968.  
  1969. set nocount off
  1970.  
  1971. select * from #spsegtab2
  1972. order by name
  1973. go
  1974. /****************************************************************************/
  1975. /* This stored procedure will return a list of the devices that contain
  1976.    a segment of a given name.                            */
  1977. /****************************************************************************/
  1978.  
  1979. print ''
  1980. print 'Creating sp_MSsegment_devices'
  1981. print ''
  1982. go
  1983. create procedure sp_MSsegment_devices @segname varchar(30)        /* segment name */
  1984. as
  1985. declare    @segbit    int        /* this is the bit version of the segment # */
  1986. declare    @segment    int    /* the segment number of the segment */
  1987. set nocount on
  1988. /*
  1989. **  Set the bit position for the segment.
  1990. */
  1991. select @segment = segment
  1992.     from syssegments
  1993.         where name = @segname
  1994.  
  1995. /*
  1996. **  Now set the segments on @devname master.dbo.sysusages.
  1997. */
  1998. if (@segment < 31)
  1999.     select @segbit = power(2, @segment)
  2000. else
  2001.     /*
  2002.     **  Since this is segment 31, power(2, 31) will overflow
  2003.     **  since segmap is an int.  We'll grab the machine-dependent
  2004.     **  bit mask from master.dbo.spt_values to set the right bit.
  2005.     */
  2006.     select @segbit = low
  2007.         from master.dbo.spt_values
  2008.             where type = 'E'
  2009.                 and number = 2
  2010.  
  2011. select distinct device = d.name
  2012.     from master.dbo.sysusages u, master.dbo.sysdevices d,
  2013.         master.dbo.spt_values v
  2014.         where u.segmap & @segbit = @segbit
  2015.             and d.low <= u.size + u.vstart
  2016.             and d.high >= u.size + u.vstart - 1
  2017.             and u.dbid = db_id()
  2018.             and d.status & 2 = 2
  2019.             and v.number = 1
  2020.             and v.type = 'E'
  2021. set nocount off
  2022. go
  2023.  
  2024. /****************************************************************************/
  2025. /* This stored procedure will get the properties of a particular segment in
  2026.    in the system                                */
  2027. /****************************************************************************/
  2028.  
  2029. print ''
  2030. print 'Creating sp_MSseg_properties'
  2031. print ''
  2032. go
  2033. create procedure sp_MSseg_properties @segname varchar(30) as
  2034.  
  2035. declare @segbit int, @segsize varchar(10), @segment int, @segdevs varchar(255)
  2036.  
  2037. /*  TCD 8/2/91 See if the segment exists. */
  2038.  
  2039. if not exists (select * from syssegments
  2040.              where name = @segname)
  2041. begin
  2042.     raiserror 70005 'Segment doesn''t exist. The ''sa'' or ''dbo'' must have dropped the segment since you last displayed the listing.'
  2043.     return
  2044. end
  2045.  
  2046. set nocount on
  2047.  
  2048. /* Set the bit position for the segment */
  2049.  
  2050. select @segment = segment from syssegments where name = @segname
  2051. if (@segment < 31)
  2052.     select @segbit = power(2, @segment)
  2053. else
  2054.  
  2055.     /*  Since this is segment 31, power(2, 31) will overflow
  2056.     **  since segmap is an int.  We'll grab the machine-dependent
  2057.     **  bit mask from master.dbo.spt_values to set the right bit. */
  2058.  
  2059.      select @segbit = low from master.dbo.spt_values where type = 'E' and number = 2
  2060.  
  2061. /* Get the total size of the segment */
  2062.  
  2063. select @segsize = convert(varchar(20), sum(round((u.size * convert(float, v.low)) / 1048576, 0))) + 'MB'
  2064. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v
  2065. where u.segmap & @segbit = @segbit
  2066.       and d.low <= u.size + u.vstart
  2067.       and d.high >= u.size + u.vstart - 1
  2068.       and u.dbid = db_id()
  2069.       and d.status & 2 = 2
  2070.       and v.number = 1
  2071.       and v.type = 'E'
  2072.  
  2073. set nocount off
  2074.  
  2075. /* output the segment number and the segment size */
  2076.  
  2077. select segment_number = @segment, segment_size = @segsize
  2078.  
  2079. /* output the device names */
  2080.  
  2081. select distinct d.name
  2082. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v
  2083. where u.segmap & @segbit = @segbit
  2084.       and d.low <= u.size + u.vstart
  2085.       and d.high >= u.size + u.vstart - 1
  2086.       and u.dbid = db_id()
  2087.       and d.status & 2 = 2
  2088.       and v.number = 1
  2089.       and v.type = 'E'
  2090.  
  2091. go
  2092.  
  2093. /****************************************************************************/
  2094. /* This stored procedure will get the tables/indexes for a particular
  2095.    segment in the system                            */
  2096. /****************************************************************************/
  2097.  
  2098. print ''
  2099. print 'Creating sp_MSseg_tables'
  2100. print ''
  2101. go
  2102. create procedure sp_MSseg_tables @segname varchar(30) as
  2103. select table_name = object_name(i.id), index_name = i.name, i.indid
  2104. from sysindexes i, syssegments s
  2105. where s.name = @segname and s.segment = i.segment
  2106. order by table_name, indid
  2107. go
  2108. /************* DUMP THE TRANSACTION LOG **************************************/
  2109. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2110. /* script periodically, you will run out of transaction log space.           */
  2111. dump tran master with truncate_only
  2112. go
  2113. /************* END DUMP THE TRANSACTION LOG **********************************/
  2114.  
  2115. /*****************************************************************************/
  2116. /* This stored procedure gets all the options set for a particular database  */
  2117. /* It applies to the current database.                         */
  2118. /* It is generally called by sp_MSdb_properties                  */
  2119. /*****************************************************************************/
  2120.  
  2121.  
  2122. print ''
  2123. print 'Creating sp_MSdb_options_bits'
  2124. print ''
  2125. go
  2126. create procedure sp_MSdb_options_bits as
  2127. declare @bitdesc varchar(255), @curdbid int, @allopts int
  2128. select @curdbid = db_id()
  2129. select @allopts = 7196
  2130.  
  2131. set nocount on
  2132.  
  2133. create table #spdbbits
  2134. (allopts bit,
  2135.  dbuse bit,
  2136.  chkpt bit,
  2137.  readonly bit,
  2138.  bulkload bit,
  2139.  single bit,
  2140.  trunc bit)
  2141.  
  2142. insert into #spdbbits values(0,0,0,0,0,0,0)
  2143.  
  2144. /*  Check all settable options (7196) */
  2145.  
  2146. select @bitdesc = null
  2147. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2148.        where d.dbid = @curdbid and v.type = 'D'
  2149.        and d.status & v.number = 7196 and v.number = @allopts        /* all options */
  2150.  
  2151. if @bitdesc != null
  2152. begin
  2153.   update #spdbbits set allopts = 1
  2154. end
  2155.  
  2156. /* Check select into/bulk copy bit (4)    */
  2157.  
  2158. select @bitdesc = null
  2159. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2160.        where d.dbid = @curdbid and v.type = 'D'
  2161.        and d.status & v.number = 4 and v.number != @allopts      /* all options */
  2162.  
  2163. if @bitdesc != null
  2164. begin
  2165.    update #spdbbits set bulkload = 1
  2166. end
  2167.  
  2168. /*  Check no checkpoint on recovery bit (16)  */
  2169.  
  2170. select @bitdesc = null
  2171. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2172.        where d.dbid = @curdbid and v.type = 'D'
  2173.        and d.status & v.number = 16 and v.number != @allopts /* all options */
  2174.  
  2175. if @bitdesc != null
  2176. begin
  2177.   update #spdbbits set chkpt = 1
  2178. end
  2179.  
  2180. /* Check single user bit (4096) */
  2181.  
  2182. select @bitdesc = null
  2183. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2184.        where d.dbid = @curdbid and v.type = 'D'
  2185.        and d.status & v.number = 4096 and v.number != @allopts         /* all options */
  2186.  
  2187. if @bitdesc != null
  2188. begin
  2189.   update #spdbbits set single = 1
  2190. end
  2191.  
  2192. /*  Check dbo only bit (2048) */
  2193.  
  2194. select @bitdesc = null
  2195. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2196.        where d.dbid = @curdbid and v.type = 'D'
  2197.        and d.status & v.number = 2048 and v.number != @allopts         /* all options */
  2198.  
  2199. if @bitdesc != null
  2200. begin
  2201.   update #spdbbits set dbuse = 1
  2202. end
  2203.  
  2204. /*  Check read only bit (1024) */
  2205.  
  2206. select @bitdesc = null
  2207. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2208.        where d.dbid = @curdbid and v.type = 'D'
  2209.        and d.status & v.number = 1024 and v.number != @allopts         /* all options */
  2210.  
  2211. if @bitdesc != null
  2212. begin
  2213.   update #spdbbits set readonly = 1
  2214. end
  2215.  
  2216. /*  Check truncate log on checkpoint bit (8) */
  2217.  
  2218. select @bitdesc = null
  2219. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2220.        where d.dbid = @curdbid and v.type = 'D'
  2221.        and d.status & v.number = 8 and v.number != @allopts      /* all options */
  2222.  
  2223. if @bitdesc != null
  2224. begin
  2225.   update #spdbbits set trunc = 1
  2226. end
  2227.  
  2228. set nocount off
  2229.  
  2230. select * from #spdbbits
  2231. go
  2232.  
  2233.  
  2234. /*****************************************************************************/
  2235. /* This procedure gets the devices that a database resides on for segment use */
  2236. /*****************************************************************************/
  2237.  
  2238. print ''
  2239. print 'Creating sp_MSdb_devices_segments'
  2240. print ''
  2241. go
  2242. create procedure sp_MSdb_devices_segments @dbname varchar(30) as
  2243.  
  2244. /*  TCD 8/2/91 See if the database exists. */
  2245.  
  2246. if not exists (select * from master.dbo.sysdatabases
  2247.              where name = @dbname)
  2248. begin
  2249.     raiserror 70006 'Database doesn''t exist. The ''sa'' or ''dbo'' must have dropped the database since you last displayed the listing.'
  2250.     return
  2251. end
  2252.  
  2253. select distinct device = master.dbo.sysdevices.name
  2254. from master.dbo.sysdatabases, master.dbo.sysusages, master.dbo.sysdevices, master.dbo.spt_values a, master.dbo.spt_values b
  2255. where master.dbo.sysdatabases.dbid = master.dbo.sysusages.dbid
  2256.         and master.dbo.sysdevices.low <= size + vstart
  2257.         and master.dbo.sysdevices.high >= size + vstart - 1
  2258.         and master.dbo.sysdevices.status & 2 = 2
  2259.         and master.dbo.sysdatabases.name = @dbname
  2260.         and a.type = 'E' and a.number = 1 and b.type = 'S'
  2261.         and master.dbo.sysusages.segmap & 7 = b.number
  2262.         order by 1
  2263. go
  2264.  
  2265. /************* DUMP THE TRANSACTION LOG **************************************/
  2266. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2267. /* script periodically, you will run out of transaction log space.           */
  2268. dump tran master with truncate_only
  2269. go
  2270. /************* END DUMP THE TRANSACTION LOG **********************************/
  2271.  
  2272.  
  2273. /*****************************************************************************/
  2274. /* This procedure gets the space used by a particular database.          */
  2275. /* You must be in the database to access this information             */
  2276. /*****************************************************************************/
  2277.  
  2278.  
  2279. print ''
  2280. print 'Creating sp_MSdb_space'
  2281. print ''
  2282. go
  2283. create procedure sp_MSdb_space as
  2284. declare @options varchar(255)
  2285.  
  2286.  
  2287. declare @pages    float                  /* working variable for size calc. */
  2288. declare @dbsize float               /* Size of the database */
  2289.  
  2290.  
  2291. set nocount on
  2292.  
  2293. create table #spt_space
  2294. (
  2295.  db_size     float null,
  2296.  rows         int null,
  2297.  reserved     float null,
  2298.  data         float null,
  2299.  indexp      float null,
  2300.  unused      float null,
  2301.  log_size     float null,
  2302.  log_used     float null
  2303. )
  2304.  
  2305. /* Get the total size of the database */
  2306.  
  2307. declare @pagesize int
  2308. select @pagesize = low from master..spt_values where number = 1 and type = 'E'
  2309. insert into #spt_space (db_size)
  2310. select distinct (sum(convert(float,size)) * (convert(float,@pagesize)) / 1048576.0)
  2311. from master.dbo.sysusages
  2312. where dbid = db_id()
  2313.  
  2314. /* */
  2315. /*  Now calculate the summary data.
  2316.     reserved: sum(reserved) where indid in (0, 1, 255) */
  2317. /* */
  2318. update #spt_space
  2319. set reserved = (select sum(convert(float,sysindexes.reserved)) from sysindexes where indid in (0, 1, 255))
  2320.  
  2321. /* */
  2322. /* data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text) */
  2323. /* */
  2324. select @pages = sum(convert(float,dpages)) from sysindexes
  2325. where indid < 2
  2326.  
  2327. select @pages = @pages + isnull(sum(convert(float,used)), 0.0) from sysindexes
  2328. where indid = 255
  2329.  
  2330. update #spt_space set data = @pages
  2331.  
  2332.     
  2333. /* index: sum(used) where indid in (0, 1, 255) - data */
  2334.  
  2335. update #spt_space
  2336. set indexp = ((select (sum(convert(float,used))) from sysindexes where indid in (0, 1, 255)) - data)
  2337.  
  2338. /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  2339. update #spt_space
  2340. set unused = reserved - (select sum(convert(float,used)) from sysindexes where indid in (0, 1, 255))
  2341.  
  2342. /* log_size: sum(reserved) where indid in (0, 1, 255) and object is 'syslogs' */
  2343.  
  2344. update #spt_space
  2345. set log_size = (select sum(convert(float,sysindexes.reserved)) from sysindexes where indid in (0, 1, 255) and id = object_id('syslogs'))
  2346.  
  2347. /* log_used: sum(used) where indid in (0, 1, 255) and id = object_id('syslogs') */
  2348.  
  2349. update #spt_space set log_used = (select sum(convert(float,used))
  2350. from sysindexes
  2351. where indid in (0, 1, 255) and id = object_id('syslogs'))
  2352.  
  2353. set nocount off
  2354.  
  2355. select reserved = substring((convert(varchar(11), (convert(int,(reserved * (convert(float,d.low) ) / 1024.0)))) + ' KB'),1, 15),
  2356.        data = substring((convert(varchar(11),convert(int,(data * convert(float,d.low)) / 1024.0)) + ' KB'), 1, 15),
  2357.        log_space = substring ((convert(varchar(11), (round((log_size * convert(float, d.low)) / 1024.0, 0))) + ' KB'), 1, 15),
  2358.        index_size = substring((convert(varchar(11), convert(int,(indexp * convert(float,d.low)) / 1024.0)) + ' KB'), 1, 15),
  2359.        unused_reserve = substring((convert(varchar(11),convert(int,(unused * convert(float,d.low)) / 1024.0)) + ' KB'), 1, 15),
  2360.        unused_log = substring ((convert(varchar(11), (round(((log_size - log_used) * convert(float, d.low)) / 1024.0, 0))) + ' KB'), 1, 15)
  2361.        from #spt_space, master.dbo.spt_values d
  2362.        where d.number = 1 and d.type = 'E'
  2363. go
  2364.  
  2365.  
  2366. /*****************************************************************************/
  2367. /* This stored procedure gets all the options set for a particular database  */
  2368. /* It applies to the current database.                         */
  2369. /* It is generally called by sp_MSdb_properties                  */
  2370. /*****************************************************************************/
  2371.  
  2372.  
  2373. print ''
  2374. print 'Creating sp_MSdb_options'
  2375. print ''
  2376. go
  2377. create procedure sp_MSdb_options as
  2378. declare @options varchar(255), @bitdesc varchar(255), @curdbid int, @allopts int
  2379. select @curdbid = db_id()
  2380. select @allopts = 7196
  2381.  
  2382. set nocount on
  2383.  
  2384. select @options = ''
  2385.  
  2386. /* Check select into/bulk copy bit (4)    */
  2387.  
  2388. select @bitdesc = null
  2389. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2390.        where d.dbid = @curdbid and v.type = 'D'
  2391.        and d.status & v.number = 4 and v.number != @allopts      /* all options */
  2392.  
  2393. if @bitdesc != null
  2394. begin
  2395.    if @options != ''
  2396.      select @options = @options + ', ' +  @bitdesc
  2397.    else select @options = @bitdesc
  2398. end
  2399.  
  2400. /*  Check no checkpoint on recovery bit (16)  */
  2401.  
  2402. select @bitdesc = null
  2403. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2404.        where d.dbid = @curdbid and v.type = 'D'
  2405.        and d.status & v.number = 16 and v.number != @allopts /* all options */
  2406.  
  2407. if @bitdesc != null
  2408. begin
  2409.   if @options != ''
  2410.      select @options = @options + ', ' +  @bitdesc
  2411.   else select @options = @bitdesc
  2412. end
  2413.  
  2414. /* Check single user bit (4096) */
  2415.  
  2416. select @bitdesc = null
  2417. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2418.        where d.dbid = @curdbid and v.type = 'D'
  2419.        and d.status & v.number = 4096 and v.number != @allopts         /* all options */
  2420.  
  2421. if @bitdesc != null
  2422. begin
  2423.   if @options != ''
  2424.      select @options = @options + ', ' +  @bitdesc
  2425.   else select @options = @bitdesc
  2426. end
  2427.  
  2428. /*  Check dbo only bit (2048) */
  2429.  
  2430. select @bitdesc = null
  2431. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2432.        where d.dbid = @curdbid and v.type = 'D'
  2433.        and d.status & v.number = 2048 and v.number != @allopts         /* all options */
  2434.  
  2435. if @bitdesc != null
  2436. begin
  2437.   if @options != ''
  2438.      select @options = @options + ', ' +  @bitdesc
  2439.   else select @options = @bitdesc
  2440. end
  2441.  
  2442. /*  Check read only bit (1024) */
  2443.  
  2444. select @bitdesc = null
  2445. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2446.        where d.dbid = @curdbid and v.type = 'D'
  2447.        and d.status & v.number = 1024 and v.number != @allopts         /* all options */
  2448.  
  2449. if @bitdesc != null
  2450. begin
  2451.   if @options != ''
  2452.      select @options = @options + ', ' +  @bitdesc
  2453.   else select @options = @bitdesc
  2454. end
  2455.  
  2456. /*  Check load only bit (512) */
  2457.  
  2458. select @bitdesc = null
  2459. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2460.        where d.dbid = @curdbid and v.type = 'D'
  2461.        and d.status & v.number = 512 and v.number != @allopts        /* all options */
  2462.  
  2463. if @bitdesc != null
  2464. begin
  2465.   if @options != ''
  2466.      select @options = @options + ', ' +  @bitdesc
  2467.   else select @options = @bitdesc
  2468. end
  2469.  
  2470. /* Check not recovered only bit (256)  */
  2471.  
  2472. select @bitdesc = null
  2473. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2474.        where d.dbid = @curdbid and v.type = 'D'
  2475.        and d.status & v.number = 256 and v.number != @allopts  /* all options */
  2476.  
  2477. if @bitdesc != null
  2478. begin
  2479.   if @options != ''
  2480.      select @options = @options + ', ' +  @bitdesc
  2481.   else select @options = @bitdesc
  2482. end
  2483.  
  2484. /* Check don't recover bit (32)  */
  2485.  
  2486. select @bitdesc = null
  2487. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2488.        where d.dbid = @curdbid and v.type = 'D'
  2489.        and d.status & v.number = 32 and v.number != @allopts       /* all options */
  2490.  
  2491. if @bitdesc != null
  2492. begin
  2493.   if @options != ''
  2494.      select @options = @options + ', ' +  @bitdesc
  2495.   else select @options = @bitdesc
  2496. end
  2497.  
  2498. /*  Check truncate log on checkpoint bit (8) */
  2499.  
  2500. select @bitdesc = null
  2501. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2502.        where d.dbid = @curdbid and v.type = 'D'
  2503.        and d.status & v.number = 8 and v.number != @allopts      /* all options */
  2504.  
  2505. if @bitdesc != null
  2506. begin
  2507.   if @options != ''
  2508.      select @options = @options + ', ' +  @bitdesc
  2509.   else select @options = @bitdesc
  2510. end
  2511.  
  2512. /* If no flags are set, say so. */
  2513.  
  2514. if @options = ''
  2515. begin
  2516.   select @options = 'no options set'
  2517. end
  2518.  
  2519. set nocount off
  2520.  
  2521. select @options
  2522. go
  2523.  
  2524. /************* DUMP THE TRANSACTION LOG **************************************/
  2525. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2526. /* script periodically, you will run out of transaction log space.           */
  2527. dump tran master with truncate_only
  2528. go
  2529. /************* END DUMP THE TRANSACTION LOG **********************************/
  2530.  
  2531. /*****************************************************************************/
  2532. /* This procedure gets the devices that a database resides on.             */
  2533. /*****************************************************************************/
  2534.  
  2535. print ''
  2536. print 'Creating sp_MSdb_devices'
  2537. print ''
  2538. go
  2539. create procedure sp_MSdb_devices @dbname varchar(30) as
  2540.  
  2541. select device = master.dbo.sysdevices.name,
  2542.        size = convert(varchar(10), round((a.low * convert(float, size))/ 1048576, 0)) + ' ' + 'MB',
  2543.        usage = b.name
  2544. from master.dbo.sysdatabases, master.dbo.sysusages, master.dbo.sysdevices, master.dbo.spt_values a, master.dbo.spt_values b
  2545. where master.dbo.sysdatabases.dbid = master.dbo.sysusages.dbid
  2546.       and master.dbo.sysdevices.low <= size + vstart
  2547.       and master.dbo.sysdevices.high >= size + vstart - 1
  2548.       and master.dbo.sysdevices.status & 2 = 2
  2549.       and master.dbo.sysdatabases.name = @dbname
  2550.       and a.type = 'E' and a.number = 1 and b.type = 'S'
  2551.       and master.dbo.sysusages.segmap & 7 = b.number
  2552.       order by 1
  2553. go
  2554.  
  2555. /*****************************************************************************/
  2556. /* These routines will get all the necessary data for the Database Properties */
  2557. /* dialog box.    You must be in the database to access this information         */
  2558. /*****************************************************************************/
  2559.  
  2560. print ''
  2561. print 'Creating sp_MSdb_properties'
  2562. print ''
  2563. go
  2564. create procedure sp_MSdb_properties as
  2565. declare @dbname varchar(255), @users int, @groups int
  2566.  
  2567. /* Get number of users and number of groups */
  2568.  
  2569. set nocount on
  2570. select @users = count(*) from sysusers where uid < 16383 and uid > 0
  2571. select @groups = count(*) from sysusers where uid >= 16383 or uid = 0
  2572. set nocount off
  2573.  
  2574. select Number_users = @users, Number_groups = @groups
  2575.  
  2576. /* Get the object counts and db options */
  2577.  
  2578. exec sp_MSdb_options
  2579.  
  2580. /* Get the space used by the database */
  2581.  
  2582. exec sp_MSdb_space
  2583. go
  2584.  
  2585. /*****************************************************************/
  2586. /* This stored procedure will calculate thread usage             */
  2587. /****************************************************************/
  2588.  
  2589.  
  2590. print ''
  2591. print 'Creating sp_MSthread_list'
  2592. print ''
  2593. go
  2594. create procedure sp_MSthread_list as
  2595.  
  2596. set nocount on
  2597.  
  2598. declare @devs int, @servs int, @total int, @avail_threads int,
  2599. @avail_servs int, @mirrors int, @outline varchar(50)
  2600.  
  2601. select @devs = count(*) from master.dbo.sysdevices where cntrltype = 0
  2602. select @mirrors = count(*) from master.dbo.sysdevices where status & 512 = 512
  2603. select @servs = 2 * count(*) from master.dbo.sysservers where srvid != 0
  2604. select @total = 4 + @devs + @mirrors+ @servs
  2605. select @avail_threads = 53-4-@devs-@mirrors-@servs
  2606. if @avail_threads % 2 = 1
  2607.     select @avail_servs = (@avail_threads - 1)/2
  2608. else
  2609.     select @avail_servs = @avail_threads/2
  2610.  
  2611.  
  2612. set nocount off
  2613. select '4 SQL Server Threads'
  2614. select @outline = convert(varchar(5), @devs+@mirrors)+ ' Database Device(s) (including mirrors)'
  2615. select @outline
  2616. select @outline = convert(varchar(5), @servs/2)+ ' Remote Server(s)'
  2617. select @outline
  2618. select @outline = convert(varchar(5),  @total) + ' Threads Currently Used'
  2619. select @outline
  2620. select @outline = 'You May Create Up To ' + convert(varchar(5), (53 - @total)) + ' More Devices'
  2621. select @outline
  2622. select @outline = 'Or You May Create Up To ' + convert(varchar(5), @avail_servs) + ' Remote Servers'
  2623. select @outline
  2624. go
  2625.  
  2626. /************* DUMP THE TRANSACTION LOG **************************************/
  2627. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2628. /* script periodically, you will run out of transaction log space.           */
  2629. dump tran master with truncate_only
  2630. go
  2631. /************* END DUMP THE TRANSACTION LOG **********************************/
  2632.  
  2633. /***************************************************************************/
  2634. /* This stored procedure is a copy of sp_helpdb, but returns the full      */
  2635. /* length of the database information columns.  In cases where the db name */
  2636. /* is at or near 30 characters, sp_helpdb would truncate the name.         */
  2637. /***************************************************************************/
  2638. print ''
  2639. print 'Creating sp_MShelpdb'
  2640. print ''
  2641. go
  2642.  
  2643. create procedure sp_MShelpdb
  2644. @dbname varchar(30) = NULL            /* database name to change */
  2645. as
  2646.  
  2647. declare @showdev    bit
  2648. declare @allopts    int
  2649.  
  2650. set nocount on
  2651.  
  2652. /*
  2653. **  If no database name given, get 'em all.
  2654. */
  2655. if @dbname = null
  2656.     select @dbname = '%', @showdev = 0
  2657. else select @showdev = 1
  2658.  
  2659. /*
  2660. **  Sure the database exists
  2661. */
  2662. if not exists (select *
  2663.     from master.dbo.sysdatabases
  2664.         where name like @dbname)
  2665. begin
  2666.     print 'No such database exists.'
  2667.     return (1)
  2668. end
  2669.  
  2670. /*
  2671. **  Set allopts to be the sum of all possible user-settable database status
  2672. **  bits.
  2673. */
  2674. select @allopts = 7196
  2675.  
  2676. /*
  2677. **  Since we examine the status bits in sysdatabase and turn them
  2678. **  into english, we need a temporary table to build the descriptions.
  2679. */
  2680. create table #spdbdesc
  2681. (
  2682.     dbid    smallint null,
  2683.     dbdesc    varchar(102) null
  2684. )
  2685.  
  2686. /*
  2687. **  Initialize #spdbdesc from master.dbo.sysdatabases
  2688. */
  2689. insert into #spdbdesc (dbid)
  2690.         select dbid
  2691.             from master.dbo.sysdatabases
  2692.                 where name like @dbname
  2693.  
  2694. /*
  2695. **  Now for each dbid in #spdbdesc, build the database status
  2696. **  description.
  2697. */
  2698. declare @curdbid smallint        /* the one we're currently working on */
  2699. declare @dbdesc varchar(102)        /* the total description for the db */
  2700. declare @bitdesc varchar(30)        /* the bit description for the db */
  2701.  
  2702. /*
  2703. **  Set @curdbid to the first dbid.
  2704. */
  2705. select @curdbid = min(dbid)
  2706.     from #spdbdesc
  2707. while @curdbid != NULL
  2708. begin
  2709.     /*
  2710.     **  Initialize @dbdesc.
  2711.     */
  2712.     select @dbdesc = ''
  2713.  
  2714.     /*
  2715.     **  Check select into/bulk copy bit (4)
  2716.     */
  2717.     select @bitdesc = null
  2718.     select @bitdesc = v.name
  2719.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2720.             where d.dbid = @curdbid
  2721.                 and v.type = 'D'
  2722.                 and d.status & v.number = 4
  2723.                 and v.number != @allopts       /* all options */
  2724.     if @bitdesc != null
  2725.     begin
  2726.         if @dbdesc != ''
  2727.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2728.         else select @dbdesc = @bitdesc
  2729.     end
  2730.  
  2731.     /*
  2732.     **  Check no checkpoint on recovery bit (16)
  2733.     */
  2734.     select @bitdesc = null
  2735.     select @bitdesc = v.name
  2736.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2737.             where d.dbid = @curdbid
  2738.                 and v.type = 'D'
  2739.                 and d.status & v.number = 16
  2740.                 and v.number != @allopts /* all options */
  2741.     if @bitdesc != null
  2742.     begin
  2743.         if @dbdesc != ''
  2744.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2745.         else select @dbdesc = @bitdesc
  2746.     end
  2747.  
  2748.     /*
  2749.     **  Check single user bit (4096)
  2750.     */
  2751.     select @bitdesc = null
  2752.     select @bitdesc = v.name
  2753.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2754.             where d.dbid = @curdbid
  2755.                 and v.type = 'D'
  2756.                 and d.status & v.number = 4096
  2757.                 and v.number != @allopts       /* all options */
  2758.     if @bitdesc != null
  2759.     begin
  2760.         if @dbdesc != ''
  2761.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2762.         else select @dbdesc = @bitdesc
  2763.     end
  2764.  
  2765.     /*
  2766.     **  Check dbo only bit (2048)
  2767.     */
  2768.     select @bitdesc = null
  2769.     select @bitdesc = v.name
  2770.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2771.             where d.dbid = @curdbid
  2772.                 and v.type = 'D'
  2773.                 and d.status & v.number = 2048
  2774.                 and v.number != @allopts       /* all options */
  2775.     if @bitdesc != null
  2776.     begin
  2777.         if @dbdesc != ''
  2778.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2779.         else select @dbdesc = @bitdesc
  2780.     end
  2781.  
  2782.     /*
  2783.     **  Check read only bit (1024)
  2784.     */
  2785.     select @bitdesc = null
  2786.     select @bitdesc = v.name
  2787.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2788.             where d.dbid = @curdbid
  2789.                 and v.type = 'D'
  2790.                 and d.status & v.number = 1024
  2791.                 and v.number != @allopts       /* all options */
  2792.     if @bitdesc != null
  2793.     begin
  2794.         if @dbdesc != ''
  2795.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2796.         else select @dbdesc = @bitdesc
  2797.     end
  2798.  
  2799.     /*
  2800.     **  Check load only bit (512)
  2801.     */
  2802.     select @bitdesc = null
  2803.     select @bitdesc = v.name
  2804.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2805.             where d.dbid = @curdbid
  2806.                 and v.type = 'D'
  2807.                 and d.status & v.number = 512
  2808.                 and v.number != @allopts       /* all options */
  2809.     if @bitdesc != null
  2810.     begin
  2811.         if @dbdesc != ''
  2812.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2813.         else select @dbdesc = @bitdesc
  2814.     end
  2815.  
  2816.     /*
  2817.     **  Check not recovered only bit (256)
  2818.     */
  2819.     select @bitdesc = null
  2820.     select @bitdesc = v.name
  2821.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2822.             where d.dbid = @curdbid
  2823.                 and v.type = 'D'
  2824.                 and d.status & v.number = 256
  2825.                 and v.number != @allopts       /* all options */
  2826.     if @bitdesc != null
  2827.     begin
  2828.         if @dbdesc != ''
  2829.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2830.         else select @dbdesc = @bitdesc
  2831.     end
  2832.  
  2833.     /*
  2834.     **  Check don't recover bit (32)
  2835.     */
  2836.     select @bitdesc = null
  2837.     select @bitdesc = v.name
  2838.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2839.             where d.dbid = @curdbid
  2840.                 and v.type = 'D'
  2841.                 and d.status & v.number = 32
  2842.                 and v.number != @allopts       /* all options */
  2843.     if @bitdesc != null
  2844.     begin
  2845.         if @dbdesc != ''
  2846.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2847.         else select @dbdesc = @bitdesc
  2848.     end
  2849.  
  2850.     /*
  2851.     **  Check truncate log on checkpoint bit (8)
  2852.     */
  2853.     select @bitdesc = null
  2854.     select @bitdesc = v.name
  2855.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2856.             where d.dbid = @curdbid
  2857.                 and v.type = 'D'
  2858.                 and d.status & v.number = 8
  2859.                 and v.number != @allopts       /* all options */
  2860.     if @bitdesc != null
  2861.     begin
  2862.         if @dbdesc != ''
  2863.             select @dbdesc = @dbdesc + ', ' +  @bitdesc
  2864.         else select @dbdesc = @bitdesc
  2865.     end
  2866.  
  2867.     /*
  2868.     **  If not flags are set, say so.
  2869.     */
  2870.     if @dbdesc = ''
  2871.     begin
  2872.         select @dbdesc = 'no options set'
  2873.     end
  2874.  
  2875.     /*
  2876.     **  Save the description.
  2877.     */
  2878.     update #spdbdesc
  2879.         set dbdesc = @dbdesc
  2880.             from #spdbdesc
  2881.                 where dbid = @curdbid
  2882.  
  2883.     /*
  2884.     **  Now get the next, if any dbid.
  2885.     */
  2886.     select @curdbid = min(dbid)
  2887.         from #spdbdesc
  2888.             where dbid > @curdbid
  2889. end
  2890.  
  2891. /*
  2892. **  Now #spdbdesc is complete so we can print out the db info
  2893. */
  2894.  
  2895. select distinct name = d.name,
  2896.         db_size = substring( convert( varchar(10), round( convert(
  2897.                   float,v.low * (select sum(convert(float,u.size)) from master.dbo.sysusages u where dbid = d.dbid)
  2898.       ) / 1048576.0, 0 ) ) + 'MB', 1, 8 ),
  2899.         owner = suser_name(suid),
  2900.       dbid = d.dbid,
  2901.         created = convert(char(11), d.crdate),
  2902.         status = #spdbdesc.dbdesc
  2903. from master.dbo.sysdatabases d, master.dbo.spt_values v, #spdbdesc
  2904.       where v.type = 'E' and v.number = 1
  2905.       and d.dbid = #spdbdesc.dbid
  2906. order by d.name
  2907.  
  2908. /*
  2909. **  If we are looking at one database, show it's device allocation.
  2910. */
  2911. if @showdev = 1
  2912. begin
  2913.     select device_fragments = master.dbo.sysdevices.name, size =
  2914.         convert(varchar(10),
  2915.             round(
  2916.             (a.low * convert(float, size))
  2917.             / 1048576, 0)) + ' ' + 'MB',
  2918.         usage = b.name
  2919.     from master.dbo.sysdatabases, master.dbo.sysusages, master.dbo.sysdevices, master.dbo.spt_values a, master.dbo.spt_values b
  2920.         where master.dbo.sysdatabases.dbid = master.dbo.sysusages.dbid
  2921.             and master.dbo.sysdevices.low <= size + vstart
  2922.             and master.dbo.sysdevices.high >= size + vstart - 1
  2923.             and master.dbo.sysdevices.status & 2 = 2
  2924.             and master.dbo.sysdatabases.name = @dbname
  2925.             and a.type = 'E'
  2926.             and a.number = 1
  2927.             and b.type = 'S'
  2928.             and master.dbo.sysusages.segmap & 7 = b.number
  2929.     order by 1
  2930.  
  2931.     /*
  2932.     **  If there is only one database and we are in it, show the
  2933.     **  segments.
  2934.     */
  2935.     if exists (select *
  2936.             from #spdbdesc
  2937.                 where db_id() = dbid)
  2938.     begin
  2939.         declare @curdevice    varchar(30),
  2940.             @curseg        smallint,
  2941.             @segbit        int
  2942.  
  2943.         delete #spdbdesc
  2944.  
  2945.         select @curdevice = min(d.name)
  2946.             from  master.dbo.sysusages u, master.dbo.sysdevices d
  2947.                 where u.dbid = db_id()
  2948.                     and d.low <= size + vstart
  2949.                     and d.high >= size + vstart - 1
  2950.                     and d.status & 2 = 2
  2951.         while (@curdevice != null)
  2952.         begin
  2953.             /*
  2954.             ** We need an inner loop here to go through
  2955.             **  all the possible segment.
  2956.             */
  2957.             select @curseg = min(segment)
  2958.                     from syssegments
  2959.             while (@curseg != null)
  2960.             begin
  2961.                 if (@curseg < 31)
  2962.                     select @segbit = power(2, @curseg)
  2963.                 else select @segbit = low
  2964.                     from master.dbo.spt_values
  2965.                         where type = 'E'
  2966.                             and number = 2
  2967.                 insert into #spdbdesc
  2968.                     select @curseg, @curdevice
  2969.                         from master.dbo.sysusages u,
  2970.                             master.dbo.sysdevices d,
  2971.                             master.dbo.spt_values v
  2972.                     where u.segmap & @segbit = @segbit
  2973.                         and d.low <= u.size + u.vstart
  2974.                         and d.high >= u.size + u.vstart - 1
  2975.                         and u.dbid = db_id()
  2976.                         and d.status & 2 = 2
  2977.                         and v.number = 1
  2978.                         and v.type = 'E'
  2979.                         and d.name = @curdevice
  2980.                 select @curseg = min(segment)
  2981.                         from syssegments
  2982.                             where segment > @curseg
  2983.             end
  2984.  
  2985.             select @curdevice = min(d.name)
  2986.                 from  master.dbo.sysusages u,
  2987.                     master.dbo.sysdevices d
  2988.                 where u.dbid = db_id()
  2989.                     and d.low <= size + vstart
  2990.                     and d.high >= size + vstart - 1
  2991.                     and d.status & 2 = 2
  2992.                     and d.name > @curdevice
  2993.         end
  2994.  
  2995.         /*
  2996.         **  One last check for any devices that have no segments.
  2997.         */
  2998.         insert into #spdbdesc
  2999.             select null, d.name
  3000.                 from master.dbo.sysusages u,
  3001.                     master.dbo.sysdevices d
  3002.             where u.segmap = 0
  3003.                 and d.low <= u.size + u.vstart
  3004.                 and d.high >= u.size + u.vstart - 1
  3005.                 and u.dbid = db_id()
  3006.                 and d.status & 2 = 2
  3007.  
  3008.         select distinct device = dbdesc,
  3009.             segment = isnull(name, ' -- unused by any segments --')
  3010.         from #spdbdesc, syssegments
  3011.             where dbid *= segment
  3012.         order by 1, 2
  3013.     end
  3014.  
  3015. end
  3016.  
  3017. drop table #spdbdesc
  3018. return (0)
  3019. go
  3020.  
  3021. /************* DUMP THE TRANSACTION LOG **************************************/
  3022. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  3023. /* script periodically, you will run out of transaction log space.           */
  3024. dump tran master with truncate_only
  3025. go
  3026. /************* END DUMP THE TRANSACTION LOG **********************************/
  3027.  
  3028. /***************************************************************************/
  3029. /* This stored procedure is a copy of sp_helpremotelogin, but returns the  */
  3030. /* full length of the login information columns.  In cases where the       */
  3031. /* remote login name is at or near 30 characters, sp_helpremotelogins      */
  3032. /* would truncate the name.                                                */
  3033. /***************************************************************************/
  3034. print ''
  3035. print 'Creating sp_MShelpremotelogin'
  3036. print ''
  3037. go
  3038.  
  3039. create procedure sp_MShelpremotelogin
  3040. @remoteserver varchar(30) = NULL,    /* remote server name */
  3041. @remotename varchar(30) = NULL        /* remote login name */
  3042. as
  3043.  
  3044. declare    @msg    varchar(100)
  3045.  
  3046. set nocount on
  3047.  
  3048. /*
  3049. **  If no server given, get 'em all.
  3050. */
  3051. if @remoteserver = null
  3052. begin
  3053.     select @remoteserver = '%'
  3054. end
  3055.  
  3056. else
  3057. begin
  3058.     if not exists (select *
  3059.             from master.dbo.sysservers s,
  3060.                 master.dbo.sysremotelogins r
  3061.             where s.srvid = r.remoteserverid
  3062.                 and s.srvname like @remoteserver)
  3063.     begin
  3064.         if @remoteserver = '%'
  3065.         begin
  3066.             select @msg = 'There are no remote servers defined.'
  3067.             print @msg
  3068.             return (1)
  3069.         end
  3070.         return (1)
  3071.     end
  3072. end
  3073.  
  3074. /*
  3075. **  If no remotename given, get 'em all.
  3076. */
  3077. if @remotename = null
  3078. begin
  3079.     select @remotename = '%'
  3080. end
  3081.  
  3082. else
  3083. begin
  3084.     if not exists (select *
  3085.             from master.dbo.sysremotelogins
  3086.             where isnull(remoteusername, '') like @remotename)
  3087.     begin
  3088.         if @remotename = '%'
  3089.         begin
  3090.             select @msg = 'There are no remotelogins defined.'
  3091.             print @msg
  3092.             return (1)
  3093.         end
  3094.  
  3095.         select @msg = 'There are no remote logins for '''
  3096.             + @remotename + '''.'
  3097.         print @msg
  3098.         return (1)
  3099.     end
  3100. end
  3101.  
  3102. /*
  3103. **  Check for empty results.
  3104. */
  3105. if not exists (select *
  3106.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  3107.         where isnull(r.remoteusername, '') like @remotename
  3108.             and s.srvid = r.remoteserverid
  3109.                 and s.srvname like @remoteserver)
  3110. begin
  3111.     if ((@remoteserver = '%') and (@remotename = '%'))
  3112.         select @msg = 'There are no remote logins.'
  3113.     else select @msg = 'There are no remote logins for '''
  3114.         + @remotename + ''' on remote server ''' + @remoteserver + '''.'
  3115.     print @msg
  3116.     return (1)
  3117. end
  3118.  
  3119. /*
  3120. **  Select the information.
  3121. */
  3122. select server = s.srvname,
  3123.     local_user_name =
  3124.         isnull(suser_name(r.suid), '** use local name **'),
  3125.     remote_user_name =
  3126.         isnull(r.remoteusername, '** mapped locally **'),
  3127.     options = v.name
  3128.         from master.dbo.sysservers s, master.dbo.sysremotelogins r,
  3129.             master.dbo.spt_values v
  3130.     where s.srvid = r.remoteserverid
  3131.         and s.srvname like @remoteserver
  3132.         and isnull(r.remoteusername, '') like @remotename
  3133.         and v.type = 'F'
  3134.         and v.number = r.status
  3135. order by server, remote_user_name
  3136.  
  3137. return (0)
  3138. go
  3139.  
  3140. /************* DUMP THE TRANSACTION LOG **************************************/
  3141. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  3142. /* script periodically, you will run out of transaction log space.           */
  3143. dump tran master with truncate_only
  3144. go
  3145. /************* END DUMP THE TRANSACTION LOG **********************************/
  3146.  
  3147. /***************************************************************************/
  3148. /* This stored procedure is a copy of sp_helpserver, but returns the       */
  3149. /* full length of the server information columns.  In cases where the      */
  3150. /* server name is at or near 30 characters, sp_helpserver would truncate   */
  3151. /* the name.                                                               */
  3152. /***************************************************************************/
  3153. print ''
  3154. print 'Creating sp_MShelpserver'
  3155. print ''
  3156. go
  3157.  
  3158. create procedure sp_MShelpserver
  3159. @server varchar(30) = NULL        /* server name */
  3160. as
  3161.  
  3162. declare    @msg    varchar(100)
  3163.  
  3164. set nocount on
  3165.  
  3166. /*
  3167. **  If no server name given, get 'em all.
  3168. */
  3169. if @server = null
  3170. begin
  3171.     select @server = '%'
  3172. end
  3173.  
  3174. /*
  3175. **  Does the server exist?
  3176. */
  3177. if not exists (select *
  3178.         from master.dbo.sysservers
  3179.             where srvname like @server
  3180.                 or srvnetname like @server)
  3181. begin
  3182.     if @server = '%'
  3183.         select @msg = 'There are no servers defined.'
  3184.     else select @msg = 'There is no server named ''' + @server + '''.'
  3185.     print @msg
  3186.     return (1)
  3187. end
  3188.  
  3189. /*
  3190. **  Display server information.
  3191. **  First put it into a temp table so we can blot inappropriate status
  3192. **  for the local server.  It's ugly but I couldn't think of another way.
  3193. */
  3194. create table #spt_server
  3195. (
  3196.     name    varchar(30),
  3197.     network    varchar(30),
  3198.     status    varchar(20),
  3199.     id    smallint
  3200. )
  3201. insert into #spt_server
  3202.     select s.srvname, s.srvnetname, status = isnull(v.name, ''),
  3203.         id = s.srvid
  3204.     from master.dbo.sysservers s, master.dbo.spt_values v
  3205.         where s.srvstatus *= v.number
  3206.             and v.type = 'A'
  3207.             and (s.srvname like @server
  3208.                 or s.srvnetname like @server)
  3209. update #spt_server
  3210.     set status = ''
  3211.         from master.dbo.sysservers s, #spt_server t
  3212.             where s.srvid = 0
  3213.                 and s.srvname = t.name
  3214.  
  3215. /* Sql95 uses totally different status bits and timeouts has lost its */
  3216. /* meaning, so always clear this if we're on sql95 */
  3217. update #spt_server set status = '' where @@microsoftversion & 0x00FFFFFF >= 0x00050000
  3218.  
  3219. select name, network_name = network,
  3220.     status = status, id = convert(char(4), id)
  3221.         from #spt_server
  3222.     order by name
  3223.  
  3224. return (0)
  3225. go
  3226.  
  3227.  
  3228. /***************************************************************************/
  3229. /* This section grants execute permission on the stored procedure and
  3230.    select permissions on all the tables                    */
  3231. /***************************************************************************/
  3232.  
  3233. /* */
  3234. /* Grant permission on the procedures */
  3235. /* */
  3236.  
  3237. print ''
  3238. print 'Granting execute permissions on procedures'
  3239. print ''
  3240. go
  3241. grant execute on sp_MSdevice_list to public
  3242. grant execute on sp_MScheck_admin to public
  3243. grant execute on sp_MSdev_description to public
  3244. grant execute on sp_MSdev_databases to public
  3245. grant execute on sp_MSsys_monitor to public
  3246. grant execute on sp_MSnext_devnumber to public
  3247. grant execute on sp_MSdatabase_list to public
  3248. grant execute on sp_MSdatabase_avail to public
  3249. grant execute on sp_MSbackup_now to public
  3250. grant execute on sp_MSuser_list to public
  3251. grant execute on sp_MSmonitor to public
  3252. grant execute on sp_MSdb_devices to public
  3253. grant execute on sp_MSdb_options to public
  3254. grant execute on sp_MSdb_properties to public
  3255. grant execute on sp_MSdb_space to public
  3256. grant execute on sp_MSbackup_schedule to public
  3257. grant execute on sp_MScmd_permissions to public
  3258. grant execute on sp_MSuser_cmd_permissions to public
  3259. grant execute on sp_MSdb_options_bits to public
  3260.  
  3261. /* */
  3262. /* these are 4.2 only */
  3263. /* */
  3264.  
  3265. grant execute on sp_MSdev_mirror to public
  3266. grant execute on sp_MSdb_devices_segments to public
  3267. grant execute on sp_MSsegment_list to public
  3268. grant execute on sp_MSsegment_devices to public
  3269. grant execute on sp_MSseg_properties to public
  3270. grant execute on sp_MSseg_tables to public
  3271. grant execute on sp_MSthread_list to public
  3272. grant execute on sp_MShelpdb to public
  3273. grant execute on sp_MShelpremotelogin to public
  3274. grant execute on sp_MShelpserver to public
  3275. go
  3276.  
  3277. /* */
  3278. /* Grant permissions on tables */
  3279. /* */
  3280.  
  3281. print ''
  3282. print 'Creating permissions on tables'
  3283. print ''
  3284. go
  3285. grant select, update, delete, insert on MSscheduled_backups to dbo
  3286. grant select, update, delete, insert on MSscheduled_backups_log to dbo
  3287. go
  3288.  
  3289.  
  3290. /* logdevice    27.1    3/5/90 */
  3291. /* rv, 2/4/92, bug 1265 */
  3292.  
  3293. sp_configure 'allow updates',1
  3294. go
  3295. reconfigure with override
  3296. go
  3297.  
  3298. create procedure sp_MSlogdevice
  3299. @dbname varchar(30),            /* database name that has the syslogs */
  3300. @devname        varchar(30),    /* device name to put syslogs on */
  3301. @fragflag       varchar(3)=' '  /* if ='*', change all fragments */
  3302. as
  3303.  
  3304. declare @dbid   smallint        /* dbid of the database to be changed */
  3305. declare @dbuid  smallint        /* id of the owner of the database */
  3306. declare @logbit int             /* this is the bit to turn on in sysusages */
  3307. declare @fragcnt smallint       /* count of allocation fragments */
  3308. declare @fragstart int          /* lowest lstart of fragments to change */
  3309.  
  3310. select @logbit = 4              /* bit 3 is the one to turn on */
  3311. select @fragstart = 0           /* lowest lstart of fragments to change */
  3312.  
  3313. /*
  3314. **  Verify the database name and get the @dbid and @dbuid
  3315. */
  3316. select @dbid = dbid, @dbuid = suid
  3317.         from sysdatabases
  3318.                 where name = @dbname
  3319.  
  3320. /*
  3321. **  If @dbname not found, say so and list the databases.
  3322. */
  3323. if @dbid = NULL
  3324. begin
  3325.         print 'No such database -- run sp_helpdb to list databases.'
  3326.         return (1)
  3327. end
  3328.  
  3329. /*
  3330. **  See if the device exists.
  3331. */
  3332. if not exists (select *
  3333.                 from master.dbo.sysdevices
  3334.                         where name like @devname)
  3335. begin
  3336.         print 'No such device exists -- run sp_helpdevice to list the SQLServer devices.'
  3337.         return (1)
  3338. end
  3339.  
  3340. /*
  3341. **  You must be SA or the dbo to execute this sproc.
  3342. */
  3343. if suser_id() != 1 and suser_id() != @dbuid
  3344. begin
  3345.         print 'Only the System Administrator (SA) or the Database Owner (dbo) may move the syslogs table.'
  3346.         return (1)
  3347. end
  3348.  
  3349. /*
  3350. **  Now see if the @dbname uses the @devname
  3351. */
  3352. if not exists (select *
  3353.                 from sysusages u, sysdevices d
  3354.                         where d.name = @devname
  3355.                                 and u.vstart between d.low and d.high
  3356.                                 and u.dbid = @dbid)
  3357. begin
  3358.         print 'The specified device is not used by the database.'
  3359.         return (1)
  3360. end
  3361.  
  3362. /*
  3363. **  Count the allocation fragments on the specified device, excluding
  3364. **  the one containing the system tables (lstart=0).
  3365. */
  3366. select @fragcnt=count(*)
  3367.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3368.                 where d.name = @devname
  3369.                         and u.vstart between d.low and d.high
  3370.                         and u.dbid = @dbid
  3371.                         and lstart != 0
  3372. /*
  3373. **  If there are none, it cannot be changed to log-only status
  3374. */
  3375. if @fragcnt = 0
  3376. begin
  3377.      print 'Can''t make the only segment on original device log-only'
  3378.      return (1)
  3379. end
  3380.  
  3381. /*
  3382. **  If more than one allocation fragment and '*' option not specified,
  3383. **  change only the one with the highest lstart.
  3384. */
  3385. if @fragcnt > 1 and @fragflag!='*'
  3386. begin
  3387.      print 'Warning, there are multiple allocation fragments on this device.'
  3388.      print 'Only the last fragment will be changed to log-only status.  If you'
  3389.      print 'wish to change them all, run sp_MSlogdevice database,device,''*'' '
  3390.  
  3391.      select @fragstart=max(lstart)
  3392.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3393.                 where d.name = @devname
  3394.                         and u.vstart between d.low and d.high
  3395.                         and u.dbid = @dbid
  3396. end
  3397.  
  3398.  
  3399. /*
  3400. **  clear the bit from any database segments that aren't already log only
  3401. */
  3402. update master.dbo.sysusages
  3403.         set segmap = segmap & ~@logbit
  3404.                 from master.dbo.sysusages
  3405.                         where dbid = @dbid
  3406.                         and segmap != @logbit
  3407. /*
  3408. **  Now set the segments on @devname as log-only.
  3409. */
  3410. update master.dbo.sysusages
  3411.         set segmap = @logbit
  3412.                 from master.dbo.sysusages u, master.dbo.sysdevices d
  3413.                         where d.name = @devname
  3414.                                 and u.vstart between d.low and d.high
  3415.                                 and u.dbid = @dbid
  3416.                                 and lstart != 0
  3417.                                 and lstart>=@fragstart
  3418.  
  3419. /*
  3420. **  Now we need to activate the new segment map.
  3421. */
  3422. dbcc dbrepair (@dbname, remap)
  3423. print 'Syslogs moved.'
  3424.  
  3425. return (0)
  3426. go
  3427. sp_configure 'allow updates',0
  3428. go
  3429. print ''
  3430. print 'reconfiguring'
  3431. print ''
  3432. go
  3433.  
  3434. reconfigure with override
  3435. go
  3436.  
  3437.  
  3438. grant execute on sp_MSlogdevice to public
  3439. go
  3440.  
  3441.  
  3442. /************* SQL ADMIN VERSION CONTROL *************************************/
  3443. /*    4.20.xx.yy where yy is script version.                                 */
  3444. /*****************************************************************************/
  3445. create procedure sp_MSAdmin_version as
  3446.    select 'Microsoft SQL Administrator script version 6.00.00.1'
  3447. go
  3448.  
  3449. print ''
  3450. print 'Reconfiguring to set the category bit'
  3451. print ''
  3452. go
  3453. sp_configure 'allow updates',1
  3454. go
  3455. reconfigure with override
  3456. go
  3457.  
  3458. /* Set the 'MS-installed object' category bit.  Must be done AFTER all objects created. */
  3459. update master..sysobjects set category = category | 0x02 where sysstat & 7 = 4 and name in (
  3460.       'sp_MSdevice_list',
  3461.       'sp_MScheck_admin',
  3462.       'sp_MSdev_description',
  3463.       'sp_MSdev_databases',
  3464.       'sp_MSsys_monitor',
  3465.       'sp_MSnext_devnumber',
  3466.       'sp_MSdatabase_list',
  3467.       'sp_MSdatabase_avail',
  3468.       'sp_MSbackup_now',
  3469.       'sp_MSuser_list',
  3470.       'sp_MSmonitor',
  3471.       'sp_MSdb_devices',
  3472.       'sp_MSdb_options',
  3473.       'sp_MSdb_properties',
  3474.       'sp_MSdb_space',
  3475.       'sp_MSbackup_schedule',
  3476.       'sp_MScmd_permissions',
  3477.       'sp_MSuser_cmd_permissions',
  3478.       'sp_MSdb_options_bits',
  3479.       'sp_MSdev_mirror',
  3480.       'sp_MSdb_devices_segments',
  3481.       'sp_MSsegment_list',
  3482.       'sp_MSsegment_devices',
  3483.       'sp_MSseg_properties',
  3484.       'sp_MSseg_tables',
  3485.       'sp_MSthread_list',
  3486.       'sp_MShelpdb',
  3487.       'sp_MShelpremotelogin',
  3488.       'sp_MSlogdevice',
  3489.       'sp_MSAdmin_version',
  3490.       'sp_MShelpserver')
  3491. go
  3492. update master..sysobjects set category = category | 0x02 where sysstat & 7 = 3 and name in (
  3493.       'MSscheduled_backups',
  3494.       'MSscheduled_backups_log',
  3495.       'MSsystem_monitor',
  3496.       'MSlast_monitor')
  3497. go
  3498.  
  3499. sp_configure 'allow updates',0
  3500. go
  3501. reconfigure with override
  3502. go
  3503.  
  3504. set nocount on
  3505.  
  3506. create table #spmissing
  3507. (name varchar(30),
  3508.  type varchar(10)
  3509. )
  3510. go
  3511.  
  3512. /* */
  3513. /* Check for the stored procedures first */
  3514. /* */
  3515.  
  3516. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdevice_list' and sysstat & 7 = 4)
  3517. begin
  3518.   insert into #spmissing values('sp_MSdevice_list', 'procedure')
  3519. end
  3520. go
  3521. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_description' and sysstat & 7 = 4)
  3522. begin
  3523.   insert into #spmissing values('sp_MSdev_description', 'procedure')
  3524. end
  3525. go
  3526. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_databases' and sysstat & 7 = 4)
  3527. begin
  3528.   insert into #spmissing values('sp_MSdev_databases', 'procedure')
  3529. end
  3530. go
  3531. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsys_monitor' and sysstat & 7 = 4)
  3532. begin
  3533.   insert into #spmissing values('sp_MSsys_monitor', 'procedure')
  3534. end
  3535. go
  3536. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  3537. begin
  3538.   insert into #spmissing values('sp_MSnext_devnumber', 'procedure')
  3539. end
  3540. go
  3541. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_list' and sysstat & 7 = 4)
  3542. begin
  3543.   insert into #spmissing values('sp_MSdatabase_list', 'procedure')
  3544. end
  3545. go
  3546. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_avail' and sysstat & 7 = 4)
  3547. begin
  3548.   insert into #spmissing values('sp_MSdatabase_avail', 'procedure')
  3549. end
  3550. go
  3551. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_now' and sysstat & 7 = 4)
  3552. begin
  3553.   insert into #spmissing values('sp_MSbackup_now', 'procedure')
  3554. end
  3555. go
  3556. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_list' and sysstat & 7 = 4)
  3557. begin
  3558.   insert into #spmissing values('sp_MSuser_list', 'procedure')
  3559. end
  3560. go
  3561. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSmonitor' and sysstat & 7 = 4)
  3562. begin
  3563.   insert into #spmissing values('sp_MSmonitor', 'procedure')
  3564. end
  3565. go
  3566. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices' and sysstat & 7 = 4)
  3567. begin
  3568.   insert into #spmissing values('sp_MSdb_devices', 'procedure')
  3569. end
  3570. go
  3571. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options' and sysstat & 7 = 4)
  3572. begin
  3573.   insert into #spmissing values('sp_MSdb_options', 'procedure')
  3574. end
  3575. go
  3576. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_properties' and sysstat & 7 = 4)
  3577. begin
  3578.   insert into #spmissing values('sp_MSdb_properties', 'procedure')
  3579. end
  3580. go
  3581. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_space' and sysstat & 7 = 4)
  3582. begin
  3583.   insert into #spmissing values('sp_MSdb_space', 'procedure')
  3584. end
  3585. go
  3586. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_schedule' and sysstat & 7 = 4)
  3587. begin
  3588.   insert into #spmissing values('sp_MSbackup_schedule', 'procedure')
  3589. end
  3590. go
  3591. if not exists (select * from master.dbo.sysobjects where name = 'sp_MScmd_permissions' and sysstat & 7 = 4)
  3592. begin
  3593.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  3594. end
  3595. go
  3596. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_cmd_permissions' and sysstat & 7 = 4)
  3597. begin
  3598.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  3599. end
  3600. go
  3601. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options_bits' and sysstat & 7 = 4)
  3602. begin
  3603.   insert into #spmissing values('sp_MSdb_options_bits', 'procedure')
  3604. end
  3605. go
  3606. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_mirror' and sysstat & 7 = 4)
  3607. begin
  3608.   insert into #spmissing values('sp_MSdev_mirror', 'procedure')
  3609. end
  3610. go
  3611. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices_segments' and sysstat & 7 = 4)
  3612. begin
  3613.   insert into #spmissing values('sp_MSdb_devices_segments', 'procedure')
  3614. end
  3615. go
  3616. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_list' and sysstat & 7 = 4)
  3617. begin
  3618.   insert into #spmissing values('sp_MSsegment_list', 'procedure')
  3619. end
  3620. go
  3621. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_devices' and sysstat & 7 = 4)
  3622. begin
  3623.   insert into #spmissing values('sp_MSsegment_devices', 'procedure')
  3624. end
  3625. go
  3626. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_properties' and sysstat & 7 = 4)
  3627. begin
  3628.   insert into #spmissing values('sp_MSseg_properties', 'procedure')
  3629. end
  3630. go
  3631. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_tables' and sysstat & 7 = 4)
  3632. begin
  3633.   insert into #spmissing values('sp_MSseg_tables', 'procedure')
  3634. end
  3635. go
  3636. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSthread_list' and sysstat & 7 = 4)
  3637. begin
  3638.   insert into #spmissing values('sp_MSthread_list', 'procedure')
  3639. end
  3640. go
  3641. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpdb' and sysstat & 7 = 4)
  3642. begin
  3643.   insert into #spmissing values('sp_MShelpdb', 'procedure')
  3644. end
  3645. go
  3646. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpremotelogin' and sysstat & 7 = 4)
  3647. begin
  3648.   insert into #spmissing values('sp_MSremotelogin', 'procedure')
  3649. end
  3650. go
  3651. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpserver' and sysstat & 7 = 4)
  3652. begin
  3653.   insert into #spmissing values('sp_MShelpserver', 'procedure')
  3654. end
  3655. go
  3656. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSlogdevice' and sysstat & 7 = 4)
  3657. begin
  3658.   insert into #spmissing values('sp_MSlogdevice', 'procedure')
  3659. end
  3660. go
  3661.  
  3662. /* */
  3663. /* Check for the tables next */
  3664. /* */
  3665.  
  3666. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups')
  3667. begin
  3668.   insert into #spmissing values('MSschedule_backups','table')
  3669. end
  3670. go
  3671. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups_log')
  3672. begin
  3673.   insert into #spmissing values('MSschedule_backups_log','table')
  3674. end
  3675. go
  3676. if not exists (select * from master.dbo.sysobjects where name = 'MSsystem_monitor')
  3677. begin
  3678.   insert into #spmissing values('MSsystem_monitor','table')
  3679. end
  3680. go
  3681. if not exists (select * from master.dbo.sysobjects where name = 'MSlast_monitor')
  3682. begin
  3683.   insert into #spmissing values('MSlast_monitor','table')
  3684. end
  3685. go
  3686.  
  3687. if exists (select * from #spmissing)
  3688. begin
  3689.    print ''
  3690.    print ''
  3691.    print ' ====================  ERRORS!  ===================='
  3692.    print '       The following objects were not created.'
  3693.    print ' Sql Administrator will not run against this server.'
  3694.    print ''
  3695.    select * from #spmissing
  3696.    drop procedure sp_MSAdmin_version
  3697. end
  3698. else
  3699. begin
  3700.    print ''
  3701.    print ''
  3702.    print ' Successful installation.'
  3703.    grant execute on sp_MSAdmin_version to public
  3704.    exec sp_MSAdmin_version
  3705. end
  3706. go
  3707. drop table #spmissing
  3708. go
  3709. set nocount off
  3710. go
  3711.  
  3712. /************* DUMP THE TRANSACTION LOG **************************************/
  3713. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  3714. /* script periodically, you will run out of transaction log space.           */
  3715. dump tran master with truncate_only
  3716. go
  3717. /************* END DUMP THE TRANSACTION LOG **********************************/
  3718. checkpoint
  3719. go
  3720. 
  3721.