usql icon indicating copy to clipboard operation
usql copied to clipboard

variable assignment while NOT in interactive mode

Open kishorerathi opened this issue 6 years ago • 3 comments

I need to run a script using -f option in non-interactive mode at command line. This script needs to have some value for query filters. I tried the following

driverFile.sql \prompt filterValue 'Filter Value for Query ' \i actualSQL.sql

actualSQL.sql select count(*) from someTable where filterColumn = :'filterValue'

And when I execute it, following error is generated error: not interactive

How do I use a variable and make script execution more dynamic using usql with -f and -o command line arguments?

kishorerathi avatar Jun 13 '18 15:06 kishorerathi

You're using \prompt (not possible in non-interactive mode). Use \set instead. If you need to pass values before the script is executed, then you can use usql --set NAME=VALUE:

$ usql --set FOO=BAR
Type "help" for help.

(not connected)=> \set
FOO = 'BAR'
(not connected)

kenshaw avatar Jun 13 '18 18:06 kenshaw

So -- an update, and my apologies for closing this previously:

I checked the behavior against psql, and psql definitely allows \prompt to work in non-interactive terminals. Although this seems counter-intuitive to me, usql endeavors to be compatible with psql -- as such, I will fix this behavior.

However, the issue is that the readline package usql is using makes it difficult to set / enable this on scripts, due to the way the various packages/components for usql relate to each other. I will make it a point to have this fixed in the next major release, which should also bring in many more compatibility fixes with psql, such as proper formatting, pipe, pager, and other support.

kenshaw avatar Jun 16 '18 05:06 kenshaw

Hello,

I'm trying usql as a non interactive tool for sql extraction.

Is this issue solved?

I tried running my sql file along with the --set option but my variable isn't replaced : usql --file test.sql --set=foo=bar --csv oracle://stuff

where test.sql is :

SELECT ':foo' FROM dual;

The output is simply ":foo".

EDIT : Nevermind, my syntax was wrong, :'foo' works great! 👍

jgoux avatar Dec 02 '19 10:12 jgoux