Klaus Nji
Klaus Nji

Reputation: 18847

pgsql sql functions sequential execution

If I have these two Postgres function definitions saved in two seperate .sql files:

CREATE OR REPLACE FUNCTION column_exists(tablename text, colname text) RETURNS boolean AS 
        $BODY$
        DECLARE
            q text;
            field_name text;
            onerow record;
        BEGIN
            q = 'SELECT column_name FROM information_schema.columns WHERE table_name='''||tablename||''' AND table_schema =''public''';
            FOR onerow IN EXECUTE q
                LOOP
                   field_name := onerow.column_name;
                   IF ((field_name = colname)) then
                    RETURN true;
                   END IF;                  
                END LOOP;
            RETURN false;
        END;
    $BODY$
LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION correct_col_names() RETURNS VOID AS
    $BODY$
        DECLARE
            q boolean;
        BEGIN                       

             -- rename name column to Name
             select column_exists('National_Parks', 'name') as q;
             IF q = TRUE THEN
            alter table "National_Parks"
            rename column name to "Name";
             END IF;    

             -- remance descriptio column to description
             select column_exists('National_Parks', 'descriptio') as q;
             IF q = TRUE THEN
            alter table "Natioanl_Parks"
            rename column descriptio to "Description";
             END IF;    

        END
    $BODY$
LANGUAGE plpgsql

What is the syntax I need to use to call the sequentially, say in another script? I tried

select correct_col_names()

and this returns the following error:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "correct_col_names" line 7 at SQL statement

********** Error **********

ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Context: PL/pgSQL function "correct_col_names" line 7 at SQL statement

TIA.

Upvotes: 0

Views: 1355

Answers (1)

Scott S
Scott S

Reputation: 2746

The problem is that you have SELECT statements that aren't doing anything with the data. Your

select column_exists('National_Parks', 'name') as q;

should be

select column_exists('National_Parks', 'name') INTO q;

The as simply aliases the result as "q" for that query, it doesn't actually put it into the q variable.

Your syntax for calling the functions (select correct_col_names()) is correct for SQL. Once you fix the two errors in that function, it should work.

However, if you were to try select correct_col_names() inside another PL/PGSQL function, you would get the same error, because the select statement isn't actually doing anything with the results. perform correct_col_names() would run without error, because PERFORM is PL/PGSQL syntax for calling something when you don't want to save the result.

Upvotes: 3

Related Questions