The following UNLOAD:
UNLOAD ('SELECT "Field1" from "XX"."XX" where "Field1" = \'String\';')
TO 'XXX'
IAM_ROLE 'XXX'
DELIMITER AS ','
ADDQUOTES
ESCAPE
NULL AS ''
MANIFEST
causes the error:
java.sql.SQLException: [Amazon](500310) Invalid operation: unterminated quoted string at or near "'SELECT "Field1" from "XX"."XX" where "Field1" = \'String\'";
It seems to be the combination of the escaped single quotes and the semicolon. Since this works:
UNLOAD ('SELECT "Field1" from "XX"."XX" where "Field1" = \'String\'')
TO 'XXX'
IAM_ROLE 'XXX'
DELIMITER AS ','
ADDQUOTES
ESCAPE
NULL AS ''
MANIFEST
and this works:
UNLOAD ('SELECT "Field1" from "XX"."XX" where "Field1" = "Field2";')
TO 'XXX'
IAM_ROLE 'XXX'
DELIMITER AS ','
ADDQUOTES
ESCAPE
NULL AS ''
MANIFEST
I was wondering if anyone has seen anything about this. Haven't found the answer via https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html
Related
psql version 9.2, server version 12.0.
Trying to pass parameter to sql script ,
Executing the script and passing parameter :
psql --echo-queries -v ON_ERROR_STOP=0 -v TEST=100 postgresql://${curr_connection}<< EOF 1 > ${log_file} 2>&1
\timing
\i sql_script.sql
EOF
Please note I confirm script is working with no issue when using hardcoded value(without the parameter) ,
I guess it may relate that the parameter inside a quote or somthing,
sql_script.sql
INSERT INTO ACT_HI_COMMENT (action_,full_msg_,id_,message_,proc_inst_id_,task_id_,time_,type_,user_id_)
SELECT * FROM DBLINK ('host= MYHOST user=MYUSER password = MYPASS dbname=MYDB port=6432',
'SELECT ACT_HI_COMMENT.message_,ACT_HI_COMMENT.proc_inst_id_ FROM ACT_HI_COMMENT
where ACT_HI_COMMENT.TASK_ID_ in (Select distinct CBAN_A.TASK_ID_ from CBAN_OSS_ACT_RU_VARIABLE_TASK
where CBAN_OSS_ACT_RU_VARIABLE_TASK.GROUP_ID = :TEST )')
AS LINKTABLE (proc_inst_id_ character varying,task_id_ character varying,time_ timestamp without time zone,type_ character varying,
user_id_ character varying);
Getting error:
psql:/infadmin/inf/sql_script.sql:2: ERROR: syntax error at or near ":"
CONTEXT: while executing query on unnamed dblink connection
I tried also a single quotes around it as :'TEST'
INSERT INTO ACT_HI_COMMENT (action_,full_msg_,id_,message_,proc_inst_id_,task_id_,time_,type_,user_id_)
SELECT * FROM DBLINK ('host= MYHOST user=MYUSER password = MYPASS dbname=MYDB port=6432',
'SELECT ACT_HI_COMMENT.message_,ACT_HI_COMMENT.proc_inst_id_ FROM ACT_HI_COMMENT
where ACT_HI_COMMENT.TASK_ID_ in
(Select distinct CBAN_A.TASK_ID_ from CBAN_OSS_ACT_RU_VARIABLE_TASK
where CBAN_OSS_ACT_RU_VARIABLE_TASK.GROUP_ID = :'TEST' )')
AS LINKTABLE (proc_inst_id_ character varying,task_id_ character varying,time_ timestamp without time zone,type_ character varying,user_id_ character varying);
Getting error:
psql:/infadmin/inf/sql_script.sql:2: ERROR: syntax error at or near "TEST"
LINE 2: ... where CBAN_OSS_ACT_RU_VARIABLE_TASK.GROUP_ID = :'TEST' ..
As suspect , it didnt worked as the parameter is inside the single quotes,
I solved it by spliting the whole string inside the quotes into 2 ,
by
' first_part '|| :'TEST'||' <second_part>'
So sql looks like
INSERT INTO ACT_HI_COMMENT (action_,full_msg_,id_,message_,proc_inst_id_,task_id_,time_,type_,user_id_)
SELECT * FROM DBLINK ('host= MYHOST user=MYUSER password = MYPASS dbname=MYDB port=6432',
'SELECT ACT_HI_COMMENT.message_,ACT_HI_COMMENT.proc_inst_id_ FROM ACT_HI_COMMENT
where ACT_HI_COMMENT.TASK_ID_ in
(Select distinct CBAN_A.TASK_ID_ from CBAN_OSS_ACT_RU_VARIABLE_TASK
where CBAN_OSS_ACT_RU_VARIABLE_TASK.GROUP_ID = '|| :'TEST'||' )')
AS LINKTABLE (proc_inst_id_ character varying,task_id_ character varying,time_ timestamp without time zone,type_ character varying,user_id_ character varying);
``
I use below command to export table to csv
COPY (
SELECT
*
FROM
table_name
TO '/data/test/test.csv' WITH CSV DELIMITER ',' HEADER;
I used it in Postgres function but i cannot pass string to file path
CREATE OR REPLACE FUNCTION backup (filePath CHARACTER VARYING)
RETURNS void
LANGUAGE SQL
AS $$
-- Export data to csv
COPY (
SELECT
*
FROM
table_name)
TO filePath WITH CSV DELIMITER ',' HEADER;
$$
err: Query 1 ERROR: ERROR: syntax error at or near "filePath" LINE 16: TO filePath WITH CSV DELIMITER ',' HEADER;
See here SQL Functions:
SQL function arguments can only be used as data values, not as identifiers. Thus for example this is reasonable: ...
I'm going to say the filePath is being seen as an identifier. You will need to use plpgsql like so:
CREATE OR REPLACE FUNCTION public.backup(filepath character varying)
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
-- Export data to csv
EXECUTE 'COPY ( SELECT * FROM table_name) TO ' || quote_literal(filePath) || ' WITH CSV DELIMITER '','' HEADER';
RETURN;
END;
$function$
I've been trying to add stored generated column with this command
ALTER TABLE mtn_order
ADD COLUMN textsearchable_index_col tsvector
GENERATED ALWAYS AS (to_tsvector('english', coalesce(descr, '') || ' ' || coalesce(descrrep, ''))) STORED;
But I get error:
ERROR: syntax error at or near "("
LINE 3: GENERATED ALWAYS AS (to_tsvector('english', coalesce(descr, ...
^
What am i doing wrong?
UPDATE:
Had to upgrade to version 12
I am trying to import a table to postgresql database.
Some fields in the csv file has null value as "\N" or "N".
and I am getting this error
ERROR: syntax error at or near "FORCE_NOT_NULL"
LINE 1: ...r_name ) FROM STDIN CSV QUOTE '"' ESCAPE '''' FORCE_NOT_...
The syntax is incorrect. It should be
COPY ... FROM ... (FORMAT 'csv', QUOTE '"', ESCAPE '''', FORCE_NOT_NULL (...))
I am using postgres to create several INSERT scripts to insert data from one database to another.
The statement is giving me an error for invalid syntax for integer.
Here is the script I am trying to run
select 'INSERT INTO public."HoldingMasters"(
"AccountID", "AssetID", "CreatedDate", "DateAcquired", "GainsLongTerm", "LotNumber", "Managed", "ModifiedDate", "Sweep", "UID", "Units", "UnitsPledged", "IsModified", "ModifiedCount")
SELECT ' + CAST("AccountID" as integer) + ',' + CAST("AssetID" as integer) + ',' + "CreatedDate" + ',' + "DateAcquired" + ',' + "GainsLongTerm" + ',' + CAST("LotNumber" as integer) + ',' + "Managed" + ',' + "ModifedDate" + ',' + "Sweep" + ',' + CAST("UID" as integer) + ',' + "Units" + ',' + ',' + "UnitsPledged" + ',' + "IsModifed" + ',' + CAST("ModifiedCount" as integer) +
'WHERE NOT EXISTS(SELECT "UID" from "HoldingMasters" where "UID" = ' + CAST("UID" as integer) +')'
as script from "HoldingMasters";
And this gives me the error:
ERROR: invalid input syntax for integer: "INSERT INTO public."HoldingMasters"(
"AccountID", "AssetID", "CreatedDate", "DateAcquired", "GainsLongTerm", "LotNumber", "Managed", "ModifiedDate", "Sweep", "UID", "Units", "UnitsPledged", "IsModified", "ModifiedCount")
SELECT "
LINE 1: select 'INSERT INTO public."HoldingMasters"(
^
SQL state: 22P02
Character: 8
Several of these columns are integers and I am not sure what syntax to use to get them to cast properly in the INSERT statement within my scripts.
I have tried:
SUM(NULLIF("AccountID", '')::integer)
"AccountID"::integer
to_number - does not seem to work on my machine
I have run this INSERT statement by itself without any issue, it just seems to have troubles when within the "as script" generator. I have not seen anyone try this syntax within a script generator.
Any help would be appreciated.