0

Here I am trying to assign a "SELECT" query to a specific variable. The select query is retrieving rows from other database table using dblink. For example as shown below:

Example: My try

create or replace function fun(names varchar(60), id bigint) returns void as
$$
Declare
        sql varchar;
Begin
        sql := 'Select * from dblink'('conn','select * from tablex') /* Error occurred here */  
               'where name = ''' || names ||'''';

        raise info '%',sql;

        execute sql,'names varchar(60)',names=names;
end;
$$
Language plpgsql;

ERROR: syntax error at or near "("

Sarfaraz Makandar
  • 5,933
  • 16
  • 59
  • 84

2 Answers2

1

You've messed up your quotimg.

You can use dollar-quoting inside the fumction too.

sql := $SQL$...$SQL$
Richard Huxton
  • 21,516
  • 3
  • 39
  • 51
0

Solution: Here is the solution

create or replace function fun(names varchar(60), id bigint) returns void as
$$
Declare
        sql varchar;
Begin
        sql := 'Select * from dblink(''conn','select * from tablex'') 

           where name = ''' || names ||'''';

    raise info '%',sql;

    execute sql,'names varchar(60)',names=names;
end;
$$
Language plpgsql;
Sarfaraz Makandar
  • 5,933
  • 16
  • 59
  • 84