Re: returning values from dynamic SQL to a variable

From: Sergey Konoplev <gray(dot)ru(at)gmail(dot)com>
To: James Sharrett <jsharrett(at)tidemark(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: returning values from dynamic SQL to a variable
Date: 2012-09-10 11:15:33
Message-ID: CAL_0b1vmtwqqjK4B2o7p9n3CWnV0SqGw6qms2zjm0oRfak=Myg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Sep 8, 2012 at 11:39 PM, James Sharrett <jsharrett(at)tidemark(dot)net> wrote:
> Sql := 'select max(run number) into v_runnumber from ' || MySchema ||
> '.log_table;';
> Execute Sql;
>
> I get the following error message (even though the resulting value in the
> text variable Sql is valid code):
>
> ERROR: query string argument of EXECUTE is null

It means that the MySchema variable is NULL.

(it smells like you might have a character case issue here)

> sql := 'select * into v_retcode from public.elt_set_locking(1,' ||
> quote_literal(tenant) || ',' || quote_literal(app) || ',' ||
> quote_literal(cycle) || ',' || v_runnumber || ');';
> execute sql;
>
> "EXECUTE of SELECT ... INTO is not implemented"

Just remove "into v_retcode" from the sql string and specify it in the
EXECUTE like this:

sql := 'select * from public.elt_set_locking(...';

EXECUTE sql INTO v_retcode;

Here you will find more info about EXECUTE in plpgsql.

http://www.postgresql.org/docs/9.1/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

--
Sergey Konoplev

a database and software architect
http://www.linkedin.com/in/grayhemp

Jabber: gray(dot)ru(at)gmail(dot)com Skype: gray-hemp Phone: +79160686204

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gary Stainburn 2012-09-11 14:42:09 weird join producing too many rows
Previous Message David Johnston 2012-09-10 01:49:09 Re: Query with LIMIT clause