Redshift Correlated Subquery within copy command - amazon-redshift

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.

Related

Challenges with 'INSERT' query using dblink_exec

I've read the docs repeatedly, but I'm unclear why I keep getting a
ERROR: syntax error at or near "into"
On
SELECT dblink_exec ('dbname=database_test',
insert into public.names (gid,name,"default",class,last_updated,description)
values ('124565555','dblink_test','true','10','2017-01-30 14:14:40.581',null));
Can I not INSERT to a remote db (note: 'database_test' .is on the same server as the database I am working in)
According to docs you should single quote ' sql statement.
SELECT dblink_exec('myconn', 'insert into foo values(21,''z'',''{"a0","b0","c0"}'');');

DB2 query with slash in table name

please I have table name SAPPRD./CS1/TB2_SOPBV and I canĀ“t do this query:
transfer ownership of table SAPPRD./CS1/TB2_SOPBV TO USER SAPQAS preserve privileges;
I am getting error:
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned: SQL0104N An unexpected token "/CS1/" was found following
"hip of table SAPFIP.". Expected tokens may include: "".
SQLSTATE=42601
So I tried to do escaping, I edited query to:
transfer ownership of table SAPPRD.\"/CS1/TB2_SOPBV" TO USER SAPQAS preserve privileges
But It will not escape, I am still getting error:
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned: SQL0007N The statement was not processed because a character
that is not supported in SQL statements was included in the SQL
statement. Invalid character: "\". Text preceding the invalid
character: "hip of table SAPPRD.". SQLSTATE=42601
Is possible to do escaping here and proceed with this query?
Thank you!
Solved!
transfer ownership of table SAPPRD."/CS1/TB2_SOPBV" TO USER SAPQAS preserve privileges

Continue sql statement after an error in postgresql database

I am working on a view which this is its sql
-- DROP VIEW bd_segment_id;
CREATE OR REPLACE VIEW bd_segment_id AS
SELECT final.gid,
row_number() OVER (ORDER BY final.gid) AS row_number,
degrees(st_azimuth(ff.sp, ff.ep) - 1.57079633::double precision) AS az_deg,
st_length(ff.st_makeline) AS st_length,
ff.st_makeline
FROM bd_segment_geom ff
JOIN final ON st_touches(ff.st_makeline, final.geom)
GROUP BY final.gid, ff.sp, ff.ep, ff.st_makeline;
ALTER TABLE bd_segment_id
OWNER TO postgres;
while running each query of this table I have faced this error
ERROR: GEOSTouches: TopologyException: side location conflict at 553655.77720000315 3474241.5185000021
********** Error **********
ERROR: GEOSTouches: TopologyException: side location conflict at 553655.77720000315 3474241.5185000021
SQL state: XX000
Is there any way for sql to pass errors and continues to do the rest of sql?I know the problem is an internal error but I want sql jump of it..
I have searched and found this in section 39.6.6. Trapping Errors is says how we can use an EXCEPTION clause but I don't know how to write handler_statements that says sql to continue.
I need sth like try catch in C# or jave
can some one please help me with this problem?
thank you

Nested query as PostGIS function parameter

I have a PostGIS query where I really need to have nested queries inside PostGIS function calls:
UPDATE raw.geocoding
SET the_geom = ST_Centroid(
ST_Collect(
SELECT the_geom
FROM raw.geocoding
WHERE hash = ((E'0101000020090C000081610F9CC5DC3341EE672E6E723B3241')::varchar),
SELECT the_geom
FROM raw.geocoding
WHERE hash = ((E'0101000020090C00002CF887E0C5DC3341C9E5B2DF2A383241')::varchar)
)
)
WHERE hash = ((E'3e638a27c6c38f05026252f4a0b57b2e')::varchar)
Unfortunately, this doesn't work. I get a syntax error at the beginning of the nested query:
ERROR: syntax error at or near "SELECT"
LINE 4: SELECT the_geom
^
********** Error **********
ERROR: syntax error at or near "SELECT"
SQL state: 42601
Character: 86
Looks like I cannot have a nested query as a PostGIS function parameter?
I've perused through the PostGIS documentation and cannot find any clear guidance for dealing with this.
It appears Postgres has a way of doing variables in pgSQL, but it's unclear to me how this would be pulled off in a standard query. This is a query that will be run tens or hundreds of thousands of times from a C# program. That aside, I could do a pgSQL stored procedure if required; just wanted to make sure there wasn't a simpler alternative first.
In case you were wondering, the query looks messy because it's the result of a npgsql-generated parameterized query. I think it's fair to say that npgsql is being extra-cautious with redundant typing and escaping.
I am running PostGIS 2.0.1, Postgres 9.1.5, and npgsql 2.0.12.
It sounds like you want a scalar subquery, an expression written like (SELECT ....) (note enclosing parentheses) that contains a query returning either zero rows (NULL result) or one field from one row.
You were most of the way there, you just needed the parens:
UPDATE raw.geocoding
SET the_geom = ST_Centroid(
ST_Collect(
(SELECT the_geom
FROM raw.geocoding
WHERE hash = ((E'0101000020090C000081610F9CC5DC3341EE672E6E723B3241')::varchar)),
(SELECT the_geom
FROM raw.geocoding
WHERE hash = ((E'0101000020090C00002CF887E0C5DC3341C9E5B2DF2A383241')::varchar))
)
)
WHERE hash = ((E'3e638a27c6c38f05026252f4a0b57b2e')::varchar)
Note that subqueries can be used in other places too - table returning subqueries can appear in FROM, for example. The PostgreSQL manual teaches about all this, and is well worth a cover-to-cover read.
If you're doing a lot of these updates, you may find it more efficient to formulate the UPDATE as a join using the PostgreSQL extension UPDATE ... FROM ... WHERE rather than running lots of individual UPDATEs over and over. I just wanted to raise the possibility. See from-list in UPDATE

currval Function in PostgreSQL complaining that "column does not exist"

I am trying to use PostgreSQL's currval function to return the last inserted row id of a table called Concept. Concept has a serial primary key called cid and there was an automatically generated Sequence called Concept_cid_seq.
I try the following statement and get an error:
SELECT currval("Concept_cid_seq");
ERROR: column "Concept_cid_seq" does not exist
LINE 1: SELECT currval("Concept_cid_seq");
^
********** Error **********
ERROR: column "Concept_cid_seq" does not exist
SQL state: 42703
Character: 16
But when I run the query :
SELECT * from "Concept_cid_seq";
I get a table with one row (as I'd expect) showing columns like last_value, start_value, etc...
What am I missing here? Am I passing the wrong information to currval? Why does it say the 'column does not exist?'
It turns out that this was an issue with capitalization and quotes. Because I wanted to preserve the capitalization of the relation name I needed to use both single and double quotes in order to pass the correct relation name to currval.
I changed the query to SELECT currval('"Concept_cid_seq"'); (note the outer single quotes) and it worked correctly.