Challenges with 'INSERT' query using dblink_exec - postgresql

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"}'');');

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.

How to use a subquery as a database name in a DDL command?

I am wondering if it's possible to use the result of a subquery as database name in a PostgreSQL (9.5.1) DDL statement.
For example, I wanted to alter the current database with something like:
ALTER DATABASE (SELECT current_database()) SET a_var TO 'a_value';
If I run this, an error occurs:
ERROR: syntax error at or near "("
LINE 1: ALTER DATABASE (SELECT current_database()) SET ...
What's the correct way to use the sub-query (if possible)?
You need dynamic SQL for that:
DO
$do$
BEGIN
EXECUTE format($f$ALTER DATABASE %I SET x.a_var TO 'a_value'$f$, current_database());
END
$do$;
Using format() to escape the db name safely while being at it.
BTW, to unset:
ALTER DATABASE your_db RESET x.a_var;
To see the current setting:
SELECT current_setting('x.a_var');
(The DB default is not active before you start a new session.)
Related:
Table name as a PostgreSQL function parameter
Error when setting n_distinct using a plpgsql variable

Unexpected end of command - line 1, column 532676549 (with execute statement in firebird 2.5)

I have a problem with execeute statement in a stored procedure and i cant figure it out, i try the text of the query in a variable, show it in a exception to see if was well, copy and execute it and worket, but inside the execute statement i get the same error "Unexpected end of command - line 1, column 532676549"
execute statement 'insert into rep_balancediario(id_rep_balancediario,id_plancuenta,id_saldocontable,nivel,codigoreducido,
SALDOANTERIOR,CREDITO,DEBITO,SALDODIA, cuentacontable,imputable,id_moneda,contracuenta,saldocontabledes,ID_SUCURSALAGENCIA)
values(gen_id(id_rep_balancediario,1),'||:v_id_PlanCuenta||','||:V_NROSALDOCONTABLE||','||:v_Nivel||',"'||:v_CodigoReducido||'",'||
:V_SALDOANTERIORMN||','||:V_CREDITOMN||','||:V_DEBITOMN||','||:v_resultadosaldoactual||',"'||cast(:v_CuentaContable as VARCHAR(11))||'","S",'
||:v_nromoneda||',
"'||cast(:v_contracuenta as VARCHAR (11))||'","'||cast(:V_DESSALDOCONTABLE as VARCHAR(255))||' >'||cast(:v_des_suc as varchar(100))||'",'
||:VE_IDSUCURSALAGENCIA||')'
--:V_str_sql
with AUTONOMOUS TRANSACTION;
does some one know what could by the problem?
Unexpected end of command usually means that the parser reached the end of the statement while it was still expecting tokens, or reached a parser state where it should be complete but there are still tokens left. The fact you are concatenating values into the query is a red flag. Also the "column 532676549" could be an indication of problem, as that exceeds query length limits (64k in Firebird 2.5 and earlier), and also suggests you have literals that are larger than supported (32k).
Execute statement supports parametrized queries: use it. I'd suggest that you rewrite your query to a parametrized query and see if that works, as an example:
execute statement ('insert into aTable (column1, column2) values (?, ?)') (aVariable, 'literal');
or
execute statement ('insert into aTable (column1, column2) values (:var1, :var2)') (var1 := aVariable, var2 := 'literal');
I also see that your query contains double quotes where I'd expect single quotes, which suggests that you use the deprecated dialect 1, which brings yet another set of potential problems.

update remote db using dblink_exec fails

The below statement is failing by saying syntax error, I do not see the syntax error, please help:
SELECT dblink_exec(
'dbname=billing user=billing password=billing port=5432',
'insert into md.radacct values('2013-01-01 00:00:00+01:30')');
table was created like this
create table md.radacct(date_time timestamp with time zone);
You need to double up your single-quotes inside the outermost single-quotes.
select dblink_exec('...', ' insert into ... values(''2013-01-01 ... '')');