I have a somewhat detailed query in a script that uses ?
placeholders. I wanted to test this same query directly from the psql command line (outside the script). I want to avoid going in and replacing all the ?
with actual values, instead I'd like to pass the arguments after the query.
Example:
SELECT * FROM foobar WHERE foo = ? AND bar = ? OR baz = ? ;
Looking for something like:
%> {select * from foobar where foo=? and bar=? or baz=? , 'foo','bar','baz' };
Another easiest and most used way to run any SQL file in PostgreSQL is via its SQL shell. Open the SQL shell from the menu bar of Windows 10. Add your server name, database name where you want to import the file, the port number you are currently active on, PostgreSQL username, and password to start using SQL shell.
<> is the standard SQL operator meaning "not equal". Many databases, including postgresql, supports != as a synonym for <> . They're exactly the same in postgresql.
You can use the -v construct e.g
psql -v v1=12 -v v2="'Hello World'" -v v3="'2010-11-12'"
and then refer to the variables in sql as :v1, :v2 etc
select * from table_1 where id = :v1;
Please pay attention on how we pass string/date value using two quotes " '...' "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With