Run a query with named parameters and provided parameter types.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication.md.txt#client-libs).

```java
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.QueryParameterValue;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.TableResult;

// Sample to run query with named types parameters.
public class QueryWithNamedTypesParameters {

  public static void main(String[] args) {
    String[] words = {"and", "is", "the", "moon"};
    String corpus = "romeoandjuliet";
    Integer wordsCount = 250;
    String query =
        "SELECT word, word_count"
            + " FROM `bigquery-public-data.samples.shakespeare`"
            + " WHERE word IN UNNEST(@wordList)"
            + " AND corpus = @corpus"
            + " AND word_count >= @minWordCount"
            + " ORDER BY word_count DESC";
    queryWithNamedTypesParameters(query, words, corpus, wordsCount);
  }

  public static void queryWithNamedTypesParameters(
      String query, String[] words, String corpus, Integer wordsCount) {
    try {
      // Initialize client that will be used to send requests. This client only needs to be created
      // once, and can be reused for multiple requests.
      BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

      QueryParameterValue wordList = QueryParameterValue.array(words, StandardSQLTypeName.STRING);
      QueryParameterValue corpusParam = QueryParameterValue.of(corpus, StandardSQLTypeName.STRING);
      QueryParameterValue minWordCount =
          QueryParameterValue.of(wordsCount, StandardSQLTypeName.INT64);

      // Note: Standard SQL is required to use query parameters.
      QueryJobConfiguration queryConfig =
          QueryJobConfiguration.newBuilder(query)
              .addNamedParameter("wordList", wordList)
              .addNamedParameter("corpus", corpusParam)
              .addNamedParameter("minWordCount", minWordCount)
              .build();

      TableResult results = bigquery.query(queryConfig);

      results
          .iterateAll()
          .forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));

      System.out.println("Query with named types parameters performed successfully.");
    } catch (BigQueryException | InterruptedException e) {
      System.out.println("Query not performed \n" + e.toString());
    }
  }
}
```

### Rust

```rust
use google_cloud_bigquery::client::BigQuery;
use google_cloud_bigquery_v2::model::{QueryParameter, QueryParameterType, QueryParameterValue};

pub async fn sample(project_id: &str) -> anyhow::Result<()> {
    let client = BigQuery::builder().build().await?;

    let mut rows = client
        .query(
            "SELECT \
        @date_param AS date_col, \
        @numeric_param AS num_col, \
        @bool_param AS bool_col;",
        )
        .with_project_id(project_id)
        .set_parameter_mode("NAMED")
        .set_query_parameters([
            QueryParameter::new()
                .set_name("date_param")
                .set_parameter_type(QueryParameterType::new().set_type("DATE"))
                .set_parameter_value(QueryParameterValue::new().set_value("2026-01-15")),
            QueryParameter::new()
                .set_name("numeric_param")
                .set_parameter_type(QueryParameterType::new().set_type("NUMERIC"))
                .set_parameter_value(QueryParameterValue::new().set_value("12345.67")),
            QueryParameter::new()
                .set_name("bool_param")
                .set_parameter_type(QueryParameterType::new().set_type("BOOL"))
                .set_parameter_value(QueryParameterValue::new().set_value("true")),
        ])
        .set_location("US")
        .until_done()
        .await?
        .read();

    while let Some(row) = rows.next().await.transpose()? {
        let date_col: String = row.get("date_col")?;
        let num_col: String = row.get("num_col")?;
        let bool_col: bool = row.get("bool_col")?;
        println!("Date: {date_col}, Numeric: {num_col}, Bool: {bool_col}");
    }
    Ok(())
}
```

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples.md.txt?product=bigquery).