How to Drop Orphan User in SQL Server (Msg 15138)

I am not able to drop a SQL user and keep getting this error message;

Msg 15138, Level 16, State 1, Line 5

The database principal owns a schema in the database, and cannot be dropped.

There is an orphan user who owns a schema or role and can not be dropped until user is detached from schema/role.

First see if there is any role associated and remove it;

-- Query to get the orphan users
EXEC sys.sp_change_users_login 'REPORT'

-- Query to get the user associated Database Role
SELECT 
	DBPrincipal_2.name as [Role], DBPrincipal_1.name as [OWNER] 
FROM sys.database_principals as DBPrincipal_1 
INNER JOIN sys.database_principals as DBPrincipal_2 
	ON DBPrincipal_1.principal_id = DBPrincipal_2.owning_principal_id 
WHERE DBPrincipal_1.name = 'ADDUSER'

--Query to fix the role
ALTER AUTHORIZATION ON ROLE::[db_owner] TO [dbo]

SSMS STEPS: Object Explorer->Target Server->Target Database->Security->roles->Right click on database role. Change user name to your selected name or “dbo” and click OK.

Now fix the issue where we will transfer the ownership of the database role/schema to dbo.

----*** Query to get the user associated schema
select * from information_schema.schemata
where schema_owner = MyUser'

--Query to fix the error Msg 15138 on database schema
ALTER AUTHORIZATION ON SCHEMA::[MyDatabaseSchema] TO [dbo]

--Query to drop the user
DROP USER [MyUser]
GO

SSMS STEPS: Object Explorer->Target Server->Target Database -> Security->Schemas->Right Click on schema->Change user name to your selected name or “dbo”.

Schema and/or database role has been transferred to “dbo”. You are safe to drop the user.

Shortcuts to run window command with elevated permission

A common trick to run command in elevated permission is to right click and select Run As administrator. Other work arounds are;

  1. Press Ctrl+Shift+Esc to open the Task Manager. Click on File menu > Run new task. To open a command prompt window, type cmd. Remember to check the Create this task with administrative privileges check-box. Then hit Enter.
  2. You can also open an elevated command prompt from the Task Manager using CTRL Key.
  3. Simply open the Start Menu or Start Screen and start typing the command line. Next, hold the Shift and Ctrl keys, and then hit Enter to open the command line in an elevated command prompt.

FIND AND REPLACE using Regular Expression in SSMS

We can use Regular Expression to find and replace, valid with all versions of SSMS

  • Find what: {.+}
  • Replace with: ‘\1’,
  • Look in: Selection
  • Expand Find Option
  • Use: Regular expression (checked)

That regular expression indicates find everything and remember what we found Replace everything we found \1 by wrapping it with tic marks and a comma.

If you have more complex requirements, the right chevron next to the drop down arrow on Find what lists the regular expression dialect SSMS/Visual Studio understands

References

https://dba.stackexchange.com/questions/96371/applying-quotes-across-multiple-lines

Updating SSIS packages for specific SQL server version

Updating SSIS packages for a specific SQL server target version (valid only for SQL Server 2012 and above)

  1. Right click on a project, click properties and select your target server version from the drop down

Click Apply, read the warning then click Yes as shown below, and finally click Ok

Re-open the packages and notice how the development platform (control flow and data flow tabs) changes

Before

After

Check for any odd behavior (discontinued/deprecated tasks), build/rebuild your packages, test your packages, and deploy/redeploy them.