home *** CD-ROM | disk | FTP | other *** search
/ Enter 2001 April / EnterCD4.iso / Update / SQL Server SP3 / sql70sp3i.exe / INSTALL / sp1.sql < prev    next >
Encoding:
Text File  |  1999-04-15  |  34.5 KB  |  862 lines

  1. -- sp1.sql
  2. -- Upgrade system tables from SQL Server 7.0 to SQL Server 7.0 SP1
  3. --    Copyright 1999, Microsoft Corp.    All rights reserved.
  4. --
  5. -- Contains "deltas" extracted from the following scripts:
  6. --        UPGRADE.SQL
  7. --        INSTCAT.SQL
  8. --        MESSAGES.SQL
  9. --        SQLDMO.SQL
  10. --        INSTMSDB.SQL
  11.  
  12. --***********************************************************************
  13. -- PROLOGUE
  14. --***********************************************************************
  15.  
  16. use master
  17. go
  18.  
  19. execute sp_configure 'allow updates',1
  20. go
  21.  
  22. reconfigure with override
  23. go
  24.  
  25. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  26. go
  27.  
  28. --***********************************************************************
  29. -- START MASTER
  30. --***********************************************************************
  31.  
  32. dump tran master with no_log
  33. go
  34.  
  35. --***********************************************************************
  36. -- UPGRADE.SQL
  37. --***********************************************************************
  38.  
  39. delete syscharsets where id between 73 and 75
  40. go
  41.  
  42. -- Feb10 1999 New case sensitive Scandinavian sort orders
  43. -- Sort order 73: LCID = 0x414, NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH
  44. insert syscharsets (type, id, csid, status, name, description, binarydefinition)
  45.     values (2001, 73, 1, 0, N'dnk_nor_dictionary', N'Danish/Norwegian case sensitive sort order for code page 1252',
  46.     0x800000001404000000000300)
  47. go
  48.  
  49. -- Sort order 74: LCID = 0x41d, NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH 
  50. insert syscharsets (type, id, csid, status, name, description, binarydefinition)
  51.     values (2001, 74, 1, 0, N'fin_swe_dictionary', N'Finnish/Swedish case sensitive sort order for code page 1252',
  52.     0x800000001d04000000000300)
  53. go
  54.  
  55. -- Sort order 75: LCID = 0x40f, NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH 
  56. insert syscharsets (type, id, csid, status, name, description, binarydefinition)
  57.     values (2001, 75, 1, 0, N' isl_dictionary', N'Icelandic case sensitive sort order for code page 1252',
  58.     0x800000000f04000000000300)
  59. go
  60.  
  61. --***********************************************************************
  62. -- INSTCAT.SQL
  63. --***********************************************************************
  64.  
  65. set quoted_identifier on
  66. go
  67.  
  68. update spt_server_info set attribute_value = @@version where attribute_id = 2
  69. go
  70.  
  71. update spt_server_info set attribute_value = '7.00.624' where attribute_id = 500
  72. go
  73.  
  74. print 'creating sp_datatype_info'
  75. go
  76.  
  77. if object_id(N'sp_datatype_info') is not null
  78.     drop proc sp_datatype_info
  79. go
  80.  
  81. /*    Procedure for 7.0 server */
  82. create proc sp_datatype_info
  83.     (@data_type int = 0, @ODBCVer tinyint = 2)
  84. as
  85.     declare @mintype int
  86.     declare @maxtype int
  87.  
  88.     if @ODBCVer <> 3
  89.         select @ODBCVer = 2
  90.     if @data_type = 0
  91.     begin
  92.         select @mintype = -32768
  93.         select @maxtype = 32767
  94.     end
  95.     else
  96.     begin
  97.         select @mintype = @data_type
  98.         select @maxtype = @data_type
  99.     end
  100.  
  101.     select
  102.         convert(sysname,case
  103.             when t.xusertype > 255 then t.name
  104.             else d.TYPE_NAME
  105.         end) TYPE_NAME,
  106.         d.DATA_TYPE,
  107.         convert(int,case
  108.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  109.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  110.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  111.             else OdbcPrec(t.xtype, t.length, t.xprec)
  112.         end) "PRECISION",
  113.         d.LITERAL_PREFIX,
  114.         d.LITERAL_SUFFIX,
  115.         e.CREATE_PARAMS,
  116.         convert(smallint,case
  117.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  118.             else TypeProperty (t.name, 'AllowsNull')
  119.         end) NULLABLE,
  120.         d.CASE_SENSITIVE,
  121.         d.SEARCHABLE,
  122.         d.UNSIGNED_ATTRIBUTE,
  123.         d.MONEY,
  124.         d.AUTO_INCREMENT,
  125.         convert(sysname,case
  126.             when t.xusertype > 255 then t.name
  127.             else d.LOCAL_TYPE_NAME
  128.         end) LOCAL_TYPE_NAME,
  129.         convert(smallint,case
  130.             when type_name(d.ss_dtype) IN ('numeric','decimal') and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  131.             else d.numeric_scale
  132.         end) MINIMUM_SCALE,
  133.         convert(smallint,case
  134.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  135.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  136.             else TypeProperty (t.name, 'Scale')
  137.         end) MAXIMUM_SCALE,
  138.         d.SQL_DATA_TYPE,
  139.         d.SQL_DATETIME_SUB,
  140.         NUM_PREC_RADIX = convert(int,d.RADIX),
  141.         INTERVAL_PRECISION = convert(smallint,NULL),
  142.         USERTYPE = t.usertype
  143.     from master.dbo.spt_datatype_info d
  144.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  145.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  146.             t.xusertype = e.user_type
  147.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  148.     where
  149.         d.DATA_TYPE between @mintype and @maxtype
  150.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  151.         and (t.xusertype <= 255 or
  152.             isnull(d.AUTO_INCREMENT,0) = 0)
  153.     order by 2, 12, 11,
  154.     case
  155.         when t.usertype=18 then 255
  156.         else t.usertype
  157.     end
  158.  
  159. go
  160.  
  161. grant execute on sp_datatype_info to public
  162. go
  163.  
  164. dump tran master with no_log
  165. go
  166.  
  167. set quoted_identifier off
  168. go
  169.  
  170. --***********************************************************************
  171. -- MESSAGES.SQL
  172. --***********************************************************************
  173.  
  174. DELETE sysmessages WHERE error BETWEEN 21142 AND 21144
  175.     or error = 21161
  176. go
  177.  
  178. insert into master..sysmessages (error ,severity ,dlevel ,description ,msglangid)
  179.     values
  180.     (21142,16,0,'The SQL Server ''%s'' could not get Windows NT group membership information for login ''%s''. Verify that the Windows NT account that the SQL Server is running in has access to the domain of the login.' ,1033)
  181. go
  182.  
  183. insert into master..sysmessages (error ,severity ,dlevel ,description ,msglangid)
  184.     values
  185.         (21143,16,0,'The custom stored procedure schema option is invalid for a Snapshot publication article.' ,1033)
  186. go
  187.  
  188. insert into master..sysmessages (error ,severity ,dlevel ,description ,msglangid)
  189.     values
  190.         (21144,10,0,'Duplicate rows found in %s. Not creating unique index.' ,1033)
  191. GO
  192.  
  193. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  194.     values
  195.         (21161,16,0,'Could not change the publisher because the subscription has been dropped. Use sp_subscription_cleanup to cleanup the triggers.',1033) 
  196. go
  197.  
  198. --***********************************************************************
  199. -- SQLDMO.SQL
  200. --***********************************************************************
  201. exec sp_MS_upd_sysobj_category 2   -- Turn "system" bit off for SQLDMO
  202. go
  203.  
  204. if exists (select * from master..sysobjects where (OBJECTPROPERTY(id, N'IsProcedure') = 1 or OBJECTPROPERTY(id, N'IsExtendedProc') = 1) and name = N'sp_MStablekeys')
  205.     drop procedure sp_MStablekeys
  206. go
  207.  
  208. /*******************************************************************************/
  209. print N''
  210. print N'Creating sp_MStablekeys'
  211. print N''
  212. go
  213.  
  214. create procedure sp_MStablekeys
  215. @tablename nvarchar(776) = null, @colname nvarchar(258) = null, @type int = null, @keyname nvarchar(517) = null, @flags int = null
  216. as
  217.     /* This proc returns the table's DRI keys.  @type is the type(s) of key(s) to return. */
  218.     /* Make sure @type is only the key types (DRI_PRIMARYKEY, DRI_UNIQUE, DRI_REFERENCE). */
  219.     if (@type is null)
  220.         select @type = 0x000e
  221.     else
  222.         select @type = @type & 0x000e
  223.  
  224.     /* Flags usage:  For daVinci, to pass call thru to sp_MStablerefs. */
  225.     if (@flags is null)
  226.         select @flags = 0
  227.  
  228.     set nocount on
  229.     create table #spkeys
  230.     (
  231.         cType                tinyint            NOT NULL,    /* key Type */
  232.         cName                nvarchar(258)        NOT NULL,    /* key Name */
  233.         cFlags                int                NULL,        /* e.g., 1 = clustered for PK/Unique */
  234.         cColCount            int                NULL,        /* number of columns (or column pairs) in the key */
  235.         cFillFactor            tinyint            NULL,        /* Fill factor of index creation */
  236.         cRefTable            nvarchar(520)        NULL,        /* owner-qual Referenced table name for FKs */
  237.         cRefKey                nvarchar(260)        NULL,        /* name of referenced key in referenced table */
  238.             -- Note:  cConstID replaces the column list used in 6.0, for speed.
  239.             -- The output set MUST replace this with either index_col(@tablename, cIndexID, 1-16) and NULL * 16
  240.             -- (for PK/UQ) UNION col_name(r.fkeyid, r.fkey1-16) and col_name(r.rkeyid, r.rkey1-16), for SQLDMO,
  241.             -- and these MUST BE nvarchar(132) for alignment in the SQLDMO cache structure!
  242.         cConstID            int                NULL,        /* Reference constraint ID, if Foreign Key  */
  243.         cIndexID            int                NULL,        /* ID of this key's index, if PK/UQ */
  244.         cGroupName        sysname        NULL,        /* FileGroup name of this key, if PK/UQ */
  245.         cDisabled        int                NULL,        /* 0 if enabled, 1 if disabled */
  246.         cPrimaryFG        int                NULL,        /* 1 if primary FG, 0 otherwise */
  247.     )
  248.  
  249.     declare @cType int, @cName nvarchar(258), @cFlags int, @cRefTable nvarchar(520), @fillfactor tinyint
  250.     declare @objid int, @constid int, @indid int, @keycnt int, @q1 nvarchar(2000), @q2 nvarchar(2000), @objtype int, @groupname sysname
  251.     declare @haskeytypes int, @wantkeytypes int
  252.    declare @cDisabled int, @PrimaryFG int
  253.  
  254.     /* First see if @keyname was defined, and override @tablename and @type if so. */
  255.     if (@keyname is not null)
  256.     begin
  257.          select @objid = id, @type = power(2, status & 0x0f) from sysconstraints where constid = object_id(@keyname)
  258.          if (@objid is null)    begin
  259.             RAISERROR (15001, -1, -1, @keyname)
  260.             return 1
  261.          end
  262.          /* Now get the tablename for the index_col below */
  263.          select @tablename = N'[' + REPLACE(user_name(uid), N']', N']]') + N']' + N'.' + N'[' + REPLACE(name, N']', N']]') + N']' from sysobjects where id = @objid
  264.     end else begin
  265.         /* Want all keys for this table (of @type type). */
  266.         select @objid = id, @objtype = (case when OBJECTPROPERTY(id, N'IsTable') = 1 then 1 else 0 end), @haskeytypes = category & 0x0604
  267.             from sysobjects where id = object_id(@tablename)
  268.         if (@objid is null)    begin
  269.             RAISERROR (15001, -1, -1, @tablename)
  270.             return 1
  271.         end
  272.         if (@objtype <> 1)    begin
  273.             RAISERROR (15218, -1, -1, @tablename)
  274.             return 1
  275.         end
  276.         if @colname is not null and not exists (select * from syscolumns where id = @objid and name = @colname) begin
  277.             RAISERROR (15253, -1, -1, @colname, @tablename)
  278.             return 1
  279.         end
  280.  
  281.         /* Skip cursor opening if we don't have any keys (of the type wanted); return a set anyway, for the cache. */
  282.         if (@haskeytypes = 0)
  283.             goto ReturnSet
  284.  
  285.         /* Map from the input bitmask to the category bitmask */
  286.         select @wantkeytypes = 0
  287.         if ((@type & power(2, 1)) <> 0)
  288.             select @wantkeytypes = @wantkeytypes | 0x200
  289.         if ((@type & power(2, 2)) <> 0)
  290.             select @wantkeytypes = @wantkeytypes | 0x400
  291.         if ((@type & power(2, 3)) <> 0)
  292.             select @wantkeytypes = @wantkeytypes | 0x4
  293.         if ((@haskeytypes & @wantkeytypes) = 0)
  294.             goto ReturnSet
  295.     end
  296.  
  297.     /* Preprocessor won't replace within quotes so have to use str(). */
  298.     declare @sysgenname nvarchar(12), @pkstr nvarchar(12), @uqstr nvarchar(12), @fkstr nvarchar(12), @objtypebits nvarchar(12)
  299.     select @sysgenname = ltrim(str(convert(int, 0x00020000)))
  300.     select @pkstr = ltrim(str(convert(int, 1)))
  301.     select @uqstr = ltrim(str(convert(int, 2)))
  302.     select @fkstr = ltrim(str(convert(int, 3)))
  303.     select @objtypebits = ltrim(str(convert(int, 0x0f)))
  304.  
  305.     /* Other ints we need strings for */
  306.     declare @objidstr nvarchar(12), @typestr nvarchar(12)
  307.     select @objidstr = ltrim(str(@objid))
  308.     select @typestr = ltrim(str(@type))
  309.  
  310.     /* Qualifying key name. */
  311.     declare @qualkeyname nvarchar(100)
  312.     select @qualkeyname = null
  313.     if (@keyname is not null) begin
  314.       select @qualkeyname = N' and constid = object_id(''' + @keyname + N''')'
  315.    end
  316.  
  317.     /*********************/
  318.     /* Main cursor loop. */
  319.     /*********************/
  320.     exec(N'declare hC insensitive cursor for select constid, status & ' + @objtypebits + N', status & ' + @sysgenname +
  321.             N' from sysconstraints where id = ' + @objidstr + N' and (' + @typestr + N' & power(2, status & 0x0f) != 0) ' + @qualkeyname)
  322.     open hC
  323.     fetch hC into @constid, @cType, @cFlags
  324.     while (@@fetch_status >= 0) begin
  325.         if (object_name(@constid) is null) begin
  326.             raiserror 55555 N'Assert failed:  object_name(@constid) is null in sp_MStablekeys (pk/uq)'
  327.             return 1
  328.         end
  329.  
  330.         /* DRI_PRIMARYKEY, DRI_UNIQUE */
  331.         if (@cType in (1, 2)) begin
  332.             /* Get the index id enforcing this constraint. */
  333.             select @indid = i.indid, @cName = o.name, @fillfactor = i.OrigFillFactor,
  334.                     @cFlags = @cFlags | (case indid when 1 then 0x00000001 else 0 end),        /* test for clustered index */
  335.                /* clustered index keys are part of non-clustered index key list, which cause incorrect sysindexes.keycnt */
  336.                     @keycnt = case indid when 1 then keycnt else (select count(x.id) from sysindexkeys x where i.indid = x.indid and x.id = @objid) end,
  337.                @groupname = f.groupname,
  338.                @PrimaryFG = FILEGROUPPROPERTY( f.groupname, N'IsPrimaryFG' )
  339.                 from sysindexes i, sysobjects o, sysfilegroups f
  340.             /* Use '=' instead of 'LIKE' in comparision, so we can handle wide card character correctly */
  341.                 where o.id = @constid and i.name = o.name and i.status & 0x1800 <> 0 and i.groupid = f.groupid
  342.             if (@indid is null) begin
  343.                 raiserror 77777 N'Assert failed:  @indid is null in sp_MStablekeys (pk/uq)'
  344.                 return 1
  345.             end
  346.  
  347.             /* Load our temp table. */
  348.             insert #spkeys values (@cType, @cName, @cFlags, @keycnt, @fillfactor, null, null, null, @indid, @groupname, 0, @PrimaryFG)
  349.         end
  350.  
  351.         /* DRI_REFERENCE */
  352.         else if (@cType in (3)) begin
  353.             /* Get the key column information from sysreferences. */
  354.          select @keycnt = r.keycnt, @cName = object_name(r.constid), @cRefTable = N'[' + user_name(o.uid) + N']' + N'.' + N'[' + o.name + N']',
  355.                @cDisabled = OBJECTPROPERTY( r.constid, N'CnstIsDisabled' )
  356.             from sysreferences r, sysobjects o where r.constid = @constid and o.id = r.rkeyid
  357.  
  358.             /* Follow r.rkeyindid back to sysindexes to get the ref key name. */
  359.             declare @cRefKey nvarchar(132)
  360.             select @cRefKey = i.name, @cFlags = c.status from sysreferences r, sysindexes i, sysconstraints c
  361.                 where c.constid = r.constid and r.constid = @constid
  362.                 and i.id = r.rkeyid and i.indid = r.rkeyindid and i.status & 0x1800 <> 0
  363.  
  364.             /* Load our temp table. */
  365.             insert #spkeys values (@cType, @cName, @cFlags, @keycnt, null, @cRefTable, @cRefKey, @constid, null, null, @cDisabled, 0)
  366.         end        /* Key type */
  367.  
  368.         /* Get the next row. */
  369.         fetch hC into @constid, @cType, @cFlags
  370.     end            /* PRIMARY/UNIQUE */
  371.     deallocate hC
  372.  
  373.     /* Now output the data */
  374. ReturnSet:
  375.     set nocount off
  376.     select cType, cName, cFlags, cColCount, cFillFactor, cRefTable, cRefKey,
  377.             cKeyCol1 = convert(nvarchar(132), index_col(@tablename, cIndexID, 1)),
  378.             cKeyCol2 = convert(nvarchar(132), index_col(@tablename, cIndexID, 2)),    
  379.             cKeyCol3 = convert(nvarchar(132), index_col(@tablename, cIndexID, 3)),
  380.             cKeyCol4 = convert(nvarchar(132), index_col(@tablename, cIndexID, 4)),
  381.             cKeyCol5 = convert(nvarchar(132), index_col(@tablename, cIndexID, 5)),
  382.             cKeyCol6 = convert(nvarchar(132), index_col(@tablename, cIndexID, 6)),    
  383.             cKeyCol7 = convert(nvarchar(132), index_col(@tablename, cIndexID, 7)),
  384.             cKeyCol8 = convert(nvarchar(132), index_col(@tablename, cIndexID, 8)),
  385.             cKeyCol9 = convert(nvarchar(132), index_col(@tablename, cIndexID, 9)),
  386.             cKeyCol10 = convert(nvarchar(132), index_col(@tablename, cIndexID, 10)),
  387.             cKeyCol11 = convert(nvarchar(132), index_col(@tablename, cIndexID, 11)),
  388.             cKeyCol12 = convert(nvarchar(132), index_col(@tablename, cIndexID, 12)),
  389.             cKeyCol13 = convert(nvarchar(132), index_col(@tablename, cIndexID, 13)),
  390.             cKeyCol14 = convert(nvarchar(132), index_col(@tablename, cIndexID, 14)),    
  391.             cKeyCol15 = convert(nvarchar(132), index_col(@tablename, cIndexID, 15)),
  392.             cKeyCol16 = convert(nvarchar(132), index_col(@tablename, cIndexID, 16)),
  393.             cRefCol1 = convert(nvarchar(132), null),
  394.             cRefCol2 = convert(nvarchar(132), null),
  395.             cRefCol3 = convert(nvarchar(132), null),
  396.             cRefCol4 = convert(nvarchar(132), null),
  397.             cRefCol5 = convert(nvarchar(132), null),
  398.             cRefCol6 = convert(nvarchar(132), null),
  399.             cRefCol7 = convert(nvarchar(132), null),
  400.             cRefCol8 = convert(nvarchar(132), null),
  401.             cRefCol9 = convert(nvarchar(132), null),
  402.             cRefCol10 = convert(nvarchar(132), null),
  403.             cRefCol11 = convert(nvarchar(132), null),
  404.             cRefCol12 = convert(nvarchar(132), null),
  405.             cRefCol13 = convert(nvarchar(132), null),
  406.             cRefCol14 = convert(nvarchar(132), null),
  407.             cRefCol15 = convert(nvarchar(132), null),
  408.             cRefCol16 = convert(nvarchar(132), null),
  409.             cIndexID,
  410.             cGroupName,
  411.          cDisabled,
  412.           cPrimaryFG
  413.         from #spkeys where cType in (1, 2)
  414.             and (@colname is null or
  415.                 index_col(@tablename, cIndexID, 1) = @colname or
  416.                 index_col(@tablename, cIndexID, 2) = @colname or
  417.                 index_col(@tablename, cIndexID, 3) = @colname or
  418.                 index_col(@tablename, cIndexID, 4) = @colname or
  419.                 index_col(@tablename, cIndexID, 5) = @colname or
  420.                 index_col(@tablename, cIndexID, 6) = @colname or
  421.                 index_col(@tablename, cIndexID, 7) = @colname or
  422.                 index_col(@tablename, cIndexID, 8) = @colname or
  423.                 index_col(@tablename, cIndexID, 9) = @colname or
  424.                 index_col(@tablename, cIndexID, 10) = @colname or
  425.                 index_col(@tablename, cIndexID, 11) = @colname or
  426.                 index_col(@tablename, cIndexID, 12) = @colname or
  427.                 index_col(@tablename, cIndexID, 13) = @colname or
  428.                 index_col(@tablename, cIndexID, 14) = @colname or
  429.                 index_col(@tablename, cIndexID, 15) = @colname or
  430.                 index_col(@tablename, cIndexID, 16) = @colname
  431.             )
  432.         UNION
  433.         select c.cType, c.cName, c.cFlags, c.cColCount, c.cFillFactor, c.cRefTable, c.cRefKey,
  434.             cKeyCol1 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey1)),
  435.             cKeyCol2 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey2)),
  436.             cKeyCol3 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey3)),
  437.             cKeyCol4 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey4)),
  438.             cKeyCol5 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey5)),
  439.             cKeyCol6 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey6)),
  440.             cKeyCol7 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey7)),
  441.             cKeyCol8 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey8)),
  442.             cKeyCol9 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey9)),
  443.             cKeyCol10 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey10)),
  444.             cKeyCol11 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey11)),
  445.             cKeyCol12 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey12)),
  446.             cKeyCol13 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey13)),
  447.             cKeyCol14 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey14)),
  448.             cKeyCol15 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey15)),
  449.             cKeyCol16 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey16)),
  450.             cRefCol1 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey1)),
  451.             cRefCol2 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey2)),    
  452.             cRefCol3 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey3)),
  453.             cRefCol4 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey4)),
  454.             cRefCol5 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey5)),
  455.             cRefCol6 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey6)),
  456.             cRefCol7 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey7)),
  457.             cRefCol8 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey8)),
  458.             cRefCol9 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey9)),
  459.             cRefCol10 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey10)),
  460.             cRefCol11 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey11)),
  461.             cRefCol12 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey12)),
  462.             cRefCol13 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey13)),
  463.             cRefCol14 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey14)),
  464.             cRefCol15 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey15)),
  465.             cRefCol16 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey16)),
  466.             cIndexID,
  467.             cGroupName,
  468.          cDisabled,
  469.           cPrimaryFG
  470.         from #spkeys c, sysreferences r where c.cType = 3 and r.constid = c.cConstID
  471.             and (@colname is null or
  472.                 col_name(r.fkeyid, r.fkey1) = @colname or
  473.                 col_name(r.fkeyid, r.fkey2) = @colname or
  474.                 col_name(r.fkeyid, r.fkey3) = @colname or
  475.                 col_name(r.fkeyid, r.fkey4) = @colname or
  476.                 col_name(r.fkeyid, r.fkey5) = @colname or
  477.                 col_name(r.fkeyid, r.fkey6) = @colname or
  478.                 col_name(r.fkeyid, r.fkey7) = @colname or
  479.                 col_name(r.fkeyid, r.fkey8) = @colname or
  480.                 col_name(r.fkeyid, r.fkey9) = @colname or
  481.                 col_name(r.fkeyid, r.fkey10) = @colname or
  482.                 col_name(r.fkeyid, r.fkey11) = @colname or
  483.                 col_name(r.fkeyid, r.fkey12) = @colname or
  484.                 col_name(r.fkeyid, r.fkey13) = @colname or
  485.                 col_name(r.fkeyid, r.fkey14) = @colname or
  486.                 col_name(r.fkeyid, r.fkey15) = @colname or
  487.                 col_name(r.fkeyid, r.fkey16) = @colname
  488.             )
  489.         order by cType, cName
  490.  
  491.     if (@flags & 1 <> 0)
  492.         exec sp_MStablerefs @tablename, N'actualkeycols', N'foreign'
  493.  
  494. go
  495. /* End sp_MStablekeys */
  496.  
  497. exec sp_MS_marksystemobject sp_MStablekeys
  498. go
  499. grant execute on sp_MStablekeys to public
  500. go
  501.  
  502. if exists (select * from master..sysobjects where (OBJECTPROPERTY(id, N'IsProcedure') = 1 or OBJECTPROPERTY(id, N'IsExtendedProc') = 1) and name = N'sp_MSloginmappings')
  503.     drop procedure sp_MSloginmappings
  504. go
  505.  
  506. /*******************************************************************************/
  507.  
  508. print N''
  509. print N'Creating sp_MSloginmappings'
  510. print N''
  511. go
  512.  
  513.  
  514.  
  515. create proc sp_MSloginmappings
  516.     @loginname nvarchar(258) = null, @flags int = 0
  517. as
  518.     /*
  519.      * @flags bits:
  520.      *        0x01    - current db only
  521.      */
  522.     /*
  523.      * Added @dbname so dbo can see everyone in current database.
  524.      * Use hacky 4.21 syntax so it will run there, instead of a case..when.
  525.      */
  526.     declare @checkmultilogin int
  527.     select @checkmultilogin = 1
  528.     if ((@flags & 0x01 <> 0) and user_id() = 1)
  529.         select @checkmultilogin = 0
  530.  
  531.     declare @logincount int
  532.     select @logincount = 0
  533.     if (@loginname is not null)
  534.         select @logincount = count(*) from syslogins where loginname = @loginname
  535.  
  536.     /* Gotta be sa or dbo to see other than just current login. */
  537.     declare @numlogins int, @whereloginname nvarchar(258), @name nvarchar(258), @retval int
  538.     if (@loginname is null)
  539.         select @numlogins = 2
  540.     else
  541.         select @numlogins = count(*) from syslogins where loginname = @loginname
  542.  
  543.     if (@numlogins = 0) begin
  544.         RAISERROR (15007, -1, -1, @loginname)        /* Login not found */
  545.         return 1
  546.     end
  547.     if (@checkmultilogin <> 0) begin
  548.       /* We do not want to allow everybody to execute this SP */
  549.         if (is_member(N'db_ddladmin') <> 1 and is_member(N'db_owner') <> 1 and is_member(N'db_accessadmin') <> 1 and is_member(N'db_securityadmin') <> 1 and (@numlogins > 1 or suser_sid() <> suser_sid(@loginname))) begin
  550.             RAISERROR (14301, -1, -1, N'')                /* Only sa can see other than the current login */
  551.             return 1
  552.         end
  553.     end
  554.     if (@loginname is not null)
  555.         select @whereloginname = N' and loginname = ''' + @loginname + N''''
  556.    else
  557.       select @whereloginname = N' '
  558.  
  559.     /*
  560.      * This proc returns a result set with one or more rows for each database for which a login is a user or aliased to one.
  561.      * If loginname is specified, the results are limited to that login.  First load a temp table with all logins that are
  562.      * in a db, then add those which aren't mapped to any db.
  563.      */
  564.     create table #loginmappings(
  565.         LoginName            nvarchar(128)        NULL,
  566.         DBName                nvarchar(128)        NULL,
  567.         UserName            nvarchar(128)        NULL,
  568.         AliasName            nvarchar(128)        NULL
  569.     )
  570.     if (@flags & 0x01 <> 0) begin
  571.         INSERT #loginmappings select l.loginname, db_name(), u.name, null from master..syslogins l, sysusers u where l.sid = u.sid and l.loginname is not NULL
  572.         /*
  573.          * We only allow multi-db on a 6.x server because dynamic exec() didn't exist before then,
  574.          * hence there is no way to loop thru every database.  This is caught in SQLDMO so no
  575.          * need for error message here; we'll just return no result sets.
  576.          */
  577.     end else begin
  578.         exec @retval = sp_MSforeachdb
  579.             N'use [?] INSERT #loginmappings select l.loginname, db_name(), u.name, null from master..syslogins l, sysusers u where l.sid = u.sid and l.loginname is not NULL'
  580.         if (@retval <> 0)
  581.             return 1
  582.         insert #loginmappings select l.loginname, null, null, null from master..syslogins l where l.loginname not in (select LoginName from #loginmappings) and l.loginname is not NULL
  583.     end
  584.  
  585.     /*
  586.      * Now bring them out by loginname, each in its own result set.
  587.      * If this is for all logins, we'll return all logins; if for curdb,
  588.      * only those in #loginmappings (i.e. only those mapped in curdb).
  589.      */
  590.     exec(N'declare hCForEachLogin cursor for select loginname from master..syslogins where loginname is not NULL ' + @whereloginname + N' order by loginname')
  591.     if (@@error = 0)
  592.         open hCForEachLogin
  593.     if (@@error <> 0)
  594.         return @@error
  595.     fetch hCForEachLogin into @name
  596.     while (@@fetch_status >= 0) begin
  597.       /* Use '=' instead of 'LIKE' in comparision, so we can handle wide card character correctly */
  598.         if ((@flags & 0x01 = 0) or exists (select * from #loginmappings where LoginName = @name))
  599.             select * from #loginmappings where LoginName = @name
  600.         fetch hCForEachLogin into @name
  601.     end /* FETCH_SUCCESS */
  602.     close hCForEachLogin
  603.     deallocate hCForEachLogin
  604.     return @@error
  605. go
  606. /* End sp_MSloginmappings */
  607.  
  608. exec sp_MS_marksystemobject sp_MSloginmappings
  609. go
  610.  
  611. grant execute on sp_MSloginmappings to public
  612. go
  613.  
  614. if exists (select * from master..sysobjects where (OBJECTPROPERTY(id, N'IsProcedure') = 1 or OBJECTPROPERTY(id, N'IsExtendedProc') = 1) and name = N'sp_MSscriptdb_worker')
  615.     drop procedure sp_MSscriptdb_worker
  616. go
  617.  
  618. /*******************************************************************************/
  619. print N''
  620. print N'Creating sp_MSscriptdb_worker'
  621. print N''
  622. go
  623. create procedure sp_MSscriptdb_worker
  624. as
  625.     set nocount on
  626.  
  627.    declare @PageSize int;
  628.    select @PageSize = (low/1024) from master..spt_values where number = 1 and type = N'E'
  629.  
  630.     create table #tempFG
  631.     (
  632.      cDefault     int,                                  /* 1 for default FG, 0 for user defined */
  633.      cDBFile      int,                                  /* 1 for DB file, 0 for Log file */
  634.       cSize        int,                                  /* in 8K page */
  635.       cMaxSize     int,
  636.       cGrowth      int,
  637.      cGrowthType  int,                                  /* 1 for GrowthInMB, 0 for GrowthInPercent */
  638.      cFGName      nvarchar(132) NOT NULL,      /* FG name */
  639.       cName        nchar(132) NOT NULL,         /* Logical */
  640.       cFileName    nchar(264) NOT NULL,        /* Physical */
  641.     )
  642.  
  643.    /* Default FileGroup first, which should cover all the log files */
  644.    /* This one to pick up all the db files in Primary file group, while group id = 1 */
  645.    insert #tempFG select 1, 1, (o.size * @PageSize)/1024, (case when (o.maxsize < 1) then o.maxsize else (o.maxsize * @PageSize)/1024 end), o.growth, (case when (o.status & 0x100000 = 0) then 1 else 0 end), g.groupname, o.name, o.filename from sysfiles o, sysfilegroups g where g.groupid = 1 and g.groupid = o.groupid and (o.status & 0x40) = 0
  646.    /* This one to pick up all the log files in Primary file group, while group id = 0, note that group id 0 does not exist in sysfilegroups */
  647.    insert #tempFG select 1, 0, (o.size * @PageSize)/1024, (case when (o.maxsize < 1) then o.maxsize else (o.maxsize * @PageSize)/1024 end), o.growth, (case when (o.status & 0x100000 = 0) then 1 else 0 end), N'PRIMARY', o.name, o.filename from sysfiles o where o.groupid = 0 and (o.status & 0x40) <> 0
  648.    /* Other FileGroups, we should have DBFiles, no log files */
  649.  
  650.     create table #tempID
  651.    (
  652.         cGroupID int
  653.     )
  654.    insert #tempID select groupid from sysfilegroups where groupid <> 1
  655.  
  656.    declare @FGid int
  657.     exec(N'declare hC cursor for select cGroupID from #tempID')
  658.     open hC
  659.     fetch hC into @FGid
  660.     while (@@fetch_status >= 0) begin
  661.       insert #tempFG select 0, 1, (o.size * @PageSize)/1024, (case when (o.maxsize < 1) then o.maxsize else (o.maxsize * @PageSize)/1024 end), o.growth, (case when (o.status & 0x100000 = 0) then 1 else 0 end), g.groupname, o.name, o.filename from sysfiles o, sysfilegroups g where g.groupid = @FGid and g.groupid = o.groupid and (o.status & 0x40) = 0
  662.       fetch hC into @FGid
  663.    end
  664.     deallocate hC
  665.  
  666.    select * from #tempFG
  667.    DROP TABLE #tempFG
  668.  
  669. go
  670. /* End sp_MSscriptdb_worker */
  671.  
  672. exec sp_MS_marksystemobject sp_MSscriptdb_worker
  673. go
  674. grant execute on sp_MSscriptdb_worker to public
  675. go
  676.  
  677. exec sp_MS_upd_sysobj_category 1   -- Turn "system" bit back on
  678. go
  679.  
  680. --***********************************************************************
  681. -- XPSTAR.SQL (drop the SPs that were added in SP1 Beta)
  682. --***********************************************************************
  683. if exists (select * from master.dbo.sysobjects where name = 'sp_enum_oledb_providers')
  684.   drop procedure sp_enum_oledb_providers
  685. go
  686. if exists (select * from master.dbo.sysobjects where name = 'sp_prop_oledb_provider')
  687.   drop procedure sp_prop_oledb_provider
  688. go
  689.  
  690. --***********************************************************************
  691. -- END MASTER
  692. --***********************************************************************
  693. dump tran master with no_log
  694. go
  695. checkpoint
  696. go
  697.  
  698. --***********************************************************************
  699. -- START MSDB
  700. --***********************************************************************
  701. --***********************************************************************
  702. -- INSTMSDB.SQL
  703. --***********************************************************************
  704.  
  705. -- Explicitly set the options that the server stores with the object in sysobjects.status
  706. -- so that it doesn't matter if the script is run using a DBLib or ODBC based client.
  707. SET QUOTED_IDENTIFIER OFF -- We don't use quoted identifiers
  708. SET ANSI_NULLS ON         -- We don't want (NULL = NULL) == TRUE
  709. go
  710. SET ANSI_PADDING ON       -- Set so that trailing zeros aren't trimmed off sysjobs.owner_login_sid
  711. go
  712.  
  713. EXECUTE sp_dboption msdb, N'trunc. log on chkpt.', TRUE
  714. go
  715.  
  716. USE msdb
  717. go
  718.  
  719. -- Check that we're in msdb
  720. IF (DB_NAME() <> N'msdb')
  721.   RAISERROR('A problem was encountered accessing msdb. SP1.SQL terminating.', 20, 127) WITH LOG
  722. go
  723.  
  724. DUMP TRANSACTION msdb WITH NO_LOG
  725. go
  726. CHECKPOINT
  727. go
  728.  
  729. PRINT ''
  730. PRINT 'Creating procedure sp_enum_sqlagent_subsystems...'
  731. go
  732. IF (EXISTS (SELECT *
  733.             FROM msdb.dbo.sysobjects
  734.             WHERE (name = N'sp_enum_sqlagent_subsystems')
  735.               AND (type = 'P')))
  736.   DROP PROCEDURE sp_enum_sqlagent_subsystems
  737. go
  738. CREATE PROCEDURE sp_enum_sqlagent_subsystems
  739. AS
  740. BEGIN
  741.   DECLARE @part                  NVARCHAR(300)
  742.   DECLARE @fmt                   NVARCHAR(300)
  743.   DECLARE @subsystem             NVARCHAR(40)
  744.   DECLARE @replication_installed INT
  745.  
  746.   SET NOCOUNT ON
  747.  
  748.   CREATE TABLE #xp_results (subsystem   NVARCHAR(40)  NOT NULL,
  749.                             description NVARCHAR(300) NOT NULL)
  750.   CREATE TABLE #sp_enum_ss_temp (subsystem          NVARCHAR(40) NOT NULL,
  751.                                  description        NVARCHAR(80) NOT NULL,
  752.                                  subsystem_dll      NVARCHAR(80) NULL,
  753.                                  agent_exe          NVARCHAR(80) NULL,
  754.                                  start_entry_point  NVARCHAR(30) NULL,
  755.                                  event_entry_point  NVARCHAR(30) NULL,
  756.                                  stop_entry_point   NVARCHAR(30) NULL,
  757.                                  max_worker_threads INT          NULL)
  758.  
  759.   -- Check if replication is installed
  760.   EXECUTE master.dbo.xp_regread N'HKEY_LOCAL_MACHINE',
  761.                                 N'SOFTWARE\Microsoft\MSSQLServer\Replication',
  762.                                 N'IsInstalled',
  763.                                  @replication_installed OUTPUT,
  764.                                 N'no_output'
  765.   SELECT @replication_installed = ISNULL(@replication_installed, 0)
  766.  
  767.   INSERT INTO #xp_results
  768.   EXECUTE master.dbo.xp_regenumvalues N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent\SubSystems'
  769.  
  770.   IF (@replication_installed = 0)
  771.   BEGIN
  772.     DELETE FROM #xp_results
  773.     WHERE (subsystem IN (N'Distribution', N'LogReader', N'Merge', N'Snapshot'))
  774.   END
  775.  
  776.   DECLARE all_subsystems CURSOR LOCAL
  777.   FOR
  778.   SELECT subsystem, description
  779.   FROM #xp_results
  780.  
  781.   OPEN all_subsystems
  782.   FETCH NEXT FROM all_subsystems INTO @subsystem, @part
  783.   WHILE (@@fetch_status = 0)
  784.   BEGIN
  785.     IF (@subsystem = N'TSQL')
  786.       INSERT INTO #sp_enum_ss_temp VALUES (N'TSQL', FORMATMESSAGE(14556), FORMATMESSAGE(14557), FORMATMESSAGE(14557), FORMATMESSAGE(14557), FORMATMESSAGE(14557), FORMATMESSAGE(14557), CONVERT(INT, @part))
  787.     ELSE
  788.     BEGIN
  789.       SELECT @fmt = N''
  790.       WHILE (CHARINDEX(N',', @part) > 0)
  791.       BEGIN
  792.         SELECT @fmt = @fmt + 'N''' + SUBSTRING(@part, 1, CHARINDEX(N',', @part) - 1) + ''', '
  793.         SELECT @part = RIGHT(@part, (DATALENGTH(@part) / 2) - CHARINDEX(N',', @part))
  794.       END
  795.       SELECT @fmt = @fmt + @part
  796.       IF (DATALENGTH(@fmt) > 0)
  797.         INSERT INTO #sp_enum_ss_temp
  798.         EXECUTE(N'SELECT ''' + @subsystem + N''', N'''', ' + @fmt) 
  799.     END
  800.     FETCH NEXT FROM all_subsystems INTO @subsystem, @part
  801.   END
  802.   DEALLOCATE all_subsystems
  803.  
  804.   UPDATE #sp_enum_ss_temp SET description = FORMATMESSAGE(14550)
  805.   WHERE (subsystem = N'CmdExec')
  806.   UPDATE #sp_enum_ss_temp SET description = FORMATMESSAGE(14551)
  807.   WHERE (subsystem = N'Snapshot')
  808.   UPDATE #sp_enum_ss_temp SET description = FORMATMESSAGE(14552)
  809.   WHERE (subsystem = N'LogReader')
  810.   UPDATE #sp_enum_ss_temp SET description = FORMATMESSAGE(14553)
  811.   WHERE (subsystem = N'Distribution')
  812.   UPDATE #sp_enum_ss_temp SET description = FORMATMESSAGE(14554)
  813.   WHERE (subsystem = N'Merge')
  814.   UPDATE #sp_enum_ss_temp SET description = FORMATMESSAGE(14555)
  815.   WHERE (subsystem = N'ActiveScripting')
  816.  
  817.   -- 'TSQL' is always available (since it's a built-in subsystem), so we explicity add it
  818.   -- to the result set
  819.   IF (NOT EXISTS (SELECT * 
  820.                   FROM #sp_enum_ss_temp 
  821.                   WHERE (subsystem = N'TSQL')))
  822.     INSERT INTO #sp_enum_ss_temp VALUES (N'TSQL', FORMATMESSAGE(14556), FORMATMESSAGE(14557), FORMATMESSAGE(14557), FORMATMESSAGE(14557), FORMATMESSAGE(14557), FORMATMESSAGE(14557), CASE (PLATFORM() & 0x2) WHEN 0x2 THEN 10 ELSE 20 END) -- Worker thread rule should match DEF_REG_MAX_TSQL_WORKER_THREADS
  823.  
  824.   SELECT subsystem,
  825.          description,
  826.          subsystem_dll,
  827.          agent_exe,
  828.          start_entry_point,
  829.          event_entry_point,
  830.          stop_entry_point,
  831.          max_worker_threads
  832.   FROM #sp_enum_ss_temp
  833.   ORDER BY subsystem
  834. END
  835. go
  836.  
  837. --***********************************************************************
  838. -- END MSDB
  839. --***********************************************************************
  840.  
  841. DUMP TRANSACTION msdb WITH NO_LOG
  842. go
  843. CHECKPOINT
  844. go
  845.  
  846. --***********************************************************************
  847. -- EPILOGUE
  848. --***********************************************************************
  849.  
  850. exec sp_configure 'allow updates',0
  851. go
  852.  
  853. reconfigure with override
  854. go
  855.  
  856. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  857. go
  858.  
  859. print ''
  860. print 'sp1.sql completed successfully.'
  861. go
  862.