Dropping a role in a SQL Server database returns Error: 15138 - The database principal owns a sch...

Learn dropping a role in a sql server database returns error: 15138 - the database principal owns a schema int he database, and cannot be dropped with practical examples, diagrams, and best practic...

Resolving SQL Server Error 15138: Cannot Drop Role Due to Schema Ownership

Hero image for Dropping a role in a SQL Server database returns Error: 15138 - The database principal owns a sch...

Learn how to troubleshoot and resolve SQL Server Error 15138, which prevents dropping a database role because it owns a schema. This article provides practical steps and SQL scripts to identify and transfer schema ownership.

When managing SQL Server databases, you might encounter Error 15138: "The database principal owns a schema in the database, and cannot be dropped." This error occurs when you attempt to drop a database role that is currently listed as the owner of one or more schemas within that database. SQL Server's security model prevents dropping a principal (like a role) if it owns other objects, ensuring data integrity and preventing orphaned schemas.

Understanding Schema Ownership in SQL Server

In SQL Server, a schema is a container for database objects like tables, views, stored procedures, and functions. Every schema must have an owner, which can be a database user or a database role. This ownership is crucial for permissions and object management. When a role owns a schema, it means that the role has implicit control over all objects within that schema. Attempting to drop such a role directly would leave the schema without an owner, leading to potential access issues and database inconsistencies.

flowchart TD
    A[Attempt to DROP ROLE] --> B{Role owns Schema?}
    B -- Yes --> C[Error 15138: Cannot drop role]
    C --> D[Identify owned Schemas]
    D --> E[Transfer Schema Ownership]
    E --> F[Retry DROP ROLE]
    B -- No --> F[DROP ROLE Successful]

Flowchart of troubleshooting Error 15138

Identifying Schemas Owned by the Role

The first step to resolve Error 15138 is to identify which schemas are owned by the role you wish to drop. SQL Server provides system views that allow you to query this information. You'll typically look at sys.schemas and join it with sys.database_principals to find the owner's name.

SELECT
    s.name AS SchemaName,
    dp.name AS SchemaOwner
FROM
    sys.schemas s
JOIN
    sys.database_principals dp ON s.principal_id = dp.principal_id
WHERE
    dp.name = 'YourRoleName';

SQL query to identify schemas owned by a specific role.

Transferring Schema Ownership

Once you've identified the schemas, the next step is to transfer their ownership to another principal. A common practice is to transfer ownership to dbo (the database owner) or to another appropriate role or user that will manage those schemas. The ALTER AUTHORIZATION statement is used for this purpose.

ALTER AUTHORIZATION ON SCHEMA::[SchemaName] TO [NewOwnerPrincipal];

SQL statement to transfer schema ownership.

1. Connect to the Database

Using SQL Server Management Studio (SSMS) or Azure Data Studio, connect to the SQL Server instance and the specific database where the role exists.

2. Identify Owned Schemas

Execute the query provided in the 'Identifying Schemas Owned by the Role' section to list all schemas owned by the role you want to drop. Make sure to replace 'YourRoleName'.

3. Transfer Ownership for Each Schema

For each SchemaName returned by the previous query, execute the ALTER AUTHORIZATION statement. Replace [SchemaName] with the actual schema name and [NewOwnerPrincipal] with the desired new owner (e.g., dbo).

4. Verify Ownership Transfer

Re-run the identification query to confirm that the role no longer owns any schemas. The query should return no rows for YourRoleName.

5. Drop the Role

Once all schemas have been transferred, you can now successfully drop the role using the DROP ROLE statement.

DROP ROLE [YourRoleName];

Final SQL statement to drop the role after transferring schema ownership.