Re: Linked List

From: "Ben K(dot)" <bkim(at)coe(dot)tamu(dot)edu>
To: "Gregory S(dot) Williamson" <gsw(at)globexplorer(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Linked List
Date: 2006-05-01 04:49:45
Message-ID: Pine.GSO.4.64.0604302327350.27523@coe.tamu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> The pgsql function is compiled and wouldn't know how to handle a table
>name as a variable.

> If you rewrite the SQL to use the 'EXECUTE' statement I think you could
>do this, something along the lines of (untested):

> EXECUTE ''INSERT INTO '' || quote_ident(tmplist) || '' (SELECT * FROM
>links WHERE p=x)'';

Thanks. Yet, if I give the table name as the argument, I get the same
error.

drop function traverse(integer, text);
create or replace function traverse (integer, text)
returns integer as
$$
declare
x int;
tname alias for $2;
begin
x := $1;
while x is not null loop
select n into x from links where p = x;
insert into tmplink (select * from links where p=x);
EXECUTE ''INSERT INTO '' || quote_ident(tname) || '' (SELECT * FROM
links WHERE p=x)'';
end loop;
return 1 ;
end;
$$
language plpgsql;

The above gives the following error. Please note that the first and
second args are mixed up now. I tested it only on 8.0 (FC 4) and didn't on
8.1 and wonder whether it's a bug or I may be doing something wrong.

Using tname or $2 doesn't change the result.

========================================================================

# select traverse(0, 'links2');
ERROR: syntax error at or near "INSERT" at character 11
QUERY: SELECT ''INSERT INTO '' || quote_ident( $1 ) || '' (SELECT * FROM
links WHERE p= $2 )''
CONTEXT: PL/pgSQL function "traverse" line 10 at execute statement
LINE 1: SELECT ''INSERT INTO '' || quote_ident( $1 ) || '' (SELECT ...

========================================================================

Regards,

Ben K.
Developer
http://benix.tamu.edu

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message tnodev 2006-05-01 07:37:58 Like with special character
Previous Message Gregory S. Williamson 2006-05-01 01:47:47 Re: Linked List