@@ -471,6 +471,49 @@ select "id", "name", "age" from sq;
471
471
```
472
472
</Section >
473
473
474
+ You can also provide ` insert ` , ` update ` and ` delete ` statements inside ` with `
475
+
476
+ <Section >
477
+ ``` typescript copy
478
+ const sq = db .$with (' sq' ).as (
479
+ db .insert (users ).values ({ name: ' John' }).returning (),
480
+ );
481
+
482
+ const result = await db .with (sq ).select ().from (sq );
483
+ ```
484
+ ``` sql
485
+ with " sq" as (insert into " users" (" id" , " name" ) values (default, ' John' ) returning " id" , " name" )
486
+ select " id" , " name" from " sq"
487
+ ```
488
+ </Section >
489
+
490
+ <Section >
491
+ ``` typescript copy
492
+ const sq = db .$with (' sq' ).as (
493
+ db .update (users ).set ({ age: 25 }).where (eq (users .name , ' John' )).returning (),
494
+ );
495
+ const result = await db .with (sq ).select ().from (sq );
496
+ ```
497
+ ``` sql
498
+ with " sq" as (update " users" set " age" = 25 where " users" ." name" = ' John' returning " id" , " name" , " age" )
499
+ select " id" , " name" , " age" from " sq"
500
+ ```
501
+ </Section >
502
+
503
+ <Section >
504
+ ``` typescript copy
505
+ const sq = db .$with (' sq' ).as (
506
+ db .delete (users ).where (eq (users .name , ' John' )).returning (),
507
+ );
508
+
509
+ const result = await db .with (sq ).select ().from (sq );
510
+ ```
511
+ ``` sql
512
+ with " sq" as (delete from " users" where " users" ." name" = $1 returning " id" , " name" , " age" )
513
+ select " id" , " name" , " age" from " sq"
514
+ ```
515
+ </Section >
516
+
474
517
To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query,
475
518
you need to add aliases to them:
476
519
``` typescript copy
0 commit comments