Skip to content

Commit 127329e

Browse files
committed
Add Use Variables In An Anonymous Function as a Postgres TIL
1 parent 5a8b381 commit 127329e

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1323 TILs and counting..._
13+
_1324 TILs and counting..._
1414

1515
---
1616

@@ -740,6 +740,7 @@ _1323 TILs and counting..._
740740
- [Use Argument Indexes](postgres/use-argument-indexes.md)
741741
- [Use Not Valid To Immediately Enforce A Constraint](postgres/use-not-valid-to-immediately-enforce-a-constraint.md)
742742
- [Use Rename To Hot Swap Two Tables](postgres/use-rename-to-hot-swap-two-tables.md)
743+
- [Use Variables In An Anonymous Function](postgres/use-variables-in-an-anonymous-function.md)
743744
- [Using Expressions In Indexes](postgres/using-expressions-in-indexes.md)
744745
- [Using Intervals To Offset Time](postgres/using-intervals-to-offset-time.md)
745746
- [Who Is The Current User](postgres/who-is-the-current-user.md)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Use Variables In An Anonymous Function
2+
3+
I was curious how variables could be declared and used in PostgreSQL, so I did
4+
a little experiment with a `books` and `authors` schema.
5+
6+
Variables need to be declared and used within the context of a function. Using
7+
the [`do` syntax](https://www.postgresql.org/docs/9.1/sql-do.html) I am able to
8+
declare and execute an anoymous code block.
9+
10+
Within that code block I can declare one or more variables by giving them a
11+
type and optionally a default value. Below I declare `author_id` with a default
12+
and `result` as a `record` type.
13+
14+
```sql
15+
do $$
16+
declare
17+
author_id varchar := 'e2b42ebf-7ea9-4d9e-8edf-310fc1894bcd';
18+
result record;
19+
begin
20+
for result in select title from books where "authorId" = author_id
21+
loop
22+
raise notice '| % |', result.title;
23+
end loop;
24+
end $$;
25+
```
26+
27+
I'm able to use the `author_id` variable directly in a `select` statement.
28+
29+
```sql
30+
select title from books where "authorId" = author_id
31+
```
32+
33+
and then using a [for
34+
loop](https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING)
35+
with my `result` of `record` type, I can iterate over each of the results from
36+
the `select`.
37+
38+
Because this anonymous `do` block implicitly has a `void` return type, I need
39+
to do something with the result within the block. For demonstration purposes, I
40+
use [`raise
41+
notice`](https://www.postgresql.org/docs/current/plpgsql-errors-and-messages.html)
42+
to log out each book title.

0 commit comments

Comments
 (0)