0

Here I need to call a function with some parameters.

Example:

Function:

create or replace function testfunction(ids int,pcname varchar)
returns void as
$$
declare
       sql varchar;
       qu varchar := 'tabletemp_';
begin
       qu := qu ||ids ||'_'|| pcname;
       sql := 'Drop view if exists '||qu;

       raise info '%',sql;
       execute sql;
 end;
 $$
 language plpgsql;

Calling Function:

select testfunction(1,'abc-pc'); 

Error:

ERROR:  syntax error at or near "-"
Drop view if exists tabletemp_1_abc-pc
                                   ^

Question: How can I pass such parameter while calling function?

4
  • put the code for testfunction(). Commented Jul 23, 2014 at 7:44
  • @Ilesh Patel, Please checkout the updated question. Thanks Commented Jul 23, 2014 at 7:54
  • @Meem do you think select from testfunction(1,'abc-pc');is correct? correct it Commented Jul 23, 2014 at 8:41
  • @dude, Yes! Done. That was the typo. Commented Jul 23, 2014 at 9:05

2 Answers 2

2

You forgot to quote_ident.

   sql := 'Drop view if exists '|| quote_ident(qu);

but you should preferably use format:

   sql := format('Drop view if exists %I', qu || ids ||'_'|| pcname);
Sign up to request clarification or add additional context in comments.

Comments

2

Try This:

create or replace function testfunction(ids int,pcname varchar)
returns void as
$$
declare
       sql varchar;
       qu varchar := 'tabletemp_';
begin
       qu := qu ||ids ||'_'|| pcname;
       sql := 'Drop view if exists "'||qu||'"';

       raise info '%',sql;
       execute sql;
 end;
 $$
 language plpgsql;

Here I add double quote around table name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.