USER (Transact-SQL)USER (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
既定値が指定されていない場合に、現在のユーザーのデータベース ユーザー名に対するシステム定義の値を、テーブルに挿入します。Allows a system-supplied value for the database user name of the current user to be inserted into a table when no default value is specified.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
USER
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
戻り値の型Return Types
nvarchar(128)nvarchar(128)
注釈Remarks
USER では、USER_NAME システム関数と同じ機能が提供されます。USER provides the same functionality as the USER_NAME system function.
USER は、CREATE TABLE または ALTER TABLE ステートメントで DEFAULT 制約を指定して実行するか、標準的な関数として使用します。Use USER with DEFAULT constraints in either the CREATE TABLE or ALTER TABLE statements, or use as any standard function.
USER では、常に現在のコンテキストの名前が返されます。USER always returns the name of the current context. EXECUTE AS ステートメントの後に呼び出した場合は、権限を借用したコンテキストの名前が USER により返されます。When called after an EXECUTE AS statement, USER returns the name of the impersonated context.
Windows プリンシパルがグループのメンバーシップを使ってデータベースにアクセスした場合、グループの名前ではなく Windows プリンシパルの名前が USER により返されます。If a Windows principal accesses the database by way of membership in a group, USER returns the name of the Windows principal instead of the name of the group.
例Examples
A.A. USER を使用してデータベース ユーザー名を返すUsing USER to return the database user name
次の例では、変数を char
型として宣言し、USER の現在値をこの変数に割り当てた後、テキストの説明を付けてこの変数を出力します。The following example declares a variable as char
, assigns the current value of USER to it, and then prints the variable with a text description.
DECLARE @usr CHAR(30)
SET @usr = user
SELECT 'The current user''s database username is: '+ @usr
GO
結果セットは次のようになります。Here is the result set.
-----------------------------------------------------------------------
The current user's database username is: dbo
(1 row(s) affected)
B.B. USER を DEFAULT 制約と共に使用するUsing USER with DEFAULT constraints
次の例では、sales 行の販売員に対する USER
制約として DEFAULT
を使用し、テーブルを作成します。The following example creates a table by using USER
as a DEFAULT
constraint for the salesperson of a sales row.
USE AdventureWorks2012;
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
テーブルからすべての情報を選択するクエリです。This is the query to select all information from the inventory22
table:
SELECT * FROM inventory22 ORDER BY part_id;
GO
次に結果セットを示します。entry-person
の値に注意してください。Here is the result set (note the entry-person
value):
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)
C.C. USER を EXECUTE AS と組み合わせて使用するUsing USER in combination with EXECUTE AS
次の例では、権限を借用したセッションを内部で呼び出すときの、USER
の動作を示します。The following example illustrates the behavior of USER
when called inside an impersonated session.
SELECT USER;
GO
EXECUTE AS USER = 'Mario';
GO
SELECT USER;
GO
REVERT;
GO
SELECT USER;
GO
結果セットは次のようになります。Here is the result set.
DBO
Mario
DBO
参照See Also
ALTER TABLE (Transact-SQL) ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL) CREATE TABLE (Transact-SQL)
CURRENT_TIMESTAMP (Transact-SQL) CURRENT_TIMESTAMP (Transact-SQL)
CURRENT_USER (Transact-SQL) CURRENT_USER (Transact-SQL)
セキュリティ関数 (Transact-SQL) Security Functions (Transact-SQL)
SESSION_USER (Transact-SQL) SESSION_USER (Transact-SQL)
SYSTEM_USER (Transact-SQL) SYSTEM_USER (Transact-SQL)
USER_NAME (Transact-SQL)USER_NAME (Transact-SQL)