I am trying to run a for loop via psql command in Linux to do some spatial operations in PostGIS database
My SQL command which I successfully ran in dbeaver looks something like this
DO $$
declare
the_name varchar(50);
BEGIN
FOR the_name IN
SELECT "name" FROM myschema.mytable WHERE "idcolumn" = 'selected id' LIMIT 4
LOOP
some operations inside the loop...
END LOOP;
RAISE NOTICE 'the name - (%)', the_name;
END;
$$;
I usually copy-paste the dbeaver SQL command inside psql -c and it runs fine. But I am getting a syntax error when using the above code. I am a beginner in SQL and any help is appreciated. Thanks.
Edit
This is the error message - it's weird because I don't have this number in the code. It's probably reading from memory or something.
ERROR: syntax error at or near "377643"
LINE 1: DO 377643 declare the_name varchar(50); BEGIN FOR the_...
Related
I got this error while trying to run docker-compose up --build, I'm using postgresql as my database service. I think there's wrong with my query.When i remove the CREATE TRIGGER it somehow run without erros.
LINE 13: CREATE TRIGGER update_communication
backend-postgres-1 |^
Here is my sql file
CREATE OR REPLACE FUNCTION dts.save_communication_revision()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
BEGIN
INSERT INTO dts.communication_revisions(com_id, class_id, com_subject, user_id, com_source_name, "com_dateCreated", "com_dateReceived", com_urgency, com_source_position, com_source_office, com_draft, com_other_remarks, "com_controlNo", com_due_date)
VALUES(OLD.com_id, OLD.class_id, OLD.com_subject, OLD.user_id, OLD.com_source_name, OLD."com_dateCreated", OLD."com_dateReceived", OLD.com_urgency, OLD.com_source_position, OLD.com_source_office, OLD.com_draft, OLD.com_other_remarks, OLD."com_controlNo", OLD.com_due_date);
END;
$$
CREATE TRIGGER update_communication
AFTER UPDATE
ON dts.communications
FOR EACH ROW
EXECUTE PROCEDURE dts.save_communication_revision();
END;
$$;
Just added this semicolon.
I'm trying without success to create a function in Postgres that save a table or database taking one or two parameters. In this case I was trying to create it with only one parameter(name of the table or database) and backup this table/db
--SELECT backup_table(sports)
CREATE FUNCTION backup_table(TEXT) RETURNS BOOLEAN AS
$$
DECLARE
table_x ALIAS FOR $1;
BEGIN
COPY table_x FROM 'C:/path/backup_db' WITH (FORMAT CSV);
RAISE NOTICE 'Saved correctly the table %',$1;
RETURN BOOLEAN;
END;
$$ LANGUAGE plpgsql;
I've always receive the error when I try to execute the function SELECT backup_table(sports):
"The column sports doesnt exists."
SQL state: 42703
Character: 21
The idea is to create the function like the equivalent of SQL Server BACKUP DATABASE TO DISK, or equivalent to pg_dump command
pg_dump -U -W -F t sports > C:/path/backup_db;
I know about SQL but now I'm just stuck with this error.
I have the following code:
DROP TABLE IF EXISTS pltest;
CREATE TABLE pltest (x jsonb);
DO $$
DECLARE startdate text := to_char(current_date - 1, 'YYYYMMDD');
BEGIN
EXECUTE format(
'COPY pltest FROM PROGRAM ''curl "https://example.com/events/start_date=%sT000000Z"''',
startdate
);
END
$$ ;
It basically imports the URL with a parameterized COPY statement so it always imports the data of the last 24 hours, it runs perfectly in SQL shell, but when i tried adding a pgagent job with that code, pgagent returns "syntax error near DECLARE".
PostgreSQL version: 13.3
PgAgent version: 13
This code looks correct. It fails with expected error.
ERROR: invalid input syntax for type json
DETAIL: Token "<" is invalid.
CONTEXT: JSON data, line 1: <...
COPY pltest, line 1, column x: "<!doctype html>"
SQL statement "COPY pltest FROM PROGRAM 'curl "https://example.com/events /start_date=20210526T000000Z"'"
PL/pgSQL function inline_code_block line 5 at EXECUTE
Maybe your client breaks source code, or maybe your Postgres is too old.
While trying to run PL/pgSQL via JDBC driver (postgresql-9.4.1211.jre7.jar; using ANT) I get following error: ERROR: syntax error at or near "$"
Is there any way howto fix this via setting JDBC properties or changing the query of PL/pgSQL?
query:
DO $$
BEGIN
CREATE SEQUENCE id_sequence_SEQ OWNED BY id_sequence.id;
EXCEPTION WHEN duplicate_table
THEN
END
$$
LANGUAGE plpgsql;
error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$"
Position: 5
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2458)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2158)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:291)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:432)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:358)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:305)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:291)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:269)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:265)
I tried executing a DO statement with JDBC and it works just fine.
There must be something weird in your statement. Try and log the statement by setting log_min_error_statement and log_min_messages to error. Execute the statement, then look at the message in the PostgreSQL log file. It should contain the statement that was actually executed. Examine it for weird characters and other strangeness.
Please also note that there is a syntax error in you PL/pgSQL: there must be something between THEN and END. For a no-op, use the statement NULL;.
I'm using EF6 with Oracle ODP.Net and Code First migration in my app.
When i run
Update-Database
I'm getting:
Oracle.ManagedDataAccess.Client.OracleException (0x00001996): ORA-06550: line 1, column 6:
PLS-00103: Encountered the symbol "" when expecting one of the following:
but when i run
Update-Database -script
The generated script works fine on my database. After trying
Update-Database -verbose
I found that the is a difference in scripts generated by 'Update-Database -verbose'
begin
execute immediate
'create index "SYS_MZK"."IX_REALIZACJE_KURSOW_R_6940446" on "SYS_MZK"."REALIZACJE_KURSOW" ("RLK_FK_KUR_ID")';
exception
when others then
if sqlcode <> -1408 then
raise;
end if;
end;
begin
execute immediate
'create index "SYS_MZK"."IX_REALIZACJE_KURSO_1395326627" on "SYS_MZK"."REALIZACJE_KURSOW" ("RLK_FK_PNM_ID_AKT_POL")';
exception
when others then
if sqlcode <> -1408 then
raise;
end if;
end;
and 'Update-Database -script":
begin
execute immediate
'create index "SYS_MZK"."IX_REALIZACJE_KURSOW_R_6940446" on "SYS_MZK"."REALIZACJE_KURSOW" ("RLK_FK_KUR_ID")';
exception
when others then
if sqlcode <> -1408 then
raise;
end if;
end;
/
begin
execute immediate
'create index "SYS_MZK"."IX_REALIZACJE_KURSO_1395326627" on "SYS_MZK"."REALIZACJE_KURSOW" ("RLK_FK_PNM_ID_AKT_POL")';
exception
when others then
if sqlcode <> -1408 then
raise;
end if;
end;
/
The dissapearing of '/' is my problem - is there a way to change the way of generating above script