|
|
 | | From: | Tjibbe Rijpma | | Subject: | How the get variables out of a plggsql RECORD by column number | | Date: | Tue, 18 Jan 2005 00:45:59 +0100 |
|
|
 | This is a multi-part message in MIME format.
------=_NextPart_000_01C3_01C4FCF7.141FB340 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
A record type contians the 1st row form dynamic query result.=20
From that row I want to select the 1st column.
How do I do that?
CREATE OR REPLACE FUNCTION dswz.save_query (TEXT) RETURNS BOOL AS ' DECLARE _query ALIAS FOR $1; _temp_query TEXT; _row RECORD; _id INT8; _type INT8;
BEGIN /* select the 1st row*/ _temp_query :=3D ''SELECT * FROM ('' || _query || '') AS table_source = LIMIT 1'';
FOR _row IN EXECUTE temp_query LOOP
/* select the 1st column */ _id :=3D ????_row[0]?????? =20 =20 END LOOP;
/* Saves the query and his type */ SELECT INTO _type type FROM objects WHERE id =3D _id; INSERT INTO queries VALUES (_query, _type);
RETURN TRUE; END=20
' LANGUAGE 'plpgsql' SECURITY DEFINER; ------=_NextPart_000_01C3_01C4FCF7.141FB340 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
charset=3Diso-8859-1">
A record = type contians the=20 1st row form dynamic query result.
From that row I want to select the 1st=20 column.
How do I do that?
CREATE OR REPLACE FUNCTION = dswz.save_query (TEXT)=20 RETURNS BOOL AS ' DECLARE _query ALIAS FOR $1; _temp_query =
TEXT; _row RECORD; _id INT8;
_type INT8;
BEGIN
/* select the 1st row*/
_temp_query :=3D ''SELECT * FROM ('' || = _query || '')=20 AS table_source LIMIT 1'';
FOR _row IN EXECUTE temp_query LOOP
/* select the 1st column */
_id :=3D =20 ????_row[0]?????? END = LOOP;
/* Saves the query and his type */
SELECT INTO _type type FROM objects WHERE id =3D _id;
INSERT INTO queries VALUES (_query, _type);
RETURN TRUE; END
' LANGUAGE 'plpgsql' SECURITY DEFINER;
------=_NextPart_000_01C3_01C4FCF7.141FB340--
|
|
 | | From: | Jeff Eckermann | | Subject: | Re: How the get variables out of a plggsql RECORD by column number | | Date: | Tue, 18 Jan 2005 06:41:08 -0800 (PST) |
|
|
 | --- Tjibbe Rijpma wrote:
> A record type contians the 1st row form dynamic > query result. > > From that row I want to select the 1st column. > > How do I do that?
By name, i.e. "_row.firstfieldname". Perhaps you are looking for a generic function, that can be applied to any table? Pl/pgsql is not equipped to do that. I believe that you can achieve what you want with pl/tcl, pl/python or pl/perl (not sure about the last).
> > > CREATE OR REPLACE FUNCTION dswz.save_query (TEXT) > RETURNS BOOL AS ' > DECLARE > _query ALIAS FOR $1; > _temp_query TEXT; > _row RECORD; > _id INT8; > _type INT8; > > BEGIN > /* select the 1st row*/ > _temp_query := ''SELECT * FROM ('' || _query || '') > AS table_source LIMIT 1''; > > FOR _row IN EXECUTE temp_query LOOP > > /* select the 1st column */ > _id := ????_row[0]?????? > > END LOOP; > > /* Saves the query and his type */ > SELECT INTO _type type FROM objects WHERE id = _id; > INSERT INTO queries VALUES (_query, _type); > > > RETURN TRUE; > END > > ' LANGUAGE 'plpgsql' SECURITY DEFINER;
__________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250
---------------------------(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
|
|
 | | From: | Keith Worthington | | Subject: | Re: How the get variables out of a plggsql RECORD by column number | | Date: | Tue, 18 Jan 2005 13:54:00 -0500 |
|
|
 | > > A record type contians the 1st row form dynamic > > query result. > > > > From that row I want to select the 1st column. > > > > How do I do that? >
Can you use a simple LOOP?
DECLARE rcrd_data RECORD
FOR rcrd_data IN SELECT tbl_source.first_column, tbl_source.second_column, FROM tbl_source LOOP -- Now rcrd_data.first_column has data. END LOOP;
See http://www.postgresql.org/docs/7.4/interactive/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING for more information.
Kind Regards, Keith
PS Caution, I am still a novice myself.
______________________________________________ 99main Internet Services http://www.99main.com
---------------------------(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
|
|
 | | From: | Tjibbe Rijpma | | Subject: | Re: How the get variables out of a plggsql RECORD by column number | | Date: | Wed, 19 Jan 2005 11:09:59 +0100 |
|
|
 | Yes I'm looking for a generic function, because I do not know the fieldnames.
Is there no possibility to insert the query result into a ARRAY and then select _result[0][0]? (I didn't get it work.....)
Or to get the fieldnames?
Pl/tcl, pl/python or pl/perl are not installed by my provider and I have no experience with it.
Tjibbe ----- Original Message ----- From: "Jeff Eckermann" To: "Tjibbe Rijpma" ;
Sent: Tuesday, January 18, 2005 15:41 Subject: Re: [NOVICE] How the get variables out of a plggsql RECORD by column number
> --- Tjibbe Rijpma > wrote: > > > A record type contians the 1st row form dynamic > > query result. > > > > From that row I want to select the 1st column. > > > > How do I do that? > > By name, i.e. "_row.firstfieldname". Perhaps you are > looking for a generic function, that can be applied to > any table? Pl/pgsql is not equipped to do that. I > believe that you can achieve what you want with > pl/tcl, pl/python or pl/perl (not sure about the > last). > > > > > > > CREATE OR REPLACE FUNCTION dswz.save_query (TEXT) > > RETURNS BOOL AS ' > > DECLARE > > _query ALIAS FOR $1; > > _temp_query TEXT; > > _row RECORD; > > _id INT8; > > _type INT8; > > > > BEGIN > > /* select the 1st row*/ > > _temp_query := ''SELECT * FROM ('' || _query || '') > > AS table_source LIMIT 1''; > > > > FOR _row IN EXECUTE temp_query LOOP > > > > /* select the 1st column */ > > _id := ????_row[0]?????? > > > > END LOOP; > > > > /* Saves the query and his type */ > > SELECT INTO _type type FROM objects WHERE id = _id; > > INSERT INTO queries VALUES (_query, _type); > > > > > > RETURN TRUE; > > END > > > > ' LANGUAGE 'plpgsql' SECURITY DEFINER; > > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - now with 250MB free storage. Learn more. > http://info.mail.yahoo.com/mail_250 >
---------------------------(end of broadcast)--------------------------- TIP 3: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to majordomo@postgresql.org so that your message can get through to the mailing list cleanly
|
|
 | | From: | Jeff Eckermann | | Subject: | Re: How the get variables out of a plggsql RECORD by column number | | Date: | Wed, 19 Jan 2005 08:04:42 -0800 (PST) |
|
|
 | --- Tjibbe Rijpma wrote:
> Yes I'm looking for a generic function, because I do > not know the > fieldnames. > > Is there no possibility to insert the query result > into a ARRAY and then > select _result[0][0]? (I didn't get it work.....) > > Or to get the fieldnames?
You could query the system catalogs. I haven't tried this, but: a query like "select column_name into columnnamevariable from information_schema.columns where table_name = whatever and ordinal_position = 1" should get you what you want.
> > Pl/tcl, pl/python or pl/perl are not installed by my > provider and I have no > experience with it. > > > Tjibbe > ----- Original Message ----- > From: "Jeff Eckermann" > To: "Tjibbe Rijpma" ; > > Sent: Tuesday, January 18, 2005 15:41 > Subject: Re: [NOVICE] How the get variables out of a > plggsql RECORD by > column number > > > > --- Tjibbe Rijpma > > wrote: > > > > > A record type contians the 1st row form dynamic > > > query result. > > > > > > From that row I want to select the 1st column. > > > > > > How do I do that? > > > > By name, i.e. "_row.firstfieldname". Perhaps you > are > > looking for a generic function, that can be > applied to > > any table? Pl/pgsql is not equipped to do that. > I > > believe that you can achieve what you want with > > pl/tcl, pl/python or pl/perl (not sure about the > > last). > > > > > > > > > > > CREATE OR REPLACE FUNCTION dswz.save_query > (TEXT) > > > RETURNS BOOL AS ' > > > DECLARE > > > _query ALIAS FOR $1; > > > _temp_query TEXT; > > > _row RECORD; > > > _id INT8; > > > _type INT8; > > > > > > BEGIN > > > /* select the 1st row*/ > > > _temp_query := ''SELECT * FROM ('' || _query || > '') > > > AS table_source LIMIT 1''; > > > > > > FOR _row IN EXECUTE temp_query LOOP > > > > > > /* select the 1st column */ > > > _id := ????_row[0]?????? > > > > > > END LOOP; > > > > > > /* Saves the query and his type */ > > > SELECT INTO _type type FROM objects WHERE id = > _id; > > > INSERT INTO queries VALUES (_query, _type); > > > > > > > > > RETURN TRUE; > > > END > > > > > > ' LANGUAGE 'plpgsql' SECURITY DEFINER; > > > > > > > > > > __________________________________ > > Do you Yahoo!? > > Yahoo! Mail - now with 250MB free storage. Learn > more. > > http://info.mail.yahoo.com/mail_250 > > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please > send an appropriate > subscribe-nomail command to > majordomo@postgresql.org so that your > message can get through to the mailing list > cleanly >
__________________________________ Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses. http://promotions.yahoo.com/new_mail
---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
|
|
|