<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="set-role">
      </a>
      15.7.1.11 SET ROLE Statement
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045172894256">
  </a>
  <a class="indexterm" name="idm46045172893184">
  </a>
  <a class="indexterm" name="idm46045172891696">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa3926288"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SET</span> <span class="token keyword">ROLE</span> {
    <span class="token keyword">DEFAULT</span>
  <span class="token operator">|</span> <span class="token keyword">NONE</span>
  <span class="token operator">|</span> <span class="token keyword">ALL</span>
  <span class="token operator">|</span> <span class="token keyword">ALL</span> <span class="token keyword">EXCEPT</span> <span class="token keyword"><em class="replaceable">role</em></span> <span class="token punctuation">[</span><span class="token punctuation">,</span> <span class="token keyword"><em class="replaceable">role</em></span> <span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  <span class="token operator">|</span> <span class="token keyword"><em class="replaceable">role</em></span> <span class="token punctuation">[</span><span class="token punctuation">,</span> <span class="token keyword"><em class="replaceable">role</em></span> <span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
}</code></pre>
  </div>
  <p>
   <a class="link" href="set-role.html" title="15.7.1.11 SET ROLE Statement">
    <code class="literal">
     SET ROLE
    </code>
   </a>
   modifies the current
        user's effective privileges within the current session by
        specifying which of its granted roles are active. Granted roles
        include those granted explicitly to the user and those named in
        the
   <a class="link" href="server-system-variables.html#sysvar_mandatory_roles">
    <code class="literal">
     mandatory_roles
    </code>
   </a>
   system
        variable value.
  </p>
  <p>
   Examples:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa42455848"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SET</span> <span class="token keyword">ROLE</span> <span class="token keyword">DEFAULT</span><span class="token punctuation">;</span>
<span class="token keyword">SET</span> <span class="token keyword">ROLE</span> <span class="token string">'role1'</span><span class="token punctuation">,</span> <span class="token string">'role2'</span><span class="token punctuation">;</span>
<span class="token keyword">SET</span> <span class="token keyword">ROLE</span> <span class="token keyword">ALL</span><span class="token punctuation">;</span>
<span class="token keyword">SET</span> <span class="token keyword">ROLE</span> <span class="token keyword">ALL</span> <span class="token keyword">EXCEPT</span> <span class="token string">'role1'</span><span class="token punctuation">,</span> <span class="token string">'role2'</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Each role name uses the format described in
   <a class="xref" href="role-names.html" title="8.2.5 Specifying Role Names">
    Section 8.2.5, “Specifying Role Names”
   </a>
   . The host name part of the role
        name, if omitted, defaults to
   <code class="literal">
    '%'
   </code>
   .
  </p>
  <p>
   Privileges that the user has been granted directly (rather than
        through roles) remain unaffected by changes to the active roles.
  </p>
  <p>
   The statement permits these role specifiers:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <code class="literal">
       DEFAULT
      </code>
      : Activate the account default
            roles. Default roles are those specified with
      <a class="link" href="set-default-role.html" title="15.7.1.9 SET DEFAULT ROLE Statement">
       <code class="literal">
        SET DEFAULT ROLE
       </code>
      </a>
      .
     </p>
     <p>
      When a user connects to the server and authenticates
            successfully, the server determines which roles to activate
            as the default roles. If the
      <a class="link" href="server-system-variables.html#sysvar_activate_all_roles_on_login">
       <code class="literal">
        activate_all_roles_on_login
       </code>
      </a>
      system variable is enabled, the server activates all granted
            roles. Otherwise, the server executes
      <a class="link" href="set-role.html" title="15.7.1.11 SET ROLE Statement">
       <code class="literal">
        SET ROLE
            DEFAULT
       </code>
      </a>
      implicitly. The server activates only
            default roles that can be activated. The server writes
            warnings to its error log for default roles that cannot be
            activated, but the client receives no warnings.
     </p>
     <p>
      If a user executes
      <a class="link" href="set-role.html" title="15.7.1.11 SET ROLE Statement">
       <code class="literal">
        SET ROLE
            DEFAULT
       </code>
      </a>
      during a session, an error occurs if any
            default role cannot be activated (for example, if it does
            not exist or is not granted to the user). In this case, the
            current active roles are not changed.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       NONE
      </code>
      : Set the active roles to
      <code class="literal">
       NONE
      </code>
      (no active roles).
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       ALL
      </code>
      : Activate all roles granted to the
            account.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       ALL EXCEPT
       <em class="replaceable">
        <code>
         role
        </code>
       </em>
       [,
       <em class="replaceable">
        <code>
         role
        </code>
       </em>
       ] ...
      </code>
      : Activate
            all roles granted to the account except those named. The
            named roles need not exist or be granted to the account.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       <em class="replaceable">
        <code>
         role
        </code>
       </em>
       [,
       <em class="replaceable">
        <code>
         role
        </code>
       </em>
       ] ...
      </code>
      : Activate
            the named roles, which must be granted to the account.
     </p>
    </li>
   </ul>
  </div>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    <a class="link" href="set-default-role.html" title="15.7.1.9 SET DEFAULT ROLE Statement">
     <code class="literal">
      SET DEFAULT ROLE
     </code>
    </a>
    and
    <a class="link" href="set-role.html" title="15.7.1.11 SET ROLE Statement">
     <code class="literal">
      SET ROLE
          DEFAULT
     </code>
    </a>
    are different statements:
   </p>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       <a class="link" href="set-default-role.html" title="15.7.1.9 SET DEFAULT ROLE Statement">
        <code class="literal">
         SET DEFAULT ROLE
        </code>
       </a>
       defines
              which account roles to activate by default within account
              sessions.
      </p>
     </li>
     <li class="listitem">
      <p>
       <a class="link" href="set-role.html" title="15.7.1.11 SET ROLE Statement">
        <code class="literal">
         SET ROLE
              DEFAULT
        </code>
       </a>
       sets the active roles within the current
              session to the current account default roles.
      </p>
     </li>
    </ul>
   </div>
  </div>
  <p>
   For role usage examples, see
   <a class="xref" href="roles.html" title="8.2.10 Using Roles">
    Section 8.2.10, “Using Roles”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
