plpgsql Functions: Text Variables

From: Carel Combrink <s25291930(at)tuks(dot)co(dot)za>
To: pgsql-novice(at)postgresql(dot)org
Subject: plpgsql Functions: Text Variables
Date: 2010-06-03 13:47:42
Message-ID: 20100603154742.lnwgcxuy0ok8kck0@student.up.ac.za
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

I want to create a function in plpgsql that must execute a INSERT
query on the database. The problem comes in I want to create the query
inside the function. Look at the following:

CREATE OR REPLACE FUNCTION Test_Function(integer) RETURNS VOID
AS $$
DECLARE
table_name text := 'My_Table_Name';
BEGIN
INSERT INTO "table_name" VALUES ($1);
END
$$ LANGUAGE plpgsql;

When I try to create the function I get the following response from Postgres:
ERROR: syntax error at or near "$1"
LINE 1: INSERT INTO $1 VALUES ( $2 )
^
QUERY: INSERT INTO $1 VALUES ( $2 )
CONTEXT: SQL statement in PL/PgSQL function "test_function" near line 4

What am I missing? Is it possible to do what I want to do? And if so
how should I do it.

Note: The function is only to show what the problem is, actual
function should look a lot different.

Any help will be appreciated
Thank you
--
Carel Combrink
s25291930(at)tuks(dot)co(dot)za

This message and attachments are subject to a disclaimer. Please refer
to www.it.up.ac.za/documentation/governance/disclaimer/ for full
details. / Hierdie boodskap en aanhangsels is aan 'n vrywaringsklousule
onderhewig. Volledige besonderhede is by
www.it.up.ac.za/documentation/governance/disclaimer/ beskikbaar.

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2010-06-03 14:01:44 Re: slony config error
Previous Message machielr 2010-06-03 10:27:02 slony config error