473,549 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SELECT INTO broken (?) in PL/pgSQL when using a set returning function in FROM clause (BGUG?)

I'm been banging my head over this for a little while now.

Here's a simple function to return a record:

<code>
CREATE OR REPLACE FUNCTION create_item_rec ord_for_overrid e(INTEGER,
VARCHAR )
RETURNS RECORD AS '
DECLARE
rec RECORD;
BEGIN
/* Normally we would not have a separate check here. We would use IF
NOT FOUND, but that appears to be broken. */
IF NOT EXISTS(SELECT 1 FROM merchandise.ds_ item($1) WHERE "client_key " =
$2) THEN
RAISE EXCEPTION ''No base row for override. dsrc_id=%,
client_key=%"'' , $1, $2;
END IF;

SELECT INTO rec * FROM merchandise.ds_ item($1) WHERE "client_key " = %2;
IF NOT FOUND THEN
/* We should NEVER get here. The EXISTS check uses the same query
and so will raise an exception under the same conditions. IT APPEARS as
though SELECT INTO is not working when there is a function in the FROM
clause. */
RAISE EXCEPTION ''No base row for override. dsrc_id=%,
client_key=%"'' , $1, $2;
END IF;

RETURN rec;
END;
' LANGUAGE plpgsql STABLE;
</code>

Basically passing in valid parameters, one's where the result of doing
SELECT * FROM merchandise.ds_ item($1) WHERE "client_key " = %2;
on the psql command line work just fine, fail always in this function. All
I can conclude (after about 2 hours) is that we can not SELECT INTO, FROM a
set returning function in PL/pgSQL.

For the curious, here is the definition of the ds_item function.

<code>
CREATE OR REPLACE FUNCTION "merchandise".d s_item(INTEGER)
RETURNS setof "merchandise"." item" AS '
SELECT DISTINCT ON (client_key) * FROM "merchandise"." item"
WHERE "ovr_status " >= 0 AND "dsrc_id" IN (SELECT * FROM
"common".get_pa th_parts("merch andise".ds_get_ id_path($1)))
ORDER BY client_key, dsrc_id DESC;
' LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT;
</code>

Nov 12 '05 #1
3 5299
Up too late. This works fine. At least it works for some simpler test
functions under the just-downloaded 7.4.1 release.

-- Ezra Epstein

"ezra epstein" <ee************ ***@prajnait.co m> wrote in message
news:RP******** ************@sp eakeasy.net...
I'm been banging my head over this for a little while now.

Here's a simple function to return a record:

<code>
CREATE OR REPLACE FUNCTION create_item_rec ord_for_overrid e(INTEGER,
VARCHAR )
RETURNS RECORD AS '
DECLARE
rec RECORD;
BEGIN
/* Normally we would not have a separate check here. We would use IF
NOT FOUND, but that appears to be broken. */
IF NOT EXISTS(SELECT 1 FROM merchandise.ds_ item($1) WHERE "client_key " = $2) THEN
RAISE EXCEPTION ''No base row for override. dsrc_id=%,
client_key=%"'' , $1, $2;
END IF;

SELECT INTO rec * FROM merchandise.ds_ item($1) WHERE "client_key " = %2; IF NOT FOUND THEN
/* We should NEVER get here. The EXISTS check uses the same query
and so will raise an exception under the same conditions. IT APPEARS as
though SELECT INTO is not working when there is a function in the FROM
clause. */
RAISE EXCEPTION ''No base row for override. dsrc_id=%,
client_key=%"'' , $1, $2;
END IF;

RETURN rec;
END;
' LANGUAGE plpgsql STABLE;
</code>

Basically passing in valid parameters, one's where the result of doing
SELECT * FROM merchandise.ds_ item($1) WHERE "client_key " = %2;
on the psql command line work just fine, fail always in this function. All I can conclude (after about 2 hours) is that we can not SELECT INTO, FROM a set returning function in PL/pgSQL.

For the curious, here is the definition of the ds_item function.

<code>
CREATE OR REPLACE FUNCTION "merchandise".d s_item(INTEGER)
RETURNS setof "merchandise"." item" AS '
SELECT DISTINCT ON (client_key) * FROM "merchandise"." item"
WHERE "ovr_status " >= 0 AND "dsrc_id" IN (SELECT * FROM
"common".get_pa th_parts("merch andise".ds_get_ id_path($1)))
ORDER BY client_key, dsrc_id DESC;
' LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT;
</code>

Nov 12 '05 #2
"ezra epstein" <ee************ ***@prajnait.co m> writes:
IF NOT EXISTS(SELECT 1 FROM merchandise.ds_ item($1) WHERE "client_key " =
$2) THEN
[ works, but this doesn't: ]
SELECT INTO rec * FROM merchandise.ds_ item($1) WHERE "client_key " = %2;


If that's an accurate transcription, then your error is using %2 where
you meant $2.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #3
Ahhh. Yes. A nasty typo. I did a global search for %1, %2, %3, etc. and
turned up several elsewhere. Ouch. Thank you for your close reading.

== Ezra Epstein

"Tom Lane" <tg*@sss.pgh.pa .us> wrote in message
news:20******** ******@sss.pgh. pa.us...
"ezra epstein" <ee************ ***@prajnait.co m> writes:
IF NOT EXISTS(SELECT 1 FROM merchandise.ds_ item($1) WHERE "client_key " = $2) THEN
[ works, but this doesn't: ]
SELECT INTO rec * FROM merchandise.ds_ item($1) WHERE "client_key " =
%2;
If that's an accurate transcription, then your error is using %2 where
you meant $2.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10753
by: Rowland Hills | last post by:
I have a table which is returning inconsistent results when I query it! In query analyzer: If I do "SELECT * FROM TABLE_NAME" I get no rows returned. If I do "SELECT COL1, COL2 FROM TABLE_NAME" I get 4 rows returned. In Enterprise manager:
5
11489
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold { CarsSoldID int (primary key) MonthID int DealershipID int NumberCarsSold int
15
2719
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to start at the bottom. What seems to plague many ORM systems is the syntactic confusion and string-manipulation required to build the SQL Statements. If...
3
7649
by: joseph speigle | last post by:
hello list, I want to do something like the following: address=# @var = select max(id) from passwd; ERROR: parser: parse error at or near "@" at character 1 address=# var = select max(id) from passwd; ERROR: parser: parse error at or near "var" at character 1 address=# :var = select max(id) from passwd; ERROR: parser: parse error at...
3
6433
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too...
19
3516
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main form, the subform returns the records based on the two criteria. The criteria used on the main form are values selected in two list boxes. When the...
8
31239
by: Andy Kriger | last post by:
I would like to select strings from a table and return them as an array For example, select new_array(name) from my_tbl would return String { name1, name2, name3, etc } Is this possible with built-in SQL/psql functions? If not, how hard would it be to write a function that does this? (given that I have coding experience but none writing...
48
4212
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) * -1 Set oConn=Server.CreateObject("ADODB.Connection") Set oRS=Server.CreateObject("ADODB.recordset") oConn.Provider="Microsoft.Jet.OLEDB.4.0"...
22
12433
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient...
0
7971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7491
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3509
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.