

# 使用执行参数
<a name="querying-with-prepared-statements-querying-using-execution-parameters"></a>

您可以在任何 DML 查询中使用问号占位符创建参数化查询，而无需先创建预准备语句。要运行这些查询，您可以使用 Athena 控制台，或者使用 AWS CLI 或 AWS SDK，并在 `execution-parameters` 参数中声明变量。

**Topics**
+ [使用 Athena 控制台](querying-with-prepared-statements-running-queries-with-execution-parameters-in-the-athena-console.md)
+ [使用 AWS CLI](querying-with-prepared-statements-running-queries-with-execution-parameters-using-the-aws-cli.md)

# 在 Athena 控制台中运行具有执行参数的查询
<a name="querying-with-prepared-statements-running-queries-with-execution-parameters-in-the-athena-console"></a>

在 Athena 控制台中运行具有执行参数（问号）的参数化查询时，系统会按照问号在查询中出现的顺序提示您输入值。

**要运行具有执行参数的查询**

1. 请在 Athena 编辑器中输入带有问号占位符的查询，如以下示例所示。

   ```
   SELECT * FROM "my_database"."my_table"
   WHERE year = ? and month= ? and day= ?
   ```

1. 选择**运行**。

1. 在 **Enter parameters（输入参数）**对话框中，按查询中每个问号的顺序输入值。  
![\[按顺序输入查询参数的值\]](http://docs.aws.amazon.com/zh_cn/athena/latest/ug/images/querying-with-prepared-statements-1.png)

1. 输入完参数后，选择 **Run（运行）**。编辑器会显示您输入的参数值的查询结果。

在这种情况下，您可以执行下列操作之一：
+ 为同一个查询输入不同的参数值，然后再次选择 **Run（运行）**。
+ 要立即清除输入的所有值，请选择 **Clear（清除）**。
+ 要直接编辑查询（例如，添加或删除问号），请关闭 **Enter parameters（输入参数）**对话框。
+ 要保存参数化查询以供后续使用，请选择 **Save（保存）**或者 **Save as（另存为）**，然后为查询命名。有关使用已保存查询的更多信息，请参阅 [使用已保存的查询](saved-queries.md)。

为方便起见，**Enter parameters（输入参数）**对话框会记住先前为查询输入的值，只要您在查询编辑器中使用同一选项卡。

# 使用 AWS CLI 运行具有执行参数的查询
<a name="querying-with-prepared-statements-running-queries-with-execution-parameters-using-the-aws-cli"></a>

要使用 AWS CLI 运行具有执行参数的查询，请使用 `start-query-execution` 命令并在 `query-string` 参数中提供参数化查询。然后，在 `execution-parameters` 参数中，提供执行参数的值。以下示例对此方法进行了说明。

```
aws athena start-query-execution 
--query-string "SELECT * FROM table WHERE x = ? AND y = ?"
--query-execution-context "Database"="default" 
--result-configuration "OutputLocation"="s3://amzn-s3-demo-bucket;/..."
--execution-parameters "1" "2"
```