Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TATRL-0006Sv-HY for pgsql-sql@postgresql.org; Sat, 08 Sep 2012 22:23:47 +0000 Received: from mail-pb0-f46.google.com ([209.85.160.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TATRJ-0004m3-7X for pgsql-sql@postgresql.org; Sat, 08 Sep 2012 22:23:46 +0000 Received: by pbbrr13 with SMTP id rr13so1237768pbb.19 for ; Sat, 08 Sep 2012 15:23:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=user-agent:date:subject:from:to:message-id:thread-topic :mime-version:content-type:x-gm-message-state; bh=EoKO45gvyMV530rlgpJMghNAXKpRIU2DTgQ44amaAtQ=; b=fpca3O5vX0IK6CpgDtvEvAwVwpZdRzIQyK3/HwI6hqyIEp+i/FZio6LVQZQ5w9u2zS BrkNLbhPEIxshQ9bEbz5lVm0JqYOSOufqBNWuIZ9hgeMoCbqFme3h1rYUb0e+SYlU80/ RZGn8BPP9r3XswVe76WHuPLId7I6QVbvjFl6FOu3HKZjnjRD56j60Z+MayEg1Bk5swzm qp8A569mVRTyVD5QM8z8u6fS536LDd1f+O/hdbo9NUBEcTOBFqk63QgpXHpkeTiC4BeZ zsLZTUijoGjKl4wMi//1/dZTydbfTcuW/a/MoMiad1nZyUncArZG9vbn3KbWXdjYZVj5 WN4w== Received: by 10.68.200.227 with SMTP id jv3mr17553197pbc.162.1347143023606; Sat, 08 Sep 2012 15:23:43 -0700 (PDT) Received: from [192.168.1.22] (adsl-074-245-040-156.sip.clt.bellsouth.net. [74.245.40.156]) by mx.google.com with ESMTPS id qp6sm6030926pbc.55.2012.09.08.15.23.40 (version=SSLv3 cipher=OTHER); Sat, 08 Sep 2012 15:23:42 -0700 (PDT) User-Agent: Microsoft-MacOutlook/14.2.3.120616 Date: Sat, 08 Sep 2012 18:23:36 -0400 Subject: returning values to variables from dynamic SQL From: James Sharrett To: Message-ID: Thread-Topic: returning values to variables from dynamic SQL Mime-version: 1.0 Content-type: multipart/alternative; boundary="B_3429973421_9601075" X-Gm-Message-State: ALoCoQmdzdUyhDiq/QSCPyqlL1M5h6e/UrSI2S4iZhagBP2T8xqxFhR5Hi3QTllvGIqOn7oCHMVW X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201209/13 X-Sequence-Number: 36815 > This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --B_3429973421_9601075 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit I have a PG function ( using plpgsql) that calls a number of sub functions also in plpgsql. I have two main problems that seem to be variations on the same theme of running dynamic SQL from a variable with the EXECUTE statement and returning the results back to a variable defined in the calling function. Problem 1: return the results of a table query to a variable. I have a logging table that my sub functions write to. At the beginning of my main function I want to read a run number from the logging table and increment it by one to then pass into my sub functions. I've properly declared the variable (v_runnumber) and the data type is correct. The following statement works fine in the main function and stores the value in the variable. select max(runnumber) into v_runnumber from MySchema.log_table; However, MySchema is a parameter that gets passed into the main function because I need this to work for multiple schemas. If I try and make this dynamic by using the following statement: 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 SQL state: 22004 Problem 2: returning the results of a function call to a variable. This is a similar issue to #1 but in this case, I'm calling a function from the main function and trying to get the return value back (a single integer) from the sub function to test for errors. Again, I'm calling the function with dynamical SQL because of the need to take user values from the main function to call the sub functions. The function call: sql := 'select * from public.elt_set_locking(1,' || quote_literal(tenant) || ',' || quote_literal(app) || ',' || quote_literal(cycle) || ',' || v_runnumber || ');'; execute sql; Works fine. However when I try and store the value coming back from the function into a main variable with the following call I get an error: 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" --B_3429973421_9601075 Content-type: text/html; charset="US-ASCII" Content-transfer-encoding: quoted-printable
I have a PG function ( = using plpgsql) that calls a number of sub functions also in plpgsql.  I= have two main problems that seem to be variations on the same theme of runn= ing dynamic SQL from a variable with the EXECUTE statement and returning the= results back to a variable defined in the calling function.

<= /div>
Problem 1:  return the results of a table query to a variable= .

I have a logging table that my sub functions writ= e to.  At the beginning of my main function I want to read a run number= from the logging table and increment it by one to then pass into my sub fun= ctions.  I've properly declared the variable (v_runnumber) and the data= type is correct.  The following statement works fine in the main funct= ion and stores the value in the variable.

  se= lect max(runnumber) into v_runnumber from MySchema.log_table;

=
However, MySchema is a parameter that gets passed into the main f= unction because I need this to work for multiple schemas.  If I try and= make this dynamic by using the following statement:

Sql :=3D 'select max(run number) into v_runnumber from ' || MySchema || '.lo= g_table;';
Execute Sql;

I get the followi= ng error message (even though the resulting value in the text variable Sql i= s valid code):

ERROR: query string argument of EXECUTE is null

SQL state: = 22004


=


<= p style=3D"margin: 0px; font-size: 12px; font-family: Monaco; ">Problem 2: ret= urning the results of a function call to a variable.


This is a similar issue to #1 but in t= his case, I'm calling a function from the main function and trying to get th= e return value back (a single integer) from the sub function to test for err= ors.  Again, I'm calling the function with  dynamical SQL because = of the need to take user values from the main function to call the sub funct= ions.  The function call:


sql :=3D 'select * from public.el= t_set_locking(1,' || quote_literal(tenant) || ','  || quote_literal(app= ) || ','  || quote_literal(cycle) || ','  || v_runnumber || ');';<= /p>

execute sql;


Works fine.  However when I try and store the value coming back from t= he function into a main variable with the following call I get an error:

sql :=3D 'select * into v_retcode from public.elt_s= et_locking(1,' || quote_literal(tenant) || ','  || quote_literal(app) |= | ','  || quote_literal(cycle) || ','  || v_runnumber || ');';
 execute sql;

"EXECUTE of SELECT = ... INTO is not implemented"
--B_3429973421_9601075--