r/SQL • • Aug 12 '26

Discussion Shebang equivalent for SQL dialects?

When writing shell scripts it's common to have on line 1

#!/bin/bash

which would differentiate it from

#!/bin/sh

I have a lot of SQL saved in text files — all .sql extension — for processing CSVs at work; these are all in-memory instances that ingest CSVs and output CSVs. For many years I only used SQLite so I only had to remember sqlite3 < query.sql.

Recently, I started writing for DuckDB as well, initially only because of its ability to load JSON/CSV from a URL; I'm also quickly seeing how advantageous its extra functions are.

Is there any shebang-style line that I can start my files with, or do I just need to write a freetext comment like this?

-- This is for DuckDB
12 Upvotes

16 comments sorted by

2

u/jynus Aug 12 '26

Not really an SQL question. But I think you are confusing 2 things:

* "what application should open this type of files by default". Which is done mostly for desktop assiociations like based on the extension (windows) o the mime-type: freedesktop on Linux (xdg-open); and:
* "how should I run this executable/which interpreter I should use", which is what the shebang is, to be used for not only shell scripts but also interpreted languages like perl and python.

You certainly can do the first, but it is not intended for the second, as you don't "execute" .sql, you send it normally to the standard input of a client.

You can setup a personal standard like the intended client and version of an export like you say as part of the comment; or setup your own helpers on the command line like aliases to make your life easier.

1

u/deuvisfaecibusque Aug 12 '26

Thanks.

Point 1 is not really what I'm thinking of.

Point 2 is more the issue: I don't want to forget which dialect I was writing for and have to do detective work on years-old files; I was wondering if there was a "best practice"/standard way to indicate this.

1

u/Temporary_Pie2733 Aug 12 '26

You can try to avoid dialect-specific features, and stick to POSIX-specific SQL, or use SQL libraries in other programming languages (rather than raw SQL) that hide dialect-specific features from view. 

1

u/deuvisfaecibusque Aug 13 '26

Thanks.

Part of the point of using DuckDB, though, is that it has a much nicer feature set for my analysis than SQLite. I appreciate it limits the portability of my work but for my use case that's completely fine.

As for e.g. wrapping it all in Python, that's something I need to do when working with SQLite to download from a URL and then load it into a table; but given that DuckDB allows loading from a URL directly, I saw the opportunity to make everything much leaner.

5

u/Lumethys Aug 12 '26

getAllProducts.postgres.sql

getAllProducts.sqlite.sql

9

u/HazirBot Aug 12 '26

id suffix them differently .duck.sql

9

u/RevolutionaryRush717 Aug 12 '26

#!/usr/bin/env -S psql -X postgresql://user:password@localhost/mydb

SELECT now();

For PostgreSQL.

2

u/zbignew Aug 12 '26

Woo! An actual answer!

I wouldn’t do this, though. SQL needs more than dialect to run (which you solved) but connection strings need to vary and include secrets that can’t go in source control.

So the best solution is to write scripts that manage switching connections as needed for each variant, and connect to the appropriate databases, and refer to passwords/secrets stored in ~/.dotfiles or a secret manager.

Then you can have project/duck/run-qa.sh and pass in the sql file as an argument to run it against a QA system.

Setting up infra like this used to be so annoying because you never knew if it was going to be worth the time to write and test it. Now with LLMs, it takes so little time to set up, and if you find you’ve shoehorned yourself into the wrong workflow, you can fix it later in another 5 minutes.

And if you document the test scripts in AGENTS.md, you won’t need to constantly remind your LLMs how to check their own work.

4

u/RevolutionaryRush717 Aug 12 '26

psql is completely configurable by environment variables.

No need to commit anything anywhere.

I suggest dotenv convention, as it works nicely here.

1

u/asp174 Aug 12 '26

you could do something like :

#!/bin/bash
tail +3 $0 | my_db_app -u user mydb; exit

select 
  * 
from
  my_table

or a little more sophisticated version

#!/bin/bash

LINENR=$(awk '/#### payload below ####$/ { print NR + 1; exit 0; }' $0)

tail +$LINENR $0 | my_db_app -u user mydb
exit;

#### payload below ####
select 
  * 
from
  my_table

1

u/deuvisfaecibusque Aug 12 '26

Thanks.

I have in some cases written a simple generate.bash to live alongside the .sql file:

#!/bin/bash
sqlite3 < runme.sql

which seems to achieve a similar result, at the cost of having to manage 2 files instead of 1.

2

u/TheTeamDad Aug 12 '26

Here Docs would simplify this even further.

1

u/asp174 Aug 12 '26

Not really. With tail you just throw the rest of the file at it. With a heredoc you have to end the block with a delimiter.

That's why this approach is also used for those nasty install.sh with embedded .tgz files.

2

u/RonJohnJr Aug 12 '26

Modelines (or something similar) might be worthwhile. They are, though, conceptually similar to what you're doing.

Barring that, I'd change the comment to something like:

/*
  Target DB: DuckDB
*/

This lets you also add Written By, Version, Date, etc.

2

u/vintagegeek Aug 12 '26

she bang he bang we bang

...I'm so sorry....