CURRENT_ROLE

CURRENT_ROLE in SQL is a system function that identifies the current role being used during a SQL session's execution. It is used to obtain the active role's name, which may be necessary for the dynamic management of data access permissions. It returns the name of the current role as a VARCHAR type. The current role is set by the SET ROLE statement.

Example

SELECT CURRENT_ROLE;

Output

myusername

Explanation

The CURRENT_ROLE function in PostgreSQL returns the name of the current role for the active session. In the output, myusername represents the current role name that the function returned.

Example

SELECT CURRENT_USER;

Output

'username'

Explanation

In this example, the CURRENT_USER function in SQL Server is used to return the name of the user who is currently connected to the database. The output will be the ‘username’ of the currently logged-in user.

Note: SQL Server does not have a CURRENT_ROLE function, but CURRENT_USER or USER can be used to determine the user context in which SQL Server code is running.

Example

SELECT CURRENT_ROLE FROM dual;

Output

CONNECT

Explanation

The CURRENT_ROLE is a built-in function in Oracle that returns the name of the active role for the current user session. In the example, it is used to pull the current active role from the dual table, a special one-row, one-column table present by default in all Oracle databases. The output CONNECT is the active role for the current database session.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.