Don't understand where is syntax error ? PostgreSQL 11 - sql

PostgreSQL version : 11.1
Platform : OSX Mojave 10.14.1
That's my SQL code:
COPY (select nom,prenom,num_carte,pdv_carte,email,date_naissance from compte where num_carte != '' order by id_compte) TO :export_file WITH DELIMITER AS ';' CSV FORCE QUOTE * ENCODING 'UTF-8';
That line is in a .sql file called by shell script like this :
psql --dbname=test -U postgres --set adresses=$DATA_ADRESSE --set export_file="$EXPORT_FILE" --file=$ANO_SQL 1>>$ANO_LOG
With EXPORT_FILE variable declared like that :
export EXPORT_FILE=‎⁨"'export_for_fid.csv'"
Tried many solutions but none worked, always the same syntax error:
ERROR: syntax error at or near "‎⁨"
LINE 1: ...where num_carte != '' order by id_compte) TO ‎⁨'export_for_fid.csv' WITH D...
^

Instead of using --file and --set arguments, you could use a here-document in your shell script: (NOTE: I replaced the COPY by a \COPY )
#!/bin/sh
EXPORT_FILE="export_for_fid.csv"
DB_NAME="test"
psql --dbname=${DB_NAME} -U postgres <<OMG
\COPY (select nom,prenom,num_carte,pdv_carte,email,date_naissance from compte where num_carte <> '' order by id_compte) TO '${EXPORT_FILE}' WITH DELIMITER AS ';' CSV FORCE QUOTE * ENCODING 'UTF-8';
OMG
#eof

Related

Error ZeosLib Open Query character $ in string query

When executing a query string with the $ character, example 'select id from table where fieldname = 'abc$', it is giving error , preparade statament does not exists
Zeos Lib 7.2
Postgresql 9.6

Why is my script not printing output on one line?

This is an image of what I'm asking for
I am using the following -echo- in a script and after I execute, the output format is as shown below:
`echo -e "UPDATE table1 SET table1_f1='$Fname' ,table1_f2='$Lname' where table1_f3='$id';\ncommit;" >> $OutputFile`
output: UPDATE table1 SET table1_f1='Fname' ,table1_f2='Lname' where table1_f3='id ';
the '; is appearing on a new line, why is that happening?
The variable $id in your shell script actually contains that newline (\n or \r\n) at the end; so there isn't really anything wrong in the part of the script you've shown here.
This effect is pretty common if the variable is created based on external commands (update:) or by reading external files as you are here.
For simple values, one way to strip the newline off the end of the value, prior to using it in your echo is:
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
or for scripts that already rely on a particular bash IFS value:
OLDIFS="${IFS}";
IFS=$'\n\t ';
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
IFS="${OLDIFS}";

Postgres copy to TSV file with header

I have a function like so -
CREATE
OR REPLACE FUNCTION ind (bucket text) RETURNS table (
middle character varying (100),
last character varying (100)
) AS $body$ BEGIN return query
select
fname as first,
lname as last
from all_records
; END;
$body$ LANGUAGE PLPGSQL;
How do I output the results of select ind ('Mob') into a tsv file?
I want the output to look like this -
first last
MARY KATHERINE
You can use the COPY command
example:
COPY (select * from ind('Mob')) TO '/tmp/ind.tsv' CSV HEADER DELIMITER E'\t';
the file '/tmp/ind.tsv' will contain you data
Postgres doesn't allow copy with header for tsv for some reason.
If you're using a linux based system you can do it with a script like this:
#create file with tab delimited column list (use \t between each column name)
echo -e "user_id\temail" > user_output.tsv
#now you can append the results of your query to that file by copying to STDOUT
psql -h your_host_name -d your_database_name -c "\copy (SELECT user_id, email FROM my_user_table) to STDOUT;" >> user_output.tsv
Alternatively, if your script is long and you don't want to pass it in with -c command you can use the same approach from a .sql file, use "--quiet" to avoid notices being passed into your file
psql --quiet -h your_host_name -d your_database_name -f your_sql_file.sql >> user_output.tsv

Hive {Case..when} is not recognizing the hiveconf variable passed from shell script

I am getting below error while running hive query.
FAILED: ParseException line 36:106 cannot recognize input near '$' '{' 'hiveconf' in expression specification
Here is my requirement:
I have two roll_condition..
if roll_cond=1 then execute something
if roll_cond=2 then execute something
Trying to achieve this with the below code
INSERT OVERWRITE TABLE db.tab_name partition(yr,mon)
select xxxx,xxx,xx,xx
from db.tab_name
left join db.name
on condition
where case when ${hiveconf:roll_cond}=1 then yr = case
when ${hiveconf:refresh_yr} <> 0 then ${hiveconf:refresh_yr}
else year(current_date) end;
when ${hiveconf:roll_cond}=2 then condition end;
So when I run the script with '1' it ran fine but when I give '2' it is giving me above mentioned error.
Please suggest where the code is failing?
Thanks in advance.

Escaping special character when SQL output to variable in shell script

Trying to assign output from SQL query on an object containing special characters to a variable in shell script.
Running directly on the database:
db2 -x 'select count(*) from <SCHEMA>."/BIC/TEST"'
11000
Yet when I include this in script I need to use double quotes as I am using variables passed into the sql. Using single quotes
Output=$(db2 -x 'select count(*) from ${_SCHEMA}."/BIC/TEST"')
echo -e "$Output"
Results in:
SQL20521N Error occurred processing a conditional compilation directive near
"_". Reason code="7". SQLSTATE=428HV
When I use double quotes I hit:
SQL0104N An unexpected token "'/BIC/TEST'" was found following "ount(*)
Tried to escape the double quotes using another set of double quotes:
db2 -x 'select count(*) from ${_SCHEMA}.""/BIC/TEST""'
But this doesn't seem to work in script. It works for tables where there is no special characters/requirement to encase in quotations.
Any help is appreciated.
The code below works fine for me, notice the escaped quotes. If it fails for you, you need to give more details of your DB2-version and the DB2-server operating system platform.
#!/bin/ksh
db2 connect to sample
(($? > 0 )) && print "Failed to connect to database" && exit 1
db2 -o- "drop table \"/bin/test\" "
db2 -v "create table \"/bin/test\"(a integer)"
(($? > 0 )) && print "Create table failed" && exit 1
db2 -v "insert into \"/bin/test\"(a) values(1),(2),(3),(4)"
(($? > 0 )) && print "insert rows failed" && exit 1
db2 -v describe table \"/bin/test\"
typeset -i count_rows=$(db2 -x "select count(*) from \"/bin/test\"" )
(($? > 0 )) && print "query count rows failed" && exit 1
print "\nRow count is: ${count_rows}\n"
db2 -o- connect reset