Microsoft sql server ошибка 15138

SQL error 15138 triggers while dropping a database user that owns some schemas at the database level.

As a part of our Server Management Services, we help our Customers to fix SQL related errors regularly.

Let us today discuss the possible causes and fixes for this error.

What is SQL error 15138?

As we discussed earlier the SQL error 15138 occurs while dropping a database user. It generally happens when the user owns some schemas at the database level. A typical error message would look like:

SQL Server error 15138

Changing the owner of the identified schema prior to dropping the user can fix it. Let us now look at the steps to perform this task.

What is SQL error 15138?

The steps to resolve the SQL error 15138 includes the following processes:

  1. Identify the schemas owned by the user
  2. Transfer the ownership of the schema
  3. Drop the User

Lets us now look at each of these in detail.

Identify the schemas owned by the user

First process to resolve the 15138 error is to identify the schemas that the user to be deleted owns. With the series of steps below, we can check the schema that a user owns in the database.

1. Connect to SQL Server Instance in SQL Server Management Studio.
2. Expand the database folder followed by the actual database name in which the user exists or created.
3. Expand Security folder inside the target database followed by the Users folder and we will find a list of database users.
4. Right-click on the user and select Properties.
5. Select Owned Schemas in the left pane of the window. A list of schemas will be displayed. The user owns the ones which are ticked.

Another option is to query schema names from sys.schemas table for the user. Here is the query:

USE DBName
GO
SELECT s.name SchemaName
FROM sys.schemas
WHERE s.principal_id = USER_ID('xyz')

Change DBName with the database name which is owned by the user “xyz”.

Transfer the ownership of the schema

Once we have identified the schemas that are owned by the user, the next step is to transfer its ownership to some other user.

We can change the owner of the schema using the ALTER AUTHORIZATION command. This command can be used to change the ownership of any securable that has an owner. We transfer the ownership to dbo, which is a type of user account in SQL Server that has permissions to perform all activities in the database. We can drop the user after changing the ownership.

To change the owner of the schema, execute the query given below:

USE [DBName]
G0
Alter Authorization ON Schema::[SCHEMA_NAME] TO [dbo]
Go

Change DBName with the database name and Schema_Name with the schema name that the user owns. This can also be done using SQL Server Management Studio. The steps to be followed for it include:

1. Connect to SQL Server Instance in SQL Server Management Studio.
2. Expand the database folder followed by the actual database name in which the user exists or created.
3. Expand Security folder inside the target database followed by the Schemas folder.
4. Right-click on the schema that has to be modified. We can see the user “xyz” as the owner. Change it to “dbo” or some other user to resolve the error. We can just enter the user and click OK to save the change or use Search to find a user.

Drop the user

As we have removed the user from the ownership of the schema, we can finally proceed to drop the user. Run below command to drop the user.

USE [DBName]
Go
DROP USER USERNAME
Go

Change DBName with the database name where the user exists and USERNAME with the user name which you want to drop.

We can perform this from SQL Server Management Studio as well by right-clicking on the user and choose the delete option.

1. Connect to target SQL Server Instance.
2. Expand the Database folder in which the user exists.
3. Expand the Security folder and then Users folder to get the target user name.
4. Right-click and choose delete on the identified user which needs to be deleted.
5. Click on the Ok button of the user deletion window.

[Need any further assistance in fixing SQL errors? – We’re available 24*7]

Conclusion

In short, SQL error 15138  occurs while dropping a user. This happens mainly when the user owns a schema at the database level. Today, we saw how our Support Engineers fix this error.

  • Remove From My Forums
  • Question

  • The title makes it sound as if question has been answered, but in fact it has not.  Because of differences.

    A db_owner created a user in the database.  This person erroneously left the default schema as dbo.  When the attempt was made to delete the user the error message cited above was received.  So created a schema in the database with the
    same name as the user.  Gave the user ownership.  Then set the default schema to be the new schema.  Tried to delete.  The schema deleted just fine.  The user refused to delete again with the error message above.  Ran below query
    to see what schemas were  owned by this user.

    SELECT name FROM sys.schemas WHERE principal_id = USER_ID(‘myuser’)

    But the query returned a result set with zero rows.  So how can this user be owning a schema when it doesn’t own any?  Why can’t we drop this user for the reason of schema ownership when it owns no schemas?


    Edward R. Joell MCSD MCDBA

    • Edited by

      Wednesday, June 20, 2012 3:52 PM
      Query hidden by formating

Answers

  • Unfortunately, I have little idea of what is going on. I see that message 15138 has a parameter where the word «schema» appears in the actual message. Maybe there is a bug so that it says «schema» when it should say something else?

    I don’t have much hope, but try this:

    SELECT ‘ IF EXISTS (SELECT * FROM sys.’ + quotename(o.name) +
           ‘ WHERE principal_id = user_id(»youruser»)) PRINT »sys.’ + o.name + »»
    FROM   sys.all_objects o
    JOIN   sys.all_columns c ON o.object_id = c.object_id
    WHERE  c.name = ‘principal_id’
      AND  o.schema_id = 4
      AND  o.type = ‘V’
      AND  o.name NOT LIKE ‘pdw%’

    Run the result set from the above, and if prints something that catalog view may be worth looking into a little more. Change «youruser» to the correct username — and of course run in the right database.


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

    • Marked as answer by
      amber zhang
      Tuesday, June 26, 2012 1:27 AM
    • Unmarked as answer by
      joeller
      Monday, July 2, 2012 4:36 PM
    • Marked as answer by
      joeller
      Monday, July 2, 2012 7:01 PM

  • Erland ran your script and and ran the result set. The user showed up in two sys views, sys.database_principals and sys.schemas. (as expected)

    Then did select top 1000 from sys.shemas and altered the query to join to sys.database_principals and included name column from that view in the select.

    SELECT s.*, p.[name] 
    
    FROM sys.schemas s
    JOIN sys.database_principals p ON s.Principal_id = p.principal_id

    And that query result showed that somehow it was now showing that user as the owner of the schemas db_datareader and db_denydatareader. (!?!) For some reason this did NOT show up when we ran the query below which returned an empty result set (see OP). (Again
    !?!)

    SELECT s.name As SchemaNameResult 
    FROM sys.schemas s
    WHERE s.principal_id = USER_ID(‘youruserid’)

    So I opened the properties gui for those schemas sure enough those schemas were showing our user as the owner. (Now bear in mind that I did look in all of those property guis when the problem first occurred. I can only figure that the government database
    manager, whom I am supporting, changed something after I viewed those results.)

    Anyway, I then reset their owners back to be the roles that are supposed to own them. Voila!!! The user was able to be deleted.

    Very bizarre all around.


    Edward R. Joell MCSD MCDBA

    • Marked as answer by
      joeller
      Monday, July 2, 2012 7:01 PM

Here we will learn how to fix Microsoft SQL Server error 15138 & error 3729 that we receive while dropping a database user and a schema in SQL Server.  Let us check the details to fix this issue one after another.

Microsoft SQL Server Error 15138

I have discussed similar type of issue in my last tip, where a server level login was owner of some databases which causes to fail the login removal and throws error 15174. We changed the ownership and then tried to drop that login and it was successful.

Microsoft SQL Server Error 15138 which i am discussing in this tip is also similar kind of error. Here, user has owned some schemas at database level. We will do the same thing, we will change the owner of identified schema and drop the user.

The details of SQL Server error 15138 is given below.

Drop failed for User ‘xyz’.
The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error:15138)

error 15138

As error clearly says that a schema is owned by this user in the database so let us check the schema that is owned by this user. Please follow the below process to check the owned schema for an user in the database.

  • Connect to SQL Server Instance in SSMS.
  • Expand the database folder followed by the actual database name in which your user exists or created.
  • Expand Security folder inside your target database followed by the Users folder and double click on the target user which you want to drop.
  • You can see a section named “Owned Schemas. Here you can see the schemas that are owned by this user.

If you find any schema that is owned by this user in above steps, you need to change the owner from this user to some other user or you can change it to dbo as well. See the below screenshot of the owned schema for a user.

owned schema

Solution

Now we have identified the schema which is owned by this user. Our Next step is to change is owner of the identified schema and then drop the login.

 --Change DBName with your database name and Schema_Name with your schema name which is owned by this user.
USE [DBName] 
G0 
Alter Authorization ON Schema::[SCHEMA_NAME] TO [dbo] 
Go

Once above command will execute, next step is to drop the user again. Run below command to drop the user.

--Change DBName with your database name where user exists and USERNAME with user name which you want to drop.
USE [DBName] 
Go 
DROP USER USERNAME 
Go

This time it will work and user will drop from the database. You can also delete this user from SSMS by right click on the user and choose the delete option. SSMS way is given below:

  • Connect to target SQL Server Instance.
  • Expand Database folder in which user exists.
  • Expand the Security folder and then Users folder to get the target user name.
  • Right click and choose delete on the identified user which needs to be deleted.
  • Click on Ok button of the user deletion window.

There are many reasons which causes to fail the user or login deletion. You can find many errors and their solutions in given links. Learn to fix:

  • Error Code 15174: Login Owns one or more databases(s). Change the owner of database(s) before dropping the login.
  • Error Code 15173: Revoke the permission(s) before dropping the login.

Microsoft SQL Server Error 3729

We have seen how to drop an user who was owner of a schema in above section. Now we will understand how to drop a schema that is referenced by an object. If you try to run DROP Schema statement and that particular schema is referenced to some object you will get below error. The details of SQL Server error 3729 is given below.

Drop failed for Schema ‘abc’.
Cannot drop schema ‘abc’ because it is being referenced by object ‘Locations’. (Microsoft SQL Server, Error 3729)

In order to fix this issue, we will change the schema of table “Locations” to remove the reference. We will use sp_changeobjectowner system stored procedure to change schema from abc to dbo. Run below command to change the schema of the table “Locations” from abc to dbo.

--Change DBName with your database name where user/schema/table exists
--Change the abc to your schema name.
USE DBName
GO
sp_changeobjectowner 'abc.Locations','dbo'

Now go ahead and drop the schema post executing above command. This time it will drop without an error.

--Change DBName with your database name where user/schema/table exists
--Change the abc to your schema name which you want to delete.
USE [DBName]
GO
DROP SCHEMA [abc]
GO

You can also drop the same using SSMS as well. Go to schema folder inside the security folder of your database. Right click on identified schema and choose delete option.  it will delete your schema.

Learn to fix below errors as well:

  • Error Code 15174: Login Owns one or more databases(s). Change the owner of database(s) before dropping the login.
  • Error Code 15173: Revoke the permission(s) before dropping the login.

I hope you like this article. Please follow us on our facebook page and on Twitter handle to get latest updates.

  • Author
  • Recent Posts

Manvendra Deo Singh

I am working as a Technical Architect in one of the top IT consulting firm. I have expertise on all versions of SQL Server since SQL Server 2000. I have lead multiple SQL Server projects like consolidation, upgrades, migrations, HA & DR. I love to share my knowledge. You can contact me on my social accounts for any consulting work.

Manvendra Deo Singh

  • Remove From My Forums
  • Question

  • Error: 15138 The database principal owns a schema in the database, and cannot be dropped.

    I have tried most options with google, but unable to drop the user, checked SUSER, SID nothing found. Please advise.

    • Edited by

      Thursday, February 11, 2016 10:24 AM
      formatted the question

Answers

  • Hi Satishs1206,

    Please execute the following query provided by Erland in this similar
    thread.

    SELECT ' IF EXISTS (SELECT * FROM sys.' + quotename(o.name) +
    
            ' WHERE principal_id = user_id(''youruser'')) PRINT ''sys.' + o.name + ''''
    
     FROM   sys.all_objects o
    
     JOIN   sys.all_columns c ON o.object_id = c.object_id
    
     WHERE  c.name = 'principal_id'
    
       AND  o.schema_id = 4
    
       AND  o.type = 'V'
    
       AND  o.name NOT LIKE 'pdw%'
    

    Run result set from the above, and investigate the catalog view it prints.

    Thanks,
    Lydia Zhang


    Lydia Zhang
    TechNet Community Support

    • Proposed as answer by
      Lydia ZhangMicrosoft contingent staff
      Monday, February 22, 2016 4:27 AM
    • Marked as answer by
      Lydia ZhangMicrosoft contingent staff
      Tuesday, February 23, 2016 7:23 AM

I need to drop a user with dbowner schema from a SQL Server database. I cannot drop it as it is since I get this error message

Drop failed for User ‘network service’. (Microsoft.SqlServer.Smo)

The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error: 15138)

When I try to uncheck the schema owned by this user to remove the DB owner it does nothing. My question is how I can drop this user or edit its name from 'network service' to 'NT AUTHORITYNETWORK SERVICE'

Hasan Fathi's user avatar

Hasan Fathi

5,5304 gold badges42 silver badges58 bronze badges

asked Feb 16, 2016 at 13:50

Sofia Khwaja's user avatar

Sofia KhwajaSofia Khwaja

1,8593 gold badges17 silver badges20 bronze badges

0

I had the same problem, I run two scripts then my problem is solved.

Try this:

In this query you can get user schema as a result for AdventureWorks database:

USE AdventureWorks;
SELECT s.name
FROM sys.schemas s
WHERE s.principal_id = USER_ID('your username');

after take schema name you can alter authorization on schema like this:

ALTER AUTHORIZATION ON SCHEMA::db_owner TO dbo;

in this query db_owner schema name that get from first query.

finally you can delete user without error.

my source: SQL SERVER – Fix: Error: 15138

Majid's user avatar

Majid

13.7k15 gold badges77 silver badges113 bronze badges

answered May 9, 2018 at 9:36

Hasan Fathi's user avatar

2

I have the same issue, I cannot delete the user

foo

since it says:

enter image description here

So I need to go to Security -> Schemas and look for dbo, then right click and choose Properties:

enter image description here

Then change

foo

to

dbo

then after that I can now delete the user that I want to delete.

enter image description here

answered Sep 26, 2019 at 17:15

Willy David Jr's user avatar

Willy David JrWilly David Jr

8,4965 gold badges43 silver badges55 bronze badges

0

In my case I execute these commands and problem solved:

ALTER AUTHORIZATION ON SCHEMA::db_owner TO dbo;
ALTER AUTHORIZATION ON SCHEMA::db_datareader TO dbo;
ALTER AUTHORIZATION ON SCHEMA::db_datawriter TO dbo;

Hasan Fathi's user avatar

Hasan Fathi

5,5304 gold badges42 silver badges58 bronze badges

answered Dec 9, 2020 at 13:08

Sayed Abolfazl Fatemi's user avatar

ALTER AUTHORIZATION ON SCHEMA::[NT AUTHORITYSYSTEM] TO dbo

Thom A's user avatar

Thom A

87.3k10 gold badges41 silver badges74 bronze badges

answered Nov 25, 2018 at 13:07

Joy Bhattacharya's user avatar

Понравилась статья? Поделить с друзьями:
  • Microsoft office ошибка 1335
  • Microsoft office ошибка 0 2035
  • Microsoft office обновляется и выдает ошибку
  • Microsoft office непредвиденная ошибка
  • Microsoft office код ошибки 30175 4