Thread: copying few fields into an existing table

copying few fields into an existing table

From
Alexander Farber
Date:
Hello,

sadly I have to ban few users from my game web
site daily and so I'm trying to write a procedure for
first copying their id, name, city, IP into a pref_ban table
and then erasing their comments and statistics:

        create or replace function pref_delete_user(_id varchar,
            _reason varchar) returns void as $BODY$
                begin

                select into pref_ban id, first_name, last_name, city,
last_ip from pref_users where id=_id;

                delete from pref_rep where author=_id;
                delete from pref_rep where id=_id;
                delete from pref_catch where id=_id;
                delete from pref_game where id=_id;
                delete from pref_hand where id=_id;
                delete from pref_luck where id=_id;
                delete from pref_match where id=_id;
                delete from pref_misere where id=_id;
                delete from pref_money where id=_id;
                delete from pref_pass where id=_id;
                delete from pref_status where id=_id;
                delete from pref_users where id=_id;

                end;
        $BODY$ language plpgsql;

Unfortunately, I can't figure out the correct syntax
for the first operation (copying into existing table)

ERROR:  syntax error at "pref_ban"
DETAIL:  Expected record variable, row variable, or list of scalar
variables following INTO.
CONTEXT:  compilation of PL/pgSQL function "pref_delete_user" near line 3


Using PostgreSQL 8.4.7 @ CentOS 6 / 64 bit.

Any help please?
Alex

Re: copying few fields into an existing table

From
Joe Abbate
Date:
On 10/22/2011 04:31 PM, Alexander Farber wrote:
> Hello,
>
> sadly I have to ban few users from my game web
> site daily and so I'm trying to write a procedure for
> first copying their id, name, city, IP into a pref_ban table
> and then erasing their comments and statistics:
>
>         create or replace function pref_delete_user(_id varchar,
>             _reason varchar) returns void as $BODY$
>                 begin
>
>                 select into pref_ban id, first_name, last_name, city,
> last_ip from pref_users where id=_id;
>
>                 delete from pref_rep where author=_id;
>                 delete from pref_rep where id=_id;
>                 delete from pref_catch where id=_id;
>                 delete from pref_game where id=_id;
>                 delete from pref_hand where id=_id;
>                 delete from pref_luck where id=_id;
>                 delete from pref_match where id=_id;
>                 delete from pref_misere where id=_id;
>                 delete from pref_money where id=_id;
>                 delete from pref_pass where id=_id;
>                 delete from pref_status where id=_id;
>                 delete from pref_users where id=_id;
>
>                 end;
>         $BODY$ language plpgsql;
>
> Unfortunately, I can't figure out the correct syntax
> for the first operation (copying into existing table)
>
> ERROR:  syntax error at "pref_ban"
> DETAIL:  Expected record variable, row variable, or list of scalar
> variables following INTO.
> CONTEXT:  compilation of PL/pgSQL function "pref_delete_user" near line 3

What you want is:  INSERT INTO pref_ban SELECT ... FROM pref_users ...

or, if the pref_ban table does not yet exist:

CREATE TABLE pref_ban AS SELECT ... FROM pref_users

Joe