USER(Transact-SQL)
SQL Server 2008
기본값이 지정되지 않은 경우에 현재 사용자의 데이터베이스 사용자 이름에 대해 시스템이 제공한 값을 테이블에 삽입할 수 있도록 허용합니다.
1. USER를 사용하여 데이터베이스 사용자 이름 반환
다음 예에서는 변수를 char로 선언하고 USER의 현재 값을 변수에 할당한 다음 텍스트 설명과 함께 변수를 인쇄합니다.
DECLARE @usr char(30) SET @usr = user SELECT 'The current user''s database username is: '+ @usr GO
결과 집합은 다음과 같습니다.
----------------------------------------------------------------------- The current user's database username is: dbo (1 row(s) affected)
2. DEFAULT 제약 조건으로 USER 사용
다음 예에서는 판매 행의 판매원에 대한 DEFAULT 제약 조건으로 USER를 사용하여 테이블을 만듭니다.
USE AdventureWorks;
GO
CREATE TABLE inventory22
(
part_id int IDENTITY(100, 1) NOT NULL,
description varchar(30) NOT NULL,
entry_person varchar(30) NOT NULL DEFAULT USER
)
GO
INSERT inventory22 (description)
VALUES ('Red pencil')
INSERT inventory22 (description)
VALUES ('Blue pencil')
INSERT inventory22 (description)
VALUES ('Green pencil')
INSERT inventory22 (description)
VALUES ('Black pencil')
INSERT inventory22 (description)
VALUES ('Yellow pencil')
GO
다음은 inventory22 테이블에서 모든 정보를 선택하는 쿼리입니다.
SELECT * FROM inventory22 ORDER BY part_id; GO
결과 집합은 다음과 같습니다(entry-person 값 참고).
part_id description entry_person ----------- ------------------------------ ------------------------- 100 Red pencil dbo 101 Blue pencil dbo 102 Green pencil dbo 103 Black pencil dbo 104 Yellow pencil dbo (5 row(s) affected)
3. EXECUTE AS와 함께 USER 사용
다음 예에서는 가장된 세션 내부에서 호출된 USER의 동작을 보여 줍니다.
SELECT USER; GO EXECUTE AS USER = 'Mario'; GO SELECT USER; GO REVERT; GO SELECT USER; GO
결과 집합은 다음과 같습니다.
DBO Mario DBO
