# SQL and Macros

**URL:** https://discourse.julialang.org/t/sql-and-macros/124673
**Category:** General Usage
**Tags:** macros
**Created:** [January 11, 2025, 12:25pm UTC](https://discourse.julialang.org/t/sql-and-macros/124673 "2025-01-11T12:25:11Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [January 11, 2025, 12:25pm UTC](https://discourse.julialang.org/t/sql-and-macros/124673/1 "2025-01-11T12:25:11Z")

</div>

I have the following script that runs fine:

```julia
using DuckDB
using DBInterface
using DataFrames

# Global connection variable for reuse
const duckdb_connection = Ref{Union{DuckDB.DB, Nothing}}(nothing)

# Function to manage the connection
function get_duckdb_connection()
    if isnothing(duckdb_connection[])
        duckdb_connection[] = DBInterface.connect(DuckDB.DB, ":memory:")
    end
    return duckdb_connection[]
end

# SQL macro that accepts a string literal
macro SQL1(query::String)
    return quote
        try
            con = get_duckdb_connection()
            DBInterface.execute(con, $query)
        catch err
            println("Error executing SQL: ", err)
            throw(err)
        end
    end
end

# Execute statements individually
@SQL1 "CREATE TABLE sample (id INTEGER, name TEXT, gender TEXT)"
@SQL1 "INSERT INTO sample VALUES (1, 'Alice', 'F'), (2, 'Bob', 'M'), (3, 'Charlie', 'M')"
result = @SQL1 "SELECT * FROM sample"
@show DataFrame(result)

```

However, I would like to have sequences of SQL statements inside a `begin ... end` block without quotes, something like:

```julia
result = @SQL2 begin
    CREATE TABLE sample (id INTEGER, name TEXT);
    INSERT INTO sample VALUES (1, 'Alice', 'F'), (2, 'Bob', 'M'), (3, 'Charlie', 'M');
    SELECT * FROM sample;
end

```

Would this be possible and if so, I wonder if there is a simple way to achieve this (e.g. without custom parsers)? Thank you!

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [January 11, 2025, 3:59pm UTC](https://discourse.julialang.org/t/sql-and-macros/124673/2 "2025-01-11T15:59:21Z")

</div>

> [@askvorts](#):
>
> ```julia
> begin
> CREATE TABLE sample (id INTEGER, name TEXT);
> INSERT INTO sample VALUES (1, 'Alice', 'F'), (2, 'Bob', 'M'), (3, 'Charlie', 'M');
> SELECT * FROM sample;
> end
> 
> ```

This is not valid Julia source code (immediate `ParseError`), so no, a macro can’t transform this. If you want different syntax, you have to start from a string, maybe multiple if easier. `SQL1` doesn’t do any transformations that needs a macro so far, you’re basically inlining the `try-catch` code.
