Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_changedbowner

  No additional text.


Syntax
create procedure sys.sp_changedbowner
    @loginame       sysname,		-- login to become dbo
    @map            varchar(5) = NULL	-- True to map aliases, else drop
as
    -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES 
	set nocount on
	declare @ret        int,
		@exec_stmt nvarchar(4000)

    -- MAKE THE FOLLOWING REMOVE/REMAP/DELETES ATOMIC 
	BEGIN TRANSACTION

	set @exec_stmt = 'alter authorization on database::' + quotename(db_name()) + ' to ' + quotename(@loginame)
	exec (@exec_stmt)	

    if @@ERROR <> 0
    begin
        -- Nested transaction is used by alter authorization statement
        COMMIT TRANSACTION
        return (1)
    end
		
    -- REMOVE OTHER DBO-ALIASES IF REMAPPING NOT REQUESTED 
    if lower(@map) <> 'true'
    begin
		declare @alias sysname
		declare ms_crs_aliases cursor local static for
			select name from sys.database_principal_aliases where alias_principal_id = 1
		open ms_crs_aliases
		fetch ms_crs_aliases into @alias
		while @@fetch_status = 0
		begin
			EXEC %%Alias(Name = @alias).Remove()
			fetch ms_crs_aliases into @alias
		end
		close ms_crs_aliases
		deallocate ms_crs_aliases
        raiserror(15500,-1,-1)
    end

    COMMIT TRANSACTION

    -- CHECKPOINT DATABASE TO FORCE CHANGES TO IN-MEMORY STRUCTURE 
    checkpoint
    return (0) -- sp_changedbowner

 
Last revision 2008RTM
See also

  sp_adddistributiondb (Procedure)
sp_attachsubscription (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash