# Load data with Raw SQL statement

**URL:** <https://forum.elixirforum.com/t/load-data-with-raw-sql-statement/63312>\
**Category:** Questions\
**Tags:** ash, ash-postgres\
**Created:** [May 4, 2024, 10:44am UTC](https://forum.elixirforum.com/t/load-data-with-raw-sql-statement/63312 "2024-05-04T10:44:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mudspot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mudspot/32/4262_2.png) [@mudspot](https://forum.elixirforum.com/u/mudspot)\
**Post date:** [May 4, 2024, 10:44am UTC](https://forum.elixirforum.com/t/load-data-with-raw-sql-statement/63312/1 "2024-05-04T10:44:45Z")

</div>

Hi,

Given an AshResource, how do I load data using raw sql statements?

PS. Use case: We have an existing sql load script that has complicated filter conditions what we don’t want to spend time rewriting in Ash Query

Best regards  
Terry

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [May 4, 2024, 11:06am UTC](https://forum.elixirforum.com/t/load-data-with-raw-sql-statement/63312/2 "2024-05-04T11:06:32Z")

</div>

Are you looking to supply _all_ of the data from the query? i.e “run a query, return list of resources” from that result? Or is it a calculation, like a SQL condition that should be run per-record? (i.e fragments, if you’re familiar with them in ecto.

---

<div class="post-metadata">

**Author:** ![mudspot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mudspot/32/4262_2.png) [@mudspot](https://forum.elixirforum.com/u/mudspot)\
**Post date:** [May 4, 2024, 12:28pm UTC](https://forum.elixirforum.com/t/load-data-with-raw-sql-statement/63312/3 "2024-05-04T12:28:28Z")

</div>

I’m looking to supply all the data from the query. Basically I would like to do something like

```elixir
users = MyResource.User
|> Ash.Query.... set_sql("SELECT * FROM users WHERE <legacy_complicated_condition>")
|> MyResource.read!

for user <- users, do: something(user)

```

PS. `set_sql` is the imaginary function I am looking for. (?)

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [May 4, 2024, 2:27pm UTC](https://forum.elixirforum.com/t/load-data-with-raw-sql-statement/63312/4 "2024-05-04T14:27:13Z")

</div>

The way that you would implement this would be with a manual action:

```elixir
read :your_legacy_thing do
  manual fn _ash_query, ecto_query, context -> 
    # my suggestion, in order to get the most bang for your buck, is to use Ecto
    # here, and *join* to the ecto_query we provide.
  end
end

```

If you’re familiar with Ecto, this should be doable. You can also use `YourRepo.query!` to run raw SQL, but you will have two major drawbacks of this:

1. you will lose whatever logic Ash already did to honor the original request
2. you will need to post-process the results into records (not that hard, typically just `Enum.map(…, struct!(Resource, &1))
