Skip to content

execute_statement

execute_statement(
    resource_arn: Arn,
    secret_arn: Arn,
    sql: SqlStatement,
    *,
    config_overrides: Optional[RDSDataClientConfig] = None,
    database: Optional[DbName] = None,
    schema: Optional[DbName] = None,
    parameters: Optional[SqlParametersList] = None,
    transaction_id: Optional[Id] = None,
    include_result_metadata: Optional[Boolean] = None,
    continue_after_timeout: Optional[Boolean] = None,
    result_set_options: Optional[ResultSetOptions] = None,
    format_records_as: Optional[RecordsFormatType] = None,
) -> ExecuteStatementResponse

Runs a SQL statement against a database.

If a call isn't part of a transaction because it doesn't include the transactionID parameter, changes that result from the call are committed automatically.

If the binary response data from the database is more than 1 MB, the call is terminated.

Parameters:

  • resource_arn (Arn) –

    The Amazon Resource Name (ARN) of the Aurora Serverless DB cluster.

  • secret_arn (Arn) –

    The ARN of the secret that enables access to the DB cluster. Enter the database user name and password for the credentials in the secret.

    For information about creating the secret, see Create a database secret.

    When you use the CLI on Linux to reference a secret created in the RDS console, the ARN might include special characters like rds!cluster. If you enclose the ARN in double quotes, the ! character might trigger a shell expansion error, such as -bash: !cluster: event not found. To avoid this, escape the exclamation mark (\!) in the ARN or enclose the entire ARN in single quotes (') instead of double quotes.

    Alternatively, disable shell history expansion by running set +H before you execute the command.

  • sql (SqlStatement) –

    The SQL statement to run.

  • database (Optional[DbName], default: None ) –

    The name of the database.

  • schema (Optional[DbName], default: None ) –

    The name of the database schema.

    Currently, the schema parameter isn't supported.

  • parameters (Optional[SqlParametersList], default: None ) –

    The parameters for the SQL statement.

    Array parameters are not supported.

  • transaction_id (Optional[Id], default: None ) –

    The identifier of a transaction that was started by using the BeginTransaction operation. Specify the transaction ID of the transaction that you want to include the SQL statement in.

    If the SQL statement is not part of a transaction, don't set this parameter.

  • include_result_metadata (Optional[Boolean], default: None ) –

    A value that indicates whether to include metadata in the results.

  • continue_after_timeout (Optional[Boolean], default: None ) –

    A value that indicates whether to continue running the statement after the call times out. By default, the statement stops running when the call times out.

    For DDL statements, we recommend continuing to run the statement after the call times out. When a DDL statement terminates before it is finished running, it can result in errors and possibly corrupted data structures.

  • result_set_options (Optional[ResultSetOptions], default: None ) –

    Options that control how the result set is returned.

  • format_records_as (Optional[RecordsFormatType], default: None ) –

    A value that indicates whether to format the result set as a single JSON string. This parameter only applies to SELECT statements and is ignored for other types of statements. Allowed values are NONE and JSON. The default value is NONE. The result is returned in the formattedRecords field.

    For usage information about the JSON format for result sets, see Using the Data API in the Amazon Aurora User Guide.

Raises:

Source code in docs_src/capo_rds_data/_services/rds_data.py
def execute_statement(
    self,
    resource_arn: "capo_rds_data.types.arn.Arn",
    secret_arn: "capo_rds_data.types.arn.Arn",
    sql: "capo_rds_data.types.sql_statement.SqlStatement",
    *,
    config_overrides: Optional[RDSDataClientConfig] = None,
    database: Optional["capo_rds_data.types.db_name.DbName"] = None,
    schema: Optional["capo_rds_data.types.db_name.DbName"] = None,
    parameters: Optional[
        "capo_rds_data.types.sql_parameters_list.SqlParametersList"
    ] = None,
    transaction_id: Optional["capo_rds_data.types.id.Id"] = None,
    include_result_metadata: Optional["capo_rds_data.types.boolean.Boolean"] = None,
    continue_after_timeout: Optional["capo_rds_data.types.boolean.Boolean"] = None,
    result_set_options: Optional[
        "capo_rds_data.types.result_set_options.ResultSetOptions"
    ] = None,
    format_records_as: Optional[
        "capo_rds_data.types.records_format_type.RecordsFormatType"
    ] = None,
) -> "capo_rds_data.types.execute_statement_response.ExecuteStatementResponse":
    r"""<p>Runs a SQL statement against a database.</p> <note> <p>If a call isn't part of a transaction because it doesn't include the <code>transactionID</code> parameter, changes that result from the call are committed automatically.</p> <p>If the binary response data from the database is more than 1 MB, the call is terminated.</p> </note>

    Args:
        resource_arn: <p>The Amazon Resource Name (ARN) of the Aurora Serverless DB cluster.</p>
        secret_arn: <p>The ARN of the secret that enables access to the DB cluster. Enter the database user name and password for the credentials in the secret.</p> <p>For information about creating the secret, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/create_database_secret.html">Create a database secret</a>.</p> <note> <p>When you use the CLI on Linux to reference a secret created in the RDS console, the ARN might include special characters like <code>rds!cluster</code>. If you enclose the ARN in double quotes, the <code>!</code> character might trigger a shell expansion error, such as <code>-bash: !cluster: event not found</code>. To avoid this, escape the exclamation mark (\!) in the ARN or enclose the entire ARN in single quotes (') instead of double quotes.</p> <p>Alternatively, disable shell history expansion by running <code>set +H</code> before you execute the command.</p> </note>
        sql: <p>The SQL statement to run.</p>
        database: <p>The name of the database.</p>
        schema: <p>The name of the database schema.</p> <note> <p>Currently, the <code>schema</code> parameter isn't supported.</p> </note>
        parameters: <p>The parameters for the SQL statement.</p> <note> <p>Array parameters are not supported.</p> </note>
        transaction_id: <p>The identifier of a transaction that was started by using the <code>BeginTransaction</code> operation. Specify the transaction ID of the transaction that you want to include the SQL statement in.</p> <p>If the SQL statement is not part of a transaction, don't set this parameter.</p>
        include_result_metadata: <p>A value that indicates whether to include metadata in the results.</p>
        continue_after_timeout: <p>A value that indicates whether to continue running the statement after the call times out. By default, the statement stops running when the call times out.</p> <note> <p>For DDL statements, we recommend continuing to run the statement after the call times out. When a DDL statement terminates before it is finished running, it can result in errors and possibly corrupted data structures.</p> </note>
        result_set_options: <p>Options that control how the result set is returned.</p>
        format_records_as: <p>A value that indicates whether to format the result set as a single JSON string. This parameter only applies to <code>SELECT</code> statements and is ignored for other types of statements. Allowed values are <code>NONE</code> and <code>JSON</code>. The default value is <code>NONE</code>. The result is returned in the <code>formattedRecords</code> field.</p> <p>For usage information about the JSON format for result sets, see <a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html">Using the Data API</a> in the <i>Amazon Aurora User Guide</i>.</p>

    Raises:
        capo_rds_data.errors.access_denied_exception.AccessDeniedException: <p>You don't have sufficient access to perform this action.</p>
        capo_rds_data.errors.bad_request_exception.BadRequestException: <p>There is an error in the call or in a SQL statement. This exception is deprecated.</p>
        capo_rds_data.errors.database_error_exception.DatabaseErrorException: <p>There was an error in processing the SQL statement.</p>
        capo_rds_data.errors.database_not_found_exception.DatabaseNotFoundException: <p>The DB cluster doesn't have a DB instance.</p>
        capo_rds_data.errors.database_resuming_exception.DatabaseResumingException: <p>A request was cancelled because the Aurora Serverless DB instance was paused. The Data API request automatically resumes the DB instance. Wait a few seconds and try again.</p>
        capo_rds_data.errors.database_unavailable_exception.DatabaseUnavailableException: <p>The writer instance in the DB cluster isn't available.</p>
        capo_rds_data.errors.forbidden_exception.ForbiddenException: <p>There are insufficient privileges to make the call.</p>
        capo_rds_data.errors.http_endpoint_not_enabled_exception.HttpEndpointNotEnabledException: <p>The HTTP endpoint for using RDS Data API isn't enabled for the DB cluster.</p>
        capo_rds_data.errors.internal_server_error_exception.InternalServerErrorException: <p>An internal error occurred.</p>
        capo_rds_data.errors.invalid_resource_state_exception.InvalidResourceStateException: <p>The resource is in an invalid state.</p>
        capo_rds_data.errors.invalid_secret_exception.InvalidSecretException: <p>The Secrets Manager secret used with the request isn't valid.</p>
        capo_rds_data.errors.secrets_error_exception.SecretsErrorException: <p>There was a problem with the Secrets Manager secret used with the request, caused by one of the following conditions:</p> <ul> <li> <p>RDS Data API timed out retrieving the secret.</p> </li> <li> <p>The secret provided wasn't found.</p> </li> <li> <p>The secret couldn't be decrypted.</p> </li> </ul>
        capo_rds_data.errors.service_unavailable_error.ServiceUnavailableError: <p>The service specified by the <code>resourceArn</code> parameter isn't available.</p>
        capo_rds_data.errors.statement_timeout_exception.StatementTimeoutException: <p>The execution of the SQL statement timed out.</p>
        capo_rds_data.errors.transaction_not_found_exception.TransactionNotFoundException: <p>The transaction ID wasn't found.</p>
        capo_rds_data.errors.unsupported_result_exception.UnsupportedResultException: <p>There was a problem with the result because of one of the following conditions:</p> <ul> <li> <p>It contained an unsupported data type.</p> </li> <li> <p>It contained a multidimensional array.</p> </li> <li> <p>The size was too large.</p> </li> </ul>
        capo_rds_data.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_rds_data.types.execute_statement_request.ExecuteStatementRequest]",
    ) -> OperationResponse[
        "capo_rds_data.types.execute_statement_response.ExecuteStatementResponse"
    ]:
        import capo_rds_data._operations.rds_data_service.execute_statement

        output, http_response = (
            capo_rds_data._operations.rds_data_service.execute_statement.execute_statement(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("ExecuteStatement")
    )
    input_: capo_rds_data.types.execute_statement_request.ExecuteStatementRequest = {
        "resource_arn": resource_arn,
        "secret_arn": secret_arn,
        "sql": sql,
    }
    if database is not None:
        input_["database"] = database
    if schema is not None:
        input_["schema"] = schema
    if parameters is not None:
        input_["parameters"] = parameters
    if transaction_id is not None:
        input_["transaction_id"] = transaction_id
    if include_result_metadata is not None:
        input_["include_result_metadata"] = include_result_metadata
    if continue_after_timeout is not None:
        input_["continue_after_timeout"] = continue_after_timeout
    if result_set_options is not None:
        input_["result_set_options"] = result_set_options
    if format_records_as is not None:
        input_["format_records_as"] = format_records_as

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output