You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Support for multiple queries in a single prepared statement
10
10
description: |-
11
11
You can now have multiple queries in a single prepared statement when using `db.prepare`. To use this feature, separate each query with a semi-colon. Prepared statements with multiple queries only returns the results of the last query, even though all queries are executed. Additionally, you can only bind parameters to the last query in the prepared statement.
Copy file name to clipboardExpand all lines: src/content/docs/d1/get-started.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -532,5 +532,5 @@ In this tutorial, you have:
532
532
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the [Cloudflare Developers community on Discord](https://discord.cloudflare.com).
533
533
534
534
- See supported [Wrangler commands for D1](/workers/wrangler/commands/#d1).
535
-
- Learn how to use the [D1 Worker Binding API](/d1/worker-api/) within your Worker.
535
+
- Learn how to use the [D1 Worker Binding API](/d1/worker-api/) within your Worker, and test them from the [API playground](/d1/worker-api/#api-playground).
536
536
- Explore [community projects built on D1](/d1/reference/community-projects/).
You can execute queries on your D1 database through SQL query statements. To do this, you need to perform the following steps:
10
+
You can execute SQL queries on your D1 database from a Worker using the Worker Binding API. To do this, you can perform the following steps:
11
11
12
12
1.[Prepare a statement](/d1/worker-api/prepare-a-statement).
13
13
2.[Run the prepared statement](/d1/worker-api/run-a-statement).
@@ -17,9 +17,9 @@ Refer to the relevant sections for the API documentation.
17
17
18
18
## Typescript support
19
19
20
-
D1 Worker Bindings API is fully-typed via the `@cloudflare/workers-types` package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional _type parameter_ so that a function understands the type of the data it is handling.
20
+
D1 Worker Bindings API is fully-typed via the [`@cloudflare/workers-types`](/workers/languages/typescript/#typescript) package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional _type parameter_ so that a function understands the type of the data it is handling.
21
21
22
-
When using the [query statement methods](#query-statement-methods)`stmt.run()`, `stmt.raw()` and `stmt.first()`, you can provide a type representing each database row. D1's API will [return the result object](#return-object) with the correct type.
22
+
When using the [query statement methods](/d1/worker-api/run-a-statement)`stmt.run()`, `stmt.raw()` and `stmt.first()`, you can provide a type representing each database row. D1's API will [return the result object](#return-object) with the correct type.
23
23
24
24
For example, providing an `OrderRow` type as a type parameter to `stmt.run()` will return a typed `Array<OrderRow>` object instead of the default `Record<string, unknown>` type:
25
25
@@ -56,40 +56,55 @@ Replace the contents of your `index.js` file with the below to view the effect o
56
56
<Detailsheader="index.js"open={false}>
57
57
```js
58
58
exportdefault {
59
-
asyncfetch(request, env) {
60
-
const { pathname } =newURL(request.url);
61
-
62
-
constcompanyName1=`Bs Beverages`;
63
-
constcompanyName2=`Around the Horn`;
64
-
conststmt=env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
Copy file name to clipboardExpand all lines: src/content/docs/d1/worker-api/prepare-a-statement.mdx
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,12 +31,6 @@ const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bin
31
31
32
32
#### Guidance
33
33
34
-
- You can pass multiple queries into a single `.prepare()` statement. Simply delineate each query with a semi-colon.
35
-
- The statement only returns the results of the last query, even though all queries are executed.
36
-
- You can only bind parameters to the last query.
37
-
```js
38
-
conststmt=db.prepare(`SELECT * FROM users WHERE name = "Anthony"; SELECT * FROM users WHERE name = ?1`).bind("Joe")
39
-
```
40
34
- D1 follows the [SQLite convention](https://www.sqlite.org/lang_expr.html#varparam) for prepared statements parameter binding. Currently, D1 only supports Ordered (`?NNNN`) and Anonymous (`?`) parameters. In the future, D1 will support named parameters as well.
41
35
42
36
| Syntax | Type | Description |
@@ -64,6 +58,13 @@ const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bin
64
58
.bind(1, "Alfreds Futterkiste");
65
59
```
66
60
61
+
- You can pass multiple queries into a single `.prepare()` statement. Simply delineate each query with a semi-colon.
62
+
- The statement only returns the results of the last query, even though all queries are executed.
63
+
- You can only bind parameters to the last query.
64
+
```js
65
+
conststmt=db.prepare(`SELECT * FROM users WHERE name = "Anthony"; SELECT * FROM users WHERE name = ?1`).bind("Joe")
66
+
```
67
+
67
68
## Static statements
68
69
69
70
D1 API supports static statements. Static statements are SQL statements where the variables have been hard coded. When writing a static statement, you manually type the variable within the statement string.
- An array of objects containing the results of the `.db.prepare()` statements. Each object is in the array position corresponding to the array position of the initial `db.prepare()` statement within the `statementArray`.
298
-
-For more information on the returned object, refer to [Return objects](/d1/worker-api/return-object).
297
+
- An array of `D1Result`objects containing the results of the `.db.prepare()` statements. Each object is in the array position corresponding to the array position of the initial `db.prepare()` statement within the `statementArray`.
298
+
-Refer to [`D1Result`](/d1/worker-api/return-object/d1result) for more information about this object.
299
299
300
300
<Detailsheader="Example of return values"open={false}>
0 commit comments