home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / i386 / upgrade1.sql < prev    next >
Encoding:
Text File  |  1995-12-13  |  41.0 KB  |  1,482 lines

  1.  
  2. -- - -----
  3. /*
  4. ** Upgrade1.SQL        1995/12/13 10:55
  5. **
  6. ** (From old Upgrade.SQL)
  7. **
  8. ** Copyright Microsoft, Inc. 1994, 1995, 1996
  9. ** All Rights Reserved.
  10. ** Use, duplication, or disclosure by the United States Government
  11. ** is subject to restrictions as set forth in subdivision (c) (1) (ii)
  12. ** of the Rights in Technical Data and Computer Software clause
  13. ** at CFR 252.227-7013. Microsoft, Inc. One Microsoft Way, Redmond WA
  14. ** 98052.
  15. */
  16.  
  17. go
  18. use master
  19. go
  20. dump tran master with no_log
  21. go
  22. set nocount on
  23. go
  24. declare @dbver int
  25. dbcc getvalue('current_version')
  26. select @dbver = @@error        -- Get expected db schema version level from server.
  27.  
  28. if exists (select * from sysobjects where name='sp_configure' and type='P ')
  29.     begin
  30.  
  31.     if (select version from sysdatabases where dbid = db_id('master')) = @dbver
  32.         begin
  33.         exec sp_configure 'allow updates',1
  34.         reconfigure with override
  35.         end
  36.     else
  37.         raiserror('(master db lower version prevents usage of sp_configure re turning on ''allow updates''. Thus -m expected.)',1,1)
  38.     end
  39. else
  40.     raiserror('(sp_configure does not now exist, so it cannot be used to turn on ''allow updates''. Thus -m expected.)',1,1)
  41. go
  42.  
  43. print ''
  44. print 'Making sure that updates to system tables are allowed.'
  45. go
  46.  
  47. /*
  48. ** Make sure server was started in single user mode or that sp_configure was used
  49. ** to enable updates to system tables.
  50. */
  51. if (select value from syscurconfigs where config = 102) <> 1
  52.     raiserror('Cannot run upgrade.sql unless updates to system tables are enabled.  Shutdown server and restart with the ''-m'' option.',20,127)
  53.         with log
  54. go
  55.  
  56. declare @vc1 varchar(55)
  57. select  @vc1 = convert(varchar,getdate(),113)
  58. raiserror('
  59. DateTime near the top of this file Upgrade1.SQL is %s' ,1,1 ,@vc1)
  60. go
  61.  
  62. raiserror('
  63. Capture baseline info on current environment...',1,1)
  64.  
  65. raiserror(' ',1,1)
  66. dbcc tracestatus(-1)
  67.  
  68. raiserror(' ',1,1)
  69. dbcc useroptions
  70.  
  71. go
  72. print ''
  73. print 'Checking size of master device.'
  74. go
  75. dbcc updateusage('master') with no_infomsgs
  76. go
  77.  
  78. /*
  79. ** Determine amount of free space in master. We need a 4 MB buffer
  80. ** to upgrade versions >= 6 and 15 MB for versions < 6.
  81. */
  82. declare @free_db_space int
  83. declare @free_dev_space int
  84. declare @space_needed int
  85. declare @cmd varchar(255)
  86.  
  87. select @free_db_space =
  88.     (select sum(size) from sysusages where dbid = 1)
  89.     - (select sum(reserved) from sysindexes
  90.         where indid in (0, 1, 255))
  91.  
  92. if (@free_db_space < 7680        -- This will be a 4.2x server
  93.         and
  94.         (select version from sysdatabases where dbid = 1) < 400)
  95.     or @free_db_space < 2048    -- post 4.2x upgrade.
  96.  
  97. begin
  98.     raiserror('(version &  sum(size) (%d)  branch taken.)',1,1,@free_db_space)
  99.  
  100.     if (select version from sysdatabases where dbid = 1) < 400
  101.         select @space_needed = 7680 - @free_db_space
  102.     else
  103.         select @space_needed = 2048
  104.  
  105.  
  106.     /*
  107.     ** See if the master device has enough free space to alter the
  108.     ** master database.
  109.     */
  110.     select @free_dev_space =
  111.             (select sum(high-low+1) from sysdevices where name = 'master')
  112.         - (select sum(size) from sysusages where vstart between
  113.             (select low from sysdevices where name = 'master')
  114.             and
  115.             (select high from sysdevices where name = 'master'))
  116.  
  117.     if @free_dev_space < @space_needed
  118.         begin
  119.             print 'Expanding master device.'
  120.             select @cmd =
  121.                 'disk resize name = master,size = ' +
  122.                 convert(varchar(15), @space_needed +
  123.                 (select sum(high-low+1) from sysdevices where name = 'master') - @free_dev_space)
  124.             exec(@cmd)
  125.             if @@error <> 0
  126.                 raiserror('Could not expand master device.  See event viewer or errorlog for details.',16,127)
  127.                     with log
  128.         end
  129.     else
  130.         raiserror('(NOT  @free_dev_space < @space_needed  (%d>=%d))',1,1,@free_dev_space,@space_needed)
  131.  
  132.     print ''
  133.     print 'Altering master database.'
  134.  
  135.     select @cmd='alter database master on master = ' +
  136.         convert(varchar(10),ceiling(@space_needed/512.))
  137.  
  138.     exec(@cmd)
  139.     if @@error <> 0
  140.         raiserror('Could not expand master database.  Please free some space on the master device or expand it.',16,127)
  141.             with log
  142. end
  143. go
  144.  
  145.  
  146. if exists (select * from sysdatabases where name='master' and version between 400 and 406)
  147.     begin
  148.     raiserror('Dropping proc sp_dbcoalesce (6.0 name)....',1,1)
  149.     drop procedure sp_dbcoalesce
  150.     end
  151. go
  152.  
  153. /*
  154. ** Add xp_regread if necessary.
  155. */
  156. if not exists (select * from sysobjects where name = 'xp_regread')
  157.     begin
  158.     raiserror('(addextendedproc a1)',1,1)
  159.     dbcc addextendedproc( 'xp_regread', 'xpstar.dll')
  160.     end
  161. go
  162.  
  163.  
  164. ----print 'Shrink size of model db to 1 Mb (512 2_Kb pages) IF this is a NewInstall run.'
  165. go
  166.  
  167. /******************************
  168. sp_makewebpage must fit in 6.5 model db.  model db is normally 2 Mb.
  169.  
  170.  
  171. declare @i varchar(255)
  172.  
  173. exec master..xp_regread 'HKEY_LOCAL_MACHINE',
  174.      'SOFTWARE\Microsoft\MSSQLServer\Setup', 'SetupStatus',
  175.       @i out
  176.  
  177. print ''
  178.  
  179. if isnull(lower(@i),'null') not in ('upgrade','installed')
  180.     begin
  181.         print 'Performing new installation - shrinking model database.'
  182.  
  183.         ----A dynam exec handles case where 'allow updates' is set relatively late.
  184.  
  185.         execute('update master.dbo.sysusages set size = 512
  186.             where dbid = (select db_id(''model''))')
  187.  
  188.         dbcc dbrepair('model',remap)
  189.     end
  190. else
  191.         print 'Performing upgrade - size of model database not modified.'
  192. ******************************/
  193. go
  194.  
  195. print ''
  196. print 'Checking to see if sysdatabases needs to be upgraded.'
  197. go
  198. if not exists (select * from syscolumns
  199.         where name = 'category'
  200.         and id=object_id('sysdatabases'))
  201.     begin
  202.         print ''
  203.         print 'Updating sysdatabases, adding column  ''category''.'
  204.         alter table sysdatabases add category int null
  205.     end
  206. else
  207.         raiserror('sysdatabases already has ''category'' column.',1,1)
  208. go
  209.  
  210. print ''
  211. print 'Initializing sydatabases.category.'
  212. update sysdatabases set category = 0 where category is null --better in exec() immed after alter?
  213. go
  214.  
  215. print ''
  216. print 'Make sure the maxlen of sysdatabases clustered index was updated'
  217. print 'to include the new category column.'
  218. update sysindexes set maxlen = 78 where id = object_id('sysdatabases') and
  219.    indid = 1 and maxlen <> 78
  220. go
  221.  
  222. print ''
  223. print 'Checking to see if sysdevices needs to be upgraded.'
  224. go
  225. if not exists (select * from syscolumns
  226.         where name = 'stripeset'
  227.         and id=object_id('sysdevices'))
  228.     begin
  229.         print ''
  230.         print 'Updating sysdevices.'
  231.         alter table sysdevices add stripeset varchar(30) null
  232.     end
  233. else
  234.         raiserror('sysdevices already has column ''stripeset''.',1,1)
  235. go
  236.  
  237.  
  238. print ''
  239. print 'Checking to see if sysservers needs to be upgraded.'
  240. go
  241. if not exists (select * from syscolumns
  242.     where name = 'topologyx'
  243.     and id=object_id('sysservers'))
  244.     begin
  245.         print ''
  246.     print 'Updating sysservers.'
  247.     alter table sysservers add topologyx int null, topologyy int null
  248.     end
  249. else
  250.         raiserror('sysservers already has column ''topologyx''.',1,1)
  251. go
  252.  
  253. /* Set creation date for master and model databases. */
  254. print ''
  255. print 'Setting creation date for master and model databases.'
  256. go
  257. update sysdatabases set crdate = getdate()
  258.     where name in ('master','model') and crdate like '%1900%'
  259. go
  260.  
  261. /* Set creation date for system tables. */
  262. print ''
  263. print 'Setting creation date for system tables.'
  264. go
  265. update sysobjects set crdate = getdate()
  266.     where id < 100 and crdate like '%1900%'
  267. go
  268.  
  269. print ''
  270. print 'Fixing syscolumns.usertype for syscurconfigs entry.'
  271. go
  272. /* Fix usertype value for syscurconfigs before upgrade. */
  273. update syscolumns set usertype = 6
  274.     where name='status' and id=object_id('syscurconfigs')
  275. go
  276.  
  277. print ''
  278. print 'Checking page counts in ''model'' database.'
  279. go
  280. dbcc checkdb('model') with no_infomsgs
  281. go
  282.  
  283. print ''
  284. print 'Initializing sysconfigures.status.'
  285. go
  286. update sysconfigures set status = status | 2 where config >= 1000
  287. go
  288.  
  289. /*
  290. ** Create procedures to perform upgrade functions.
  291. */
  292. if exists (select * from sysobjects
  293.             where name = 'sp_db_upgrade1'
  294.             and sysstat & 0xf = 4)
  295.     begin
  296.         raiserror('Dropping    sp_db_upgrade1...',1,1)
  297.             drop procedure sp_db_upgrade1
  298.     end
  299. else
  300.         raiserror('proc sp_db_upgrade1 does not pre-exist.',1,1)
  301. go
  302. if exists (select * from sysobjects
  303.             where name = 'sp_db_upgrade2'
  304.             and sysstat & 0xf = 4)
  305.     begin
  306.         raiserror('Dropping    sp_db_upgrade2...',1,1)
  307.             drop procedure sp_db_upgrade2
  308.     end
  309. else
  310.         raiserror('proc sp_db_upgrade2 does not pre-exist.',1,1)
  311. go
  312.  
  313. dbcc traceon(3643) -- Ensure that we are allowed access to databases with old schema version.
  314.  
  315. go
  316. print ''
  317. print 'Creating procedure sp_db_upgrade1.'
  318. go
  319. create procedure sp_db_upgrade1 as
  320. --Makes SQL 6.0 specific schema changes to the current database.
  321.  
  322.     declare @curdbname varchar(30)
  323.     declare @pmsg varchar(255)
  324.     declare @dumptran varchar(255)
  325.  
  326.     select @curdbname = db_name()
  327.     select @dumptran = 'dump tran '+db_name()+' with no_log'
  328.  
  329.     raiserror('
  330.  
  331. Top of sp_db_upgrade1.  Upgrading and creating new system tables, db=%s.'
  332.         ,1,1,@curdbname)
  333.  
  334.  
  335.     /* See if database is 'offline' or 'Read only' and, if so, exit. */
  336.     if (select status & (512+1024) from master..sysdatabases where name=db_name())
  337.         <> 0
  338.         begin
  339.             select @pmsg = 'Database '''+db_name()+''' cannot be upgraded because it is ''offline'' or ''Read only''.'
  340.             print ''
  341.             print @pmsg
  342.             print 'Use sp_dboption to change status.'
  343.             return(1)
  344.         end
  345.  
  346.     exec(@dumptran)
  347.  
  348.     /* Make changes to sysobjects to avoid keyword conflict. */
  349.     print 'Section for updating sysindexes (schema_ver).'
  350.     if exists (select * from syscolumns
  351.         where id=object_id('sysobjects')
  352.         and name='schema')
  353.         begin
  354.         raiserror('syscolumns entry for sysobjects.schema will now be upd to schema_ver, db=%s.',1,1,@curdbname)
  355.         exec('update syscolumns set name=''schema_ver''
  356.             where id=object_id(''sysobjects'') and name = ''schema''')
  357.         end
  358.  
  359.     if @@error<>0
  360.         begin
  361.         raiserror('Could not successfully upgrade table ''sysobjects'' (%s).'
  362.                 ,11,1,@curdbname)
  363.         return(1)
  364.         end
  365.     else
  366.         begin
  367.         raiserror('Upgraded col name sysobjects.schema_ver will now be incremented, db=%s.',1,1,@curdbname)
  368.         exec('update sysobjects set schema_ver = schema_ver+1
  369.                 where id = object_id(''sysobjects'')')
  370.         end
  371.  
  372.  
  373.     /* Make changes to sysindexes for enhanced fillfactor support. */
  374.     print 'Section for updating sysindexes (OrigFillFactor).'
  375.     if exists (select * from syscolumns
  376.             where id=object_id('sysindexes')
  377.                 and name='usagecnt')
  378.         begin
  379.         raiserror('syscolumns row for sysindexes OrigFillFactor will be updated.',1,1)
  380.         select 'sysindexes_before'='sysindexes_before',colid,name
  381.            from syscolumns
  382.            where id=object_id('sysindexes') and name in ('usagecnt','OrigFillFactor')
  383.            order by colid
  384.  
  385.         update syscolumns set
  386.             name='OrigFillFactor', type=48, usertype=5,length=1
  387.                 where id=object_id('sysindexes') and name = 'usagecnt'
  388.         end
  389.     else
  390.         print 'syscolumns row for sysindexes OrigFillFactor already Updated.'
  391.  
  392.     if @@error<>0
  393.         begin
  394.             print 'Could not successfully upgrade table ''sysindexes'' (OFFactor).'
  395.             return(1)
  396.         end
  397.     else
  398.         begin
  399.             print '''sysindexes'' successfully upgraded (sysobjects.schema_ver).'
  400.         select 'sysindexes_after '='sysindexes_after ',colid,name
  401.            from syscolumns
  402.            where id=object_id('sysindexes') and name in ('usagecnt','OrigFillFactor')
  403.            order by colid
  404.  
  405.             exec('update sysobjects set schema_ver = schema_ver + 1
  406.                 where id=object_id(''sysindexes'')')
  407.         end
  408.  
  409.         /* Convert unused column sysobjects.expdate to sysobjects.version */
  410.     print 'Converting sysobjects.expdate to sysobjects.version.'
  411.     update syscolumns
  412.         set name='version', type=56, usertype=7, length=4
  413.         where id=object_id('sysobjects')
  414.             and name = 'expdate'
  415.  
  416.     if @@error<>0
  417.         begin
  418.             print 'Could not successfully upgrade table ''sysobjects'' (syscolumns.name=expdate-->version).'
  419.             return(1)
  420.         end
  421.     else
  422.         begin
  423.             print '''Sysobjects.expdate'' successfully converted (increment schema_ver).'
  424.             exec('update sysobjects set schema_ver = schema_ver + 1
  425.                 where id=object_id(''sysobjects'')')
  426.         end
  427.  
  428.     /* Create table to support DRI. */
  429.     print 'Checking to see if ''sysreferences'' exists.'
  430.     if not exists (select * from sysobjects
  431.                     where name = 'sysreferences')
  432.         begin
  433.             print 'Creating sysreferences.'
  434.  
  435.             create table sysreferences
  436.             (
  437.             constid        int        not null,
  438.             fkeyid        int        not null,
  439.             fkeydbid    smallint    not null,
  440.             rkeyid        int        not null,
  441.             rkeydbid    smallint    not null,
  442.             rkeyindid    smallint    not null,
  443.             keycnt        smallint    not null,
  444.             fkey1        tinyint        not null,
  445.             fkey2        tinyint        not null,
  446.             fkey3        tinyint        not null,
  447.             fkey4        tinyint        not null,
  448.             fkey5        tinyint        not null,
  449.             fkey6        tinyint        not null,
  450.             fkey7        tinyint        not null,
  451.             fkey8        tinyint        not null,
  452.             fkey9        tinyint        not null,
  453.             fkey10        tinyint        not null,
  454.             fkey11        tinyint        not null,
  455.             fkey12        tinyint        not null,
  456.             fkey13        tinyint        not null,
  457.             fkey14        tinyint        not null,
  458.             fkey15        tinyint        not null,
  459.             fkey16        tinyint        not null,
  460.             rkey1        tinyint        not null,
  461.             rkey2        tinyint        not null,
  462.             rkey3        tinyint        not null,
  463.             rkey4        tinyint        not null,
  464.             rkey5        tinyint        not null,
  465.             rkey6        tinyint        not null,
  466.             rkey7        tinyint        not null,
  467.             rkey8        tinyint        not null,
  468.             rkey9        tinyint        not null,
  469.             rkey10        tinyint        not null,
  470.             rkey11        tinyint        not null,
  471.             rkey12        tinyint        not null,
  472.             rkey13        tinyint        not null,
  473.             rkey14        tinyint        not null,
  474.             rkey15        tinyint        not null,
  475.             rkey16        tinyint        not null
  476.             )
  477.             on system
  478.  
  479.             if @@error<>0
  480.                 begin
  481.                 print 'Could not successfully create table ''sysreferences''.'
  482.                 return(1)
  483.                 end
  484.             else
  485.                 print '''Sysreferences'' successfully created (now IXs).'
  486.  
  487.             create unique clustered index ucsysreferences
  488.                 on sysreferences(constid) on system
  489.             create nonclustered index nc1sysreferences
  490.                 on sysreferences(fkeyid) on system
  491.             create nonclustered index nc2sysreferences
  492.                 on sysreferences(rkeyid) on system
  493.         end
  494.     else
  495.         raiserror('''sysreferences'' already exists.',1,1)
  496.  
  497.     /* Create new table to support DRI. */
  498.     print 'Checking to see if ''sysconstraints'' exists.'
  499.     if not exists (select * from sysobjects
  500.                     where name = 'sysconstraints')
  501.         begin
  502.             print 'Creating sysconstraints.'
  503.  
  504.             create table sysconstraints
  505.             (
  506.             constid        int    not null,
  507.             id        int    not null,
  508.             colid        tinyint not null,
  509.             spare1        tinyint    not null,
  510.             status        int    not null,
  511.             actions        int    not null,
  512.             error        int    not null
  513.             )
  514.             on system
  515.  
  516.             if @@error<>0
  517.                 begin
  518.                     print 'Could not successfully create table ''sysconstraints''.'
  519.                     return(1)
  520.                 end
  521.             else
  522.                 print '''Sysconstraints'' successfully created (now IXs).'
  523.  
  524.                 create clustered index csysconstraints
  525.                 on sysconstraints(id, colid) on system
  526.             create unique nonclustered index unc1sysconstraints
  527.                 on sysconstraints(constid) on system
  528.          end
  529.     else
  530.         raiserror('''sysconstraints already exists.',1,1)
  531.  
  532.     if not exists (select * from sysobjects
  533.                     where name = 'syssubscriptions')
  534.         begin
  535.             print 'Creating syssubscriptions.'
  536.  
  537.             CREATE TABLE syssubscriptions
  538.             (
  539.             artid     int         NOT NULL,
  540.             srvid     smallint    NOT NULL,
  541.             dest_db   varchar(30) NOT NULL,
  542.             status    tinyint     NOT NULL,
  543.             sync_type tinyint     NOT NULL,
  544.             timestamp
  545.             )
  546.             on system
  547.  
  548.             if @@error<>0
  549.                 begin
  550.                     print 'Could not successfully create table ''syssubscriptionss''.'
  551.                     return(1)
  552.                 end
  553.             else
  554.                 print '''Syssubscriptions'' successfully created (now IXs).'
  555.  
  556.             create unique nonclustered index unc1syssubscriptions
  557.                 on syssubscriptions (artid, srvid) on system
  558.         end
  559.     else
  560.         raiserror('''syssubscriptions already exists.',1,1)
  561.  
  562.     /*
  563.     ** Add precision and scale columns to systypes and syscolumns
  564.     ** if necessary.
  565.     */
  566.     print 'Checking ''systypes'' for prec and scale columns.'
  567.     if not exists (select * from syscolumns
  568.         where id = object_id('systypes')
  569.         and name = 'prec')
  570.         begin
  571.             print 'Adding columns for support of exact numeric datatypes.'
  572.             alter table systypes add prec tinyint null, scale tinyint null
  573.             if @@error<>0
  574.                 begin
  575.                     print 'Could not successfully upgrade table ''systypes''.'
  576.                     return(1)
  577.                 end
  578.             else print '''Systypes'' successfully upgraded'
  579.         end
  580.     else
  581.         raiserror('''prec'' and ''scale'' are already cols in ''systypes''.',1,1)
  582.  
  583.     exec(@dumptran)
  584.  
  585.     print 'Checking ''syscolumns'' for prec and scale columns.'
  586.     if not exists (select * from syscolumns
  587.         where id = object_id('syscolumns')
  588.         and name = 'prec')
  589.         begin
  590.             print 'Adding columns for support of exact numeric datatypes.'
  591.             alter table syscolumns add prec tinyint null, scale tinyint null
  592.             if @@error<>0
  593.                 begin
  594.                     print 'Could not successfully upgrade table ''syscolumns''.'
  595.                     return(1)
  596.                 end
  597.             else print '''Syscolumns'' successfully upgraded'
  598.         end
  599.     else
  600.         raiserror('''prec'' and ''scale'' are already cols in ''syscolumns''.',1,1)
  601.  
  602.  
  603.     ---------------------  add sysprotects.grantor col  -----------------
  604.  
  605.  
  606.     print 'Checking ''sysprotects'' for grantor column.'
  607.  
  608.     if not exists (select * from syscolumns
  609.         where id = object_id('sysprotects')
  610.         and name = 'grantor')
  611.         begin
  612.             ALTER table sysprotects add grantor smallint null
  613.  
  614.             if @@error<>0
  615.                 begin
  616.                 print 'Could not successfully upgrade table ''sysprotects''.'
  617.                 return(1)
  618.                 end
  619.             else
  620.                 print '''sysprotects'' successfully upgraded.'
  621.         end
  622.     else
  623.         raiserror('''grantor'' col already exists in ''sysprotects''.',1,1)
  624.  
  625.  
  626.     ------------------------  dbcc, extracting syscomments.text  ---------
  627.  
  628.     exec(@dumptran)
  629.  
  630.     /* Upgrade procs., triggers, etc. */
  631.     print 'Upgrading procedures, triggers, views and rules.'
  632.  
  633.     dbcc upgradedb(null)
  634.     if @@error<>0
  635.         begin
  636.         print ''
  637.         print 'WARNING - Encountered error(s) upgrading some procedures, views, triggers, rules or defaults.'
  638.         print 'These objects will NOT be accessible after completion of upgrade.'
  639.         print 'Please see event viewer or errorlog for more information and then use sp_db_upgrade <dbname> to complete the upgrade process for this database.'
  640.         end
  641.     else
  642.         print '''dbcc upgradedb'' successfully completed without errors.'
  643.  
  644.     exec(@dumptran)
  645.  
  646.     select @pmsg='Running dbcc updateusage on '''+db_name()+'''.'
  647.     print @pmsg
  648.     dbcc updateusage (0) with count_rows
  649.  
  650.     /*
  651.     ** Set flag to indicate that database schema has been updated
  652.     ** and is ready for data initialization.
  653.     */
  654.     update master..sysdatabases set version = 32767 where name=db_name()
  655.  
  656.     raiserror('Ending sp_db_upgrade1.  Database %s successfully upgraded, now needs initializing (version set=32767).'
  657.             ,1,1,@curdbname)
  658.     print ' '  --resets @@error=0, from 5000
  659.     return(0)
  660. go
  661. dump tran master with no_log
  662. go
  663.  
  664. /*****************************
  665. sysprocesses colums update
  666. ******************************/
  667.  
  668.  
  669. print ''
  670. print 'Checking to see if sysprocesses needs to be upgraded.'
  671. go
  672.  
  673. delete from syscolumns where id = 32 and colid > 5
  674. go
  675.  
  676. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  677. values (32,0,6,0,47,30,48,1,0,0,"program_name")
  678. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  679. values (32,0,7,0,47,8,78,1,0,0,"hostprocess")
  680. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  681. values (32,0,8,0,47,16,86,1,0,0,"cmd")
  682. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  683. values (32,0,9,0,56,4,104,7,0,0,"cpu")
  684. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  685. values (32,0,10,0,56,4,108,7,0,0,"physical_io")
  686. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  687. values (32,0,11,0,56,4,112,7,0,0,"memusage")
  688. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  689. values (32,0,12,0,52,2,116,6,0,0,"blocked")
  690. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  691. values (32,0,13,0,45,2,118,3,0,0,"waittype")
  692. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  693. values (32,0,14,0,52,2,120,6,0,0,"dbid")
  694. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  695. values (32,0,15,0,52,2,122,6,0,0,"uid")
  696. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  697. values (32,0,16,0,52,2,124,6,0,0,"gid")
  698. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  699. values (32,0,17,0,61,8,128,1,0,0,"login_time")
  700. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  701. values (32,0,18,0,61,8,136,1,0,0,"last_batch")
  702. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  703. values (32,0,19,0,47,30,144,1,0,0,"nt_domain")
  704. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  705. values (32,0,20,0,47,30,174,1,0,0,"nt_username")
  706. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  707. values (32,0,21,0,47,12,204,1,0,0,"net_address")                    
  708. insert syscolumns (id,number,colid,status,type,length,offset,usertype,cdefault,domain,name)
  709. values (32,0,22,0,47,12,216,1,0,0,"net_library")
  710. go
  711.  
  712. /* Update sysindexes to reflect the new mixlen,maxlen and rowpage for sysprocesses */
  713. update sysindexes set minlen=228,maxlen=228,rowpage=8 where id =32
  714. go
  715.  
  716. /************************************
  717. end of sysprocesses columns update
  718. *************************************/
  719.  
  720.  
  721. print ''
  722. print 'Checking for databases with old schemas.'
  723. go
  724. /*
  725. ** Upgrade system tables in each database created with an old schema.
  726. */
  727. declare @dbver int
  728. dbcc getvalue('current_version')
  729. select @dbver = @@error        -- Get expected db schema version level from server.
  730.  
  731. declare c1 scroll cursor for select name from master..sysdatabases
  732.     where version < @dbver
  733. go
  734.  
  735. declare @dbname varchar(30)
  736. open c1
  737. fetch c1 into @dbname
  738. while @@fetch_status >= 0    -- make sure a valid row was fetched
  739.     begin
  740.     raiserror('(Inside loop exec sp_db_upgrade1, db=%s)',1,1,@dbname)
  741.  
  742.     exec('use '+@dbname+'
  743.         declare @rc int
  744.         exec @rc = sp_db_upgrade1
  745.         if @rc <> 0 RaisERROR(''Bad rc from sp_db_upgrade1!  Breaking connection.''
  746.                 ,22,127)
  747.        ')
  748.  
  749.     fetch c1 into @dbname    /* Get next db name. */
  750.     end
  751. go
  752.  
  753. use master
  754. go
  755.  
  756. declare @curdbname varchar(30) select @curdbname=db_name()
  757. raiserror('
  758. After USE MASTER above, current db of context is:  %s.',1,1,@curdbname)
  759. go
  760.  
  761. /*
  762. ** Had to add new columns before referencing them in declaration
  763. ** of procedure that initializes existing rows and adds new ones.
  764. */
  765. print ''
  766. print '
  767. Creating procedure sp_db_upgrade2.'
  768. go
  769. create procedure sp_db_upgrade2 as
  770.     declare @pmsg varchar(255)
  771.     declare @dumptran varchar(255)
  772.     declare @id int
  773.     declare @indid int
  774.     declare @dbstat int
  775.     declare @repl_uid int
  776.     declare @curdbname varchar(30)
  777.     declare @counter int
  778.  
  779.     select  @curdbname = db_name()  ,@counter=0
  780.     select @dumptran = 'dump tran '+db_name()+' with no_log'
  781.  
  782.     raiserror('
  783.  
  784. At top inside sp_db_upgrade2, db=%s.',1,1,@curdbname)
  785.  
  786.     if (select version from master..sysdatabases where name = db_name()) <> 32767
  787.         begin
  788.             select @pmsg=
  789.             'Cannot initialize '''+db_name()+''' database until schema has been modified.'
  790.             print @pmsg
  791.             print 'Please correct upgrade errors and then use sp_db_upgrade <dbname> to complete the upgrade process for this database.'
  792.             return(1)
  793.         end
  794.  
  795.     /* See if database is 'Read only' and, if so, exit. */
  796.     if (select status & 1024 from master..sysdatabases where name=db_name())
  797.         <> 0
  798.         begin
  799.             select @pmsg = 'Database '''+db_name()+''' cannot be initialized because it is ''Read only''.'
  800.             print @pmsg
  801.             print 'Use sp_dboption to change status.'
  802.             return(1)
  803.         end
  804.  
  805.     select @pmsg = 'Initializing database '''+db_name()+'''.'
  806.     print ''
  807.     print @pmsg
  808.  
  809.     exec(@dumptran)
  810.  
  811.     update sysobjects set crdate = getdate()
  812.         where id < 100 and crdate like '%1900%'
  813.  
  814.     print 'Converting timestamp datatype to fixed length.'
  815.         update systypes set type=45,allownulls=0
  816.             where name = 'timestamp'
  817.  
  818.     print 'Initializing new columns in systypes.'
  819.     update systypes set prec = NULL, scale = NULL
  820.         where type in (34,35)            -- Text/image
  821.     update systypes set prec = length, scale = NULL
  822.         where type in (37,38,39,45,47)        -- char/varchar,
  823.                             -- binary/varbinary
  824.                             
  825.     update systypes set prec = 10, scale = 0
  826.         where type in (38,56)            -- int/intn
  827.     update systypes set prec = 5, scale = 0
  828.         where type = 52                -- smallint
  829.     update systypes set prec = 3, scale = 0
  830.         where type = 48                -- tinyint
  831.     update systypes set prec = 1, scale = 0
  832.         where type = 50                -- bit
  833.     update systypes set prec = 19, scale = 4
  834.         where type in (60,110)            -- money/moneyn
  835.     update systypes set prec = 10, scale = 4
  836.         where type = 122            -- smallmoney
  837.  
  838.     update systypes set prec = 53, scale = NULL    --6.0=15, 6.5=53
  839.         where type in (62,109)            -- float/floatn
  840.         and (  (prec<>53 or prec is null)
  841.             OR  scale is not null
  842.             )
  843.     update systypes set prec = 24, scale = NULL    --6.0=7, 6.5=24
  844.         where type = 59                -- real
  845.         and (  (prec<>24 or prec is null)
  846.                     OR  scale is not null
  847.             )
  848.  
  849.     update systypes set prec = 23, scale = 3
  850.         where type in (61,111)            -- datetime/datetimn
  851.     update systypes set prec = 16, scale = 0
  852.         where type = 58                -- smalldatetime
  853.  
  854.     print 'Inserting rows for exact numerics into systypes.'
  855.     if not exists (select * from systypes where name = 'numeric')
  856.         begin
  857.         insert systypes
  858.         (uid,usertype,variable,allownulls,type,length,tdefault,domain,name,printfmt,prec,scale)
  859.         values
  860.         (1,10,0,1,63,17,0,0,'numeric',NULL,38,38)
  861.  
  862.         raiserror('systypes rows for exact numerics were just inserted.',1,1)
  863.         end
  864.     else
  865.         raiserror('systypes already has rows for exact numerics.',1,1)
  866.  
  867.     if not exists (select * from systypes where name = 'numericn')
  868.         insert systypes
  869.         (uid,usertype,variable,allownulls,type,length,tdefault,domain,name,printfmt,prec,scale)
  870.         values
  871.         (1,25,0,1,108,17,0,0,'numericn',NULL,38,38)
  872.  
  873.     if not exists (select * from systypes where name = 'decimal')
  874.         insert systypes
  875.         (uid,usertype,variable,allownulls,type,length,tdefault,domain,name,printfmt,prec,scale)
  876.          values
  877.          (1,24,0,1,55,17,0,0,'decimal',NULL,38,38)
  878.  
  879.     if not exists (select * from systypes where name = 'decimaln')
  880.     insert systypes
  881.         (uid,usertype,variable,allownulls,type,length,tdefault,domain,name,printfmt,prec,scale)
  882.         values
  883.         (1,26,0,1,106,17,0,0,'decimaln',NULL,38,38)
  884.  
  885.     exec(@dumptran)
  886.  
  887.     print 'Initializing new columns in syscolumns.'
  888.     update syscolumns
  889.         set prec = t.prec, scale = t.scale
  890.             from systypes t, syscolumns c
  891.             where c.usertype = t.usertype
  892.             and c.usertype not in (10,24,25,26) /* exact numerics. */
  893.  
  894.     update syscolumns set prec = length, scale = NULL
  895.         where type in (37,38,39,45,47)    /* char/varchar,binary/varbinary */
  896.  
  897.     /*
  898.     ** Can't update more than one row at a time in sysindexes
  899.     ** due to an internal restriction.
  900.     */
  901.     print 'Initializing sysindexes.OrigFillFactor.'
  902.     select @counter=0
  903.     exec('declare c2 cursor for select id from sysindexes (holdlock)')
  904.     open c2
  905.     fetch c2 into @id
  906.     while @@fetch_status <> -1
  907.         begin
  908.             select @counter=@counter+1
  909.             update sysindexes
  910.                 set OrigFillFactor = 0
  911.                     where current of c2
  912.             fetch c2 into @id
  913.         end
  914.  
  915.     deallocate c2
  916.  
  917.     raiserror('Number of sysindexes.OrigFillFactor cells Upd to 0 was %d',1,1,@counter)
  918.  
  919.     print 'Converting syscomments.texttype to bitmap.'
  920.  
  921.     update syscomments set texttype = 2 where texttype = 0
  922.  
  923.  
  924.     ---------------  initialize new sysprotects.grantor col  ----------
  925.  
  926.     if 0 = (select count(*) from sysprotects where grantor is NOT null)
  927.         begin
  928.  
  929.         raiserror('Some sysprotects.grantor cells are Null, so fix.',1,1)
  930.  
  931.         UPDATE         sysprotects
  932.             set     grantor =
  933.                     (select     sysobjects.uid
  934.                      from     sysobjects
  935.                      where     sysobjects.id = sysprotects.id
  936.                     )
  937.             where     id <> 0
  938.  
  939.  
  940.         if (@@error <> 0)
  941.             begin
  942.             print 'sysprotects.grantor init errored!'
  943.             return(1)
  944.             end
  945.  
  946.  
  947.         -----  modify index  -----
  948.  
  949.         DROP index sysprotects.sysprotects
  950.  
  951.         CREATE Clustered
  952.             index    sysprotects
  953.             on    sysprotects
  954.                 (id, action, grantor, uid, protecttype)
  955.  
  956.  
  957.         if (@@error <> 0)
  958.             begin
  959.             print 'index sysprotects.sysprotects creation errored!'
  960.             return(1)
  961.             end
  962.  
  963.         print 'sysprotects.grantor nulls & new index initialized.'
  964.         end
  965.     else
  966.         raiserror('All sysprotects.grantor cells are already nonNull.',1,1)
  967.  
  968.  
  969.  
  970.     -----------------  create sys-tables which have identity col  -------
  971.  
  972.     /*
  973.     ** Need to create these here since identity cols. depend on
  974.     ** on values in prec. and scale. cols.
  975.     */
  976.     print 'Checking to see if ''sysarticles''already exists.'
  977.  
  978.     if not exists (select * from sysobjects where name = 'sysarticles')
  979.         begin
  980.             print 'Creating sysarticles.'
  981.  
  982.             create table sysarticles
  983.             (
  984.             artid        int        identity,
  985.             columns        varbinary(32)    NULL,
  986.             creation_script    varchar(127)    NULL,
  987.             del_cmd        varchar(255)    NULL,
  988.             description    varchar(255)    NULL,
  989.             dest_table    varchar(30)    NOT NULL,
  990.             filter        int        NOT NULL,
  991.             filter_clause    text         NULL,
  992.             ins_cmd        varchar(255)    NULL,
  993.             name        varchar(30)    NOT NULL,
  994.             objid        int        NOT NULL,
  995.             pubid        int        NOT NULL,
  996.             pre_creation_cmd tinyint    NOT NULL,
  997.             status        tinyint        NOT NULL,
  998.             sync_objid    int        NOT NULL,
  999.             type        tinyint        NOT NULL,
  1000.             upd_cmd        varchar(255)    NULL
  1001.             )
  1002.             on system
  1003.  
  1004.             if @@error<>0
  1005.                 begin
  1006.                 print 'Could not successfully create table ''sysarticles''.'
  1007.                 return(1)
  1008.                 end
  1009.             else
  1010.                 print '''Sysarticles'' successfully created, with identity (now IXs).'
  1011.  
  1012.             create unique nonclustered index unc1sysarticles
  1013.                 on sysarticles(artid, pubid) on system
  1014.          end
  1015.     else
  1016.         raiserror('sysarticles already exists.',1,1)
  1017.  
  1018.     exec(@dumptran)
  1019.  
  1020.     print 'Checking to see if ''syspublications'' exists.'
  1021.  
  1022.     if not exists (select * from sysobjects
  1023.                 where name = 'syspublications')
  1024.         begin
  1025.             print 'Creating syspublications.'
  1026.  
  1027.             CREATE TABLE syspublications (
  1028.             description    varchar(255)     NULL,
  1029.             name         varchar(30)    NOT NULL,
  1030.             pubid         int        identity,
  1031.             repl_freq     tinyint        NOT NULL,
  1032.             restricted     bit        NOT NULL,
  1033.             status         tinyint        NOT NULL,
  1034.             sync_method    tinyint        NOT NULL,
  1035.             taskid        int        NOT NULL
  1036.             )
  1037.             on system
  1038.  
  1039.             if @@error<>0
  1040.                 begin
  1041.                 print 'Could not successfully create table ''syspublications''.'
  1042.                 return(1)
  1043.                 end
  1044.             else
  1045.             print '''Syspublications'' successfully created, with identity (now IXs).'
  1046.  
  1047.             create unique nonclustered index unc1syspublications
  1048.                 on syspublications (pubid) on system
  1049.             create unique nonclustered index unc2syspublications
  1050.                 on syspublications (name) on system
  1051.         end
  1052.     else
  1053.         raiserror('syspublications already exists.',1,1)
  1054.  
  1055.     exec(@dumptran)
  1056.  
  1057.     /*
  1058.     ** Set sysdatabases.version to indicate that db has been upgraded.
  1059.     */
  1060.         declare @dbver int
  1061.     dbcc getvalue('current_version')
  1062.     select @dbver = @@error        -- Get expected db schema version level from server.
  1063.  
  1064.     update sysdatabases set version = @dbver where name=db_name()
  1065.  
  1066.     raiserror('Ending sp_db_upgrade2.  Database %s was successfully initialized, version set=%d.'
  1067.             ,1,1,@curdbname,@dbver)
  1068.     return(0)
  1069. go
  1070.  
  1071. if exists (select * from master.dbo.sysobjects where name='sp_db_upgrade2')
  1072.     begin
  1073.     raiserror('(sp_db_upgrade2 exists in master, as it should.)',1,1) with nowait
  1074.     end
  1075. else
  1076.     begin
  1077.     raiserror('(sp_db_upgrade2 does NOT exists in master!)',11,1) with nowait
  1078.     end
  1079. go
  1080.  
  1081. declare     @dbname varchar(32)
  1082.     ,@counter int
  1083. select     @counter=0
  1084. fetch first from c1 into @dbname
  1085.  
  1086. while @@fetch_status >= 0
  1087.   begin
  1088.     select @counter = @counter + 1
  1089.  
  1090.     exec('use '+@dbname+' exec sp_db_upgrade2')
  1091.     fetch c1 into @dbname        -- Get next db name.
  1092.   end
  1093.  
  1094. raiserror('Number of loops to exec sp_db_upgrade2 was %d.',1,1,@counter)
  1095. go
  1096. deallocate c1        -- No longer need cursor./
  1097. go
  1098.  
  1099. use master        -- Safety measure, no longer needed (6.01.144).
  1100. go
  1101.  
  1102. dump tran master with no_log
  1103. go
  1104.  
  1105. /*
  1106. ** Now we can create a 'wrapper' proc. for future manual upgrades.
  1107. */
  1108. print ''
  1109. print 'Creating procedure sp_db_upgrade.'
  1110. go
  1111. if exists (select * from sysobjects
  1112.             where name = 'sp_db_upgrade'
  1113.             and sysstat & 0xf = 4)
  1114.     begin
  1115.         raiserror('Dropping    sp_db_upgrade...',1,1)
  1116.             drop procedure sp_db_upgrade
  1117.     end
  1118. else
  1119.     raiserror('sp_db_upgrade does not pre-exist.',1,1)
  1120. go
  1121.  
  1122. dbcc traceoff(3643)    -- Disable access to databases with old schema.
  1123. go
  1124.  
  1125. create procedure sp_db_upgrade
  1126.     @dbname varchar(30) = null
  1127. as
  1128.     declare @cur_db varchar(30)
  1129.     select @cur_db=db_name()    /* Keep track of which db we started in. */
  1130.  
  1131.     if @dbname is null
  1132.         select @dbname = db_name()
  1133.  
  1134.     dbcc traceon(3643)    /* Enables us to 'use' a database with an 'outdated' schema. */
  1135.     exec('use '+@dbname+' exec sp_db_upgrade1 exec sp_db_upgrade2')
  1136.     dbcc traceoff(3643)
  1137.  
  1138.     exec('use '+@cur_db)    --In 6.01.144+ this is essentially a NOP.
  1139. go
  1140.  
  1141. print ''
  1142. print 'Updating sysrefences.'
  1143. go
  1144.  
  1145. declare @counter int select @counter=0
  1146. declare @dbname varchar(30)
  1147. declare sysrefupdcur cursor for select name from sysdatabases
  1148.  
  1149. open sysrefupdcur
  1150.  
  1151. fetch sysrefupdcur into @dbname
  1152.  
  1153. while @@fetch_status >= 0
  1154.     begin
  1155.         select @counter = @counter + 1
  1156.  
  1157.         exec('use '+@dbname + ' ' +
  1158.              'update sysreferences set
  1159.                 fkeydbid = 0,
  1160.                 rkeydbid = 0
  1161.             where  fkeydbid > 0
  1162.                 or rkeydbid > 0'
  1163.                     )
  1164.         fetch sysrefupdcur into @dbname
  1165.     end
  1166.  
  1167. deallocate sysrefupdcur
  1168.  
  1169. raiserror('Number of loops to Upd sysreferences._keydbid to 0 was %d.',1,1,@counter)
  1170. go
  1171.  
  1172. use master
  1173. go
  1174.  
  1175. print ''
  1176. print 'Decrypting passwords encrypted by prior builds for upgrade.'
  1177. go
  1178. update syslogins set password = olddecrypt(password), status = status & (~4)
  1179.     where (status & 4) = 4
  1180. go
  1181.  
  1182. print ''
  1183. print 'Encrypting existing passwords.'
  1184. go
  1185. update syslogins set password = pwdencrypt(password), status = status | 8
  1186.     where (status & 8) = 0
  1187. go
  1188.  
  1189. print ''
  1190. print 'Setting all existing servers to be ''rpc'' servers.'
  1191. go
  1192. update sysservers set srvstatus = srvstatus | 1
  1193.     where srvid <> 0    /* Don't want to do this to the 'local' server. */
  1194. go
  1195.  
  1196. --exec sp_coalesce_fragments '%'  --easy chance to simplify legacy sysusages
  1197. go
  1198.  
  1199.  
  1200. use master
  1201. go
  1202.  
  1203.  
  1204. -- - ---------------------------------------------------------------------------------
  1205. go
  1206. dump transaction model with no_log
  1207. go
  1208.  
  1209. ---- 6.5 Beta (only) needs db=model at least 2 Mb large, for sp_makewebpage.
  1210. ----    (Thus no 1.44Mb floppy RemovableMedia DB's.)
  1211.  
  1212. ---- IF db=model is already at least 2 Mb big,
  1213. ----    THEN most of this section's logic will be skipped.
  1214.  
  1215. declare
  1216.      @Int1            integer
  1217.     ,@Int2            integer
  1218.     ,@Num1            numeric(21,1)
  1219.     ,@vc1            varchar(33)
  1220. declare
  1221.      @2KbPagesModel        numeric(21,1)
  1222.     ,@2KbPer1Mb        numeric(21,1)
  1223.     ,@NumMbFinalForModel    numeric(21,1)
  1224.     ,@dbid_model        integer
  1225.     ,@AddToModel2Kb        numeric(21,1)
  1226.     ,@RawAddToMasterDV2Kb    numeric(21,1)
  1227. declare
  1228.      @Reserved        numeric(21,1)
  1229. declare
  1230.      @DVSize2Kb        numeric(21,1)
  1231.     ,@DVConsumed2Kb        numeric(21,1)
  1232.  
  1233. select
  1234.      @2KbPer1Mb        = 512.0 -- 512 2Kb pages per 1 Mb
  1235.     ,@NumMbFinalForModel    = 2.0
  1236.  
  1237. select @dbid_model = db_id('model') ,@Int1 = @NumMbFinalForModel
  1238. select @Int2 = @Int1 * @2KbPer1Mb
  1239.  
  1240. raiserror('
  1241.  
  1242. db=model (dbid=%d) might need to be increased to a new total size of %d Mb (=%d 2Kb pages)....
  1243. ',1,1 ,@dbid_model,@Int1,@Int2)
  1244.  
  1245. --raiserror('
  1246. --(This logic ASSUMES there is enough unused space inside db=model for NewInstall/Upgrade
  1247. --activities, if db=model is already at least 2 Mb big.)
  1248. --
  1249. --',1,1)
  1250.  
  1251.  
  1252. select 'Before',ug.* from sysusages ug ,sysdevices dv
  1253.         where dv.name='master' and ug.vstart between dv.low and dv.high order by ug.vstart
  1254. select 'Before',dv.* from sysdevices dv where dv.name='master'
  1255.  
  1256.  
  1257. ---- Get the current size of db=model.
  1258.  
  1259.  
  1260. select
  1261.      @2KbPagesModel        = sum(ug.size)
  1262.   from
  1263.      master.dbo.sysusages        ug
  1264.     ,master.dbo.sysdevices        dv
  1265.     ,master.dbo.sysdatabases    db
  1266.   where
  1267.      ug.vstart between dv.low and dv.high
  1268.   and     db.dbid    = ug.dbid
  1269.   and     db.name    = 'model'
  1270.   and     dv.cntrltype    = 0
  1271.  
  1272. select @Int1 = @2KbPagesModel
  1273. raiserror('
  1274. The ''before'' size of db=model, in 2Kb pages, is %d.
  1275.  
  1276. ',1,1 ,@Int1)
  1277.  
  1278.  
  1279. ---- Get the number of Reserved pages inside db=model.
  1280.  
  1281.  
  1282. select @Reserved = sum(reserved) from model.dbo.sysindexes where indid in (0,1,255)
  1283.  
  1284. select @Int1 = convert(integer,@Reserved)
  1285. raiserror('In db=model there are %d pages Reserved.',1,1 ,@Int1)
  1286.  
  1287.  
  1288. ---- Skip the rest of this section?
  1289.  
  1290.  
  1291. if (@2KbPagesModel / @2KbPer1Mb >= @NumMbFinalForModel)  -- So far no action needed.
  1292.     begin
  1293.  
  1294.     select @Int1 = convert(integer,@NumMbFinalForModel)
  1295.     raiserror('db=model is already at least %d Mb large.',1,1 ,@Int1)
  1296.  
  1297.     if (@2KbPagesModel - @Reserved  >= @2KbPer1Mb / 2.0)  -- So far no action needed.
  1298.         begin
  1299.         raiserror('Sufficient UnReserved space available in db=model.',1,1)
  1300.  
  1301.         raiserror('Skipping over most of this db=model upsizing section, no action needed.',1,1)
  1302.  
  1303.         goto LABEL_153MODELBIGENOUGH
  1304.  
  1305.         end
  1306.  
  1307.     raiserror('Due to extra user objects or data, db=model will be increased in size by 1 Mb.',1,1)
  1308.  
  1309.     select @NumMbFinalForModel = @NumMbFinalForModel + 1
  1310.     end
  1311. else
  1312.     begin
  1313.     raiserror('db=model does need to be increased to a new total size.  Upgrade will now do this....',1,1)
  1314.     raiserror('(If a Disk Resize is performed, it will try to bring the device size to end on a 1Mb boundary.)',1,1)
  1315.     raiserror('(Consider manually running ''sp_coalesce_fragments'' any time after this SETUP completes.)',1,1)
  1316.     end
  1317.  
  1318.  
  1319. ---- Still here, so calculate additional space needed for db=model.
  1320.  
  1321.  
  1322. select @AddToModel2Kb = (@NumMbFinalForModel * @2KbPer1Mb) - @2KbPagesModel
  1323.  
  1324.  
  1325. ---- Get the current amount of unclaimed space on dv=master.
  1326.  
  1327.  
  1328. select
  1329.      @DVSize2Kb        = (dv.high - dv.low + 1)
  1330.     ,@DVConsumed2Kb        = sum(ug.size)
  1331.   from
  1332.      master.dbo.sysusages        ug
  1333.     ,master.dbo.sysdevices        dv
  1334.   where
  1335.      ug.vstart between dv.low and dv.high
  1336.   and     dv.name        = 'master'
  1337.   group by
  1338.      (dv.high - dv.low + 1)
  1339.  
  1340.  
  1341. raiserror('
  1342. (Notice-  The first server fragment starts at vstart=4 not =0.)',1,1)
  1343. -- Thus 4+252 pages are unreachable (essentially "claimed"), because Alter behaves in 0.5 Mb units.
  1344. select @DVConsumed2Kb = @DVConsumed2Kb + 4 + 252
  1345.  
  1346.  
  1347. select @Int1 = @DVSize2Kb ,@Int2 = @DVConsumed2Kb
  1348. raiserror('
  1349. The ''before'' size of dv=master, in 2Kb pages, is %d.  Of that, the amount already claimed is %d.
  1350.  
  1351. ',1,1 ,@Int1,@Int2)
  1352.  
  1353.  
  1354. ---- If necessary, increase the size of dv=master.
  1355.  
  1356.  
  1357. select @RawAddToMasterDV2Kb = @AddToModel2Kb - (@DVSize2Kb-@DVConsumed2Kb)
  1358.  
  1359.  
  1360. if @RawAddToMasterDV2Kb > 0
  1361.     begin
  1362.  
  1363.     -- Num of whole 1Mb chunks needed for dv=master, before accounting for fraction.
  1364.     select @Int1 =      convert(integer,@RawAddToMasterDV2Kb)
  1365.             / convert(integer,@2KbPer1Mb) -- truncates fraction
  1366.  
  1367.  
  1368.     -- If any fraction, then round up by just saying 1 more 1Mb chunk.
  1369.     select @Int2 =      convert(integer,@RawAddToMasterDV2Kb)
  1370.             % convert(integer,@2KbPer1Mb) -- gets only fraction
  1371.     if @Int2 > 0
  1372.         select @Int1 = @Int1 + 1  -- 1 more 1Mb chunk
  1373.  
  1374.  
  1375.     -- Get character format of new total size for dv=master.
  1376.     select @Int2 = (@Int1 * convert(integer,@2KbPer1Mb))
  1377.             +
  1378.             convert(integer,@DVSize2Kb)
  1379.  
  1380.     select @vc1 = convert(varchar,@Int2)
  1381.  
  1382.  
  1383.     raiserror('Will now attempt Disk Resize = %s 2Kb pages total' ,1,1 ,@vc1)
  1384.  
  1385.     execute('disk resize name=''master'',size=' + @vc1)
  1386.  
  1387.     if @@error <> 0
  1388.         begin
  1389.         raiserror('Warning bsdf3117u, dv=master could not be increased.',2,2)
  1390.  
  1391.         goto LABEL_153MODELBIGENOUGH
  1392.  
  1393. --        raiserror('Error bsdf3117n, dv=master could not be increased to a new total size!'
  1394. --            ,22,127) with log -- kills spid
  1395.         end
  1396.  
  1397.     end
  1398.  
  1399.  
  1400. ---- Finally, increase size of db=model.
  1401.  
  1402.  
  1403. -- Num of whole 1Mb chunks needed for db=model, before accounting for fraction.
  1404. select @Int1 =      convert(integer,@AddToModel2Kb)
  1405.         / convert(integer,@2KbPer1Mb) -- truncates fraction
  1406.  
  1407.  
  1408. -- If any fraction, then round up by just saying 1 more 1Mb chunk.
  1409. select @Int2 =      convert(integer,@AddToModel2Kb)
  1410.         % convert(integer,@2KbPer1Mb) -- gets only fraction
  1411. if @Int2 > 0
  1412.     select @Int1 = @Int1 + 1  -- 1 more 1Mb chunk
  1413.  
  1414.  
  1415. -- Get character format of additional Mb for db=model.
  1416. select @vc1 = convert(varchar,@Int1)
  1417.  
  1418.  
  1419. raiserror('
  1420. Will now attempt alter = %s 1Mb units additional.
  1421.  
  1422. ',1,1 ,@vc1)
  1423.  
  1424. execute('alter database model on master = ' + @vc1)
  1425.  
  1426. if @@error <> 0
  1427.     begin
  1428.     raiserror('Warning bsdf4285y, db=model could not be increased.',2,2)
  1429.  
  1430.     goto LABEL_153MODELBIGENOUGH
  1431.  
  1432. --    raiserror('Error bsdf4285p db=model could not be increased!'
  1433. --            ,22,127) with log -- kills spid
  1434.     end
  1435.  
  1436.  
  1437. select 'After_',ug.* from sysusages ug ,sysdevices dv
  1438.         where dv.name='master' and ug.vstart between dv.low and dv.high order by ug.vstart
  1439. select 'After_',dv.* from sysdevices dv where dv.name='master'
  1440.  
  1441.  
  1442.  
  1443. LABEL_153MODELBIGENOUGH:
  1444.  
  1445.  
  1446. raiserror('
  1447. Now done with section which considers increasing size of db=model.
  1448.  
  1449. ',1,1)
  1450.  
  1451. go
  1452. -- - - - -
  1453.  
  1454. use master
  1455. go
  1456.  
  1457. if exists (select * from sysobjects where name = 'sp_configure'
  1458.             and sysstat & 0xf = 4)
  1459.     begin
  1460.         exec sp_configure 'allow updates',0
  1461.         reconfigure with override
  1462.         print 'sp_configure & reconfigure_wo just set allow updates Off.'
  1463.     end
  1464. else
  1465.     raiserror('(sp_configure not present to turn Off ''allow updates''.)',1,1)
  1466. go
  1467.  
  1468. dump tran master with no_log
  1469. go
  1470. checkpoint
  1471. go
  1472.  
  1473.  
  1474. declare @vc1 varchar(55)
  1475. select  @vc1 = convert(varchar,getdate(),113)
  1476. raiserror('
  1477. This file completed successfully at %s' ,1,1 ,@vc1)
  1478. go
  1479.  
  1480. go
  1481. -- - -----
  1482.