home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / patches / sql / SQL70-KB815495-v7.00.1094-ENU.exe / hotfix1 / Files / sp4_serv_uni.sql < prev    next >
Encoding:
Text (UTF-16)  |  2003-05-29  |  57.8 KB  |  969 lines

  1. /*------------------------------------------------------------------------------
  2.  
  3. SP4_SERV.SQL
  4.  
  5. THIS SCRIPT TAKES THE SERVER-SIDE SYSTEM-PROCS FROM 7.0 SP3 to SP4.
  6.  
  7. Changes in this file are organized as follows (please maintain):
  8.     System Tables (UPGRADE.SQL)
  9.     System Messages (MESSAGES.SQL / MSGWORK.SQL / SERVMSGS.SQL)
  10.     Engine System Procs (U_TABLES.SQL / PROCSYST.SQL / ODSOLE.SQL)
  11.     Schema Procs (OLEDBSCH.SQL / ANSIVIEW.SQL)
  12.     ODBC/OLEDB Catalog Procs (INSTCAT.SQL)
  13.     SEM SQLDMO System Procs (SQLDMO.SQL)
  14.  
  15. Changes to these scripts should NOT be placed in this file:
  16.     Starfighter Procs (XPSTAR.SQL / INSTMSDB.SQL / SQLTRACE.SQL / WEB.SQL)
  17.     Doc's Samples (INSTPUBS.SQL / INSTNWND.SQL)
  18.     Replication Procs (REPLSYS.SQL / REPLCOM.SQL / REPLTRAN.SQL / REPLMERG.SQL)
  19. These components will maintain separate upgrade scripts.
  20.  
  21. Notes:
  22. + Catalog-updates and sp_MS_upd_sysobj_category are enabled for the entire
  23.     file.  Do not disable or re-enable them.  Please do not change set options.
  24.  
  25. ------------------------------------------------------------------------------*/
  26.  
  27.  
  28. --------------------------------------------------------------------------------
  29. -- VERIFY Server is started in single-user-mode (catalog-updates enables), and
  30. --    start marking of system-objects.
  31. --------------------------------------------------------------------------------
  32. execute sp_configure 'allow updates',1
  33. go
  34.  
  35. reconfigure with override
  36. go
  37.  
  38. exec sp_MS_upd_sysobj_category 1
  39. go
  40.  
  41. --------------------------------------------------------------------------------
  42. --    System Tables (UPGRADE.SQL)
  43. --------------------------------------------------------------------------------
  44. delete sysconfigures where config = 400
  45. go
  46.  
  47. --------------------------------------------------------------------------------
  48. --    System Messages (MESSAGES.SQL / MSGWORK.SQL / SERVMSGS.SQL) English
  49. --------------------------------------------------------------------------------
  50. DELETE sysmessages
  51.     where error in
  52.     (
  53.     1223
  54.     )
  55. go
  56. insert into master..sysmessages (error, severity, dlevel, description, msglangid)
  57.     values
  58.     (1223,13,0,'Process ID %d:%d cannot acquire lock "%hs" on resource %.*ls because a potential deadlock exists on Scheduler %d for the resource. Process ID %d:%d holds a lock "%hs" on this resource', 1033)
  59. go
  60.  
  61. --------------------------------------------------------------------------------
  62. --    System Messages (MESSAGES.SQL / MSGWORK.SQL / SERVMSGS.SQL) Localized
  63. --------------------------------------------------------------------------------
  64.  
  65.  
  66.  
  67. --------------------------------------------------------------------------------
  68. --    Engine System views (Update ANSIVIEW per database)
  69. --------------------------------------------------------------------------------
  70.  
  71. --------------------------------------------------------------------------------
  72. --    Engine System Procs (U_TABLES.SQL / PROCSYST.SQL / ODSOLE.SQL)
  73. --------------------------------------------------------------------------------
  74.  
  75. ------------------------------ sp_changedbowner -------------------------------
  76. if object_id('sp_changedbowner') is not null
  77.     drop proc sp_changedbowner
  78. raiserror(15339,-1,-1,'sp_changedbowner')
  79. go
  80. create procedure sp_changedbowner
  81.     @loginame       sysname,        -- login to become dbo
  82.     @map            varchar(5) = NULL    -- True to map aliases, else drop
  83. as
  84.     -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES --
  85.     set nocount on
  86.     declare @ret        int,
  87.             @newsid     varbinary(85),
  88.             @status     smallint
  89.  
  90.     -- CHECK PERMISSIONS (Note: All sysadmins are dbo) --
  91.     if is_member('db_owner') = 0
  92.     begin
  93.         raiserror(15108,-1,-1)
  94.         return(1)
  95.     end
  96.  
  97.     -- CANT CHANGE OWNER OF MASTER/MODEL/TEMPDB --
  98.     if db_name() in ('master', 'model', 'tempdb')
  99.     begin
  100.         raiserror(15109,-1,-1)
  101.         return(1)
  102.     end
  103.  
  104.     -- CHECK LOGIN NAME IS VALID (NT/SQL USER ONLY!) --
  105.     select @newsid = sid, @status = 2 from master.dbo.syslogins
  106.                     where loginname = @loginame and isntname = 0
  107.     if @newsid is null
  108.         select @status = 14, @newsid = get_sid('\U'+@loginame, NULL)
  109.     if @newsid is null
  110.     begin
  111.         raiserror(15007,-1,-1,@loginame)
  112.         return (1)
  113.     end
  114.  
  115.     -- CHECK IF LOGIN ALREADY ALIASED IN DB --
  116.     if exists (select sid from sysusers where isaliased = 1 and sid = @newsid)
  117.     begin
  118.         raiserror(15111,-1,-1)
  119.         return (1)
  120.     end
  121.  
  122.     -- CHECK IF LOGIN ALREADY KNOWN TO DATABASE --
  123.     if exists (select sid from sysusers where sid = @newsid and uid <> 1)
  124.     begin
  125.         raiserror(15110,-1,-1)
  126.         return (1)
  127.     end
  128.  
  129.     -- MAKE THE FOLLOWING REMOVE/REMAP/DELETES ATOMIC --
  130.     begin transaction
  131.  
  132.     -- REMAP DBO TO NEW SID --
  133.     update sysusers set sid = @newsid, status = @status, updatedate = getdate()
  134.             where name = 'dbo'
  135.  
  136.     -- REMOVE OTHER DBO-ALIASES IF REMAPPING NOT REQUESTED --
  137.     if lower(@map) <> 'true'
  138.     begin
  139.         delete from sysusers where isaliased = 1 and altuid = user_id('dbo')
  140.         raiserror(15500,-1,-1)
  141.     end
  142.     else
  143.         raiserror(15499,-1,-1)     -- nothing to do to <remap>
  144.  
  145.     -- REFLECT NEW OWNER IN SYSDATABASES --
  146.     update master.dbo.sysdatabases set sid = @newsid where dbid = db_id()
  147.     commit transaction
  148.  
  149.     -- CHECKPOINT DATABASE TO FORCE CHANGES TO IN-MEMORY STRUCTURE --
  150.     checkpoint
  151.     raiserror(15501,-1,-1)
  152.     return (0) -- sp_changedbowner
  153. go
  154.  
  155. grant execute on sp_changedbowner to public
  156. go
  157.  
  158. ------------------------------ sp_dboption -------------------------------
  159. if object_id('sp_dboption') is not null
  160.     drop proc sp_dboption
  161. go
  162.  
  163. raiserror(15339,-1,-1,'sp_dboption')
  164. go
  165. /*ANSI_NULLS ON  for creation of sp_dboption*/
  166. set ansi_nulls on
  167. go
  168. create procedure sp_dboption  -- 1996/03/15 12:51
  169. @dbname sysname = NULL,     /* database name to change */
  170. @optname varchar(35) = NULL,  /* option name to turn on/off */
  171. @optvalue varchar(10) = NULL  /* true or false */
  172. as
  173.  
  174. set nocount    on
  175.  
  176. declare @dbid int         /* dbid of the database */
  177. declare @dbsid varbinary(85)           /* id of the owner of the database */
  178. declare @statvalue int     /* number of status option */
  179. declare @statvalue2 int     /* number of status2 option */
  180. declare @catvalue int     /* number of category option */
  181. declare @optcount int      /* number of options like @optname */
  182. declare @allstatopts int    /* bit map off all options stored in sysdatqabases.status 
  183.                             ** that can be set by sp_dboption. */
  184. declare @alloptopts int    /* bit map off all options stored in sysdatqabases.status 
  185.                             ** that can be set by sp_dboption. */
  186. declare @allcatopts int    /* bit map off all options stored in sysdatqabases.category 
  187.                             ** that can be set by sp_dboption. */
  188. declare @exec_stmt nvarchar(550)
  189. declare @devname sysname
  190. declare @cur_dbname sysname
  191. declare @ret_code int
  192. declare @int1 int
  193. ,@opt_autoclose int
  194. ,@opt_trunclogonchkpt int
  195. ,@opt_tornpage int
  196. ,@fulloptname varchar(35)
  197. ,@orig_db_status int
  198. ,@orig_db_status2 int
  199. ,@opt_bit_singleuser integer
  200. ,@opt_catnull integer
  201. ,@opt_ansinulldefault integer
  202. ,@opt_defaulttolocalcursor integer
  203. ,@opt_selectintobulkcopy integer
  204. ,@opt_recursivetriggers integer
  205. ,@opt_offline integer
  206. ,@opt_quotedid integer
  207. ,@opt_cursorcommitclose integer
  208. ,@opt_compnull integer
  209. ,@opt_ansiwarnings integer
  210. ,@opt_autocrtstats integer
  211. ,@opt_autoupdstats integer
  212. ,@returncode    int
  213.  
  214. /*
  215. **  If no @dbname given, just list the possible dboptions.
  216. **  Only certain status bits may be set or cleared by sp_dboption.
  217. */
  218.  
  219. /*
  220. ** Get bitmap of all options that can be set by sp_dboption.
  221. */
  222. select @allstatopts=number from master.dbo.spt_values where type = 'D'
  223.    and name = 'ALL SETTABLE OPTIONS'
  224.  
  225. select @allcatopts=number from master.dbo.spt_values where type = 'DC'
  226.    and name = 'ALL SETTABLE OPTIONS'
  227.  
  228. select @alloptopts=number from master.dbo.spt_values where type = 'D2'
  229.    and name = 'ALL SETTABLE OPTIONS'
  230.  
  231. if @dbname is null
  232. begin
  233.    select 'Settable database options:' = name
  234.       from master.dbo.spt_values
  235.       where (type = 'D' 
  236.             and number & @allstatopts <> 0
  237.             and number not in (0,@allstatopts))  /* Eliminate non-option entries */
  238.          or (type = 'DC'
  239.             and number & @allcatopts <> 0
  240.             and number not in (0,@allcatopts))
  241.          or (type = 'D2'
  242.             and number & @alloptopts <> 0
  243.             and number not in (0,@alloptopts))
  244.       order by name
  245.    return (0)
  246. end
  247.  
  248. /*
  249. **  Verify the database name and get info
  250. */
  251. select @dbid = dbid, @dbsid = sid 
  252.     ,@orig_db_status = status
  253.     ,@orig_db_status2 = status2
  254.    from master.dbo.sysdatabases
  255.       where name = @dbname
  256.  
  257. /*
  258. **  If @dbname not found, say so and list the databases.
  259. */
  260. if @dbid is null
  261.    begin
  262.       raiserror(15010,-1,-1,@dbname)
  263.       print ' '
  264.       select 'Available databases:' = name
  265.          from master.dbo.sysdatabases
  266.       return (1)
  267.    end
  268.  
  269. /*
  270. ** If no option was supplied, display current settings.
  271. */
  272. if @optname is null
  273.    begin
  274.       select 'The following options are set:' = v.name
  275.          from master.dbo.spt_values v, master.dbo.sysdatabases d
  276.             where d.name=@dbname
  277.                and ((number & @allstatopts <> 0
  278.                      and number not in (-1,@allstatopts) 
  279.                      and v.type = 'D'
  280.                      and (v.number & d.status)=v.number)
  281.                  or (number & @allcatopts <> 0
  282.                      and number not in (-1,@allcatopts) 
  283.                      and v.type = 'DC'
  284.                      and d.category & v.number <> 0)
  285.                  or (number & @alloptopts <> 0
  286.                      and number not in (-1,@alloptopts) 
  287.                      and v.type = 'D2'
  288.                      and d.status2 & v.number <> 0))
  289.       return(0)
  290.    end
  291.  
  292. if lower(@optvalue) not in ('true', 'false', 'on', 'off') and @optvalue is not null
  293.    begin
  294.       raiserror(15241,-1,-1)
  295.       return (1)
  296.    end
  297.  
  298. /*
  299. **  Use @optname and try to find the right option.
  300. **  If there isn't just one, print appropriate diagnostics and return.
  301. */
  302. select @optcount = count(*) ,@fulloptname = min(name)
  303.       from master.dbo.spt_values
  304.       where lower(name) like '%' + lower(@optname) + '%'
  305.          and ((type = 'D'
  306.               and number & @allstatopts <> 0
  307.               and number not in (-1,@allstatopts))
  308.           or (type = 'DC'
  309.               and number & @allcatopts <> 0
  310.               and number not in (-1,@allcatopts))
  311.             or (type = 'D2'
  312.               and number & @alloptopts <> 0
  313.               and number not in (-1,@alloptopts)))
  314.  
  315. /*
  316. **  If no option, show the user what the options are.
  317. */
  318. if @optcount = 0
  319.    begin
  320.       raiserror(15011,-1,-1,@optname)
  321.       print ' '
  322.  
  323.       select 'Settable database options:' = name
  324.          from master.dbo.spt_values
  325.          where (type = 'D'
  326.                and number & @allstatopts <> 0
  327.                and number not in (-1,@allstatopts))  /* Eliminate non-option entries */
  328.             or (type = 'DC'
  329.                and number & @allcatopts <> 0
  330.                and number not in (-1,@allcatopts))
  331.             or (type = 'D2'
  332.                and number & @alloptopts <> 0
  333.                and number not in (-1,@alloptopts))
  334.          order by name
  335.  
  336.       return (1)
  337.    end
  338.  
  339.  
  340. /*
  341. **  If more than one option like @optname, show the duplicates and return.
  342. */
  343. if @optcount > 1
  344.    begin
  345.       raiserror(15242,-1,-1,@optname)
  346.       print ' '
  347.  
  348.       select duplicate_options = name
  349.          from master.dbo.spt_values
  350.          where lower(name) like '%' + lower(@optname) + '%'
  351.             and ((type = 'D'
  352.                  and number & @allstatopts <> 0
  353.                  and number not in (-1,@allstatopts))
  354.               or (type = 'DC'
  355.                  and number & @allcatopts <> 0
  356.                  and number not in (-1,@allcatopts))
  357.               or (type = 'D2'
  358.                  and number & @alloptopts <> 0
  359.                  and number not in (-1,@alloptopts))
  360.                 )
  361.       return (1)
  362.    end
  363.  
  364. /* 
  365. ** read the status value to be set or the category value to be set
  366. */
  367. select @statvalue = number
  368.       from master.dbo.spt_values
  369.       where lower(name) = lower(@fulloptname) 
  370.       and type = 'D'
  371.  
  372. select @statvalue2 = number
  373.       from master.dbo.spt_values
  374.       where lower(name) = lower(@fulloptname) 
  375.       and type = 'D2'
  376.  
  377. /* 
  378. ** if a category option is being set, set the status option value to be 0
  379. ** so that we won't be inserting a null value to sysdatabases.status
  380. */
  381. if @statvalue is null
  382.       select @statvalue = 0
  383.  
  384. if @statvalue2 is null
  385.       select @statvalue2 = 0
  386.  
  387. select @catvalue = number
  388.       from master.dbo.spt_values
  389.       where lower(name) = lower(@fulloptname) 
  390.       and type = 'DC'
  391.  
  392.     select @opt_autoclose = number from master.dbo.spt_values 
  393.         where type = 'D' and name = 'autoclose'
  394.  
  395.     select @opt_trunclogonchkpt = number from master.dbo.spt_values 
  396.         where type = 'D' and name = 'trunc. log on chkpt.'
  397.  
  398.     select @opt_tornpage = number from master.dbo.spt_values 
  399.         where type = 'D' and name = 'torn page detection'
  400.  
  401.     select @opt_catnull = number from master.dbo.spt_values 
  402.         where type = 'D2' and name = 'concat null yields null'
  403.  
  404.     select @opt_ansinulldefault = number from master.dbo.spt_values 
  405.         where type = 'D2' and name = 'ANSI null default'
  406.  
  407.     select @opt_defaulttolocalcursor = number from master.dbo.spt_values 
  408.         where type = 'D2' and name = 'default to local cursor'
  409.  
  410.     select @opt_selectintobulkcopy = number from master.dbo.spt_values 
  411.         where type = 'D' and name = 'select into/bulkcopy'
  412.  
  413.     select @opt_recursivetriggers = number from master.dbo.spt_values 
  414.         where type = 'D2' and name = 'recursive triggers'
  415.  
  416.     select @opt_offline = number from master.dbo.spt_values 
  417.         where type = 'D' and name = 'offline'
  418.  
  419.     select @opt_quotedid = number from master.dbo.spt_values 
  420.         where type = 'D2' and name = 'quoted identifier'
  421.  
  422.     select @opt_cursorcommitclose = number from master.dbo.spt_values 
  423.         where type = 'D2' and name = 'cursor close on commit'
  424.  
  425.     select @opt_compnull = number from master.dbo.spt_values 
  426.         where type = 'D2' and name = 'ANSI nulls'
  427.  
  428.     select @opt_ansiwarnings = number from master.dbo.spt_values 
  429.         where type = 'D2' and name = 'ANSI warnings'
  430.  
  431.     select @opt_autocrtstats = number from master.dbo.spt_values 
  432.         where type = 'D2' and name = 'auto create statistics'
  433.  
  434.     select @opt_autoupdstats = number from master.dbo.spt_values 
  435.         where type = 'D2' and name = 'auto update statistics'
  436.  
  437. if @statvalue > 0
  438. begin
  439.  
  440. /*
  441. **  You can not change any of the options in master, except the 'trunc log on
  442. **  checkpoint' option (8).  If the user tries to do so tell them they can't.
  443. */
  444. if (@dbid = db_id('master') and @statvalue <> @opt_trunclogonchkpt and @optvalue is not null)
  445.    begin
  446.       raiserror(15243,-1,-1,@optname)
  447.       return (1)
  448.    end
  449.  
  450. /* Check for allowable options on tempdb */
  451. if (@dbid = db_id('tempdb')
  452.     and @optvalue is not null)
  453.     begin
  454.         raiserror(15324,-1,-1,@optname, @dbname )
  455.         return (1)
  456.     end
  457.  
  458. /*Check for allowable option on Model and MSDB*/
  459. if (@dbid in (db_id('model'), db_id('msdb')) and 
  460.     @statvalue = @opt_offline
  461.     and @optvalue is not null)
  462.     begin
  463.         raiserror(15324,-1,-1,@optname, @dbname )
  464.         return (1)
  465.     end
  466. end
  467.  
  468.  
  469. if @statvalue2 > 0
  470. begin
  471. /* Check for allowable options on tempdb */
  472. if (@dbid = db_id('tempdb') and not @statvalue2 in 
  473.     ( @opt_catnull, @opt_recursivetriggers 
  474.     ,@opt_ansinulldefault,@opt_defaulttolocalcursor
  475.     ,@opt_quotedid, @opt_cursorcommitclose, @opt_compnull
  476.   ,@opt_ansiwarnings, @opt_autocrtstats, @opt_autoupdstats) 
  477.     and @optvalue is not null)
  478.     begin
  479.         raiserror(15324,-1,-1,@optname, @dbname )
  480.         return (1)
  481.     end
  482. end
  483.  
  484. /*
  485. **  Only the SA or the dbo of @dbname can execute the update part
  486. **  of this procedure so check.
  487. */
  488. if (not (is_srvrolemember('sysadmin') = 1))
  489.     and suser_sid() <> @dbsid and @optvalue is not null
  490.     -- ALSO ALLOW db_owner ONLY IF DB REQUESTED IS CURRENT DB
  491.     and (@dbid <> db_id() or is_member('db_owner') <> 1)
  492.    begin
  493.       raiserror(15244,-1,-1)
  494.       return (1)
  495.    end
  496.  
  497. /*
  498. **  If we're in a transaction, disallow this since it might make recovery
  499. **  impossible.
  500. */
  501. set implicit_transactions off
  502. if @@trancount > 0 and @optvalue is not null
  503.    begin
  504.       raiserror(15002,-1,-1,'sp_dboption')
  505.       return (1)
  506.    end
  507.  
  508.  
  509. /*
  510. **  Just want to see current setting of specified option.
  511. */
  512. if @optvalue is null
  513. begin
  514.       select OptionName = v.name
  515.  
  516.             ,CurrentSetting =
  517.                CASE
  518.                   When ( ((v.number & d.status) = v.number
  519.                           and v.type = 'D')
  520.                       or (d.category & v.number <> 0
  521.                            and v.type = 'DC')
  522.                       or (d.status2 & v.number <> 0
  523.                            and v.type = 'D2')
  524.                        )
  525.                      Then 'ON'
  526.                   When NOT
  527.                        ( ((v.number & d.status) = v.number
  528.                           and v.type = 'D')
  529.                       or (d.category & v.number <> 0
  530.                            and v.type = 'DC')
  531.                       or (d.status2 & v.number <> 0
  532.                            and v.type = 'D2')
  533.                        )
  534.                      Then 'off'
  535.                END
  536.  
  537.          from master.dbo.spt_values v, master.dbo.sysdatabases d
  538.             where d.name=@dbname
  539.                and ((v.number & @allstatopts <> 0
  540.                      and v.number not in (-1,@allstatopts)   /* Eliminate non-option entries */
  541.                      and v.type = 'D')
  542.                  or (v.number & @allcatopts <> 0
  543.                      and v.number not in (-1,@allcatopts)   /* Eliminate non-option entries */
  544.                      and v.type = 'DC')
  545.                  or (v.number & @alloptopts <> 0
  546.                      and v.number not in (-1,@alloptopts)   /* Eliminate non-option entries */
  547.                      and v.type = 'D2')
  548.                    )
  549.                 and lower(v.name) = lower(@fulloptname)
  550.  
  551.    return (0)
  552. end
  553.  
  554. ---- Pre-detect 'single user' conflicts.
  555. select @opt_bit_singleuser = number from master.dbo.spt_values
  556.   where type='D  ' and name='single user'
  557. if (@fulloptname           ='single user') --13834
  558.    begin
  559.    select @int1 = count(*) from master.dbo.sysprocesses
  560.       where dbid = @dbid and spid<>@@spid
  561.    if (@int1 > 0 and
  562.     (@orig_db_status & @opt_bit_singleuser > 0 or
  563.      lower(@optvalue) in ('true', 'on'))
  564.       )
  565.       begin
  566.       raiserror(15089,-1,-1,@fulloptname)
  567.       return (1)
  568.       end
  569.    end
  570.  
  571. /*
  572. **  Now update sysdatabases.
  573. */
  574. if lower(@optvalue) in ('true','on')
  575. begin
  576.    /*
  577.    **  If this is the option to make the database offline
  578.    **  we need to do some checking first to make sure no users.
  579.    **  Unless it's the master db, no one can be using it.
  580.    **  If it's the master db, only the SA may be using it.
  581.    */
  582.    if @statvalue in (512)
  583.       and (select count(*) from master.dbo.sysprocesses
  584.          where dbid = @dbid
  585.          and sid <> 0x1) > 0
  586.       begin
  587.          raiserror(15069,-1,-1)
  588.          return (1)
  589.       end
  590.  
  591.    /*
  592.    ** If we're setting the database 'offline' - use 'dbcc dbcontrol'
  593.    ** rather than setting the status bit directly.
  594.    */
  595.    if @statvalue = 512  -- 512 ON means db is Offline.
  596.    begin
  597.  
  598.       if (0 = (@statvalue & (select status from master.dbo.sysdatabases where name=@dbname)))
  599.          begin  -- Currently db is Online.
  600.  
  601.          dbcc dbcontrol(@dbname,offline)
  602.          if @@error>0
  603.             begin
  604.                raiserror(15245,-1,-1)
  605.                return (1)
  606.             end
  607.          else
  608.             raiserror(15327,-1,-1)
  609.          end
  610.       else
  611.          raiserror(15328,-1,-1)
  612.  
  613.       /*
  614.       ** Don't need to continue and set status bit or 'checkpoint'
  615.       ** the database if option was 'offline' since the dbcc
  616.       ** command does.
  617.       */
  618.       return(0)   /* Don't need to continue and 'checkpoint' db for this
  619.             ** option. */
  620.    end
  621.  
  622.    /* 
  623.    ** If we're setting the database  'read only', - use dbcc dbcontrol' 
  624.    ** rather than setting the status bit directly
  625.    */
  626.    if @statvalue = 1024 -- 1024 ON means db is Read only
  627.    begin
  628.       if (0 = (@statvalue & (select status from master.dbo.sysdatabases where name = @dbname)))
  629.          begin  -- currently db is not readonly
  630.          
  631.          dbcc dbcontrol(@dbname, readonly)
  632.          if @@error>0
  633.             begin
  634.                raiserror(15612, -1, -1)
  635.                return (1)
  636.             end
  637.          else
  638.             raiserror(15613, -1, -1)
  639.          end
  640.      else
  641.          raiserror(15614, -1, -1)
  642.     
  643.      /* 
  644.      ** We are done with the readony option
  645.      */    
  646.      return (0)
  647.    
  648.    end 
  649.  
  650.    /* 
  651.    ** If we're setting the database 'single user', - use dbcc dbcontrol' 
  652.    ** rather than setting the status bit directly
  653.    */
  654.    if @statvalue = 4096 -- 4096 ON means db is single user
  655.    begin
  656.       if (0 = (@statvalue & (select status from master.dbo.sysdatabases where name = @dbname)))
  657.          begin  -- currently db is not singleuser
  658.          
  659.          dbcc dbcontrol(@dbname, single)
  660.          if @@error>0
  661.             begin
  662.                raiserror(15615, -1, -1)
  663.                return (1)
  664.             end
  665.          else
  666.             raiserror(15616, -1, -1)
  667.          end
  668.      else
  669.          raiserror(15617, -1, -1)
  670.     
  671.      /* 
  672.      ** We are done with the readony option
  673.      */    
  674.      return (0)
  675.    
  676.    end 
  677.  
  678.    if @catvalue = 1   /* We're publishing the database. */
  679.       begin
  680.          select @exec_stmt = quotename(@dbname, '[')   + '.dbo.sp_replicationdboption'
  681.           /* @dbname = N' +   quotename(@dbname, '''')  + ', @optname = ''publish'' , @value =  ''true'''*/
  682.  
  683.           exec @returncode = @exec_stmt @dbname,'publish','true'
  684.  
  685.         if @returncode = 0
  686.         begin
  687.             raiserror(15435,-1,-1)
  688.             return(0)
  689.         end
  690.         else
  691.             return (1)   
  692.       end
  693.  
  694.    if @catvalue = 2  /* Allowing subscriptions in the database. */
  695.       begin
  696.            update master.dbo.sysdatabases set category = category | 2
  697.             where name = @dbname
  698.          raiserror(15436,-1,-1)
  699.          return (0)
  700.       end
  701.  
  702.     if @catvalue = 4    /* Allowing merge publishing in the database. */
  703.         begin
  704.             select @exec_stmt = quotename(@dbname, '[')   + '.dbo.sp_replicationdboption'
  705.             /*@dbname = N' +   quotename(@dbname, '''')  + ', @optname = ''merge publish'' , @value =  ''true'''*/
  706.             exec @returncode = @exec_stmt @dbname,'merge publish','true'
  707.  
  708.             if @returncode = 0
  709.             begin
  710.                 raiserror(15437,-1,-1)
  711.                 return(0)
  712.             end
  713.             else
  714.                 return (1)
  715.         end
  716.  
  717.    /* All other options get handled identically. */
  718.    update master.dbo.sysdatabases 
  719.         set status = status | @statvalue,
  720.         status2 = status2 | @statvalue2
  721.       where dbid = @dbid
  722. end
  723.  
  724. /*
  725. **  We want to set the requested option off.
  726. */
  727.  
  728. else
  729.  
  730. begin
  731.    if @statvalue = 1024
  732.         and (0 <> (2097152 & (select status from master.dbo.sysdatabases where dbid=@dbid)))
  733.    begin
  734.         /* The db is in standby mode (2097152 = 0x200000 = DBT_STANDBY)
  735.            so disallow turning the read-only bit off */
  736.         raiserror(15030,-1,-1)
  737.         return (1)
  738.    end
  739.  
  740.    if @statvalue = 512     /* We're bringing it online. */
  741.    begin
  742.         if (0 = (@statvalue & (select status from master.dbo.sysdatabases where name=@dbname)))
  743.          -- Currently db is Online.
  744.             raiserror(15438,-1,-1)
  745.         else
  746.         begin
  747.             dbcc dbcontrol(@dbname,online)
  748.             if @@error>0
  749.             begin
  750.                 return (1)
  751.             end
  752.             else
  753.             raiserror(15439,-1,-1)
  754.         end
  755.  
  756.         return(0)
  757.     end
  758.  
  759.    if @statvalue = 1024     /* We're turning off readonly. */
  760.    begin
  761.         if (0 = (@statvalue & (select status from master.dbo.sysdatabases where name=@dbname)))
  762.          -- Currently db is not readonly.
  763.             raiserror(15619,-1,-1)
  764.         else
  765.         begin
  766.             dbcc dbcontrol(@dbname,readwrite)
  767.             if @@error>0
  768.             begin
  769.                 return (1)
  770.             end
  771.             else
  772.             raiserror(15618,-1,-1)
  773.         end
  774.  
  775.         return(0)
  776.     end
  777.  
  778.    if @statvalue = 4096     /* We're turning off singleuser. */
  779.    begin
  780.         if (0 = (@statvalue & (select status from master.dbo.sysdatabases where name=@dbname)))
  781.          -- Currently db is not singleuser.
  782.             raiserror(15621,-1,-1)
  783.         else
  784.         begin
  785.             dbcc dbcontrol(@dbname,multi)
  786.             if @@error>0
  787.             begin
  788.                 return (1)
  789.             end
  790.             else
  791.             raiserror(15620,-1,-1)
  792.         end
  793.  
  794.         return(0)
  795.     end
  796.  
  797.     if @catvalue = 1   /* We're disabling publishing for the database. */
  798.     begin
  799.         select @exec_stmt = quotename(@dbname, '[')   + '.dbo.sp_replicationdboption'
  800.         /* @dbname = N' +   quotename(@dbname, '''')  + ', @optname = ''publish'' , @value =  ''false'''*/
  801.  
  802.         exec @returncode = @exec_stmt @dbname, 'publish', 'true'
  803.  
  804.         if @returncode = 0
  805.         begin
  806.             raiserror(15440,-1,-1)
  807.             return(0)
  808.         end
  809.         else
  810.             return (1)
  811.  
  812.     end
  813.  
  814.     if @catvalue = 2  /* We're disabling subscriptions to database. */
  815.     begin
  816.         update master.dbo.sysdatabases set category = category & ~2
  817.         where name = @dbname
  818.          raiserror(15441,-1,-1)
  819.         return (0)
  820.     end
  821.  
  822.     if @catvalue = 4    /* Disable merge publishing in the database. */
  823.         begin
  824.          select @exec_stmt = quotename(@dbname, '[')   + '.dbo.sp_replicationdboption'
  825.          /*@dbname = N' +   quotename(@dbname, '''')  + ', @optname = ''merge publish'' , @value =  ''false'''*/
  826.          exec @returncode = @exec_stmt @dbname, 'merge publish', 'false'
  827.  
  828.         if @returncode = 0
  829.         begin
  830.             raiserror(15442,-1,-1)
  831.             return(0)
  832.         end
  833.         else
  834.             return (1)
  835.         end
  836.  
  837.         /* All other options get handled identically. */
  838.     update master.dbo.sysdatabases 
  839.         set status = status & ~@statvalue
  840.         ,status2 = status2 & ~@statvalue2
  841.         where dbid = @dbid
  842. end
  843.  
  844. /*
  845. **  CHECKPOINT the database that was changed.
  846. */
  847. raiserror(15443,-1,-1)
  848.  
  849. select @exec_stmt = 'use ' +  quotename(@dbname, '[')   + ' checkpoint'         
  850. exec (@exec_stmt)        
  851. select @int1 = @@error
  852. if (@int1 <> 0)
  853.     begin
  854.     update         master.dbo.sysdatabases
  855.         set     status = @orig_db_status
  856.              ,status2 = @orig_db_status2
  857.         where     dbid   = @dbid
  858.     end
  859. else
  860.     /* 
  861.     **flush the stored procedures in the database involved for recompile
  862.     ** if changing compile time options 
  863.     */
  864.     begin
  865.         dbcc flushprocindb(@dbid)
  866.     end
  867.  
  868.  
  869. return (0) -- sp_dboption
  870. go
  871. /*ANSI_NULLS OFF after creation of sp_dboption*/
  872. set ansi_nulls off
  873. go
  874.  
  875. grant execute on sp_dboption to public
  876. go
  877.  
  878. ---------------------------- sp_MS_marksystemobject -----------------------------
  879.  
  880. if object_id('sp_MS_marksystemobject', 'P') IS NOT NULL
  881.     drop procedure sp_MS_marksystemobject
  882. raiserror('create procedure sp_MS_marksystemobject ...',0,1)
  883. go
  884. -- FOR INTERNAL USE ONLY ... DO NOT DOCUMENT --
  885. -- This procedure sets a bit in sysobjects.  This bit has no meaning, various
  886. --    groups (starfigther, davinci, replication) use it for different things
  887. -- MSQL makes no warranty, express or implied, on what objects will or will
  888. --    not have this bit set.  Use at your own risk.
  889. --   
  890. create procedure sp_MS_marksystemobject
  891.     @objname    nvarchar(517)  -- 517 is max for two part name
  892. as
  893.     -- pre-stuff --
  894.     set nocount on
  895.  
  896.     -- CHECK PERMISSIONS (MUST BE DBO OR SETUP ADMIN) --
  897.     if (not is_member('db_owner')=1)
  898.     begin
  899.         raiserror('sp_MS_marksystemobject: Must be db_owner',0,1)
  900.         return 1
  901.     end
  902.  
  903.     -- CHECK THE OBJECT NAME --
  904.     if object_id(@objname, 'local') is null
  905.     begin
  906.         raiserror('sp_MS_marksystemobject: Invalid object name ''%ls''',0,1,@objname)
  907.         return 1
  908.     end
  909.  
  910.     -- CHECK THE OBJECT OWNER (MUST BE A SYSTEM USER) --
  911.     if user_name(ObjectProperty(object_id(@objname, 'local'), 'ownerid'))
  912.                 not in ('dbo','INFORMATION_SCHEMA')
  913.     begin
  914.         raiserror('sp_MS_marksystemobject: Object must be owned by a system user.',0,1)
  915.         return 1
  916.     end
  917.  
  918.     -- DO THE UPDATE --
  919.     begin tran
  920.     dbcc LockObjectSchema(@objname)
  921.     update sysobjects set status = status | 0xC0000000
  922.                         where id = object_id(@objname, 'local')
  923.     commit tran
  924.     return @@error  -- sp_MS_marksystemobject
  925. go
  926. exec sp_MS_marksystemobject 'sp_MS_marksystemobject'
  927. go
  928. grant execute on sp_MS_marksystemobject to public
  929. go
  930.  
  931.  
  932. delete from spt_values where name = 'Cross DB Ownership Chaining' and number = 400 and type = 'C'
  933. go
  934.  
  935.  
  936. delete from spt_values where name = 'db chaining' and number = 0x400 and type = 'D2'
  937. go
  938.  
  939. --------------------------------------------------------------------------------
  940. --     SP_SETLOGIN USED BY AGENT
  941. --------------------------------------------------------------------------------
  942. if object_id('sp_setuserbylogin','X') IS NOT NULL
  943.     exec sp_dropextendedproc 'sp_setuserbylogin'
  944. execute sp_addextendedproc 'sp_setuserbylogin','(server internal)'
  945. go
  946. grant execute on sp_setuserbylogin to public
  947. go
  948. exec sp_MS_marksystemobject 'sp_setuserbylogin'
  949. go
  950. --------------------------------------------------------------------------------
  951. --    End of SQLDMO System Procedures (SQLDMO.SQL)
  952. --------------------------------------------------------------------------------
  953.  
  954.  
  955. --------------------------------------------------------------------------------
  956. -- END OF FILE: Turn off marking of system objects.
  957. --    DO NOT ADD ANYTHING AFTER THIS POINT
  958. --------------------------------------------------------------------------------
  959. exec sp_MS_upd_sysobj_category 2
  960. go
  961.  
  962. exec sp_configure 'allow updates',0
  963. go
  964.  
  965. reconfigure with override
  966. go
  967.  
  968.  
  969.