> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> EXECUTE AS 语句的文档

# EXECUTE AS 语句

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            Not supported in ClickHouse Cloud
        </div>;
};

允许以其他用户身份执行查询。

<div id="syntax">
  ## 语法
</div>

```sql theme={null}
EXECUTE AS target_user;
EXECUTE AS target_user subquery;
```

第一种形式 (不带 `subquery`) 会将当前 `session` 中后续的所有查询设置为以指定的 `target_user` 身份执行。

第二种形式 (带 `subquery`) 仅会以指定的 `target_user` 身份执行指定的 `subquery`。

要使这两种形式都能生效，需要将配置项 `access_control_improvements.allow_impersonate_user`
设置为 `1`，并授予 `IMPERSONATE` 权限。例如，以下命令

```sql theme={null}
GRANT IMPERSONATE ON user1 TO user2;
GRANT IMPERSONATE ON * TO user3;
```

允许用户 `user2` 执行 `EXECUTE AS user1 ...` 命令，同时也允许用户 `user3` 以任意用户身份执行命令。

在伪装为另一个用户时，函数 [currentUser()](/zh/reference/functions/regular-functions/other-functions#currentUser) 返回该用户的名称，
而函数 [authenticatedUser()](/zh/reference/functions/regular-functions/other-functions#authenticatedUser) 返回实际通过身份验证的用户名称。

<div id="examples">
  ## 示例
</div>

```sql theme={null}
SELECT currentUser(), authenticatedUser(); -- 输出 "default    default"
CREATE USER james;
EXECUTE AS james SELECT currentUser(), authenticatedUser(); -- 输出 "james    default"
```
