home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0430 - 0439 / ibm0430-0439 / ibm0438.tar / ibm0438 / SQL42109.ZIP / SQL421A.009 / SQL / I386 / ADMIN2.SQ@ / ADMIN2.SQ@
Encoding:
Text File  |  1994-07-14  |  123.5 KB  |  3,655 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. round(sum((convert(float,usg.size) * @alloc) / 1048576.0),0), "none",d.low/16777216
  360. from master.dbo.sysdevices d, master.dbo.sysusages usg
  361. where d.low <= usg.size + usg.vstart - 1
  362. and d.high >= usg.size + usg.vstart - 1
  363. and d.cntrltype = 0
  364. group by d.name
  365.  
  366. /* Insert the name, size for the database devices that are clean */
  367.  
  368. insert into #spdevtab (name, devsize, devsizeout, spaceused, mirror,cntrlnum)
  369. select d.name,round((((convert(float,d.high) - convert(float,d.low)) * @alloc) / 1048576.0),0),
  370. ltrim(str(round((((convert(float,d.high) - convert(float,d.low)) * @alloc)/1048576.0),0), 8,0)) + " MB", 0, "none",
  371. d.low/16777216
  372. from master.dbo.sysdevices d, master.dbo.sysusages usg
  373. where d.cntrltype = 0 and
  374. d.name not in (select name from #spdevtab)
  375. group by d.name
  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, #spdevtab s 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= value from master..sysconfigures where config=116
  533.  
  534. while @nextdev != @maxdev /* This is one max device number allowed */
  535. begin
  536.   if exists(select name from master.dbo.sysdevices where (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. begin
  823.  
  824. if not exists (select name from syscolumns where name = 'Stat' and id = (select id from sysobjects where name = 'MSscheduled_backups'))
  825. begin
  826.    alter table MSscheduled_backups add Stat tinyint null
  827. end
  828.  
  829. if not exists (select name from syscolumns where name = 'email_name' and id = (select id from sysobjects where name = 'MSscheduled_backups'))
  830. begin
  831.    alter table MSscheduled_backups add attempts tinyint null, email_name varchar(60) null
  832. end
  833. end
  834. go
  835.  
  836. print "Ignore errors on unknown column Stat if upgrading OS/2 SQL Server"
  837. go
  838.  
  839. if patindex('%Windows%',@@version) > 0
  840. begin
  841. /* change existing table entries to indicate append of tape dump in stat column rather than */
  842. /* in dump device name */
  843. /* no append */
  844. 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
  845. /* append database dump */
  846. update MSscheduled_backups set Stat = 2 where Database_dump like '%+' and (Log_dump not like '%+' or Log_dump = null) and Stat = NULL
  847. /* append log dump */
  848. update MSscheduled_backups set Stat = 3 where Log_dump like '%+' and (Database_dump not like '%+' or Database_dump = null) and Stat = NULL
  849. /* append both */
  850. update MSscheduled_backups set Stat = 4 where Database_dump like '%+' and Log_dump like '%+' and Stat = NULL
  851. /* change '+'s to space */
  852. update MSscheduled_backups set Database_dump = stuff(Database_dump,charindex('+',Database_dump),1,' ') where Stat != 1 and Database_dump like '%+'
  853. /* trim out extra space */
  854. update MSscheduled_backups set Database_dump = rtrim(Database_dump) where Stat != 1
  855. /* change '+'s to space */
  856. update MSscheduled_backups set Log_dump = stuff(Log_dump,charindex('+',Log_dump),1,' ') where Stat != 1  and Log_dump like '%+'
  857. /* trim out extra space */
  858. update MSscheduled_backups set Log_dump = rtrim(Log_dump) where Stat != 1
  859. end
  860. go
  861.  
  862.  
  863. /* */
  864. /* Create the scheduled backup log table */
  865. /* */
  866.  
  867. print ""
  868. print "Creating MSscheduled_backups_log, if table already exists ignore error message."
  869. print ""
  870. go
  871. create table MSscheduled_backups_log
  872. (
  873. Event_ID        int        Not Null,    /* Event ID from schedule table */
  874. Database_name        varchar(30)    Not Null,    /* Name of database to be dumped */
  875. Actual_start_time    datetime    Not Null,    /* Actual time the dump started */
  876. Actual_end_time     datetime    Null,        /* Actual time the dump completed */
  877. Status            bit        Not Null,    /* 1 = SUCCESS 0 = FAILED */
  878. Message         varchar(255)    Null        /* Error message upon failure. */
  879. )
  880. go
  881.  
  882. /* */
  883. /* Create the MSsystem_monitor table */
  884. /* */
  885.  
  886. print ""
  887. print "Creating MSsystem_monitor"
  888. print ""
  889. go
  890. create table MSsystem_monitor
  891. (
  892. Monitor_time        datetime    Not Null,    /* Time the monitor data was taken */
  893. CPU_busy_start        int        Not Null,    /* # secs CPU busy since server started */
  894. CPU_busy_last        int        Not Null,    /* # seconds CPU was busy since last monitor */
  895. CPU_busy_pct        tinyint     Not Null,    /* Percent CPU was busy since server started */
  896. IO_busy_start        int        Not Null,    /* # seconds IO was busy since server started */
  897. IO_busy_last        int        Not Null,    /* # seconds IO was busy since last monitor */
  898. IO_busy_pct        tinyint     Not Null,    /* Percent IO was busy since server started */
  899. Idle_start        int        Not Null,    /* # secs server idle since server started */
  900. Idle_last        int        Not Null,    /* # seconds server was idle since last monitor */
  901. Idle_pct        tinyint     Not Null,    /* Percent server was idle since server started */
  902. Pkts_sent_start     int        Not Null,    /* # packets sent since server started */
  903. Pkts_sent_last        int        Not Null,    /* # packets sent since last monitor */
  904. Pkts_received_start    int        Not Null,    /* # packets received since server started */
  905. Pkts_received_last    int        Not Null,    /* # packets received since last monitor */
  906. Pkt_errors_start    int        Not Null,    /* # packet errors since server started */
  907. Pkt_errors_last     int        Not Null,    /* # packet errors since last montor */
  908. Total_reads_start    int        Not Null,    /* # reads since server started */
  909. Total_reads_last    int        Not Null,    /* # reads since last monitor */
  910. Total_writes_start    int        Not Null,    /* # writes since server started */
  911. Total_writes_last    int        Not Null,    /* # write since last  monitor */
  912. Total_errors_start    int        Not Null,    /* # errors ince server started */
  913. Total_errors_last    int        Not Null,    /* # errors since last monitor */
  914. Connections_start    int        Not Null,    /* # connections since server started */
  915. Connections_last    int        Not Null,    /* # connections since last monitor */
  916. )
  917. go
  918.  
  919. /* */
  920. /* Create the MSlast_monitor table */
  921. /* */
  922.  
  923. print ""
  924. print "Creating MSlast_montior"
  925. print ""
  926. go
  927. create table MSlast_monitor
  928. (
  929. Last_time       datetime       Not Null,       /* Time of last monitor */
  930. CPU_busy       int           Not Null,       /* # seconds CPU was busy since last monitor */
  931. IO_busy        int           Not Null,       /* # seconds IO was busy since last monitor */
  932. Idle           int           Not Null,       /* # seconds server was idle since last monitor */
  933. Pkts_sent       int           Not Null,       /* # packets sent since last monitor */
  934. Pkts_received       int           Not Null,       /* # packets received since last monitor */
  935. Pkt_errors       int           Not Null,       /* # packet errors since last montor */
  936. Total_reads       int           Not Null,       /* # reads since last monitor */
  937. Total_writes       int           Not Null,       /* # write since last  monitor */
  938. Total_errors       int           Not Null,       /* # errors since last monitor */
  939. Connections       int           Not Null,       /* # connections since last monitor */
  940. )
  941. go
  942.  
  943. insert into MSlast_monitor
  944. select getdate(), @@cpu_busy, @@io_busy, @@idle, @@pack_sent, @@pack_received,
  945.        @@packet_errors, @@total_read, @@total_write, @@total_errors, @@connections
  946. go
  947.  
  948. /************* DUMP THE TRANSACTION LOG **************************************/
  949. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  950. /* script periodically, you will run out of transaction log space.           */
  951. dump tran master with truncate_only
  952. go
  953. /************* END DUMP THE TRANSACTION LOG **********************************/
  954.  
  955.  
  956.  
  957. /***************************************************************************/
  958. /* This stored procedure performs the monitoring of the system.  It is a
  959.    variation of the sp_monitor stored procedure on in the output and storage
  960.    of data                                   */
  961. /***************************************************************************/
  962.  
  963.  
  964. print ""
  965. print "Creating sp_MSsys_monitor"
  966. print ""
  967. go
  968. create procedure sp_MSsys_monitor as
  969.  
  970. /* */
  971. /*  Declare variables to be used to hold current monitor values. */
  972. /* */
  973.  
  974. declare @now         datetime
  975. declare @cpu_busy     int
  976. declare @io_busy    int
  977. declare @idle        int
  978. declare @pack_received    int
  979. declare @pack_sent    int
  980. declare @pack_errors    int
  981. declare @connections    int
  982. declare @total_read    int
  983. declare @total_write    int
  984. declare @total_errors    int
  985.  
  986. declare @oldcpu_busy     int    /* used to see if SQL Server has been rebooted */
  987. declare @interval    int
  988. declare @mspertick    int    /* milliseconds per tick */
  989.  
  990. /* */
  991. /*  Set @mspertick.  This is just used to make the numbers easier to handle
  992.     and avoid overflow. */
  993. /* */
  994.  
  995. select @mspertick = convert(int, @@timeticks / 1000.0)
  996.  
  997. /*  Get current monitor values. */
  998.  
  999. begin transaction
  1000. select
  1001.     @now = getdate(),
  1002.     @cpu_busy = @@cpu_busy,
  1003.     @io_busy = @@io_busy,
  1004.     @idle = @@idle,
  1005.     @pack_received = @@pack_received,
  1006.     @pack_sent = @@pack_sent,
  1007.     @connections = @@connections,
  1008.     @pack_errors = @@packet_errors,
  1009.     @total_read = @@total_read,
  1010.     @total_write = @@total_write,
  1011.     @total_errors = @@total_errors
  1012.  
  1013. /*
  1014. **  Check to see if SQL Server has been rebooted.  If it has then the
  1015. **  value of @@cpu_busy will be less than the value of master.dbo.spt_monitor.cpu_busy.
  1016. **  If it has update master.dbo.spt_monitor.
  1017. */
  1018.  
  1019. select @oldcpu_busy = CPU_busy from MSlast_monitor
  1020. if @oldcpu_busy > @cpu_busy
  1021. begin
  1022.    update MSlast_monitor set
  1023.       Last_time = @now,
  1024.       CPU_busy = @cpu_busy,
  1025.       IO_busy = @io_busy,
  1026.       Idle = @idle,
  1027.       Pkts_received = @pack_received,
  1028.       Pkts_sent = @pack_sent,
  1029.       Connections = @connections,
  1030.       Pkt_errors = @pack_errors,
  1031.       Total_reads = @total_read,
  1032.       Total_writes = @total_write,
  1033.       Total_errors = @total_errors
  1034. end
  1035.  
  1036. /* */
  1037. /*  Insert a row in the monitor table to reflect the current values since the
  1038.     server was started. */
  1039. /* */
  1040.  
  1041. set nocount on
  1042.  
  1043. insert into MSsystem_monitor
  1044. select @now, (@cpu_busy * @mspertick) / 1000, 0, 0, (@io_busy * @mspertick) / 1000,
  1045. 0, 0, (@idle * @mspertick) / 1000, 0,0, @pack_sent, 0, @pack_received, 0,
  1046. @pack_errors,0, @total_read, 0, @total_write, 0, @total_errors, 0, @connections, 0
  1047.  
  1048. /* */
  1049. /*  Now update the system monitor table with the difference between this monitor
  1050.     and the last monitor taken */
  1051. /* */
  1052.  
  1053. select @interval = datediff(ss, Last_time, @now)
  1054. from MSlast_monitor
  1055.  
  1056. update MSsystem_monitor
  1057.     set CPU_busy_last = ((@cpu_busy - CPU_busy) * @mspertick) / 1000,
  1058.     CPU_busy_pct = ((((@cpu_busy - CPU_busy) * @mspertick) / 1000) * 100) / @interval,
  1059.     IO_busy_last = ((@io_busy - IO_busy) * @mspertick) / 1000,
  1060.     IO_busy_pct =  ((((@io_busy - IO_busy) * @mspertick) / 1000) * 100) / @interval,
  1061.     Idle_last =  ((@idle - Idle) * @mspertick) / 1000,
  1062.     Idle_pct = ((((@idle - Idle) * @mspertick) / 1000) * 100) / @interval,
  1063.     Pkts_received_last = @pack_received - Pkts_received,
  1064.     Pkts_sent_last = @pack_sent - Pkts_sent,
  1065.     Pkt_errors_last = @pack_errors - Pkt_errors,
  1066.     Total_reads_last = @total_read - Total_reads,
  1067.     Total_writes_last = @total_write - Total_writes,
  1068.     Total_errors_last = @total_errors - Total_errors,
  1069.     Connections_last = @connections - Connections
  1070. from MSlast_monitor
  1071. where Monitor_time = @now
  1072.  
  1073. /* */
  1074. /* Now update the MSlast_monitor table for next round */
  1075. /* */
  1076.  
  1077. update MSlast_monitor set
  1078.     Last_time = @now,
  1079.     CPU_busy = @cpu_busy,
  1080.     IO_busy = @io_busy,
  1081.     Idle = @idle,
  1082.     Pkts_received = @pack_received,
  1083.     Pkts_sent = @pack_sent,
  1084.     Connections = @connections,
  1085.     Pkt_errors = @pack_errors,
  1086.     Total_reads = @total_read,
  1087.     Total_writes = @total_write,
  1088.     Total_errors = @total_errors
  1089.  
  1090. set nocount off
  1091.  
  1092.  
  1093. commit transaction
  1094.  
  1095. return
  1096. go
  1097.  
  1098.  
  1099. /************* DUMP THE TRANSACTION LOG **************************************/
  1100. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1101. /* script periodically, you will run out of transaction log space.           */
  1102. dump tran master with truncate_only
  1103. go
  1104. /************* END DUMP THE TRANSACTION LOG **********************************/
  1105.  
  1106.  
  1107. /****************************************************************************/
  1108. /* This stored procedure will get the name, size, owner, space available,
  1109.    and create date of all the databases in the system */
  1110. /****************************************************************************/
  1111.  
  1112. print ""
  1113. print "Creating sp_MSdatabase_list"
  1114. print ""
  1115. go
  1116. create procedure sp_MSdatabase_list
  1117. as
  1118.  
  1119. create table #spdbdesc        /* Create temporary table for storage. */
  1120. (
  1121.  dbid     smallint null
  1122. )
  1123.  
  1124. set nocount on
  1125.  
  1126. insert into #spdbdesc (dbid)
  1127.        select dbid from master.dbo.sysdatabases
  1128.  
  1129. set nocount off
  1130.  
  1131. select distinct name = master.dbo.sysdatabases.name,
  1132.         db_size = substring(convert(varchar(10),
  1133.             round((convert(float,master.dbo.spt_values.low) * sum((convert(float,master.dbo.sysusages.size))) / 1048576.0),0))
  1134.             + "MB", 1, 8),
  1135.         owner = master.dbo.syslogins.name,
  1136.                 spaceavail = "         ",
  1137.         created = convert(char(11), master.dbo.sysdatabases.crdate)
  1138. from master.dbo.sysdatabases, master.dbo.syslogins, master.dbo.sysusages, master.dbo.spt_values, #spdbdesc
  1139. where master.dbo.sysdatabases.dbid = #spdbdesc.dbid
  1140.       and master.dbo.sysdatabases.suid = master.dbo.syslogins.suid
  1141.       and #spdbdesc.dbid = master.dbo.sysusages.dbid
  1142.       and master.dbo.spt_values.type = "E"
  1143.       and master.dbo.spt_values.number = 1
  1144. group by #spdbdesc.dbid
  1145. having master.dbo.sysdatabases.dbid = #spdbdesc.dbid
  1146.        and master.dbo.sysdatabases.suid = master.dbo.syslogins.suid
  1147.        and #spdbdesc.dbid = master.dbo.sysusages.dbid
  1148.        and master.dbo.spt_values.type = "E"
  1149.        and master.dbo.spt_values.number = 1
  1150. order by master.dbo.sysdatabases.name
  1151. go
  1152.  
  1153.  
  1154. /****************************************************************************/
  1155. /* This stored procedure will get the space available for all the databases
  1156.    in the system */
  1157. /****************************************************************************/
  1158. print ""
  1159. print "Creating sp_MSdatabase_avail"
  1160. print ""
  1161. go
  1162. create procedure sp_MSdatabase_avail
  1163. as
  1164.  
  1165. declare @db_size float, @reserved float, @unused float, @low float
  1166. declare @spaceavail float
  1167. set nocount on
  1168.  
  1169. select @db_size =  ((sum((convert(float,size)) * d.low) / 1048576.0) * 1024.0)
  1170.            from master.dbo.sysusages, master.dbo.spt_values d
  1171.            where dbid = db_id() and d.number = 1 and d.type = "E"
  1172.            having dbid = db_id() and d.number = 1 and d.type = "E"
  1173.  
  1174. /* */
  1175. /* reserved: sum(reserved) where indid in (0, 1, 255) */
  1176. /* */
  1177.  
  1178. select @low = convert(float,d.low) from master.dbo.spt_values d where d.number = 1 and d.type = "E"
  1179.  
  1180. select @reserved = sum(convert(float,reserved)) from sysindexes where indid in (0, 1, 255)
  1181. select @reserved = (@reserved * @low)/1024.0
  1182.  
  1183. /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  1184.  
  1185. select @unused = sum(convert(float,reserved)) - sum(convert(float,used)) from sysindexes where indid in (0, 1, 255)
  1186. select @unused = (@unused * @low)/1024.0
  1187.  
  1188. set nocount off
  1189. select @spaceavail = (convert(int,round(@db_size,0)) - convert(int,round((@reserved - @unused),0)))
  1190.  
  1191. if(@spaceavail < 0)
  1192.     select space_avail = "0KB"
  1193. else
  1194.     select space_avail = convert(varchar(10),convert(int,round(@spaceavail,0)))+"KB"
  1195.  
  1196. go
  1197. /************* DUMP THE TRANSACTION LOG **************************************/
  1198. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1199. /* script periodically, you will run out of transaction log space.           */
  1200. dump tran master with truncate_only
  1201. go
  1202. /************* END DUMP THE TRANSACTION LOG **********************************/
  1203.  
  1204.  
  1205. /****************************************************************************/
  1206. /* This stored procedure will get all the databases that need to be dumped
  1207.    at the time of inquiry for a Windows NT SQL Server */
  1208. /****************************************************************************/
  1209. if exists (select * from sysobjects where name = "sp_MSbackup_now" and sysstat & 7 = 4)
  1210. begin
  1211.     drop procedure sp_MSbackup_now
  1212. end
  1213. go
  1214.  
  1215. print""
  1216. print "Ignore error message if installing on a OS/2 SQL Server"
  1217. print""
  1218. print ""
  1219. print "Creating sp_MSbackup_now"
  1220. print ""
  1221. go
  1222. create procedure sp_MSbackup_now as
  1223. declare @now datetime, @firstday smallint, @dayofweek smallint, @hour smallint, @minute smallint,
  1224. @elapsed int, @monthyear varchar(30)
  1225.  
  1226. set nocount on
  1227.  
  1228. /* */
  1229. /* Get the current date and time. */
  1230. /* Parse out the relevant parts of the date */
  1231. /* */
  1232.  
  1233. select @now = getdate()
  1234. if (@@langid=0)
  1235.     select @firstday=7
  1236. else
  1237.     select @firstday = datefirst from master..syslanguages where langid=@@langid
  1238.  
  1239. select @dayofweek = (datepart(weekday,@now)+@firstday)%7
  1240. select @monthyear = substring(convert(varchar(12),getdate()),1,12)
  1241.  
  1242. /* */
  1243. /* Create a temporary table that holds data on what needs to be dumped */
  1244. /* */
  1245.  
  1246. create table #spdumptab
  1247. (
  1248. id           int           Not Null,       /* Unique identifier */
  1249. name           varchar(30)     Not Null,       /* Name of database to be dumped */
  1250. owner           varchar(30)     Not Null,       /* Name of the database owner */
  1251. ddump           varchar(30)     Null,           /* Database dump device */
  1252. ldump           varchar(30)     Null,           /* Log dump device */
  1253. datacntrltype  smallint        Null,           /* Control type */
  1254. logcntrltype   smallint        Null,           /* Control type */
  1255. status         tinyint         Null,           /* extra dump parameters */
  1256. trys           tinyint         Null,           /* number of attempts to dump */
  1257. emailname      varchar(60)     Null,           /* email recipient(s) for notification */
  1258. dumptime       varchar(32)     Not Null,       /* scheduled event time */
  1259. day            tinyint         Not Null,       /* day of week for dump */
  1260. freq           tinyint         Not Null        /* frequency of dump */
  1261. )
  1262.  
  1263. /* */
  1264. /* Check all the databases that are dumped daily, weekly, and biweekly */
  1265. /* Note: The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1266. /* */
  1267.  
  1268. insert into #spdumptab
  1269. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL,Stat,attempts,email_name,
  1270.     @monthyear+Start_time,Day,Frequency
  1271. from MSscheduled_backups
  1272. where Enabled = 1                         /* Dump turned on */
  1273.       and ((convert(smallint,Day) = @dayofweek) or Frequency = 1)             /* Dump today or Daily */
  1274.       and Frequency <= 14                     /* Freq daily, weekly, or biweekly */
  1275.       and datediff(day, Last_dump, @now) >= convert(smallint,Frequency)         /* Freq time has elapsed */
  1276.       and @now >= convert(datetime, @monthyear + Start_time)
  1277.       and datediff(hour, Last_dump, @now) >= convert(smallint,Frequency)*24    /* Freq time has elapsed */
  1278.       and datediff(minute, Last_dump, @now) >= convert(smallint,Frequency)*24*60    /* Freq time has elapsed */
  1279.  
  1280. /* */
  1281. /* Check all the databases that are dumped monthly */
  1282. /* Note: First we get this week number, then do the same criteria as the
  1283.      Daily, weekly, bi-weekly dump.
  1284.      The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1285. /* */
  1286.  
  1287. declare @rundate datetime, @weekno smallint     /* Get this week number */
  1288. select @rundate = @now
  1289. select @weekno = 1
  1290. while datepart(month,dateadd(day,-7,@rundate)) = datepart(month,@now)
  1291. begin
  1292.     select @weekno = @weekno + 1
  1293.     select @rundate = dateadd(day,-7,@rundate)
  1294. end
  1295.  
  1296. insert into #spdumptab
  1297. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL,Stat,attempts,email_name,
  1298. @monthyear+Start_time,Day,Frequency from MSscheduled_backups
  1299. where Enabled = 1                         /* Dump turned on */
  1300.       and (convert(smallint,Day) = @dayofweek)                 /* Dump today */
  1301.       and Frequency >= 31                     /* Freq monthly */
  1302.       and (convert(smallint,Frequency) - 22) >= @weekno         /* Week of month */
  1303.       and datediff(day, Last_dump, @now) >= 22             /* Freq time has elapsed */
  1304.       and @now >= convert(datetime, @monthyear + Start_time)
  1305.       and datediff(hour, Last_dump, @now) >= 22*24    /* Freq time has elapsed */
  1306.       and datediff(minute, Last_dump, @now) >= 22*24*60    /* Freq time has elapsed */
  1307.  
  1308.  
  1309. update #spdumptab set datacntrltype = (select distinct cntrltype from master..sysdevices s where
  1310. #spdumptab.ddump = s.name)
  1311.  
  1312. update #spdumptab set logcntrltype = (select distinct cntrltype from master..sysdevices s where
  1313. #spdumptab.ldump = s.name)
  1314.  
  1315.  
  1316. set nocount off
  1317.  
  1318. /* */
  1319. /* Output the values to the daemon */
  1320. /* */
  1321.  
  1322. select id = id, name = name, owner = owner, ddump = ddump, ldump = ldump,
  1323. dcntrl = datacntrltype, lcntrl = logcntrltype, stat = status, attempt=trys,
  1324. email=emailname,dumptime=convert(varchar(32),convert(datetime,dumptime)),day,freq from #spdumptab
  1325. order by (convert(datetime,dumptime))
  1326. go
  1327.  
  1328.  
  1329. /****************************************************************************/
  1330. /* This stored procedure will get all the databases that need to be dumped
  1331.    at the time of inquiry for an OS/2 SQL Server*/
  1332. /****************************************************************************/
  1333. if patindex('%Windows%',@@version) = 0 and
  1334.    exists (select * from sysobjects where name = "sp_MSbackup_now" and sysstat & 7 = 4)
  1335. begin
  1336.         print "OS/2 server, dropping NT version of sp_MSbackup_now"
  1337.     drop procedure sp_MSbackup_now
  1338. end
  1339. go
  1340.  
  1341. print""
  1342. print "Ignore error message if installing on a Windows NT SQL Server"
  1343. print""
  1344. print ""
  1345. print "Creating sp_MSbackup_now"
  1346. print ""
  1347. go
  1348.  
  1349. create procedure sp_MSbackup_now as
  1350. declare @now datetime, @firstday smallint, @dayofweek smallint, @hour smallint, @minute smallint,
  1351. @elapsed int, @monthyear varchar(30)
  1352.  
  1353. set nocount on
  1354.  
  1355. /* */
  1356. /* Get the current date and time. */
  1357. /* Parse out the relevant parts of the date */
  1358. /* */
  1359.  
  1360. select @now = getdate()
  1361. if (@@langid=0)
  1362.     select @firstday=7
  1363. else
  1364.     select @firstday = datefirst from master..syslanguages where langid=@@langid
  1365.  
  1366. select @dayofweek = (datepart(weekday,@now)+@firstday)%7
  1367. select @monthyear = substring(convert(varchar(12),getdate()),1,12)
  1368.  
  1369. /* */
  1370. /* Create a temporary table that holds data on what needs to be dumped */
  1371. /* */
  1372.  
  1373. create table #spdumptab
  1374. (
  1375. id           int           Not Null,       /* Unique identifier */
  1376. name           varchar(30)     Not Null,       /* Name of database to be dumped */
  1377. owner           varchar(30)     Not Null,       /* Name of the database owner */
  1378. ddump           varchar(30)     Null,           /* Database dump device */
  1379. ldump           varchar(30)     Null,           /* Log dump device */
  1380. datacntrltype  smallint        Null,           /* Control type */
  1381. logcntrltype   smallint        Null,           /* Control type */
  1382. )
  1383.  
  1384. /* */
  1385. /* Check all the databases that are dumped daily, weekly, and biweekly */
  1386. /* Note: The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1387. /* */
  1388.  
  1389. insert into #spdumptab
  1390. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL
  1391. from MSscheduled_backups
  1392. where Enabled = 1                         /* Dump turned on */
  1393.       and ((convert(smallint,Day) = @dayofweek) or Frequency = 1)             /* Dump today or Daily */
  1394.       and Frequency <= 14                     /* Freq daily, weekly, or biweekly */
  1395.       and datediff(day, Last_dump, @now) >= convert(smallint,Frequency)         /* Freq time has elapsed */
  1396.       and @now >= convert(datetime, @monthyear + Start_time)
  1397.       and datediff(hour, Last_dump, @now) >= convert(smallint,Frequency)*24    /* Freq time has elapsed */
  1398.       and datediff(minute, Last_dump, @now) >= convert(smallint,Frequency)*24*60    /* Freq time has elapsed */
  1399.  
  1400. /* */
  1401. /* Check all the databases that are dumped monthly */
  1402. /* Note: First we get this week number, then do the same criteria as the
  1403.      Daily, weekly, bi-weekly dump.
  1404.      The dump can only occur if the start time(HH:MM) is > the last dump <= now */
  1405. /* */
  1406.  
  1407. declare @rundate datetime, @weekno smallint     /* Get this week number */
  1408. select @rundate = @now
  1409. select @weekno = 1
  1410. while datepart(month,dateadd(day,-7,@rundate)) = datepart(month,@now)
  1411. begin
  1412.     select @weekno = @weekno + 1
  1413.     select @rundate = dateadd(day,-7,@rundate)
  1414. end
  1415.  
  1416. insert into #spdumptab
  1417. select Event_id, Database_name, Database_owner,Database_dump,Log_dump,NULL,NULL
  1418. from MSscheduled_backups
  1419. where Enabled = 1                         /* Dump turned on */
  1420.       and (convert(smallint,Day) = @dayofweek)                     /* Dump today */
  1421.       and Frequency >= 31                     /* Freq monthly */
  1422.       and (convert(smallint,Frequency) - 22) >= @weekno                 /* Week of month */
  1423.       and datediff(day, Last_dump, @now) >= 22             /* Freq time has elapsed */
  1424.       and @now >= convert(datetime, @monthyear + Start_time)
  1425.       and datediff(hour, Last_dump, @now) >= 22*24    /* Freq time has elapsed */
  1426.       and datediff(minute, Last_dump, @now) >= 22*24*60    /* Freq time has elapsed */
  1427.  
  1428.  
  1429. update #spdumptab set datacntrltype = (select cntrltype from master..sysdevices s where
  1430. #spdumptab.ddump = s.name)
  1431.  
  1432. update #spdumptab set logcntrltype = (select cntrltype from master..sysdevices s where
  1433. #spdumptab.ldump = s.name)
  1434.  
  1435.  
  1436. set nocount off
  1437.  
  1438. /* */
  1439. /* Output the values to the daemon */
  1440. /* */
  1441.  
  1442. select id = id, name = name, owner = owner, ddump = ddump, ldump = ldump,
  1443. dcntrl = datacntrltype, lcntrl = logcntrltype from #spdumptab
  1444. go
  1445.  
  1446.  
  1447.  
  1448. /************* DUMP THE TRANSACTION LOG **************************************/
  1449. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1450. /* script periodically, you will run out of transaction log space.           */
  1451. dump tran master with truncate_only
  1452. go
  1453. /************* END DUMP THE TRANSACTION LOG **********************************/
  1454.  
  1455. /*
  1456. ** This stored procedure returns the current scheduling for backups, it will
  1457. ** get the database name, day (in chars), frequency (in chars), start time,
  1458. ** and enabled status (in chars) from MSscheduled_backups.
  1459. */
  1460.  
  1461.  
  1462. print ""
  1463. print "Creating sp_MSbackup_schedule"
  1464. print ""
  1465. go
  1466. create procedure sp_MSbackup_schedule as
  1467.  
  1468. /* Create a tempory table to hold the results */
  1469.  
  1470. create table #spbacksch
  1471. (id int,
  1472.  dataname char (30) not null,
  1473.  day char (9) null,
  1474.  frequency char (9) null,
  1475.  start_time char(10) not null,
  1476.  enabled char (8) null )
  1477.  
  1478. set nocount on
  1479.  
  1480. /* Fill the temporary table with the id, database name, and start time in the schedule table */
  1481.  
  1482. insert into #spbacksch select d.Event_id,d.Database_name,NULL,NULL,convert(char,d.Start_time),NULL from MSscheduled_backups d
  1483.  
  1484. /* Now set the day of week to it's character equivalent from a numeric value */
  1485.  
  1486. update #spbacksch set day = "*********" from MSscheduled_backups d
  1487.                 where d.Day = 0 and d.Event_id = id
  1488. update #spbacksch set day = "Sunday" from MSscheduled_backups d
  1489.         where d.Day = 1 and d.Event_id = id
  1490. update #spbacksch set day = "Monday" from MSscheduled_backups d
  1491.         where d.Day = 2 and d.Event_id = id
  1492. update #spbacksch set day = "Tuesday" from MSscheduled_backups d
  1493.         where d.Day = 3 and d.Event_id = id
  1494. update #spbacksch set day = "Wednesday" from MSscheduled_backups d
  1495.         where d.Day = 4 and d.Event_id = id
  1496. update #spbacksch set day = "Thursday" from MSscheduled_backups d
  1497.         where d.Day = 5 and d.Event_id = id
  1498. update #spbacksch set day = "Friday" from MSscheduled_backups d
  1499.         where d.Day = 6 and d.Event_id = id
  1500. update #spbacksch set day = "Saturday" from MSscheduled_backups d
  1501.         where d.Day = 7 and d.Event_id = id
  1502.  
  1503. /* Now set the frequency to it's character equivalent from a numeric value */
  1504.  
  1505. update #spbacksch set frequency = "Daily" from MSscheduled_backups d
  1506.                 where d.Frequency = 1 and d.Event_id = id
  1507. update #spbacksch set frequency = "Weekly" from MSscheduled_backups d
  1508.                 where d.Frequency = 7 and d.Event_id = id
  1509. update #spbacksch set frequency = "Bi-Weekly" from MSscheduled_backups d
  1510.                 where d.Frequency = 14 and d.Event_id = id
  1511. update #spbacksch set frequency = "Monthly - Week 1" from MSscheduled_backups d
  1512.                 where d.Frequency = 31 and d.Event_id = id
  1513. update #spbacksch set frequency = "Monthly - Week 2" from MSscheduled_backups d
  1514.                 where d.Frequency = 32 and d.Event_id = id
  1515. update #spbacksch set frequency = "Monthly - Week 3" from MSscheduled_backups d
  1516.                 where d.Frequency = 33 and d.Event_id = id
  1517. update #spbacksch set frequency = "Monthly - Week 4" from MSscheduled_backups d
  1518.                 where d.Frequency = 34 and d.Event_id = id
  1519.  
  1520. /* Now set the enabled status to it's character equivalent from a numeric value */
  1521.  
  1522. update #spbacksch set enabled = "No" from MSscheduled_backups d
  1523.                 where d.Enabled = 0 and d.Event_id = id
  1524. update #spbacksch set enabled = "Yes" from MSscheduled_backups d
  1525.                 where d.Enabled = 1 and d.Event_id = id
  1526.  
  1527. set nocount off
  1528.  
  1529. /* select the data from the temporary table to give the schedule in a human understandable form */
  1530.  
  1531. select dataname, day, frequency, start_time, enabled, convert(char,id) from #spbacksch
  1532.                order by dataname,id
  1533.  
  1534. go
  1535.  
  1536. /************* DUMP THE TRANSACTION LOG **************************************/
  1537. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1538. /* script periodically, you will run out of transaction log space.           */
  1539. dump tran master with truncate_only
  1540. go
  1541. /************* END DUMP THE TRANSACTION LOG **********************************/
  1542.  
  1543.  
  1544.  
  1545. /****************************************************************************/
  1546. /* This stored proc will get the login id, user name, alias, and group for
  1547.     all the users in a database for the MDI list box            */
  1548. /****************************************************************************/
  1549. print ""    /* print this or script hangs with some servers */
  1550.  
  1551. print ""
  1552. print "Creating sp_MSuser_list"
  1553. print ""
  1554. go
  1555. create procedure sp_MSuser_list
  1556. as
  1557.  
  1558. create table #spusers       /* Create temporary table for storage. */
  1559. (
  1560.  login_id varchar(30) null,
  1561.  user_name varchar(30) null,
  1562.  alias_name varchar(30) null,
  1563.  group_name varchar(30) null
  1564. )
  1565.  
  1566. set nocount on
  1567.  
  1568. /* */
  1569. /* Fill the temp table with system and user names */
  1570. /* */
  1571.  
  1572. insert into #spusers
  1573. select suser_name(suid), user_name(uid), "", "" from sysusers
  1574. where uid < 16383 and uid > 0
  1575.  
  1576. /* */
  1577. /* Get the aliases first */
  1578. /* */
  1579.  
  1580. insert into #spusers
  1581. select suser_name(sysalternates.suid),"", sysusers.name, ""
  1582. from sysusers, sysalternates, #spusers
  1583. where sysalternates.altsuid = suser_id(login_id)
  1584. and sysusers.suid = suser_id(login_id)
  1585.  
  1586. /* */
  1587. /* Get the group names */
  1588. /* */
  1589.  
  1590. update #spusers set group_name = g.name from sysusers u, sysusers g, #spusers
  1591. where u.suid = suser_id(#spusers.login_id) and
  1592.       u.uid = user_id(#spusers.user_name) and
  1593.       u.gid *= g.uid and
  1594.       u.uid <= 16383 and u.uid > 0
  1595.  
  1596. set nocount off
  1597.  
  1598. select * from #spusers
  1599. go
  1600.  
  1601. /****************************************************************************/
  1602. /* This stored procedure will get the needed values for the statistics
  1603. ** monitoring dialog.  This also updates the system monitor in the same
  1604. ** fashion as sp_monitor, thereby mimicing what sp_monitor does.
  1605. */
  1606. /****************************************************************************/
  1607.  
  1608. print ""
  1609. print "Creating sp_MSmonitor"
  1610. print ""
  1611. go
  1612. create procedure sp_MSmonitor
  1613. as
  1614.  
  1615. set nocount on
  1616.  
  1617. /*
  1618. **  Declare variables to be used to hold current monitor values.
  1619. */
  1620. declare @now         datetime
  1621. declare @cpu_busy     int
  1622. declare @io_busy    int
  1623. declare @idle        int
  1624. declare @pack_received    int
  1625. declare @pack_sent    int
  1626. declare @pack_errors    int
  1627. declare @connections    int
  1628. declare @total_read    int
  1629. declare @total_write    int
  1630. declare @total_errors    int
  1631.  
  1632. declare @oldcpu_busy     int    /* used to see if SQL Server has been rebooted */
  1633. declare @interval    int
  1634. declare @mspertick    int    /* milliseconds per tick */
  1635.  
  1636. /*
  1637. **  Set @mspertick.  This is just used to make the numbers easier to handle
  1638. **  and avoid overflow.
  1639. */
  1640. select @mspertick = convert(int, @@timeticks / 1000.0)
  1641.  
  1642. /*
  1643. **  Get current monitor values.
  1644. */
  1645. begin transaction
  1646. select
  1647.     @now = getdate(),
  1648.     @cpu_busy = @@cpu_busy,
  1649.     @io_busy = @@io_busy,
  1650.     @idle = @@idle,
  1651.     @pack_received = @@pack_received,
  1652.     @pack_sent = @@pack_sent,
  1653.     @connections = @@connections,
  1654.     @pack_errors = @@packet_errors,
  1655.     @total_read = @@total_read,
  1656.     @total_write = @@total_write,
  1657.     @total_errors = @@total_errors
  1658.  
  1659. /*
  1660. **  Check to see if SQL Server has been rebooted.  If it has then the
  1661. **  value of @@cpu_busy will be less than the value of master..spt_monitor.cpu_busy.
  1662. **  If it has update master..spt_monitor.
  1663. */
  1664. select @oldcpu_busy = cpu_busy
  1665.     from master..spt_monitor
  1666. if @oldcpu_busy > @cpu_busy
  1667. begin
  1668.     update master..spt_monitor
  1669.         set
  1670.             lastrun = @now,
  1671.             cpu_busy = @cpu_busy,
  1672.             io_busy = @io_busy,
  1673.             idle = @idle,
  1674.             pack_received = @pack_received,
  1675.             pack_sent = @pack_sent,
  1676.             connections = @connections,
  1677.             pack_errors = @pack_errors,
  1678.             total_read = @total_read,
  1679.             total_write = @total_write,
  1680.             total_errors = @total_errors
  1681. end
  1682.  
  1683. /*
  1684. **  Now print out old and new monitor values.
  1685. */
  1686. select @interval = datediff(ss, lastrun, @now) from master..spt_monitor s
  1687.  
  1688. select last_run = convert(char(30),lastrun),seconds = convert(char(30),@interval)
  1689.     from master..spt_monitor s
  1690.  
  1691. select
  1692.     CpuBusy = convert(varchar(30),"CPU Busy (ms)") + ","
  1693.                 + convert(varchar(18),((@cpu_busy * @mspertick) / 1000)) + ","
  1694.         + convert(varchar(18),((@cpu_busy - cpu_busy)* @mspertick) / 1000) + ","
  1695.         + convert(varchar(18), ((((@cpu_busy - cpu_busy)* @mspertick) / 1000) * 100) / @interval),
  1696.     IOBusy = convert(varchar(30),"IO Busy (ms)") + ","
  1697.                 + convert(varchar(18),((@io_busy * @mspertick) / 1000)) + ","
  1698.         + convert(varchar(18),(((@io_busy - io_busy)* @mspertick) / 1000)) + ","
  1699.         + convert(varchar(18), ((((@io_busy - io_busy)* @mspertick) / 1000) * 100) / @interval),
  1700.     Idle =   convert(varchar(30),"Idle Time (ms)") + ","
  1701.                 + convert(varchar(18),((@idle * @mspertick) / 1000)) + ","
  1702.         + convert(varchar(18),(((@idle - idle)* @mspertick) / 1000)) + ","
  1703.         + convert(varchar(18),((((@idle - idle)* @mspertick) / 1000) * 100) / @interval),
  1704.     PacketsReceived = convert(varchar(30),"Packets Received") + ","
  1705.                 + convert(varchar(18), @pack_received) + ","
  1706.         + convert(varchar(18), @pack_received - pack_received),
  1707.         PacketsSent = convert(varchar(30),"Packets Sent") + ","
  1708.         + convert(varchar(18), @pack_sent) + ","
  1709.                 + convert(varchar(18), @pack_sent - pack_sent),
  1710.     PacketErrors = convert(varchar(30),"Packet Errors") + ","
  1711.                 + convert(varchar(18), @pack_errors) + ","
  1712.                 + convert(varchar(18), @pack_errors - pack_errors),
  1713.     TotalRead = convert(varchar(30),"Total Read") + ","
  1714.                 + convert(varchar(18), @total_read) + ","
  1715.         + convert(varchar(18), @total_read - total_read),
  1716.         TotalWrite = convert(varchar(30),"Total Write") + ","
  1717.                 + convert(varchar(18), @total_write) + ","
  1718.         + convert(varchar(18), @total_write - total_write),
  1719.     TotalErrors = convert(varchar(30),"Total Errors") + ","
  1720.                 + convert(varchar(18), @total_errors) + ","
  1721.         + convert(varchar(18), @total_errors - total_errors),
  1722.     Connections = convert(varchar(30),"Connections") + ","
  1723.                 + convert(varchar(18), @connections) + ","
  1724.         + convert(varchar(18), @connections - connections)
  1725.        from master..spt_monitor
  1726.  
  1727. /*
  1728. **  Now update master..spt_monitor
  1729. */
  1730. update master..spt_monitor
  1731.     set
  1732.         lastrun = @now,
  1733.         cpu_busy = @cpu_busy,
  1734.         io_busy = @io_busy,
  1735.         idle = @idle,
  1736.         pack_received = @pack_received,
  1737.         pack_sent = @pack_sent,
  1738.         connections = @connections,
  1739.         pack_errors = @pack_errors,
  1740.         total_read = @total_read,
  1741.         total_write = @total_write,
  1742.         total_errors = @total_errors
  1743.  
  1744. commit transaction
  1745. set nocount off
  1746. return
  1747. go
  1748.  
  1749. /************* DUMP THE TRANSACTION LOG **************************************/
  1750. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1751. /* script periodically, you will run out of transaction log space.           */
  1752. dump tran master with truncate_only
  1753. go
  1754. /************* END DUMP THE TRANSACTION LOG **********************************/
  1755.  
  1756.  
  1757. /*****************************************************************************/
  1758. /* This stored procedure gets all the users that have particular permissions */
  1759. /*****************************************************************************/
  1760.  
  1761. print ""
  1762. print "Creating sp_MScmd_permissions"
  1763. print ""
  1764. go
  1765. create procedure sp_MScmd_permissions
  1766. @cre_dflt bit, @cre_proc bit, @cre_rule bit, @cre_table bit, @cre_db bit,
  1767. @cre_view bit, @dmp_db bit, @dmp_tran bit
  1768. as
  1769.  
  1770. set nocount on
  1771.  
  1772. create table #tmp_permiss
  1773. (name     varchar(30),
  1774.  dflt     bit,
  1775.  creproc bit,
  1776.  crerule bit,
  1777.  cretble bit,
  1778.  credb     bit,
  1779.  creview bit,
  1780.  dmpdb     bit,
  1781.  dmptran bit
  1782. )
  1783.  
  1784. insert into #tmp_permiss
  1785. select user_name(uid),0,0,0,0,0,0,0,0 from sysusers
  1786.  
  1787. update #tmp_permiss set dflt = 1 from sysprotects where
  1788. name = user_name(uid) and
  1789. protecttype = (select number from master..spt_values where name = 'Grant')
  1790. and action = (select number from master..spt_values where name = 'Create Default')
  1791.  
  1792. update #tmp_permiss set creproc = 1 from sysprotects where
  1793. name = user_name(uid) and
  1794. protecttype = (select number from master..spt_values where name = 'Grant')
  1795. and action = (select number from master..spt_values where name = 'Create Procedure')
  1796.  
  1797. update #tmp_permiss set crerule = 1 from sysprotects where
  1798. name = user_name(uid) and
  1799. protecttype = (select number from master..spt_values where name = 'Grant')
  1800. and action = (select number from master..spt_values where name = 'Create Rule')
  1801.  
  1802. update #tmp_permiss set cretble = 1 from sysprotects where
  1803. name = user_name(uid) and
  1804. protecttype = (select number from master..spt_values where name = 'Grant')
  1805. and action = (select number from master..spt_values where name = 'Create Table')
  1806.  
  1807. update #tmp_permiss set credb = 1 from sysprotects where
  1808. name = user_name(uid) and
  1809. protecttype = (select number from master..spt_values where name = 'Grant')
  1810. and action = (select number from master..spt_values where name = 'Create Database')
  1811.  
  1812. update #tmp_permiss set creview = 1 from sysprotects where
  1813. name = user_name(uid) and
  1814. protecttype = (select number from master..spt_values where name = 'Grant')
  1815. and action = (select number from master..spt_values where name = 'Create View')
  1816.  
  1817. update #tmp_permiss set dmpdb = 1 from sysprotects where
  1818. name = user_name(uid) and
  1819. protecttype = (select number from master..spt_values where name = 'Grant')
  1820. and action = (select number from master..spt_values where name = 'Dump Database')
  1821.  
  1822. update #tmp_permiss set dmptran = 1 from sysprotects where
  1823. name = user_name(uid) and
  1824. protecttype = (select number from master..spt_values where name = 'Grant')
  1825. and action = (select number from master..spt_values where name = 'Dump Transaction')
  1826.  
  1827. set nocount off
  1828.  
  1829. select name from #tmp_permiss
  1830. where dflt = @cre_dflt and creproc = @cre_proc and crerule = @cre_rule
  1831. and cretble = @cre_table and credb = @cre_db and creview = @cre_view
  1832. and dmpdb = @dmp_db and dmptran = @dmp_tran
  1833. go
  1834.  
  1835. /*****************************************************************************/
  1836. /* This stored procedure gets all the users that have particular permissions */
  1837. /*****************************************************************************/
  1838.  
  1839. print ""
  1840. print "Creating sp_MSuser_cmd_permissions"
  1841. print ""
  1842. go
  1843. create procedure sp_MSuser_cmd_permissions @username varchar(30)
  1844. as
  1845.  
  1846. /*  TCD 8/2/91 See if the user exists. */
  1847.  
  1848. if not exists (select * from dbo.sysusers
  1849.              where name = @username)
  1850. begin
  1851.     raiserror 70003 "User doesn't exist. The 'sa' or 'dbo' must have dropped the user since you last displayed the listing."
  1852.     return
  1853. end
  1854.  
  1855. set nocount on
  1856.  
  1857. create table #tmp_permiss
  1858. (dflt     bit,
  1859.  creproc bit,
  1860.  crerule bit,
  1861.  cretble bit,
  1862.  credb     bit,
  1863.  creview bit,
  1864.  dmpdb     bit,
  1865.  dmptran bit
  1866. )
  1867.  
  1868. if user_id(@username) = 1     /* dbo has all permissions */
  1869.   insert into #tmp_permiss
  1870.   values (1,1,1,1,1,1,1,1)
  1871. else
  1872. begin
  1873.   insert into #tmp_permiss
  1874.   values (0,0,0,0,0,0,0,0)
  1875.  
  1876.   update #tmp_permiss set dflt = 1 from sysprotects where
  1877.   user_name(uid) = @username and
  1878.   protecttype = (select number from master..spt_values where name = 'Grant')
  1879.   and action = (select number from master..spt_values where name = 'Create Default')
  1880.  
  1881.   update #tmp_permiss set creproc = 1 from sysprotects where
  1882.   user_name(uid) = @username and
  1883.   protecttype = (select number from master..spt_values where name = 'Grant')
  1884.   and action = (select number from master..spt_values where name = 'Create Procedure')
  1885.  
  1886.   update #tmp_permiss set crerule = 1 from sysprotects where
  1887.   user_name(uid) = @username and
  1888.   protecttype = (select number from master..spt_values where name = 'Grant')
  1889.   and action = (select number from master..spt_values where name = 'Create Rule')
  1890.  
  1891.   update #tmp_permiss set cretble = 1 from sysprotects where
  1892.   user_name(uid) = @username and
  1893.   protecttype = (select number from master..spt_values where name = 'Grant')
  1894.   and action = (select number from master..spt_values where name = 'Create Table')
  1895.  
  1896.   update #tmp_permiss set credb = 1 from sysprotects where
  1897.   user_name(uid) = @username and
  1898.   protecttype = (select number from master..spt_values where name = 'Grant')
  1899.   and action = (select number from master..spt_values where name = 'Create Database')
  1900.  
  1901.   update #tmp_permiss set creview = 1 from sysprotects where
  1902.   user_name(uid) = @username and
  1903.   protecttype = (select number from master..spt_values where name = 'Grant')
  1904.   and action = (select number from master..spt_values where name = 'Create View')
  1905.  
  1906.   update #tmp_permiss set dmpdb = 1 from sysprotects where
  1907.   user_name(uid) = @username and
  1908.   protecttype = (select number from master..spt_values where name = 'Grant')
  1909.   and action = (select number from master..spt_values where name = 'Dump Database')
  1910.  
  1911.   update #tmp_permiss set dmptran = 1 from sysprotects where
  1912.   user_name(uid) = @username and
  1913.   protecttype = (select number from master..spt_values where name = 'Grant')
  1914.   and action = (select number from master..spt_values where name = 'Dump Transaction')
  1915. end
  1916.  
  1917. set nocount off
  1918.  
  1919. select * from #tmp_permiss
  1920. go
  1921.  
  1922.  
  1923. /************* DUMP THE TRANSACTION LOG **************************************/
  1924. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  1925. /* script periodically, you will run out of transaction log space.           */
  1926. dump tran master with truncate_only
  1927. go
  1928. /************* END DUMP THE TRANSACTION LOG **********************************/
  1929.  
  1930. /****************************************************************************/
  1931. /* This stored procedure will get the name, device, size, and location of
  1932.    each segment for a particular database. You must be in the db to exec    */
  1933. /****************************************************************************/
  1934.  
  1935. print ""
  1936. print "Creating sp_MSsegment_list"
  1937. print ""
  1938. go
  1939. create procedure sp_MSsegment_list as
  1940.  
  1941. declare @segbit int
  1942.  
  1943. /* Create a temporary table where we can store all the necessary information */
  1944.  
  1945. set nocount on
  1946.  
  1947. create table #spsegtab1
  1948. (name varchar(30) null,
  1949.  segnumber smallint,
  1950.  segbit int null
  1951. )
  1952.  
  1953. create table #spsegtab2
  1954. (
  1955.  name varchar(30) null,
  1956.  device varchar(30) null,
  1957. )
  1958.  
  1959. /* Get all the segments in the system and their status */
  1960. /* Set the segments on @devname master.dbo.sysusages. */
  1961. /*    If segment 31, power(2, 31) will overflow
  1962.       since segmap is an int.  We'll grab the machine-dependent
  1963.       bit mask from master.dbo.spt_values to set the right bit. */
  1964.  
  1965. insert into #spsegtab1 (name, segnumber) select name, segment from syssegments
  1966. update #spsegtab1 set segbit = power(2, segnumber) where segnumber < 31
  1967. update #spsegtab1 set segbit = low from master.dbo.spt_values
  1968.           where type = "E" and number = 2
  1969.             and segnumber >= 31
  1970.  
  1971.  
  1972. /* Populate the #spsegtab2 with the devices for each segment */
  1973.  
  1974.  
  1975. insert into #spsegtab2
  1976. select distinct #spsegtab1.name, d.name
  1977. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v, #spsegtab1
  1978.         where u.segmap & #spsegtab1.segbit = #spsegtab1.segbit
  1979.             and d.low <= u.size + u.vstart
  1980.             and d.high >= u.size + u.vstart - 1
  1981.             and u.dbid = db_id()
  1982.             and d.status & 2 = 2
  1983.             and v.number = 1
  1984.             and v.type = "E"
  1985.  
  1986.  
  1987. set nocount off
  1988.  
  1989. select * from #spsegtab2
  1990. order by name
  1991. go
  1992. /****************************************************************************/
  1993. /* This stored procedure will return a list of the devices that contain
  1994.    a segment of a given name.                            */
  1995. /****************************************************************************/
  1996.  
  1997. print ""
  1998. print "Creating sp_MSsegment_devices"
  1999. print ""
  2000. go
  2001. create procedure sp_MSsegment_devices @segname varchar(30)        /* segment name */
  2002. as
  2003. declare    @segbit    int        /* this is the bit version of the segment # */
  2004. declare    @segment    int    /* the segment number of the segment */
  2005. set nocount on
  2006. /*
  2007. **  Set the bit position for the segment.
  2008. */
  2009. select @segment = segment
  2010.     from syssegments
  2011.         where name = @segname
  2012.  
  2013. /*
  2014. **  Now set the segments on @devname master.dbo.sysusages.
  2015. */
  2016. if (@segment < 31)
  2017.     select @segbit = power(2, @segment)
  2018. else
  2019.     /*
  2020.     **  Since this is segment 31, power(2, 31) will overflow
  2021.     **  since segmap is an int.  We'll grab the machine-dependent
  2022.     **  bit mask from master.dbo.spt_values to set the right bit.
  2023.     */
  2024.     select @segbit = low
  2025.         from master.dbo.spt_values
  2026.             where type = "E"
  2027.                 and number = 2
  2028.  
  2029. select distinct device = d.name
  2030.     from master.dbo.sysusages u, master.dbo.sysdevices d,
  2031.         master.dbo.spt_values v
  2032.         where u.segmap & @segbit = @segbit
  2033.             and d.low <= u.size + u.vstart
  2034.             and d.high >= u.size + u.vstart - 1
  2035.             and u.dbid = db_id()
  2036.             and d.status & 2 = 2
  2037.             and v.number = 1
  2038.             and v.type = "E"
  2039. set nocount off
  2040. go
  2041.  
  2042. /****************************************************************************/
  2043. /* This stored procedure will get the properties of a particular segment in
  2044.    in the system                                */
  2045. /****************************************************************************/
  2046.  
  2047. print ""
  2048. print "Creating sp_MSseg_properties"
  2049. print ""
  2050. go
  2051. create procedure sp_MSseg_properties @segname varchar(30) as
  2052.  
  2053. declare @segbit int, @segsize varchar(10), @segment int, @segdevs varchar(255)
  2054.  
  2055. /*  TCD 8/2/91 See if the segment exists. */
  2056.  
  2057. if not exists (select * from syssegments
  2058.              where name = @segname)
  2059. begin
  2060.     raiserror 70005 "Segment doesn't exist. The 'sa' or 'dbo' must have dropped the segment since you last displayed the listing."
  2061.     return
  2062. end
  2063.  
  2064. set nocount on
  2065.  
  2066. /* Set the bit position for the segment */
  2067.  
  2068. select @segment = segment from syssegments where name = @segname
  2069. if (@segment < 31)
  2070.     select @segbit = power(2, @segment)
  2071. else
  2072.  
  2073.     /*  Since this is segment 31, power(2, 31) will overflow
  2074.     **  since segmap is an int.  We'll grab the machine-dependent
  2075.     **  bit mask from master.dbo.spt_values to set the right bit. */
  2076.  
  2077.      select @segbit = low from master.dbo.spt_values where type = "E" and number = 2
  2078.  
  2079. /* Get the total size of the segment */
  2080.  
  2081. select @segsize = convert(varchar(20), sum(round((u.size * convert(float, v.low)) / 1048576, 0))) + "MB"
  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. set nocount off
  2092.  
  2093. /* output the segment number and the segment size */
  2094.  
  2095. select segment_number = @segment, segment_size = @segsize
  2096.  
  2097. /* output the device names */
  2098.  
  2099. select distinct d.name
  2100. from master.dbo.sysusages u, master.dbo.sysdevices d, master.dbo.spt_values v
  2101. where u.segmap & @segbit = @segbit
  2102.       and d.low <= u.size + u.vstart
  2103.       and d.high >= u.size + u.vstart - 1
  2104.       and u.dbid = db_id()
  2105.       and d.status & 2 = 2
  2106.       and v.number = 1
  2107.       and v.type = "E"
  2108.  
  2109. go
  2110.  
  2111. /****************************************************************************/
  2112. /* This stored procedure will get the tables/indexes for a particular
  2113.    segment in the system                            */
  2114. /****************************************************************************/
  2115.  
  2116. print ""
  2117. print "Creating sp_MSseg_tables"
  2118. print ""
  2119. go
  2120. create procedure sp_MSseg_tables @segname varchar(30) as
  2121. select table_name = object_name(i.id), index_name = i.name, i.indid
  2122. from sysindexes i, syssegments s
  2123. where s.name = @segname and s.segment = i.segment
  2124. order by table_name, indid
  2125. go
  2126. /************* DUMP THE TRANSACTION LOG **************************************/
  2127. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2128. /* script periodically, you will run out of transaction log space.           */
  2129. dump tran master with truncate_only
  2130. go
  2131. /************* END DUMP THE TRANSACTION LOG **********************************/
  2132.  
  2133. /*****************************************************************************/
  2134. /* This stored procedure gets all the options set for a particular database  */
  2135. /* It applies to the current database.                         */
  2136. /* It is generally called by sp_MSdb_properties                  */
  2137. /*****************************************************************************/
  2138.  
  2139.  
  2140. print ""
  2141. print "Creating sp_MSdb_options_bits"
  2142. print ""
  2143. go
  2144. create procedure sp_MSdb_options_bits as
  2145. declare @bitdesc varchar(255), @curdbid int, @allopts int
  2146. select @curdbid = db_id()
  2147. select @allopts = 7196
  2148.  
  2149. set nocount on
  2150.  
  2151. create table #spdbbits
  2152. (allopts bit,
  2153.  dbuse bit,
  2154.  chkpt bit,
  2155.  readonly bit,
  2156.  bulkload bit,
  2157.  single bit,
  2158.  trunc bit)
  2159.  
  2160. insert into #spdbbits values(0,0,0,0,0,0,0)
  2161.  
  2162. /*  Check all settable options (7196) */
  2163.  
  2164. select @bitdesc = null
  2165. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2166.        where d.dbid = @curdbid and v.type = "D"
  2167.        and d.status & v.number = 7196 and v.number = @allopts        /* all options */
  2168.  
  2169. if @bitdesc != null
  2170. begin
  2171.   update #spdbbits set allopts = 1
  2172. end
  2173.  
  2174. /* Check select into/bulk copy bit (4)    */
  2175.  
  2176. select @bitdesc = null
  2177. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2178.        where d.dbid = @curdbid and v.type = "D"
  2179.        and d.status & v.number = 4 and v.number != @allopts      /* all options */
  2180.  
  2181. if @bitdesc != null
  2182. begin
  2183.    update #spdbbits set bulkload = 1
  2184. end
  2185.  
  2186. /*  Check no checkpoint on recovery bit (16)  */
  2187.  
  2188. select @bitdesc = null
  2189. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2190.        where d.dbid = @curdbid and v.type = "D"
  2191.        and d.status & v.number = 16 and v.number != @allopts /* all options */
  2192.  
  2193. if @bitdesc != null
  2194. begin
  2195.   update #spdbbits set chkpt = 1
  2196. end
  2197.  
  2198. /* Check single user bit (4096) */
  2199.  
  2200. select @bitdesc = null
  2201. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2202.        where d.dbid = @curdbid and v.type = "D"
  2203.        and d.status & v.number = 4096 and v.number != @allopts         /* all options */
  2204.  
  2205. if @bitdesc != null
  2206. begin
  2207.   update #spdbbits set single = 1
  2208. end
  2209.  
  2210. /*  Check dbo only bit (2048) */
  2211.  
  2212. select @bitdesc = null
  2213. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2214.        where d.dbid = @curdbid and v.type = "D"
  2215.        and d.status & v.number = 2048 and v.number != @allopts         /* all options */
  2216.  
  2217. if @bitdesc != null
  2218. begin
  2219.   update #spdbbits set dbuse = 1
  2220. end
  2221.  
  2222. /*  Check read only bit (1024) */
  2223.  
  2224. select @bitdesc = null
  2225. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2226.        where d.dbid = @curdbid and v.type = "D"
  2227.        and d.status & v.number = 1024 and v.number != @allopts         /* all options */
  2228.  
  2229. if @bitdesc != null
  2230. begin
  2231.   update #spdbbits set readonly = 1
  2232. end
  2233.  
  2234. /*  Check truncate log on checkpoint bit (8) */
  2235.  
  2236. select @bitdesc = null
  2237. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2238.        where d.dbid = @curdbid and v.type = "D"
  2239.        and d.status & v.number = 8 and v.number != @allopts      /* all options */
  2240.  
  2241. if @bitdesc != null
  2242. begin
  2243.   update #spdbbits set trunc = 1
  2244. end
  2245.  
  2246. set nocount off
  2247.  
  2248. select * from #spdbbits
  2249. go
  2250.  
  2251.  
  2252. /*****************************************************************************/
  2253. /* This procedure gets the devices that a database resides on for segment use */
  2254. /*****************************************************************************/
  2255.  
  2256. print ""
  2257. print "Creating sp_MSdb_devices_segments"
  2258. print ""
  2259. go
  2260. create procedure sp_MSdb_devices_segments @dbname varchar(30) as
  2261.  
  2262. /*  TCD 8/2/91 See if the database exists. */
  2263.  
  2264. if not exists (select * from master.dbo.sysdatabases
  2265.              where name = @dbname)
  2266. begin
  2267.     raiserror 70006 "Database doesn't exist. The 'sa' or 'dbo' must have dropped the database since you last displayed the listing."
  2268.     return
  2269. end
  2270.  
  2271. select distinct device = master.dbo.sysdevices.name
  2272. from master.dbo.sysdatabases, master.dbo.sysusages, master.dbo.sysdevices, master.dbo.spt_values a, master.dbo.spt_values b
  2273. where master.dbo.sysdatabases.dbid = master.dbo.sysusages.dbid
  2274.         and master.dbo.sysdevices.low <= size + vstart
  2275.         and master.dbo.sysdevices.high >= size + vstart - 1
  2276.         and master.dbo.sysdevices.status & 2 = 2
  2277.         and master.dbo.sysdatabases.name = @dbname
  2278.         and a.type = "E" and a.number = 1 and b.type = "S"
  2279.         and master.dbo.sysusages.segmap & 7 = b.number
  2280.         order by 1
  2281. go
  2282.  
  2283. /************* DUMP THE TRANSACTION LOG **************************************/
  2284. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2285. /* script periodically, you will run out of transaction log space.           */
  2286. dump tran master with truncate_only
  2287. go
  2288. /************* END DUMP THE TRANSACTION LOG **********************************/
  2289.  
  2290.  
  2291. /*****************************************************************************/
  2292. /* This procedure gets the space used by a particular database.          */
  2293. /* You must be in the database to access this information             */
  2294. /*****************************************************************************/
  2295.  
  2296.  
  2297. print ""
  2298. print "Creating sp_MSdb_space"
  2299. print ""
  2300. go
  2301. create procedure sp_MSdb_space as
  2302. declare @options varchar(255)
  2303.  
  2304.  
  2305. declare @pages    float                  /* working variable for size calc. */
  2306. declare @dbsize float               /* Size of the database */
  2307.  
  2308.  
  2309. set nocount on
  2310.  
  2311. create table #spt_space
  2312. (
  2313.  db_size     float null,
  2314.  rows         int null,
  2315.  reserved     float null,
  2316.  data         float null,
  2317.  indexp      float null,
  2318.  unused      float null,
  2319.  log_size     float null,
  2320.  log_used     float null
  2321. )
  2322.  
  2323. /* Get the total size of the database */
  2324.  
  2325. insert into #spt_space (db_size)
  2326. select distinct (sum(convert(float,size)) * (convert(float,d.low)) / 1048576.0)
  2327. from master.dbo.sysusages, master.dbo.spt_values d
  2328. where dbid = db_id() and d.number = 1 and d.type = "E"
  2329. having dbid = db_id() and d.number = 1 and d.type = "E"
  2330.  
  2331. /* */
  2332. /*  Now calculate the summary data.
  2333.     reserved: sum(reserved) where indid in (0, 1, 255) */
  2334. /* */
  2335. update #spt_space
  2336. set reserved = (select sum(convert(float,sysindexes.reserved)) from sysindexes where indid in (0, 1, 255))
  2337.  
  2338. /* */
  2339. /* data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text) */
  2340. /* */
  2341. select @pages = sum(convert(float,dpages)) from sysindexes
  2342. where indid < 2
  2343.  
  2344. select @pages = @pages + isnull(sum(convert(float,used)), 0.0) from sysindexes
  2345. where indid = 255
  2346.  
  2347. update #spt_space set data = @pages
  2348.  
  2349.     
  2350. /* index: sum(used) where indid in (0, 1, 255) - data */
  2351.  
  2352. update #spt_space
  2353. set indexp = ((select (sum(convert(float,used))) from sysindexes where indid in (0, 1, 255)) - data)
  2354.  
  2355. /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
  2356. update #spt_space
  2357. set unused = reserved - (select sum(convert(float,used)) from sysindexes where indid in (0, 1, 255))
  2358.  
  2359. /* log_size: sum(reserved) where indid in (0, 1, 255) and object is 'syslogs' */
  2360.  
  2361. update #spt_space
  2362. set log_size = (select sum(convert(float,sysindexes.reserved)) from sysindexes where indid in (0, 1, 255) and id = object_id('syslogs'))
  2363.  
  2364. /* log_used: sum(used) where indid in (0, 1, 255) and id = object_id('syslogs') */
  2365.  
  2366. update #spt_space set log_used = (select sum(convert(float,used))
  2367. from sysindexes
  2368. where indid in (0, 1, 255) and id = object_id('syslogs'))
  2369.  
  2370. set nocount off
  2371.  
  2372. select reserved = substring((convert(varchar(11), (convert(int,(reserved * (convert(float,d.low) ) / 1024.0)))) + " KB"),1, 15),
  2373.        data = substring((convert(varchar(11),convert(int,(data * convert(float,d.low)) / 1024.0)) + " KB"), 1, 15),
  2374.        log_space = substring ((convert(varchar(11), (round((log_size * convert(float, d.low)) / 1024.0, 0))) + " KB"), 1, 15),
  2375.        index_size = substring((convert(varchar(11), convert(int,(indexp * convert(float,d.low)) / 1024.0)) + " KB"), 1, 15),
  2376.        unused_reserve = substring((convert(varchar(11),convert(int,(unused * convert(float,d.low)) / 1024.0)) + " KB"), 1, 15),
  2377.        unused_log = substring ((convert(varchar(11), (round(((log_size - log_used) * convert(float, d.low)) / 1024.0, 0))) + " KB"), 1, 15)
  2378.        from #spt_space, master.dbo.spt_values d
  2379.        where d.number = 1 and d.type = "E"
  2380. go
  2381.  
  2382.  
  2383. /*****************************************************************************/
  2384. /* This stored procedure gets all the options set for a particular database  */
  2385. /* It applies to the current database.                         */
  2386. /* It is generally called by sp_MSdb_properties                  */
  2387. /*****************************************************************************/
  2388.  
  2389.  
  2390. print ""
  2391. print "Creating sp_MSdb_options"
  2392. print ""
  2393. go
  2394. create procedure sp_MSdb_options as
  2395. declare @options varchar(255), @bitdesc varchar(255), @curdbid int, @allopts int
  2396. select @curdbid = db_id()
  2397. select @allopts = 7196
  2398.  
  2399. set nocount on
  2400.  
  2401. select @options = ""
  2402.  
  2403. /* Check select into/bulk copy bit (4)    */
  2404.  
  2405. select @bitdesc = null
  2406. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2407.        where d.dbid = @curdbid and v.type = "D"
  2408.        and d.status & v.number = 4 and v.number != @allopts      /* all options */
  2409.  
  2410. if @bitdesc != null
  2411. begin
  2412.    if @options != ""
  2413.      select @options = @options + ", " +  @bitdesc
  2414.    else select @options = @bitdesc
  2415. end
  2416.  
  2417. /*  Check no checkpoint on recovery bit (16)  */
  2418.  
  2419. select @bitdesc = null
  2420. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2421.        where d.dbid = @curdbid and v.type = "D"
  2422.        and d.status & v.number = 16 and v.number != @allopts /* all options */
  2423.  
  2424. if @bitdesc != null
  2425. begin
  2426.   if @options != ""
  2427.      select @options = @options + ", " +  @bitdesc
  2428.   else select @options = @bitdesc
  2429. end
  2430.  
  2431. /* Check single user bit (4096) */
  2432.  
  2433. select @bitdesc = null
  2434. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2435.        where d.dbid = @curdbid and v.type = "D"
  2436.        and d.status & v.number = 4096 and v.number != @allopts         /* all options */
  2437.  
  2438. if @bitdesc != null
  2439. begin
  2440.   if @options != ""
  2441.      select @options = @options + ", " +  @bitdesc
  2442.   else select @options = @bitdesc
  2443. end
  2444.  
  2445. /*  Check dbo only bit (2048) */
  2446.  
  2447. select @bitdesc = null
  2448. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2449.        where d.dbid = @curdbid and v.type = "D"
  2450.        and d.status & v.number = 2048 and v.number != @allopts         /* all options */
  2451.  
  2452. if @bitdesc != null
  2453. begin
  2454.   if @options != ""
  2455.      select @options = @options + ", " +  @bitdesc
  2456.   else select @options = @bitdesc
  2457. end
  2458.  
  2459. /*  Check read only bit (1024) */
  2460.  
  2461. select @bitdesc = null
  2462. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2463.        where d.dbid = @curdbid and v.type = "D"
  2464.        and d.status & v.number = 1024 and v.number != @allopts         /* all options */
  2465.  
  2466. if @bitdesc != null
  2467. begin
  2468.   if @options != ""
  2469.      select @options = @options + ", " +  @bitdesc
  2470.   else select @options = @bitdesc
  2471. end
  2472.  
  2473. /*  Check load only bit (512) */
  2474.  
  2475. select @bitdesc = null
  2476. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2477.        where d.dbid = @curdbid and v.type = "D"
  2478.        and d.status & v.number = 512 and v.number != @allopts        /* all options */
  2479.  
  2480. if @bitdesc != null
  2481. begin
  2482.   if @options != ""
  2483.      select @options = @options + ", " +  @bitdesc
  2484.   else select @options = @bitdesc
  2485. end
  2486.  
  2487. /* Check not recovered only bit (256)  */
  2488.  
  2489. select @bitdesc = null
  2490. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2491.        where d.dbid = @curdbid and v.type = "D"
  2492.        and d.status & v.number = 256 and v.number != @allopts  /* all options */
  2493.  
  2494. if @bitdesc != null
  2495. begin
  2496.   if @options != ""
  2497.      select @options = @options + ", " +  @bitdesc
  2498.   else select @options = @bitdesc
  2499. end
  2500.  
  2501. /* Check don't recover bit (32)  */
  2502.  
  2503. select @bitdesc = null
  2504. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2505.        where d.dbid = @curdbid and v.type = "D"
  2506.        and d.status & v.number = 32 and v.number != @allopts       /* all options */
  2507.  
  2508. if @bitdesc != null
  2509. begin
  2510.   if @options != ""
  2511.      select @options = @options + ", " +  @bitdesc
  2512.   else select @options = @bitdesc
  2513. end
  2514.  
  2515. /*  Check truncate log on checkpoint bit (8) */
  2516.  
  2517. select @bitdesc = null
  2518. select @bitdesc = v.name from master.dbo.spt_values v, master.dbo.sysdatabases d
  2519.        where d.dbid = @curdbid and v.type = "D"
  2520.        and d.status & v.number = 8 and v.number != @allopts      /* all options */
  2521.  
  2522. if @bitdesc != null
  2523. begin
  2524.   if @options != ""
  2525.      select @options = @options + ", " +  @bitdesc
  2526.   else select @options = @bitdesc
  2527. end
  2528.  
  2529. /* If no flags are set, say so. */
  2530.  
  2531. if @options = ""
  2532. begin
  2533.   select @options = "no options set"
  2534. end
  2535.  
  2536. set nocount off
  2537.  
  2538. select @options
  2539. go
  2540.  
  2541. /************* DUMP THE TRANSACTION LOG **************************************/
  2542. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2543. /* script periodically, you will run out of transaction log space.           */
  2544. dump tran master with truncate_only
  2545. go
  2546. /************* END DUMP THE TRANSACTION LOG **********************************/
  2547.  
  2548. /*****************************************************************************/
  2549. /* This procedure gets the devices that a database resides on.             */
  2550. /*****************************************************************************/
  2551.  
  2552. print ""
  2553. print "Creating sp_MSdb_devices"
  2554. print ""
  2555. go
  2556. create procedure sp_MSdb_devices @dbname varchar(30) as
  2557.  
  2558. select device = master.dbo.sysdevices.name,
  2559.        size = convert(varchar(10), round((master.dbo.spt_values.low * convert(float, size))/ 1048576, 0)) + " " + "MB",
  2560.        usage = b.name
  2561. from master.dbo.sysdatabases, master.dbo.sysusages, master.dbo.sysdevices, master.dbo.spt_values a, master.dbo.spt_values b
  2562. where master.dbo.sysdatabases.dbid = master.dbo.sysusages.dbid
  2563.       and master.dbo.sysdevices.low <= size + vstart
  2564.       and master.dbo.sysdevices.high >= size + vstart - 1
  2565.       and master.dbo.sysdevices.status & 2 = 2
  2566.       and master.dbo.sysdatabases.name = @dbname
  2567.       and a.type = "E" and a.number = 1 and b.type = "S"
  2568.       and master.dbo.sysusages.segmap & 7 = b.number
  2569.       order by 1
  2570. go
  2571.  
  2572. /*****************************************************************************/
  2573. /* These routines will get all the necessary data for the Database Properties */
  2574. /* dialog box.    You must be in the database to access this information         */
  2575. /*****************************************************************************/
  2576.  
  2577. print ""
  2578. print "Creating sp_MSdb_properties"
  2579. print ""
  2580. go
  2581. create procedure sp_MSdb_properties as
  2582. declare @dbname varchar(255), @users int, @groups int
  2583.  
  2584. /* Get number of users and number of groups */
  2585.  
  2586. set nocount on
  2587. select @users = count(*) from sysusers where uid < 16383 and uid > 0
  2588. select @groups = count(*) from sysusers where uid >= 16383 or uid = 0
  2589. set nocount off
  2590.  
  2591. select Number_users = @users, Number_groups = @groups
  2592.  
  2593. /* Get the object counts and db options */
  2594.  
  2595. exec sp_MSdb_options
  2596.  
  2597. /* Get the space used by the database */
  2598.  
  2599. exec sp_MSdb_space
  2600. go
  2601.  
  2602. /*****************************************************************/
  2603. /* This stored procedure will calculate thread usage             */
  2604. /****************************************************************/
  2605.  
  2606.  
  2607. print ""
  2608. print "Creating sp_MSthread_list"
  2609. print ""
  2610. go
  2611. create procedure sp_MSthread_list as
  2612.  
  2613. set nocount on
  2614.  
  2615. declare @devs int, @servs int, @total int, @avail_threads int,
  2616. @avail_servs int, @mirrors int, @outline varchar(50)
  2617.  
  2618. select @devs = count(*) from master.dbo.sysdevices where cntrltype = 0
  2619. select @mirrors = count(*) from master.dbo.sysdevices where status & 512 = 512
  2620. select @servs = 2 * count(*) from master.dbo.sysservers where srvid != 0
  2621. select @total = 4 + @devs + @mirrors+ @servs
  2622. select @avail_threads = 53-4-@devs-@mirrors-@servs
  2623. if @avail_threads % 2 = 1
  2624.     select @avail_servs = (@avail_threads - 1)/2
  2625. else
  2626.     select @avail_servs = @avail_threads/2
  2627.  
  2628.  
  2629. set nocount off
  2630. select '4 SQL Server Threads'
  2631. select @outline = convert(varchar(5), @devs+@mirrors)+ ' Database Device(s) (including mirrors)'
  2632. select @outline
  2633. select @outline = convert(varchar(5), @servs/2)+ ' Remote Server(s)'
  2634. select @outline
  2635. select @outline = convert(varchar(5),  @total) + ' Threads Currently Used'
  2636. select @outline
  2637. select @outline = 'You May Create Up To ' + convert(varchar(5), (53 - @total)) + ' More Devices'
  2638. select @outline
  2639. select @outline = 'Or You May Create Up To ' + convert(varchar(5), @avail_servs) + ' Remote Servers'
  2640. select @outline
  2641. go
  2642.  
  2643. /************* DUMP THE TRANSACTION LOG **************************************/
  2644. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  2645. /* script periodically, you will run out of transaction log space.           */
  2646. dump tran master with truncate_only
  2647. go
  2648. /************* END DUMP THE TRANSACTION LOG **********************************/
  2649.  
  2650. /***************************************************************************/
  2651. /* This stored procedure is a copy of sp_helpdb, but returns the full      */
  2652. /* length of the database information columns.  In cases where the db name */
  2653. /* is at or near 30 characters, sp_helpdb would truncate the name.         */
  2654. /***************************************************************************/
  2655. print ""
  2656. print "Creating sp_MShelpdb"
  2657. print ""
  2658. go
  2659.  
  2660. create procedure sp_MShelpdb
  2661. @dbname varchar(30) = NULL            /* database name to change */
  2662. as
  2663.  
  2664. declare @showdev    bit
  2665. declare @allopts    int
  2666.  
  2667. set nocount on
  2668.  
  2669. /*
  2670. **  If no database name given, get 'em all.
  2671. */
  2672. if @dbname = null
  2673.     select @dbname = "%", @showdev = 0
  2674. else select @showdev = 1
  2675.  
  2676. /*
  2677. **  Sure the database exists
  2678. */
  2679. if not exists (select *
  2680.     from master.dbo.sysdatabases
  2681.         where name like @dbname)
  2682. begin
  2683.     print "No such database exists."
  2684.     return (1)
  2685. end
  2686.  
  2687. /*
  2688. **  Set allopts to be the sum of all possible user-settable database status
  2689. **  bits.
  2690. */
  2691. select @allopts = 7196
  2692.  
  2693. /*
  2694. **  Since we examine the status bits in sysdatabase and turn them
  2695. **  into english, we need a temporary table to build the descriptions.
  2696. */
  2697. create table #spdbdesc
  2698. (
  2699.     dbid    smallint null,
  2700.     dbdesc    varchar(102) null
  2701. )
  2702.  
  2703. /*
  2704. **  Initialize #spdbdesc from master.dbo.sysdatabases
  2705. */
  2706. insert into #spdbdesc (dbid)
  2707.         select dbid
  2708.             from master.dbo.sysdatabases
  2709.                 where name like @dbname
  2710.  
  2711. /*
  2712. **  Now for each dbid in #spdbdesc, build the database status
  2713. **  description.
  2714. */
  2715. declare @curdbid smallint        /* the one we're currently working on */
  2716. declare @dbdesc varchar(102)        /* the total description for the db */
  2717. declare @bitdesc varchar(30)        /* the bit description for the db */
  2718.  
  2719. /*
  2720. **  Set @curdbid to the first dbid.
  2721. */
  2722. select @curdbid = min(dbid)
  2723.     from #spdbdesc
  2724. while @curdbid != NULL
  2725. begin
  2726.     /*
  2727.     **  Initialize @dbdesc.
  2728.     */
  2729.     select @dbdesc = ""
  2730.  
  2731.     /*
  2732.     **  Check select into/bulk copy bit (4)
  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 = 4
  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 no checkpoint on recovery bit (16)
  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 = 16
  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 single user bit (4096)
  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 = 4096
  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 dbo only bit (2048)
  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 = 2048
  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 read only bit (1024)
  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 = 1024
  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 load only bit (512)
  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 = 512
  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 not recovered only bit (256)
  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 = 256
  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 don't recover bit (32)
  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 = 32
  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.     **  Check truncate log on checkpoint bit (8)
  2869.     */
  2870.     select @bitdesc = null
  2871.     select @bitdesc = v.name
  2872.         from master.dbo.spt_values v, master.dbo.sysdatabases d
  2873.             where d.dbid = @curdbid
  2874.                 and v.type = "D"
  2875.                 and d.status & v.number = 8
  2876.                 and v.number != @allopts       /* all options */
  2877.     if @bitdesc != null
  2878.     begin
  2879.         if @dbdesc != ""
  2880.             select @dbdesc = @dbdesc + ", " +  @bitdesc
  2881.         else select @dbdesc = @bitdesc
  2882.     end
  2883.  
  2884.     /*
  2885.     **  If not flags are set, say so.
  2886.     */
  2887.     if @dbdesc = ""
  2888.     begin
  2889.         select @dbdesc = "no options set"
  2890.     end
  2891.  
  2892.     /*
  2893.     **  Save the description.
  2894.     */
  2895.     update #spdbdesc
  2896.         set dbdesc = @dbdesc
  2897.             from #spdbdesc
  2898.                 where dbid = @curdbid
  2899.  
  2900.     /*
  2901.     **  Now get the next, if any dbid.
  2902.     */
  2903.     select @curdbid = min(dbid)
  2904.         from #spdbdesc
  2905.             where dbid > @curdbid
  2906. end
  2907.  
  2908. /*
  2909. **  Now #spdbdesc is complete so we can print out the db info
  2910. */
  2911. select distinct name = master.dbo.sysdatabases.name,
  2912.         db_size = convert(varchar(10),
  2913.             round(
  2914.             (master.dbo.spt_values.low * convert(float, sum(master.dbo.sysusages.size)))
  2915.             / 1048576, 0))
  2916.             + " " + "MB",
  2917.         owner = master.dbo.syslogins.name,
  2918.         dbid = master.dbo.sysdatabases.dbid,
  2919.         created = convert(char(11), master.dbo.sysdatabases.crdate),
  2920.         status = #spdbdesc.dbdesc
  2921.             from master.dbo.sysdatabases, master.dbo.syslogins, master.dbo.sysusages, #spdbdesc,
  2922.              master.dbo.    spt_values
  2923.         where master.dbo.sysdatabases.dbid = #spdbdesc.dbid
  2924.             and master.dbo.sysdatabases.suid = master.dbo.syslogins.suid
  2925.             and #spdbdesc.dbid = master.dbo.sysusages.dbid
  2926.             and master.dbo.spt_values.type = "E"
  2927.             and master.dbo.spt_values.number = 1
  2928.         group by #spdbdesc.dbid
  2929.         having master.dbo.sysdatabases.dbid = #spdbdesc.dbid
  2930.             and master.dbo.sysdatabases.suid = master.dbo.syslogins.suid
  2931.             and #spdbdesc.dbid = master.dbo.sysusages.dbid
  2932.             and master.dbo.spt_values.type = "E"
  2933.             and master.dbo.spt_values.number = 1
  2934.         order by master.dbo.sysdatabases.name
  2935.  
  2936. /*
  2937. **  If we are looking at one database, show it's device allocation.
  2938. */
  2939. if @showdev = 1
  2940. begin
  2941.     select device_fragments = master.dbo.sysdevices.name, size =
  2942.         convert(varchar(10),
  2943.             round(
  2944.             (master.dbo.spt_values.low * convert(float, size))
  2945.             / 1048576, 0)) + " " + "MB",
  2946.         usage = b.name
  2947.     from master.dbo.sysdatabases, master.dbo.sysusages, master.dbo.sysdevices, master.dbo.spt_values a, master.dbo.spt_values b
  2948.         where master.dbo.sysdatabases.dbid = master.dbo.sysusages.dbid
  2949.             and master.dbo.sysdevices.low <= size + vstart
  2950.             and master.dbo.sysdevices.high >= size + vstart - 1
  2951.             and master.dbo.sysdevices.status & 2 = 2
  2952.             and master.dbo.sysdatabases.name = @dbname
  2953.             and a.type = "E"
  2954.             and a.number = 1
  2955.             and b.type = "S"
  2956.             and master.dbo.sysusages.segmap & 7 = b.number
  2957.     order by 1
  2958.  
  2959.     /*
  2960.     **  If there is only one database and we are in it, show the
  2961.     **  segments.
  2962.     */
  2963.     if exists (select *
  2964.             from #spdbdesc
  2965.                 where db_id() = dbid)
  2966.     begin
  2967.         declare @curdevice    varchar(30),
  2968.             @curseg        smallint,
  2969.             @segbit        int
  2970.  
  2971.         delete #spdbdesc
  2972.  
  2973.         select @curdevice = min(d.name)
  2974.             from  master.dbo.sysusages u, master.dbo.sysdevices d
  2975.                 where u.dbid = db_id()
  2976.                     and d.low <= size + vstart
  2977.                     and d.high >= size + vstart - 1
  2978.                     and d.status & 2 = 2
  2979.         while (@curdevice != null)
  2980.         begin
  2981.             /*
  2982.             ** We need an inner loop here to go through
  2983.             **  all the possible segment.
  2984.             */
  2985.             select @curseg = min(segment)
  2986.                     from syssegments
  2987.             while (@curseg != null)
  2988.             begin
  2989.                 if (@curseg < 31)
  2990.                     select @segbit = power(2, @curseg)
  2991.                 else select @segbit = low
  2992.                     from master.dbo.spt_values
  2993.                         where type = "E"
  2994.                             and number = 2
  2995.                 insert into #spdbdesc
  2996.                     select @curseg, @curdevice
  2997.                         from master.dbo.sysusages u,
  2998.                             master.dbo.sysdevices d,
  2999.                             master.dbo.spt_values v
  3000.                     where u.segmap & @segbit = @segbit
  3001.                         and d.low <= u.size + u.vstart
  3002.                         and d.high >= u.size + u.vstart - 1
  3003.                         and u.dbid = db_id()
  3004.                         and d.status & 2 = 2
  3005.                         and v.number = 1
  3006.                         and v.type = "E"
  3007.                         and d.name = @curdevice
  3008.                 select @curseg = min(segment)
  3009.                         from syssegments
  3010.                             where segment > @curseg
  3011.             end
  3012.  
  3013.             select @curdevice = min(d.name)
  3014.                 from  master.dbo.sysusages u,
  3015.                     master.dbo.sysdevices d
  3016.                 where u.dbid = db_id()
  3017.                     and d.low <= size + vstart
  3018.                     and d.high >= size + vstart - 1
  3019.                     and d.status & 2 = 2
  3020.                     and d.name > @curdevice
  3021.         end
  3022.  
  3023.         /*
  3024.         **  One last check for any devices that have no segments.
  3025.         */
  3026.         insert into #spdbdesc
  3027.             select null, d.name
  3028.                 from master.dbo.sysusages u,
  3029.                     master.dbo.sysdevices d
  3030.             where u.segmap = 0
  3031.                 and d.low <= u.size + u.vstart
  3032.                 and d.high >= u.size + u.vstart - 1
  3033.                 and u.dbid = db_id()
  3034.                 and d.status & 2 = 2
  3035.  
  3036.         select distinct device = dbdesc,
  3037.             segment = isnull(name, " -- unused by any segments --")
  3038.         from #spdbdesc, syssegments
  3039.             where dbid *= segment
  3040.         order by 1, 2
  3041.     end
  3042.  
  3043. end
  3044.  
  3045. drop table #spdbdesc
  3046. return (0)
  3047. go
  3048.  
  3049. /************* DUMP THE TRANSACTION LOG **************************************/
  3050. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  3051. /* script periodically, you will run out of transaction log space.           */
  3052. dump tran master with truncate_only
  3053. go
  3054. /************* END DUMP THE TRANSACTION LOG **********************************/
  3055.  
  3056. /***************************************************************************/
  3057. /* This stored procedure is a copy of sp_helpremotelogin, but returns the  */
  3058. /* full length of the login information columns.  In cases where the       */
  3059. /* remote login name is at or near 30 characters, sp_helpremotelogins      */
  3060. /* would truncate the name.                                                */
  3061. /***************************************************************************/
  3062. print ""
  3063. print "Creating sp_MShelpremotelogin"
  3064. print ""
  3065. go
  3066.  
  3067. create procedure sp_MShelpremotelogin
  3068. @remoteserver varchar(30) = NULL,    /* remote server name */
  3069. @remotename varchar(30) = NULL        /* remote login name */
  3070. as
  3071.  
  3072. declare    @msg    varchar(100)
  3073.  
  3074. set nocount on
  3075.  
  3076. /*
  3077. **  If no server given, get 'em all.
  3078. */
  3079. if @remoteserver = null
  3080. begin
  3081.     select @remoteserver = "%"
  3082. end
  3083.  
  3084. else
  3085. begin
  3086.     if not exists (select *
  3087.             from master.dbo.sysservers s,
  3088.                 master.dbo.sysremotelogins r
  3089.             where s.srvid = r.remoteserverid
  3090.                 and s.srvname like @remoteserver)
  3091.     begin
  3092.         if @remoteserver = "%"
  3093.         begin
  3094.             select @msg = "There are no remote servers defined."
  3095.             print @msg
  3096.             return (1)
  3097.         end
  3098.         return (1)
  3099.     end
  3100. end
  3101.  
  3102. /*
  3103. **  If no remotename given, get 'em all.
  3104. */
  3105. if @remotename = null
  3106. begin
  3107.     select @remotename = "%"
  3108. end
  3109.  
  3110. else
  3111. begin
  3112.     if not exists (select *
  3113.             from master.dbo.sysremotelogins
  3114.             where isnull(remoteusername, "") like @remotename)
  3115.     begin
  3116.         if @remotename = "%"
  3117.         begin
  3118.             select @msg = "There are no remotelogins defined."
  3119.             print @msg
  3120.             return (1)
  3121.         end
  3122.  
  3123.         select @msg = "There are no remote logins for '"
  3124.             + @remotename + "'."
  3125.         print @msg
  3126.         return (1)
  3127.     end
  3128. end
  3129.  
  3130. /*
  3131. **  Check for empty results.
  3132. */
  3133. if not exists (select *
  3134.         from master.dbo.sysremotelogins r, master.dbo.sysservers s
  3135.         where isnull(r.remoteusername, "") like @remotename
  3136.             and s.srvid = r.remoteserverid
  3137.                 and s.srvname like @remoteserver)
  3138. begin
  3139.     if ((@remoteserver = '%') and (@remotename = '%'))
  3140.         select @msg = "There are no remote logins."
  3141.     else select @msg = "There are no remote logins for '"
  3142.         + @remotename + "' on remote server '" + @remoteserver + "'."
  3143.     print @msg
  3144.     return (1)
  3145. end
  3146.  
  3147. /*
  3148. **  Select the information.
  3149. */
  3150. select server = s.srvname,
  3151.     local_user_name =
  3152.         isnull(suser_name(r.suid), "** use local name **"),
  3153.     remote_user_name =
  3154.         isnull(r.remoteusername, "** mapped locally **"),
  3155.     options = v.name
  3156.         from master.dbo.sysservers s, master.dbo.sysremotelogins r,
  3157.             master.dbo.spt_values v
  3158.     where s.srvid = r.remoteserverid
  3159.         and s.srvname like @remoteserver
  3160.         and isnull(r.remoteusername, "") like @remotename
  3161.         and v.type = "F"
  3162.         and v.number = r.status
  3163. order by server, remote_user_name
  3164.  
  3165. return (0)
  3166. go
  3167.  
  3168. /************* DUMP THE TRANSACTION LOG **************************************/
  3169. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  3170. /* script periodically, you will run out of transaction log space.           */
  3171. dump tran master with truncate_only
  3172. go
  3173. /************* END DUMP THE TRANSACTION LOG **********************************/
  3174.  
  3175. /***************************************************************************/
  3176. /* This stored procedure is a copy of sp_helpserver, but returns the       */
  3177. /* full length of the server information columns.  In cases where the      */
  3178. /* server name is at or near 30 characters, sp_helpserver would truncate   */
  3179. /* the name.                                                               */
  3180. /***************************************************************************/
  3181. print ""
  3182. print "Creating sp_MShelpserver"
  3183. print ""
  3184. go
  3185.  
  3186. create procedure sp_MShelpserver
  3187. @server varchar(30) = NULL        /* server name */
  3188. as
  3189.  
  3190. declare    @msg    varchar(100)
  3191.  
  3192. set nocount on
  3193.  
  3194. /*
  3195. **  If no server name given, get 'em all.
  3196. */
  3197. if @server = null
  3198. begin
  3199.     select @server = "%"
  3200. end
  3201.  
  3202. /*
  3203. **  Does the server exist?
  3204. */
  3205. if not exists (select *
  3206.         from master.dbo.sysservers
  3207.             where srvname like @server
  3208.                 or srvnetname like @server)
  3209. begin
  3210.     if @server = "%"
  3211.         select @msg = "There are no servers defined."
  3212.     else select @msg = "There is no server named '" + @server + "'."
  3213.     print @msg
  3214.     return (1)
  3215. end
  3216.  
  3217. /*
  3218. **  Display server information.
  3219. **  First put it into a temp table so we can blot inappropriate status
  3220. **  for the local server.  It's ugly but I couldn't think of another way.
  3221. */
  3222. create table #spt_server
  3223. (
  3224.     name    varchar(30),
  3225.     network    varchar(30),
  3226.     status    varchar(20),
  3227.     id    smallint
  3228. )
  3229. insert into #spt_server
  3230.     select s.srvname, s.srvnetname, status = isnull(v.name, ""),
  3231.         id = s.srvid
  3232.     from master.dbo.sysservers s, master.dbo.spt_values v
  3233.         where s.srvstatus *= v.number
  3234.             and v.type = "A"
  3235.             and (s.srvname like @server
  3236.                 or s.srvnetname like @server)
  3237. update #spt_server
  3238.     set status = ""
  3239.         from master.dbo.sysservers s, #spt_server t
  3240.             where s.srvid = 0
  3241.                 and s.srvname = t.name
  3242.  
  3243. select name, network_name = network,
  3244.     status = status, id = convert(char(4), id)
  3245.         from #spt_server
  3246.     order by name
  3247.  
  3248. return (0)
  3249. go
  3250.  
  3251.  
  3252. /***************************************************************************/
  3253. /* This section grants execute permission on the stored procedure and
  3254.    select permissions on all the tables                    */
  3255. /***************************************************************************/
  3256.  
  3257. /* */
  3258. /* Grant permission on the procedures */
  3259. /* */
  3260.  
  3261. print ""
  3262. print "Granting execute permissions on procedures"
  3263. print ""
  3264. go
  3265. grant execute on sp_MSdevice_list to public
  3266. grant execute on sp_MScheck_admin to public
  3267. grant execute on sp_MSdev_description to public
  3268. grant execute on sp_MSdev_databases to public
  3269. grant execute on sp_MSsys_monitor to public
  3270. grant execute on sp_MSnext_devnumber to public
  3271. grant execute on sp_MSdatabase_list to public
  3272. grant execute on sp_MSdatabase_avail to public
  3273. grant execute on sp_MSbackup_now to public
  3274. grant execute on sp_MSuser_list to public
  3275. grant execute on sp_MSmonitor to public
  3276. grant execute on sp_MSdb_devices to public
  3277. grant execute on sp_MSdb_options to public
  3278. grant execute on sp_MSdb_properties to public
  3279. grant execute on sp_MSdb_space to public
  3280. grant execute on sp_MSbackup_schedule to public
  3281. grant execute on sp_MScmd_permissions to public
  3282. grant execute on sp_MSuser_cmd_permissions to public
  3283. grant execute on sp_MSdb_options_bits to public
  3284.  
  3285. /* */
  3286. /* these are 4.2 only */
  3287. /* */
  3288.  
  3289. grant execute on sp_MSdev_mirror to public
  3290. grant execute on sp_MSdb_devices_segments to public
  3291. grant execute on sp_MSsegment_list to public
  3292. grant execute on sp_MSsegment_devices to public
  3293. grant execute on sp_MSseg_properties to public
  3294. grant execute on sp_MSseg_tables to public
  3295. grant execute on sp_MSthread_list to public
  3296. grant execute on sp_MShelpdb to public
  3297. grant execute on sp_MShelpremotelogin to public
  3298. grant execute on sp_MShelpserver to public
  3299. go
  3300.  
  3301. /* */
  3302. /* Grant permissions on tables */
  3303. /* */
  3304.  
  3305. print ""
  3306. print "Creating permissions on tables"
  3307. print ""
  3308. go
  3309. grant select, update, delete, insert on MSscheduled_backups to dbo
  3310. grant select, update, delete, insert on MSscheduled_backups_log to dbo
  3311. go
  3312.  
  3313.  
  3314. /* logdevice    27.1    3/5/90 */
  3315. /* rv, 2/4/92, bug 1265 */
  3316.  
  3317. sp_configure "allow updates",1
  3318. go
  3319. reconfigure with override
  3320. go
  3321.  
  3322. create procedure sp_MSlogdevice
  3323. @dbname varchar(30),            /* database name that has the syslogs */
  3324. @devname        varchar(30),    /* device name to put syslogs on */
  3325. @fragflag       varchar(3)=' '  /* if ='*', change all fragments */
  3326. as
  3327.  
  3328. declare @dbid   smallint        /* dbid of the database to be changed */
  3329. declare @dbuid  smallint        /* id of the owner of the database */
  3330. declare @logbit int             /* this is the bit to turn on in sysusages */
  3331. declare @fragcnt smallint       /* count of allocation fragments */
  3332. declare @fragstart int          /* lowest lstart of fragments to change */
  3333.  
  3334. select @logbit = 4              /* bit 3 is the one to turn on */
  3335. select @fragstart = 0           /* lowest lstart of fragments to change */
  3336.  
  3337. /*
  3338. **  Verify the database name and get the @dbid and @dbuid
  3339. */
  3340. select @dbid = dbid, @dbuid = suid
  3341.         from sysdatabases
  3342.                 where name = @dbname
  3343.  
  3344. /*
  3345. **  If @dbname not found, say so and list the databases.
  3346. */
  3347. if @dbid = NULL
  3348. begin
  3349.         print "No such database -- run sp_helpdb to list databases."
  3350.         return (1)
  3351. end
  3352.  
  3353. /*
  3354. **  See if the device exists.
  3355. */
  3356. if not exists (select *
  3357.                 from master.dbo.sysdevices
  3358.                         where name like @devname)
  3359. begin
  3360.         print "No such device exists -- run sp_helpdevice to list the SQLServer devices."
  3361.         return (1)
  3362. end
  3363.  
  3364. /*
  3365. **  You must be SA or the dbo to execute this sproc.
  3366. */
  3367. if suser_id() != 1 and suser_id() != @dbuid
  3368. begin
  3369.         print "Only the System Administrator (SA) or the Database Owner (dbo) may move the syslogs table."
  3370.         return (1)
  3371. end
  3372.  
  3373. /*
  3374. **  Now see if the @dbname uses the @devname
  3375. */
  3376. if not exists (select *
  3377.                 from sysusages u, sysdevices d
  3378.                         where d.name = @devname
  3379.                                 and u.vstart between d.low and d.high
  3380.                                 and u.dbid = @dbid)
  3381. begin
  3382.         print "The specified device is not used by the database."
  3383.         return (1)
  3384. end
  3385.  
  3386. /*
  3387. **  Count the allocation fragments on the specified device, excluding
  3388. **  the one containing the system tables (lstart=0).
  3389. */
  3390. select @fragcnt=count(*)
  3391.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3392.                 where d.name = @devname
  3393.                         and u.vstart between d.low and d.high
  3394.                         and u.dbid = @dbid
  3395.                         and lstart != 0
  3396. /*
  3397. **  If there are none, it cannot be changed to log-only status
  3398. */
  3399. if @fragcnt = 0
  3400. begin
  3401.      print "Can't make the only segment on original device log-only"
  3402.      return (1)
  3403. end
  3404.  
  3405. /*
  3406. **  If more than one allocation fragment and '*' option not specified,
  3407. **  change only the one with the highest lstart.
  3408. */
  3409. if @fragcnt > 1 and @fragflag!='*'
  3410. begin
  3411.      print "Warning, there are multiple allocation fragments on this device."
  3412.      print "Only the last fragment will be changed to log-only status.  If you"
  3413.      print "wish to change them all, run sp_MSlogdevice database,device,'*' "
  3414.  
  3415.      select @fragstart=max(lstart)
  3416.         from master.dbo.sysusages u, master.dbo.sysdevices d
  3417.                 where d.name = @devname
  3418.                         and u.vstart between d.low and d.high
  3419.                         and u.dbid = @dbid
  3420. end
  3421.  
  3422.  
  3423. /*
  3424. **  clear the bit from any database segments that aren't already log only
  3425. */
  3426. update master.dbo.sysusages
  3427.         set segmap = segmap & ~@logbit
  3428.                 from master.dbo.sysusages
  3429.                         where dbid = @dbid
  3430.                         and segmap != @logbit
  3431. /*
  3432. **  Now set the segments on @devname as log-only.
  3433. */
  3434. update master.dbo.sysusages
  3435.         set segmap = @logbit
  3436.                 from master.dbo.sysusages u, master.dbo.sysdevices d
  3437.                         where d.name = @devname
  3438.                                 and u.vstart between d.low and d.high
  3439.                                 and u.dbid = @dbid
  3440.                                 and lstart != 0
  3441.                                 and lstart>=@fragstart
  3442.  
  3443. /*
  3444. **  Now we need to activate the new segment map.
  3445. */
  3446. dbcc dbrepair (@dbname, remap)
  3447. print "Syslogs moved."
  3448.  
  3449. return (0)
  3450. go
  3451. sp_configure "allow updates",0
  3452. go
  3453. print ""
  3454. print "reconfiguring"
  3455. print ""
  3456. go
  3457.  
  3458. reconfigure with override
  3459. go
  3460.  
  3461.  
  3462. grant execute on sp_MSlogdevice to public
  3463. go
  3464.  
  3465.  
  3466. /************* SQL ADMIN VERSION CONTROL *************************************/
  3467. /*    4.20.xx.yy where yy is script version.                                 */
  3468. /*****************************************************************************/
  3469. create procedure sp_MSAdmin_version as
  3470.    select "Microsoft SQL Administrator script version 4.20.22.1"
  3471. go
  3472.  
  3473. set nocount on
  3474.  
  3475. create table #spmissing
  3476. (name varchar(30),
  3477.  type varchar(10)
  3478. )
  3479.  
  3480. /* */
  3481. /* Check for the stored procedures first */
  3482. /* */
  3483.  
  3484. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdevice_list' and sysstat & 7 = 4)
  3485. begin
  3486.   insert into #spmissing values('sp_MSdevice_list', 'procedure')
  3487. end
  3488. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_description' and sysstat & 7 = 4)
  3489. begin
  3490.   insert into #spmissing values('sp_MSdev_description', 'procedure')
  3491. end
  3492. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_databases' and sysstat & 7 = 4)
  3493. begin
  3494.   insert into #spmissing values('sp_MSdev_databases', 'procedure')
  3495. end
  3496. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsys_monitor' and sysstat & 7 = 4)
  3497. begin
  3498.   insert into #spmissing values('sp_MSsys_monitor', 'procedure')
  3499. end
  3500. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSnext_devnumber' and sysstat & 7 = 4)
  3501. begin
  3502.   insert into #spmissing values('sp_MSnext_devnumber', 'procedure')
  3503. end
  3504. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_list' and sysstat & 7 = 4)
  3505. begin
  3506.   insert into #spmissing values('sp_MSdatabase_list', 'procedure')
  3507. end
  3508. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdatabase_avail' and sysstat & 7 = 4)
  3509. begin
  3510.   insert into #spmissing values('sp_MSdatabase_avail', 'procedure')
  3511. end
  3512. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_now' and sysstat & 7 = 4)
  3513. begin
  3514.   insert into #spmissing values('sp_MSbackup_now', 'procedure')
  3515. end
  3516. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_list' and sysstat & 7 = 4)
  3517. begin
  3518.   insert into #spmissing values('sp_MSuser_list', 'procedure')
  3519. end
  3520. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSmonitor' and sysstat & 7 = 4)
  3521. begin
  3522.   insert into #spmissing values('sp_MSmonitor', 'procedure')
  3523. end
  3524. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices' and sysstat & 7 = 4)
  3525. begin
  3526.   insert into #spmissing values('sp_MSdb_devices', 'procedure')
  3527. end
  3528. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options' and sysstat & 7 = 4)
  3529. begin
  3530.   insert into #spmissing values('sp_MSdb_options', 'procedure')
  3531. end
  3532. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_properties' and sysstat & 7 = 4)
  3533. begin
  3534.   insert into #spmissing values('sp_MSdb_properties', 'procedure')
  3535. end
  3536. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_space' and sysstat & 7 = 4)
  3537. begin
  3538.   insert into #spmissing values('sp_MSdb_space', 'procedure')
  3539. end
  3540. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSbackup_schedule' and sysstat & 7 = 4)
  3541. begin
  3542.   insert into #spmissing values('sp_MSbackup_schedule', 'procedure')
  3543. end
  3544. if not exists (select * from master.dbo.sysobjects where name = 'sp_MScmd_permissions' and sysstat & 7 = 4)
  3545. begin
  3546.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  3547. end
  3548. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSuser_cmd_permissions' and sysstat & 7 = 4)
  3549. begin
  3550.   insert into #spmissing values('sp_MScmd_permissions', 'procedure')
  3551. end
  3552. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_options_bits' and sysstat & 7 = 4)
  3553. begin
  3554.   insert into #spmissing values('sp_MSdb_options_bits', 'procedure')
  3555. end
  3556. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdev_mirror' and sysstat & 7 = 4)
  3557. begin
  3558.   insert into #spmissing values('sp_MSdev_mirror', 'procedure')
  3559. end
  3560. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSdb_devices_segments' and sysstat & 7 = 4)
  3561. begin
  3562.   insert into #spmissing values('sp_MSdb_devices_segments', 'procedure')
  3563. end
  3564. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_list' and sysstat & 7 = 4)
  3565. begin
  3566.   insert into #spmissing values('sp_MSsegment_list', 'procedure')
  3567. end
  3568. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSsegment_devices' and sysstat & 7 = 4)
  3569. begin
  3570.   insert into #spmissing values('sp_MSsegment_devices', 'procedure')
  3571. end
  3572. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_properties' and sysstat & 7 = 4)
  3573. begin
  3574.   insert into #spmissing values('sp_MSseg_properties', 'procedure')
  3575. end
  3576. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSseg_tables' and sysstat & 7 = 4)
  3577. begin
  3578.   insert into #spmissing values('sp_MSseg_tables', 'procedure')
  3579. end
  3580. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSthread_list' and sysstat & 7 = 4)
  3581. begin
  3582.   insert into #spmissing values('sp_MSthread_list', 'procedure')
  3583. end
  3584. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpdb' and sysstat & 7 = 4)
  3585. begin
  3586.   insert into #spmissing values('sp_MShelpdb', 'procedure')
  3587. end
  3588. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpremotelogin' and sysstat & 7 = 4)
  3589. begin
  3590.   insert into #spmissing values('sp_MSremotelogin', 'procedure')
  3591. end
  3592. if not exists (select * from master.dbo.sysobjects where name = 'sp_MShelpserver' and sysstat & 7 = 4)
  3593. begin
  3594.   insert into #spmissing values('sp_MShelpserver', 'procedure')
  3595. end
  3596. if not exists (select * from master.dbo.sysobjects where name = 'sp_MSlogdevice' and sysstat & 7 = 4)
  3597. begin
  3598.   insert into #spmissing values('sp_MSlogdevice', 'procedure')
  3599. end
  3600.  
  3601. /* */
  3602. /* Check for the tables next */
  3603. /* */
  3604.  
  3605. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups')
  3606. begin
  3607.   insert into #spmissing values('MSschedule_backups','table')
  3608. end
  3609. if not exists (select * from master.dbo.sysobjects where name = 'MSscheduled_backups_log')
  3610. begin
  3611.   insert into #spmissing values('MSschedule_backups_log','table')
  3612. end
  3613. if not exists (select * from master.dbo.sysobjects where name = 'MSsystem_monitor')
  3614. begin
  3615.   insert into #spmissing values('MSsystem_monitor','table')
  3616. end
  3617. if not exists (select * from master.dbo.sysobjects where name = 'MSlast_monitor')
  3618. begin
  3619.   insert into #spmissing values('MSlast_monitor','table')
  3620. end
  3621.  
  3622. if exists (select * from #spmissing)
  3623. begin
  3624.    print ""
  3625.    print ""
  3626.    print " ====================  ERRORS!  ===================="
  3627.    print "       The following objects were not created."
  3628.    print " Sql Administrator will not run against this server."
  3629.    print ""
  3630.    select * from #spmissing
  3631.    drop procedure sp_MSAdmin_version
  3632. end
  3633. else
  3634. begin
  3635.    print ""
  3636.    print ""
  3637.    print " Successful installation."
  3638.    grant execute on sp_MSAdmin_version to public
  3639.    exec sp_MSAdmin_version
  3640. end
  3641. go
  3642. drop table #spmissing
  3643. go
  3644. set nocount off
  3645. go
  3646.  
  3647. /************* DUMP THE TRANSACTION LOG **************************************/
  3648. /* Comment this out if you don't want your log dumped.  If you rerun this    */
  3649. /* script periodically, you will run out of transaction log space.           */
  3650. dump tran master with truncate_only
  3651. go
  3652. /************* END DUMP THE TRANSACTION LOG **********************************/
  3653. checkpoint
  3654. go
  3655.