Re: Concatenate table name in Pl/Pgsql - Mailing list pgsql-general

From Ian Lawrence Barwick
Subject Re: Concatenate table name in Pl/Pgsql
Date
Msg-id CAB8KJ=j-BZaUcSjxB4cDFjFnqAtVS28kYp_33Yt8v3xuQb4GBA@mail.gmail.com
Whole thread Raw
In response to Concatenate table name in Pl/Pgsql  (Adarsh Sharma <eddy.adarsh@gmail.com>)
Responses Re: Concatenate table name in Pl/Pgsql  (Adarsh Sharma <eddy.adarsh@gmail.com>)
List pgsql-general
2013/6/26 Adarsh Sharma <eddy.adarsh@gmail.com>:
> Hi ,
>
> Today i m creating a function that includes dynamic concatenation of a
> partitioned table name as below :-
>
> test=# CREATE OR REPLACE FUNCTION tmp_trigger_function()
> test-# RETURNS TRIGGER AS $$
> test$# DECLARE
> test$# tbl_name text;
> test$# abc varchar;
> test$# BEGIN
> test$# tbl_name := 'tmp';
> test$# select to_char(NEW.a::timestamp,'yyyymmdd') into abc ;
> test$# insert into tmp || abc values ( NEW.* );
> test$# RETURN NULL;
> test$# END;
> test$# $$
> test-# LANGUAGE plpgsql;
> ERROR:  syntax error at or near "||"
> LINE 9: insert into tmp || abc values ( NEW.* );
>                         ^
> Time: 0.901 ms
> test=#
> test=#
>
> I tried with a statement variable also. Any ideas ?

You'll need to create a string and use EXECUTE, something along the lines of:

stmt := 'insert into ' || tmp || abc || ' VALUES ($1)'
EXECUTE stmt USING NEW.*;

http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

HTH

Ian Barwick


pgsql-general by date:

Previous
From: Adarsh Sharma
Date:
Subject: Re: Concatenate table name in Pl/Pgsql
Next
From: Adarsh Sharma
Date:
Subject: Re: Concatenate table name in Pl/Pgsql