recreate user with schema

180 views Asked by At

Following code recreates a user:

-- Remove link to order schema
ALTER AUTHORIZATION ON SCHEMA::order TO dbo

-- Recreate order user without login
DROP USER order
CREATE USER order WITH DEFAULT_SCHEMA = order

-- Restore link to order schema
ALTER AUTHORIZATION ON SCHEMA::order TO order

My question is, why do we need to remove link to schema before dropping a user, and restore it back after user created?

2

There are 2 answers

0
Prasanth V J On

The behavior of Schemas changed in SQL Server 2005. Schemas are no longer equivalent to database users; each schema is now a distinct namespace that exists independently of the database user who created it. In other words, a schema is simply a container of objects. A schema can be owned by any user, and its ownership is transferable.

https://msdn.microsoft.com/en-us/library/ms190387.aspx

http://basitaalishan.com/2014/05/29/the-database-principal-owns-a-schema-in-the-database-and-cannot-be-dropped/

0
Deqing On

Ok, just found why we need to temporary move the schema link to another owner before deleting a user.

It will return error if not doing so:

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