Keep getting error when trying to use '\copy' in sql - sql

Here is the query I've been trying to run:
create or replace function save_to_csv(mesa text)
returns void as $$
begin
execute '\copy (select distinct aid::text,
rid::text,
did::text
from ' || mesa ||
')
to ''/home/datascientist/Data/User_Journey_Map/updated_data/all_ids/'|| mesa ||'_ids.csv''';
end;
$$
language 'plpgsql';
I keep getting this error:
SQL Error [42601]: ERROR: syntax error at or near "\"
Where: PL/pgSQL function save_to_csv(text) line 3 at EXECUTE
SQL statement "SELECT save_to_csv(mesa)"
PL/pgSQL function save_tables() line 21 at PERFORM
However, if I delete the ' \' before 'copy' I get this error:
SQL Error [58P01]: ERROR: could not open file "/home/datascientist/Data/User Journey Map/updated_data/all_ids/tca__clicked_ids.csv" for writing: No such file or directory
Hint: COPY TO instructs the PostgreSQL server process to write a file. You may want a client-side facility such as psql's \copy.
Where: SQL statement "copy (select distinct aid::text, rid::text, did::text from tca__clicked) to '/home/datascientist/Data/User Journey Map/updated_data/all_ids/tca__clicked_ids.csv'"
PL/pgSQL function save_to_csv(text) line 3 at EXECUTE
SQL statement "SELECT save_to_csv(mesa)"
PL/pgSQL function save_tables() line 21 at PERFORM
I've tried implementing the solutions given at:
PostgreSQL syntax error when using EXECUTE in Function
and
I am trying to copy a file, but getting error message , but neither have helped and I can't find anything else that looks helpful.

Related

PLS-00103: Encountered the symbol "CREATE" when expecting one of the

I am trying to create a stored procedure in oracle 12c database and I am getting error when I am running code to store the procedure.
PLS-00103: Encountered the symbol "CREATE" when expecting one of the
There are multiple stack overflow question already asked on this topic. but they suggest some different syntax. which is deviation from actual oracle documentation. and even those didn't worked for me
I checked for documentation on multiple website including oracle documentation. oracle documetation suggest syntax as following
Oracle STORED PROCEDURE DOCUMENTATION
so I as per the syntax I wrote the following procedure.
CREATE PROCEDURE PDD_PROC_BASE
AS
--DROP TABLE BASE;
CREATE TABLE BASE as
SELECT idno
,DATE
,diff
,SUBSTR(idNO,7,2) AS PRD
,COMPLETED
,CATG
,OP_Number
FROM table1
WHERE = date >= '30-JUN-2018'
AND STATUS = 'G'
END;
and I got the following error.
Procedure PDD_PROC_BASE compiled
Errors: check compiler log
Errors for PROCEDURE AN_5043152.PDD_PROC_BASE:
LINE/COL ERROR
-------- ------------------------------------------------------------------------------
7/5 PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
json_exists json_value json_query json_object json_array
I checked other resources as well but still didn't understood what went wrong.
I even tried code example from Oracle documentation and got similar error.
I am using SQLdeveloper tool as client
You cant use directly sql ddl statements in plsql block, you can do the same thing using dynamic sql with the "EXECUTE IMMEDIATE" statement like this:
begin
execute immediate 'create table test_table1 (test_column1 varchar2(40))';--your create table statement here
end;
In Oracle in stored procedures you can use DDL statements only as dynamic SQL, that means as EXECUTE IMMEDIATE 'CREATE TABLE ' ...
Check here, for example: http://www.dba-oracle.com/t_using_ddl_create_index_table_plsql.htm

PostgreSQL accessing nested uppercased properties

I've recently switched from mongoDB to postgreSQL and for a while I got stuck with this problem - I can't seem to find a way to access nested property of an object. Yes, I shouldn't be having uppercased column/table names to start with, but I really want to keep naming consistency.
Let's say I have the following db table:
users {
ID: bigint
}
Now let's say I want to get deleted user(old) ID, how do I access this ID?
create or replace function deleted()
returns trigger AS
$body$
begin
perform pg_notify('deleted', ----->WHAT GOES HERE<-----);
return new;
end;
$body$
I've tried filling in placeholder with old."ID", then I get following error:
ERROR: function pg_notify(unknown, bigint) does not exist
LINE 1: SELECT pg_notify('deleted', old."ID")
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT pg_notify('deleted', old."ID")
CONTEXT: PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42883
If I try doing this -> old.ID then I get
ERROR: record "old" has no field "id"
CONTEXT: SQL statement "SELECT pg_notify('deleted', old.ID)"
PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42703
Also I've tried this: `"old.ID", then I get the following:
ERROR: column "old.ID" does not exist
LINE 1: SELECT pg_notify('deleted', "old.ID")
^
QUERY: SELECT pg_notify('deleted', "old.ID")
CONTEXT: PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42703
Thank you for your patience.
pg_notify() wants two arguments of text datatype. So you can cast your bigint argument to that datatype:
pg_notify('deleted', (old."ID")::text)

Postgresql 11 - Create Procedure to Execute COPY function

I'm currently trying to create a procedure to automatically copy data into my database when I call the procedure. However, every time I call it I get the following error:
ERROR: column "name" does not exist
LINE 1: SELECT format('COPY test(%L) FROM %s CSV HEADER', name, '/Us...
How does the column not exist? Here's everything I've written out:
CREATE PROCEDURE
test_insert() AS
$$
BEGIN
EXECUTE format('COPY test(%L) FROM %s CSV HEADER', name, '/Users/Receiving.csv');
END;
$$ LANGUAGE plpgsql;
If you use name without single quotes, it is interpreted as a column name in the (tacit) SELECT statement
SELECT format('...', name, '...')
that PL/pgSQL runs when you execute your function.
Since this SELECT statement does not have a FROM clause, you get the observed error.
The solution is to use a string literal instead, i.e. write 'name' instead of 'name'.

ERROR: could not access file "$libdir/postgis-2.0" postgresapp

I am running postgresapp 9.2.4.3 and postgis and trying to add a geometry column to a table. The postgis extension is running.
When I run:
SELECT AddGeometryColumn('public'::varchar,'gloutline'::varchar,'geom'::varchar,'4326','MULTIPOLY ON','2');
I'm getting the following errors:
ERROR: could not access file "$libdir/postgis-2.0": No such file or directory
LINE 1: ALTER TABLE public.gloutline ADD COLUMN geom geometry(MultiP...
QUERY: ALTER TABLE public.gloutline ADD COLUMN geom geometry(MultiPolygon, 4326)
CONTEXT: PL/pgSQL function addgeometrycolumn(character varying,character varying,character varying,character varying,integer,character varying,integer,boolean) line 110 at EXECUTE statement
SQL statement "SELECT AddGeometryColumn('',$1,$2,$3,$4,$5,$6,$7)" PL/pgSQL function addgeometrycolumn(character varying,character varying,character varying,integer,character varying,integer,boolean) line 5 at SQL statement
A reinstall of postgress.app including the application support files fixed the problem.

Problem with PostgreSQL function parameters (Converting MySQL Stored Procedures)

I am converting a few simple MySQL stored procedures to PostgreSQL Functions and for some reason when I execute the following function it throws an error when executed.
I created the function with the following code:
CREATE FUNCTION cc.fs_ivr_updatecalltransfer(_ParentContactID
int[11], _CalledID varchar[32])
RETURNS int AS $$
DECLARE
pcID int;
BEGIN
if _ParentContactID<>0 then
update cc.tblcontacts set cc.tblcontacts.transferdest =
_CalledID where cc.tblcontacts.contactid =
_ParentContactID;
end if;
RETURN _ParentContactID;
END;
$$ LANGUAGE plpgsql;
I Execute it manually like this:
SELECT cc.fs_ivr_updatecalltransfer(3,"test")
It throws this error:
SQL error:
ERROR: column "test" does not exist
LINE 1: SELECT cc.fs_ivr_updatecalltransfer(3,"test")
In statement: SELECT cc.fs_ivr_updatecalltransfer(3,"test")
Any Suggestions?
Use single quotes to quote strings, not double quotes. 'test' means the string "test", whereas "test" means the identifier "test", which postgresql tries to resolve as a column, same as using backquotes in MySQL.