AWS Redshift syntax error near $1 when compile procedure which contains FULL JOIN - amazon-redshift

I'm not able to compile Redshift stored procedure, which contains FULL JOIN. An example: let's have 3 same TEMP tables I can run a statement:
INSERT INTO tmp_account_2
SELECT N.*
FROM tmp_account_2a AS N
FULL JOIN tmp_account_2b AS R
ON N.accountid = R.accountid
;
but when try to compile procedure following error occurs:
ERROR: syntax error at or near "$2" Where: SQL statement in PL/PgSQL function
"x_test" near line 103 [ErrorId: 1-633c2eb0-1b91003e3279bd772fdb2209]

Related

Executing query inside variable in PostgreSQL function

I'm new to postgres, for a scenario i stored SQL statements inside a table with respective to table name. in the function i'm trying to filter the table name by passing them as a parameter to get the query from the table. But when i execute the query from the variable it gives out error
"SQL Error [42P01]: ERROR: relation "public.table_name" does not exist
Where: PL/pgSQL function ops_data_refresh(text) line 45 at EXECUTE"
execute format('select query from public.ops_dw_table_load where target_table=''%s'' and is_active =true',main_table)
into qry1;
if qry1 is not null then
raise notice '%',qry1;
execute qry1;
raise notice output insert into public.table_name select * from stage.table_name;
with raise notice im able to see the query which is in the table, if I run it manually things are working fine. but when running from function it throws the above error.
There is an SQL injection bug in your code. It should be:
EXECUTE format('SELECT ... target_table = %L ...', main_table);
But the problem is in the second EXECUTE: the query references a table that does not exist. Either change the query or create the table.

Redshift Correlated Subquery within copy command

I am trying to query from a table within a copy command however, I have continually gotten errors. Here is the example SQL statement.
copy schema.table
from 's3://bucket/folder`
iam_role (select value from roles.iam where key = 'IAMRole');
The inner select statement on its own returns a value however, when I run the above, I get the following error:
SQL Error [500310] [42601]: [Amazon](500310) Invalid operation: syntax error at or near "("
The COPY command, as you must suspect, does not support embedded SQL.
If you want to do something like this, you can, but you'll need a procedure.

Loop through tables in postgresql trigger

I'm trying to build a trigger on a table, depending on other tables. So after search I have something like this
in the trigger :
begin
table_name=select (...) from information schema ;
execute format('some stuff
for i in select ... loop
insert into table (...) select (...) from %I
end loop',table)
But when firing the trigger I get this error:
SQL Error [42601]: ERROR: syntax error on or near « FOR »
I can't understand why-any ideas ?
With EXECUTE (dynamic SQL) you can only execute SQL statements. You are trying to execute a PL/pgSQL block.
You have three options:
The query string only contains the INSERT statement, and the loop is regular SQL.
The query string is a DO SQL statement that contains the whole block.
Rather than writing a FOR loop, write a dynamic statement like
INSERT INTO ...
SELECT ... FROM %I

how to execute multiple statement in PostgreSQL

I have a query like below:
if extract(dow from current_date)=1 then
select * form dev
else
select * from dev1
end if
Error:
ERROR: syntax error at or near "if" LINE 1: if extract(dow from
current_date)=1 then
How can I perform ddl and dml operation in single execution?

SELECT pgr_nodeNetwork query fails

I am working on windows, and have enabled the extension postgis, pgrouting on database. I have postgreSQL 9.4 installed and i am using the data from boundless workshop (http://workshops.boundlessgeo.com/tutorial-routing/).
SELECT pgr_nodeNetwork('edges',0.001,'geom','gid','noded')
when I run this query, it runs about 1minute and after that time it results in FAIL. How can I solve this issue? My pgr_createTopology query has been successfully run.
NOTICE: PROCESSING:
NOTICE: pgr_nodeNetwork('edges',0.001,'geom','gid','noded')
NOTICE: Performing checks, pelase wait .....
NOTICE: Processing, pelase wait .....
ERROR: line_locate_point: 1st arg isnt a line
CONTEXT: SQL statement "create temp table inter_loc on commit drop as ( select * from (
(select l1id, l2id, st_linelocatepoint(line,source) as locus from intergeom)
union
(select l1id, l2id, st_linelocatepoint(line,target) as locus from intergeom)) as foo
where locus<>0 and locus<>1)"
PL/pgSQL function pgr_nodenetwork(text,double precision,text,text,text) line 184 at EXECUTE statement
********** Error **********
ERROR: line_locate_point: 1st arg isnt a line
SQL state: XX000
Context: SQL statement "create temp table inter_loc on commit drop as ( select * from (
(select l1id, l2id, st_linelocatepoint(line,source) as locus from intergeom)
union
(select l1id, l2id, st_linelocatepoint(line,target) as locus from intergeom)) as foo
where locus<>0 and locus<>1)"
PL/pgSQL function pgr_nodenetwork(text,double precision,text,text,text) line 184 at EXECUTE statement
I ran into this issue in my project and I was stuck on it for hours trying to figure out what was causing it AND how to fix it. I will describe my situation and how I fixed it so hopefully, it helps someone else in the future.
I am using ogr2ogr to import a Shapefile into my database and I was using the -nlt PROMOTE_TO_MULTI as one of my arguments during my import; this caused my geometries to be imported as MultiLineStrings.
From the behavior I've observed and what others have mentioned (and more people), the pgr_nodeNetwork() function does not play nicely with MutliLineStrings.
Since MultiLineStrings won't work for routing, I took the SQL from dkastl's answer and ran it on my data to see if I actually needed MultiLineStrings or if I could just work with LineStrings.
SELECT
COUNT(
CASE WHEN ST_NumGeometries(geom) > 1 THEN 1 END
) AS multi,
COUNT(geom) AS total
FROM network_nodes;
After running that, I found that I had zero need for MultiLineStrings so I reimported my Shapefile with ogr2ogr using -nlt LINESTRING instead and then was able to run pgr_nodeNetwork() without problems.