---------------------------------------------------------------------------------------------------------------------
SERVER LOGIN 유저와 DATABASE USER MAPPING 시 - error 15023 user already exists in current database
---------------------------------------------------------------------------------------------------------------------
데이터베이스를 복구 후 사용자 아이디로 로그인을 시도하면 매핑이 되지 않는 경우가 발생한다
이는 기존서버의 정보를 가져왔기 때문이다. 사용자 매핑을 통해 간단하게 해결이 가능하다
USE 데이터베이스명
EXEC sp_change_users_login Update_One, 계정명 , 계정명
go
https://dataedo.com/kb/query/sql-server/list-users-in-database
Users vs logins
-. Login grants access to the server - List logins in SQL Server
-. User grants a login access to the database
Server Logins
SELECT * FROM MASTER.SYS.SERVER_PRINCIPALS
WHERE TYPE IN ('SQL_LOGIN','WINDOWS_LOGIN')
ORDER BY NAME ;
-- Database Users
SELECT NAME, TYPE_DESC, DEFAULT_SCHEMA_NAME, SID , authentication_type_desc
FROM SYS.database_principals
--WHERE TYPE_DESC NOT IN ('DATABASE_ROLE','CERTIFICATE_MAPPED_USER')
WHERE 1=1
AND TYPE_DESC NOT IN ('DATABASE_ROLE')
ORDER BY NAME ;
---------------------------------------------------------------------------------------------------------------------
DATABASE DROP USER 삭제 시 - error 15138
---------------------------------------------------------------------------------------------------------------------
The reason for error is quite clear from the error message as there were schema associated with the user
and that needs to be transferred to another user.
ALTER AUTHORIZATION(Transact-SQL)
보안 개체의 소유권을 변경합니다.
use USERDB
go
SELECT s.name, 'ALTER AUTHORIZATION ON SCHEMA::'+s.name+'TO dbo;' AS ALTER_AUTHORIZATION_SQL
FROM sys.schemas s
WHERE s.principal_id = USER_ID('test1');
ALTER AUTHORIZATION ON SCHEMA::db_accessadmin TO dbo;
ALTER AUTHORIZATION ON SCHEMA::db_securityadmin TO dbo;
ALTER AUTHORIZATION ON SCHEMA::db_ddladmin TO dbo;
ALTER AUTHORIZATION ON SCHEMA::db_backupoperator TO dbo;
SELECT s.name, 'ALTER AUTHORIZATION ON SCHEMA::'+s.name+ ' TO dbo;' AS ALTER_AUTHORIZATION_SQL
FROM sys.schemas s
WHERE s.principal_id = USER_ID('test1');
-- Schema & Schema's Owner
select s.name as schema_name,
s.schema_id,
u.name as schema_owner
from sys.schemas s
inner join sys.sysusers u
on u.uid = s.principal_id
order by s.name
-- DB 간 데이타 이행 ( 스크립트로 테이블 생성 후, 아래 처럼 )
insert into targetdb.dbo.target_table
select * from sourcedb.dbo.source_table ;
-- DB INFO
SELECT NAME, SUSER_SNAME(OWNER_SID) AS DB_OWNER, compatibility_level, collation_name, STATE_DESC, recovery_model_desc
FROM SYS.DATABASES ;