SESSION_USER (Transact-SQL)SESSION_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
SESSION_USER では現在のデータベースに含まれる現在のコンテキストのユーザー名が返されます。SESSION_USER returns the user name of the current context in the current database.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
SESSION_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
SESSION_USER は、DEFAULT 制約と共に CREATE TABLE または ALTER TABLE ステートメント内で使用するか、標準の関数として使用します。Use SESSION_USER with DEFAULT constraints in either the CREATE TABLE or ALTER TABLE statements, or use it as any standard function. SESSION_USER は、既定値が指定されていなければテーブルに挿入できます。SESSION_USER can be inserted into a table when no default value is specified. この関数は引数を取りません。This function takes no arguments. SESSION_USER はクエリで使用できます。SESSION_USER can be used in queries.
コンテキスト切り替え後に SESSION_USER が呼び出された場合、SESSION_USER では借用したコンテキストのユーザー名が返されます。If SESSION_USER is called after a context switch, SESSION_USER will return the user name of the impersonated context.
例Examples
A.A. SESSION_USER を使用して現在のセッションのユーザー名を返すUsing SESSION_USER to return the user name of the current session
次の例では、変数を nchar
型として宣言し、SESSION_USER
の現在値をこの変数に割り当てた後、テキストの説明と共にこの変数を出力します。The following example declares a variable as nchar
, assigns the current value of SESSION_USER
to that variable, and then prints the variable with a text description.
DECLARE @session_usr NCHAR(30);
SET @session_usr = SESSION_USER;
SELECT 'This session''s current user is: '+ @session_usr;
GO
セッション ユーザーが Surya
の場合の結果セットは次のとおりです。This is the result set when the session user is Surya
:
--------------------------------------------------------------
This session's current user is: Surya
(1 row(s) affected)
B.B. SESSION_USER を DEFAULT 制約と共に使用するUsing SESSION_USER with DEFAULT constraints
次の例では、荷物の受領記録者の名前に対し、SESSION_USER
を DEFAULT
制約として使用するテーブルを作成します。The following example creates a table that uses SESSION_USER
as a DEFAULT
constraint for the name of the person who records receipt of a shipment.
USE AdventureWorks2012;
GO
CREATE TABLE deliveries3
(
order_id INT IDENTITY(5000, 1) NOT NULL,
cust_id INT NOT NULL,
order_date SMALLDATETIME NOT NULL DEFAULT GETDATE(),
delivery_date SMALLDATETIME NOT NULL DEFAULT
DATEADD(dd, 10, GETDATE()),
received_shipment NCHAR(30) NOT NULL DEFAULT SESSION_USER
);
GO
テーブルに追加されたレコードに対して、現在のユーザーのユーザー名が設定されます。Records added to the table will be stamped with the user name of the current user. この例では、Wanida
、Sylvester
、Alejandro
が荷物の受領を確認します。In this example, Wanida
, Sylvester
, and Alejandro
verify receipt of shipments. EXECUTE AS
を使用してユーザー コンテキストを切り替えることでも、同様の機能を実現できます。This can be emulated by switching user context by using EXECUTE AS
.
EXECUTE AS USER = 'Wanida'
INSERT deliveries3 (cust_id)
VALUES (7510);
INSERT deliveries3 (cust_id)
VALUES (7231);
REVERT;
EXECUTE AS USER = 'Sylvester'
INSERT deliveries3 (cust_id)
VALUES (7028);
REVERT;
EXECUTE AS USER = 'Alejandro'
INSERT deliveries3 (cust_id)
VALUES (7392);
INSERT deliveries3 (cust_id)
VALUES (7452);
REVERT;
GO
次のクエリでは、deliveries3
テーブルからすべての情報を選択します。The following query selects all information from the deliveries3
table.
SELECT order_id AS 'Order #', cust_id AS 'Customer #',
delivery_date AS 'When Delivered', received_shipment
AS 'Received By'
FROM deliveries3
ORDER BY order_id;
GO
結果セットは次のようになります。Here is the result set.
Order # Customer # When Delivered Received By
-------- ---------- ------------------- -----------
5000 7510 2005-03-16 12:02:14 Wanida
5001 7231 2005-03-16 12:02:14 Wanida
5002 7028 2005-03-16 12:02:14 Sylvester
5003 7392 2005-03-16 12:02:14 Alejandro
5004 7452 2005-03-16 12:02:14 Alejandro
(5 row(s) affected)
例: Azure Synapse AnalyticsAzure Synapse Analytics、Parallel Data WarehouseParallel Data WarehouseExamples: Azure Synapse AnalyticsAzure Synapse Analytics and Parallel Data WarehouseParallel Data Warehouse
C. SESSION_USER を使用して現在のセッションのユーザー名を返すC: Using SESSION_USER to return the user name of the current session
次の例では、現在のセッションのセッション ユーザーを返します。The following example returns the session user for the current session.
SELECT SESSION_USER;
参照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)
SYSTEM_USER (Transact-SQL) SYSTEM_USER (Transact-SQL)
システム関数 (Transact-SQL) System Functions (Transact-SQL)
USER (Transact-SQL) USER (Transact-SQL)
USER_NAME (Transact-SQL)USER_NAME (Transact-SQL)