Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_check_removable_sysusers

  No additional text.


Syntax

-- NOTE: FOR INTERNAL USE ONLY (sp_certify_removable)
--      DO NOT DOCUMENT OR USE!

create procedure sys.sp_check_removable_sysusers
    @autofix    varchar(4)      -- true or other
as
	declare		@alias sysname
			,@idsch int	-- schema id

	if (not (is_srvrolemember('sysadmin') = 1))  -- Make sure that it is the SA executing this.
	begin
		raiserror(15247,-1,-1)
		return(1)
	end

	BEGIN TRANSACTION

	-- LOCK CURRENT DATABASE
	EXEC %%CurrentDatabase().Lock()

	-- CHECK FOR DATABASE OWNED BY SQL USER 
	if exists (select name from sys.database_principals where name = 'dbo' and type = 'S' and sid <> 0x1) -- 'sa'
	begin
        if @autofix <> 'auto'
        begin
			ROLLBACK TRANSACTION
			raiserror(15258,-1,-1)
			return(1)
        end

        -- MAKE SA THE DBO 
		declare @dbo sysname,
				@sid varbinary(85)

		select @dbo = N'dbo', @sid = 0x1 -- 'sa'
	    -- REMAP DBO TO NEW SID 
	    -- REFLECT CHANGE IN SYSDATABASES 
		EXEC %%UserOrGroup(Name = @dbo).SetSID(SID = @sid,
				IsExternal = 0, IsGroup = 0,
				Action = NULL, UserNamePattern = NULL, LoginName = NULL, Password = NULL) -- may fail

		-- SID MAY BE DUPLICATE 
		if @@ERROR <> 0
		begin
			ROLLBACK TRANSACTION
			raiserror(15110,-1,-1)
			return (1)
		end

		raiserror(15502,-1,-1)
    end

    -- CHECK FOR PERMISSIONS GRANTED TO or BY SQL USERS 
    if exists (select grantee_principal_id from sys.database_permissions where grantee_principal_id in
                (select u.principal_id from sys.database_principals u where u.type = 'S' and u.principal_id > 4))
		OR exists (select grantor_principal_id from sys.database_permissions where grantor_principal_id in
	                (select u.principal_id from sys.database_principals u where u.type = 'S' and u.principal_id > 4))
    begin
		ROLLBACK TRANSACTION
		if @autofix = 'auto'
		    PRINT 'CANNOT AUTO-AUTOFIX GRANT-WITH-GRANT CHAINS'
		raiserror(15053,-1,-1)
		return(1)
    end

    -- CHECK FOR OBJECTS OWNED BY SQL USERS 
    if exists (select o.schema_id from sys.objects o where o.schema_id in
                (select s.schema_id from sys.schemas s where s.principal_id > 4))
    begin
		ROLLBACK TRANSACTION
		raiserror(15053,-1,-1)
        return(1)
    end

    -- CHECK FOR TYPES OWNED BY SQL USERS 
    if exists (select t.schema_id from sys.types t where t.schema_id in
                (select s.schema_id from sys.schemas s where s.principal_id > 4))
    begin
		ROLLBACK TRANSACTION
		raiserror(15053,-1,-1)
		return(1)
    end

    -- CHECK FOR ROLES OWNED BY SQL USERS 
    if exists (select owning_principal_id from sys.database_principals where type = 'R' and
        owning_principal_id in (select principal_id from sys.database_principals u where u.type = 'S' and u.principal_id > 4))
    begin
        if @autofix <> 'auto'
        begin
			ROLLBACK TRANSACTION
			raiserror(15053,-1,-1)
			return(1)
        end

        -- ASSIGN DBO AS OWNER OF ROLES 
	declare @role sysname
	declare ms_crs_roles cursor local static for
		select name from sys.database_principals where type = 'R' and
        owning_principal_id in (select principal_id from sys.database_principals u where u.type = 'S' and u.principal_id > 4)
	open ms_crs_roles
	fetch ms_crs_roles into @role
	while @@fetch_status = 0
	begin
		EXEC %%Role(Name = @role).SetOwner(OwnerID = 1)
		fetch ms_crs_roles into @role
	end
	close ms_crs_roles
	deallocate ms_crs_roles

	raiserror(15503,-1,-1)
    end

    -- CHECK FOR SQL LOGINS AS USERS 
    if exists (select principal_id from sys.database_principals u where u.type = 'S' and u.principal_id > 4)
    begin
        if @autofix <> 'auto'
        begin
			ROLLBACK TRANSACTION
			raiserror(15254,-1,-1)
			return(1)
        end

	-- DELETE SQL USERS 
	declare @user sysname, @uid int
	declare ms_crs_users cursor local static for
		select name, principal_id from sys.database_principals u where u.type = 'S' and u.principal_id > 4
	open ms_crs_users
	fetch ms_crs_users into @user, @uid
	while @@fetch_status = 0
	begin
		EXEC %%Owner(Name = @user).Remove()
		fetch ms_crs_users into @user, @uid
	end
	close ms_crs_users
	deallocate ms_crs_users

	-- DELETE DEPENDENT ALIASES 
	declare ms_crs_aliases cursor local static for
		select name from sys.database_principal_aliases where user_name(alias_principal_id) is null
	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(15504,-1,-1)
    end

    -- CHECK FOR SQL LOGINS ALIASED 
    if exists (select * from sys.database_principal_aliases where is_windows_sid = 0)
    begin
        if @autofix <> 'auto'
        begin
			ROLLBACK TRANSACTION
			raiserror(15254,-1,-1)
			return(1)
        end

	-- DELETE ALIASED SQL USERS 
	declare ms_crs_aliases cursor local static for
		select name from sys.database_principal_aliases where is_windows_sid = 0
	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(15504,-1,-1)
    end

	COMMIT TRANSACTION
	-- SUCCESS 
	return (0)	-- sp_check_removable_sysusers

 
Last revision 2008RTM
See also

  sp_check_removable (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