# Using variables in the dbt model

**URL:** <https://discuss.dataengineercafe.io/t/using-variables-in-the-dbt-model/198>\
**Category:** dbt\
**Tags:** sql, database\
**Created:** [April 19, 2022, 12:52am UTC](https://discuss.dataengineercafe.io/t/using-variables-in-the-dbt-model/198 "2022-04-19T00:52:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![atb](https://yyz1.discourse-cdn.com/flex035/user_avatar/discuss.dataengineercafe.io/atb/32/8_2.png) [@atb](https://discuss.dataengineercafe.io/u/atb)\
**Post date:** [April 19, 2022, 12:52am UTC](https://discuss.dataengineercafe.io/t/using-variables-in-the-dbt-model/198/1 "2022-04-19T00:52:23Z")

</div>

การเอา value มาสร้างเป็น variable เป็นการช่วยสื่อความหมายของ value นั้น ๆ ให้ชัดเจนมากขึ้น แถมยังช่วยให้ query อ่านง่าย เป็นระเบียบ สามารถเรียกใช้ value นั้นซ้ำได้หลายจุด และยังสามารถปรับเปลี่ยน value นั้น ๆ ได้โดยง่ายอีกด้วย

นี่คือการใช้ variables ใน dbt model ทั้ง 2 วิธีที่ผมใช้อยู่ครับ 😉

# 1. Var

กำหนด variable ไว้ในไฟล์ `dbt_project.yml` หรือกำหนดผ่าน `command line` แล้วเรียกใช้ผ่าน `{{ var(...) }}` วิธีนี้ variables จะเข้าถึงได้แบบ global ทั้งใน models, tests และอื่น ๆ ช่วยให้อ้างอิง variable จากหลาย ๆ components ได้อย่างมีประสิทธิภาพ

**🌵 ตัวอย่าง** การกำหนด variable ในไฟล์ [dbt\_project.yml](https://docs.getdbt.com/reference/dbt_project.yml)

```yaml
name: my_dbt_project
version: 1.0.0

config-version: 2

# define variables here
vars:
  event_type: activation

```

สามารถกำหนด [scope](https://docs.getdbt.com/docs/building-a-dbt-project/building-models/using-variables#defining-variables-in-dbt_projectyml) ได้เพิ่มเติม

**🌵 ตัวอย่าง** การกำหนด variable ผ่าน [command line](https://docs.getdbt.com/docs/building-a-dbt-project/building-models/using-variables#defining-variables-on-the-command-line)

วิธีนี้เหมาะสำหรับ value ที่เปลี่ยนแปลงบ่อย ๆ เช่น date ranges

```sh
$ dbt run --vars '{"event_type": "activation"}'

```

การใช้ `--vars` ป็นการ override value ที่กำหนดไว้ใน `dbt_project.yml` ด้วย

**🌵 ตัวอย่าง** การใช้งาน

```sql
SELECT
  *
FROM
  events
WHERE
  event_type = '{{ var("event_type") }}'

-- default values
SELECT
  *
FROM
  events
WHERE
  event_type = '{{ var("event_type", "registration") }}'

```

# 2. Set

ใช้ `{% set ... %}` เพื่อกำหนด variables ไว้ด้านบนของ model ลักษณะคล้าย programming languages และเรียกใช้ผ่าน `{{ ... }}` วิธีนี้ variables จะเข้าถึงได้เฉพาะ model นั้น ๆ

**🌵 ตัวอย่าง** การใช้งาน

```sql
{% set event_type = 'activation' %}

SELECT
  *
FROM
  events
WHERE
  event_type = '{{ event_type }}'

```

* * *

ส่วนเรื่อง Environment variables น่าจะไม่ค่อยได้ใช้ที่ models สามารถศึกษาเพิ่มเติมได้ที่นี่เลยครับ

> **[Environment variables | dbt Developer Hub](https://docs.getdbt.com/docs/build/environment-variables)**
>
> Use environment variables to customize the behavior of your dbt project.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/dataengineercafe/original/1X/2420f06eb670b4e614f3ccc146c6659481af8e26.jpeg)

**References**

- [Project variables | dbt Developer Hub](https://docs.getdbt.com/docs/building-a-dbt-project/building-models/using-variables)
- [Use Jinja to improve your SQL code | dbt Developer Hub](https://docs.getdbt.com/tutorial/using-jinja#set-variables-at-the-top-of-a-model)

---

<div class="post-metadata">

**Author:** ![zkan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discuss.dataengineercafe.io/zkan/32/2_2.png) [@zkan](https://discuss.dataengineercafe.io/u/zkan)\
**Post date:** [April 19, 2022, 5:01am UTC](https://discuss.dataengineercafe.io/t/using-variables-in-the-dbt-model/198/2 "2022-04-19T05:01:23Z")

</div>

ความฮาอยู่ตรง meme นี่แหละ 🤣
