mithrandie / csvq

SQL-like query language for csv
https://mithrandie.github.io/csvq
MIT License
1.5k stars 65 forks source link

Set SQL variables on the csvq command line. #20

Closed derekmahar closed 4 years ago

derekmahar commented 4 years ago

Please add a csvq option for setting an SQL variable on the command line. This would be particularly useful for SQL queries specified using the --source option. For example, the command

$ csvq --source select-query.sql --var a=1 --var b=2

would define SQL variables @a and @b and assign them the values 1 and 2, respectively, and would be equivalent to csvq executing the statement VAR @a := 1, @b := 2; before executing SQL source select-query.sql. SQL source file select-query.sql could then refer to these variables:

SELECT @a AS a, @b AS b;

The earlier csvq example command would be equivalent to the query

VAR @a := 1, @b := 2;
SELECT @a AS a, @b AS b;
derekmahar commented 4 years ago

Nevermind. I just learned that one can use environment variables to achieve the same behaviour:

$ a=1 b=2 csvq "SELECT @%a AS a, @%b AS b"
+---+---+
| a | b |
+---+---+
| 1 | 2 |
+---+---+