Re: PL/PGSQL - How to pass in variables?

Поиск
Список
Период
Сортировка
От Jaime Casanova
Тема Re: PL/PGSQL - How to pass in variables?
Дата
Msg-id c2d9e70e0605140916m466af221v43f51e6c0e2890b8@mail.gmail.com
обсуждение исходный текст
Ответ на PL/PGSQL - How to pass in variables?  ("Scott Yohonn" <syohonn@gmail.com>)
Список pgsql-sql
On 5/14/06, Scott Yohonn <syohonn@gmail.com> wrote:
>
> Using PL/PGSQL, I am trying to create a procedure to display the
> count of rows in any single table of a database. The End-user would
> pass in a table name and the prodecure would display the table name
> with the row count.
> I am able to hardcode the variable for table and get the appropriate
> results from my count function (see below), but cannot pass in a
> variable and have the function work. Any suggesstions???
>
> CREATE FUNCTION get_table_count(tablename text) RETURNS integer AS $$
> DECLARE
>
>      --tablename ALIAS FOR $1;
>
>       rowcount INTEGER;
>    BEGIN
>
>      SELECT INTO rowcount count(*) FROM tablename;
>
>      RETURN rowcount;
>
>    END;
>  $$ LANGUAGE 'plpgsql';
>

you can't do this because tablename is a variable not a table, you
have to append the content of the variable in a string that can be
EXECUTE'd
    EXECUTE 'SELECT count(*) FROM ' || tablename INTO rowcount;

--
regards,
Jaime Casanova

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs and the universe trying
to produce bigger and better idiots.
So far, the universe is winning."                                      Richard Cook


В списке pgsql-sql по дате отправления:

Предыдущее
От: Jean-Paul Argudo
Дата:
Сообщение: Re: PL/PGSQL - How to pass in variables?
Следующее
От: Jean-Paul Argudo
Дата:
Сообщение: Re: PL/PGSQL - How to pass in variables?