[SQL] dblink inside plpgsql function
Hi everybody, I created the following function: CREATE OR REPLACE FUNCTION GetReminderServices( varchar ) RETURNS SETOF reminder_services AS' BEGIN SELECT dblink_connect(''dbname=''||$1); SELECT * FROM dblink(''SELECT * FROM reminder_services'') AS reminder_services( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT); SELECT dblink_disconnect($1); RETURN; END; ' LANGUAGE plpgsql; When I call this function as SELECT * FROM GetReminderServices('eu'); I get the following errors: 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 "getreminderservices" line 2 at SQL statement Does somebody know which is the problem? Best, Loredana
Re: [SQL] dblink inside plpgsql function
Hello Every SELECT statement in PL/pgSQL have to be forward to variables. In plpgsql you can you use select only like select into variables columns from ... propably better version is (i haven't installed dblink and can't to test it) CREATE OR REPLACE FUNCTION GetReminderServices( varchar ) RETURNS SETOF reminder_services AS' DECLARE r record; BEGIN PERFORM dblink_connect(''dbname=''||$1); FOR r IN SELECT * FROM dblink(''SELECT * FROM reminder_services'') AS reminder_services( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT) LOOP RETURN NEXT r; END LOOP; PERFORM dblink_disconnect($1); RETURN END; ' LANGUAGE plpgsql; regards Pavel 2007/7/3, Loredana Curugiu <[EMAIL PROTECTED]>: Hi everybody, I created the following function: CREATE OR REPLACE FUNCTION GetReminderServices( varchar ) RETURNS SETOF reminder_services AS' BEGIN SELECT dblink_connect(''dbname=''||$1); SELECT * FROM dblink(''SELECT * FROM reminder_services'') AS reminder_services( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT); SELECT dblink_disconnect($1); RETURN; END; ' LANGUAGE plpgsql; When I call this function as SELECT * FROM GetReminderServices('eu'); I get the following errors: 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 "getreminderservices" line 2 at SQL statement Does somebody know which is the problem? Best, Loredana ---(end of broadcast)--- TIP 2: Don't 'kill -9' the postmaster
Re: [SQL] dblink inside plpgsql function
What Pavel mentions might indeed be an issue, but I think there's another one too. I think you have to call your function giving more information to the statement on what types will be returned. Since you use dblink I assume that the table or view reminder_services you are basically selecting from is in another database than the current one. That would mean that the type reminder_services is not known in the current database. In that case you should call your function just like your function calls the dblink function: by adding an AS clause to your select. So NOT SELECT * FROM GetReminderServices('eu'); but SELECT * FROM GetReminderServices('eu') AS ( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT); I think as an alternative you could define a type CREATE TYPE reminder_services AS ( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT); and then call your function like you did: SELECT * FROM GetReminderServices('eu'); This second way would work with plperl but I haven't tested it with plpgsql. Since I don't have dblink installed I haven't tested the first option either. Good luck! >>> "Pavel Stehule" <[EMAIL PROTECTED]> 2007-07-03 11:13 >>> Hello Every SELECT statement in PL/pgSQL have to be forward to variables. In plpgsql you can you use select only like select into variables columns from ... propably better version is (i haven't installed dblink and can't to test it) CREATE OR REPLACE FUNCTION GetReminderServices( varchar ) RETURNS SETOF reminder_services AS' DECLARE r record; BEGIN PERFORM dblink_connect(''dbname=''||$1); FOR r IN SELECT * FROM dblink(''SELECT * FROM reminder_services'') AS reminder_services( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT) LOOP RETURN NEXT r; END LOOP; PERFORM dblink_disconnect($1); RETURN END; ' LANGUAGE plpgsql; regards Pavel 2007/7/3, Loredana Curugiu <[EMAIL PROTECTED]>: > Hi everybody, > > I created the following function: > > CREATE OR REPLACE FUNCTION GetReminderServices( varchar ) RETURNS SETOF > reminder_services AS' > BEGIN > SELECT dblink_connect(''dbname=''||$1); > SELECT * FROM dblink(''SELECT * FROM reminder_services'') > AS reminder_services( uid INT, > theme_uid INT, > activity_MT_amount > INT, > activity_min_days > INT, > activity_max_months > INT, > inactivity_days INT, > limit_reminders INT, > limit_months INT, > scanning_time > TIMESTAMP WITH TIME ZONE, > reminder_time > TIMESTAMP WITH TIME ZONE, > message TEXT); > SELECT dblink_disconnect($1); > RETURN; > END; > ' LANGUAGE plpgsql; > > When I call this function as SELECT * FROM GetReminderServices('eu'); > I get the following errors: > 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 "getreminderservices" line 2 at SQL statement > > Does somebody know which is the problem? > > > Best, > Loredana > > > > ---(end of broadcast)--- TIP 2: Don't 'kill -9' the postmaster
Re: [SQL] dblink inside plpgsql function
I created the following function CREATE OR REPLACE FUNCTION getReminderServices( varchar ) RETURNS SETOF reminder_services AS' DECLARE r reminder_services%ROWTYPE; BEGIN SELECT dblink_connect(''dbname=''||$1); FOR r IN SELECT * FROM dblink(''SELECT * FROM reminder_services'') AS columns( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT) LOOP RETURN NEXT r; END LOOP; SELECT dblink_disconnect($1); RETURN; END; ' LANGUAGE plpgsql; and I get the same errors. I think it is a problem with the dblink because the following function it works fine if I call SELECT * FROM getReminders(). CREATE OR REPLACE FUNCTION getReminders() RETURNS SETOF reminder_services AS' DECLARE r reminder_services%ROWTYPE; BEGIN FOR r IN SELECT * FROM reminder_services LOOP RETURN NEXT r; END LOOP; RETURN; END; ' LANGUAGE plpgsql;
Re: [SQL] dblink inside plpgsql function
Loredana Curugiu wrote: I created the following function CREATE OR REPLACE FUNCTION getReminderServices( varchar ) RETURNS SETOF reminder_services AS' DECLARE r reminder_services%ROWTYPE; BEGIN SELECT dblink_connect(''dbname=''||$1); ^^^ and I get the same errors. I think it is a problem with the dblink because the following function it works fine if I call SELECT * FROM getReminders(). You still haven't fixed the line above. The same rules apply to all SELECTs -- Richard Huxton Archonet Ltd ---(end of broadcast)--- TIP 4: Have you searched our list archives? http://archives.postgresql.org
Re: [SQL] dblink inside plpgsql function
CREATE OR REPLACE FUNCTION getReminderServices( varchar ) RETURNS SETOF reminder_services AS' DECLARE r reminder_services%ROWTYPE; BEGIN PERFORM dblink_connect(''dbname=''||$1); FOR r IN SELECT * FROM dblink(''SELECT * FROM reminder_services'') AS columns( uid INT, theme_uid INT, activity_MT_amount INT, activity_min_days INT, activity_max_months INT, inactivity_days INT, limit_reminders INT, limit_months INT, scanning_time TIMESTAMP WITH TIME ZONE, reminder_time TIMESTAMP WITH TIME ZONE, message TEXT) LOOP RETURN NEXT r; END LOOP; PERFORM dblink_disconnect($1); RETURN; END; ' LANGUAGE plpgsql; Now I get the errors: connection "eu" not available CONTEXT: SQL statement "SELECT dblink_disconnect( $1 )" PL/pgSQL function "getreminderservices" line 21 at perform
Re: [SQL] dblink inside plpgsql function
Loredana Curugiu wrote: CREATE OR REPLACE FUNCTION getReminderServices( varchar ) RETURNS SETOF reminder_services AS' DECLARE r reminder_services%ROWTYPE; BEGIN PERFORM dblink_connect(''dbname=''||$1); PERFORM dblink_disconnect($1); RETURN; END; ' LANGUAGE plpgsql; Now I get the errors: connection "eu" not available CONTEXT: SQL statement "SELECT dblink_disconnect( $1 )" PL/pgSQL function "getreminderservices" line 21 at perform Well, it's complaining that a connection called "eu" isn't available in the dblink_disconnect() call. I don't use dblink much myself, so I approached this problem by looking in the documentation. According to the docs, there are two ways to call dblink_disconnect() " Synopsis dblink_disconnect() dblink_disconnect(text connname) Inputs connname if an argument is given, it is used as a name for a persistent connection to close; otherwiase the unnamed connection is closed " So - did we open a connection called "eu"? Looking at the docs again, it appears no! If we use the one-argument version of dblink_connect() we get an unnamed connection. So - either change the dblink_disconnect so there is no argument: dblink_disconnect() Or change the connection dblink_connect($1, ''dbname='' || $1) I'd do the first one, since you don't care what the connection is called and are closing it at the end of the function. HTH -- Richard Huxton Archonet Ltd ---(end of broadcast)--- TIP 7: You can help support the PostgreSQL project by donating at http://www.postgresql.org/about/donate
Re: [SQL] dblink inside plpgsql function
I should read the documentation carrefully .. Yes, you're right, Richard. Many thanks to all. Best, Loredana
Re: [SQL] Using escape strings in an insert statement.
On Jul 2, 2007, at 6:11 PM, Michael Glaesemann wrote: On Jul 2, 2007, at 17:45 , Paul Lambert wrote: tester=# insert into testing (test_text) values ('abcE'\\'123'); This should be INSERT INTO testing (test_text) values (E'abc\123'); No, that will leave him with the string 'abc23' beinginserted, he wants the backslash to be included in the string, that's why he had two, so it should be: INSERT INTO testing (test_text) values (E'abc\\123'); The help itself (ch 4.1.2.1) tells me to use double backslash "Thus, to include a backslash character, write two backslashes (\ \). " Note that the String Constants section (4.1.2.1) says put the E "before the opening single quote". http://www.postgresql.org/docs/8.2/interactive/sql-syntax- lexical.html#SQL-SYNTAX-CONSTANTS An escape string constant is specified by writing the letter E (upper or lower case) just before the opening single quote, e.g. E'foo'. Also be sure to read the Caution section. Using \ as an escape character is the old non-standard PostgreSQL escape syntax that the WARNING (above) is, uh, warning you about. With standard_conforming_strings on (i.e., follow the SQL spec), the backslash is just a backslash character. Which one is the correct syntax and how can I make it not return anything other than a successful insert? Depends on the setting of standard_conforming_strings. With standard_conforming_strings turned on, it would just need to be: INSERT INTO test (test_text) values ('abc\123'); Michael Glaesemann grzm seespotcode net ---(end of broadcast)--- TIP 7: You can help support the PostgreSQL project by donating at http://www.postgresql.org/about/donate Erik Jones Software Developer | Emma® [EMAIL PROTECTED] 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com ---(end of broadcast)--- TIP 4: Have you searched our list archives? http://archives.postgresql.org
[SQL] Informix Schema -> PostgreSQL ?
I am an ex-Informix convert. Informix used the term "schema" to refer to the SQL-format definition of how a table or view was created. E.g., CREATE TABLE john ( char(8) lid, ...); Some views we have are quite complex (and not created by me) and I want to create a similar one in Pg. If I could see the view in this SQL format, then I could use SQL to create another one using this as a template. pgadmin3 can show this definition in SQL format, but I can't use pgadmin3 on a certain box. How can I show information in Pg (psql) the way that Informix would show a schema? BTW, what does PostgreSQL call this (what Informix calls a schema)?? Mark ---(end of broadcast)--- TIP 4: Have you searched our list archives? http://archives.postgresql.org
Re: [SQL] Informix Schema -> PostgreSQL ?
On Tue, 2007-07-03 at 12:22 -0400, Mark Fenbers wrote: > I am an ex-Informix convert. Informix used the term "schema" to refer > to the SQL-format definition of how a table or view was created. E.g., > CREATE TABLE john ( char(8) lid, ...); Some views we have are quite > complex (and not created by me) and I want to create a similar one in > Pg. If I could see the view in this SQL format, then I could use SQL to > create another one using this as a template. > > pgadmin3 can show this definition in SQL format, but I can't use > pgadmin3 on a certain box. How can I show information in Pg (psql) the > way that Informix would show a schema? > > BTW, what does PostgreSQL call this (what Informix calls a schema)?? Just use pg_dump to dump/backup the schema. Don't include the data; it is just like doing an Informix "dbschema -d {database}" pg_dump -U OGo --schema-only OGo -- Adam Tauno Williams, Network & Systems Administrator Consultant - http://www.whitemiceconsulting.com Developer - http://www.opengroupware.org ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
Re: [SQL] Informix Schema -> PostgreSQL ?
Mark Fenbers wrote: I am an ex-Informix convert. Informix used the term "schema" to refer to the SQL-format definition of how a table or view was created. E.g., CREATE TABLE john ( char(8) lid, ...); Some views we have are quite complex (and not created by me) and I want to create a similar one in Pg. If I could see the view in this SQL format, then I could use SQL to create another one using this as a template. pgadmin3 can show this definition in SQL format, but I can't use pgadmin3 on a certain box. How can I show information in Pg (psql) the way that Informix would show a schema? BTW, what does PostgreSQL call this (what Informix calls a schema)?? Oddly enough, it's the same thing. There's schema, the object, which holds related objects inside it. databases contain schemas which contain tables, indexes, etc... Then there's schema, as a definition of how something it put together. Confusing, I know. The easiest way to view the sql format definition of a view is the use the pg_views view... select * from pg_views where viewname='nameofview'; You can get the same thing with pg_dump: pg_dump dbname -s -t tableorindexname ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
Re: [SQL] Using escape strings in an insert statement.
On Jul 3, 2007, at 10:49 , Erik Jones wrote: On Jul 2, 2007, at 6:11 PM, Michael Glaesemann wrote: On Jul 2, 2007, at 17:45 , Paul Lambert wrote: tester=# insert into testing (test_text) values ('abcE'\\'123'); This should be INSERT INTO testing (test_text) values (E'abc\123'); No, that will leave him with the string 'abc23' beinginserted, he wants the backslash to be included in the string, that's why he had two, so it should be: INSERT INTO testing (test_text) values (E'abc\\123'); Ah, right. Thanks for the correction, Erik. Michael Glaesemann grzm seespotcode net ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
[SQL] Iterate and write a previous row to a temp table?
Revisiting a Time In Status query I received help on - I'm trying to narrow down a subset of data I return for analysis. Given a statusLog as entityId, statusId, timestamp that might look something like entityId | statusId | timestamp 001 | HLD | 2007-06-14 11:07:35.93 001 | RDY | 2007-06-15 11:07:35.93 001 | USE | 2007-06-16 11:07:35.93 001 | RDY | 2007-06-17 11:07:35.93 001 | MNT | 2007-06-18 11:07:35.93 I need to pull for a given span of time - say 2007-06-16 00:00:00.01 (let me call it startTime) to 2007-06-17 23:59:59.99 (call it endTime) in such a way that rows with a timestamp between startTime and endTime AND the latest record prior to or equal to startTime are returned. In the above simplified example, only the second and third rows would be returned. A colleague suggested a temp table, but I'm unsure how to iterate until I pass the startTime and then write the _previous_ and all subsequent rows to a temp table, stopping when I pass the endTime parameter. Any hints? Thanks! Bob Singleton ---(end of broadcast)--- TIP 7: You can help support the PostgreSQL project by donating at http://www.postgresql.org/about/donate
Re: [SQL] Iterate and write a previous row to a temp table?
--- Bob Singleton <[EMAIL PROTECTED]> wrote: > Revisiting a Time In Status query I received help on - I'm trying to > narrow down a subset of data I return for analysis. > > Given a statusLog as entityId, statusId, timestamp that might look > something like > > entityId | statusId | timestamp > > 001 | HLD | 2007-06-14 11:07:35.93 > 001 | RDY | 2007-06-15 11:07:35.93 > 001 | USE | 2007-06-16 11:07:35.93 > 001 | RDY | 2007-06-17 11:07:35.93 > 001 | MNT | 2007-06-18 11:07:35.93 > > I need to pull for a given span of time - say 2007-06-16 00:00:00.01 > (let me call it startTime) to 2007-06-17 23:59:59.99 (call it > endTime) > in such a way that rows with a timestamp between startTime and > endTime > AND the latest record prior to or equal to startTime are returned. In > > the above simplified example, only the second and third rows would be > > returned. > > A colleague suggested a temp table, but I'm unsure how to iterate > until > I pass the startTime and then write the _previous_ and all subsequent > > rows to a temp table, stopping when I pass the endTime parameter. > > Any hints? > > Thanks! > Bob Singleton > couldn't you use the simple query: select * from sometable where timestamp between (select max(timestamp) from sometable where timestamp <= minTime) and maxTime Finding fabulous fares is fun. Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains. http://farechase.yahoo.com/promo-generic-14795097 ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] Iterate and write a previous row to a temp table?
Bob Singleton wrote: Revisiting a Time In Status query I received help on - I'm trying to narrow down a subset of data I return for analysis. Given a statusLog as entityId, statusId, timestamp that might look something like entityId | statusId | timestamp 001 | HLD | 2007-06-14 11:07:35.93 001 | RDY | 2007-06-15 11:07:35.93 001 | USE | 2007-06-16 11:07:35.93 001 | RDY | 2007-06-17 11:07:35.93 001 | MNT | 2007-06-18 11:07:35.93 I need to pull for a given span of time - say 2007-06-16 00:00:00.01 (let me call it startTime) to 2007-06-17 23:59:59.99 (call it endTime) in such a way that rows with a timestamp between startTime and endTime AND the latest record prior to or equal to startTime are returned. In the above simplified example, only the second and third rows would be returned. Can't be done, because you don't have a primary key, so no way to distinguish between duplicate rows. However, if you just eliminate duplicates you could just use a function like (not tested): CREATE FUNCTION ranged( startTime timestamp with time zone, endTime timestamp with time zone, ) RETURNS SETOF statusLog AS $$ SELECT entityid,statusid,timestamp FROM statusLog WHERE timestamp BETWEEN startTime AND endTime UNION SELECT entityid,statusid,timestamp FROM statusLog WHERE timestamp <= startTime ORDER BY timestamp DESC LIMIT 1 ORDER BY $$ LANGUAGE SQL; Note that UNION eliminates duplicates, if you want to keep them use "UNION ALL" HTH -- Richard Huxton Archonet Ltd ---(end of broadcast)--- TIP 7: You can help support the PostgreSQL project by donating at http://www.postgresql.org/about/donate
Re: [SQL] Informix Schema -> PostgreSQL ?
> pg_dump dbname -s -t tableorindexname [Also an Informix DBA] Is there a way to tweak the output of pg_dump when used in this manner to omit the verbose commentary. $ pg_dump OGo -s -t enterprise -- -- Name: unique_enterprise_login; Type: INDEX; Schema: public; Owner: OGo; Tablespace: -- CREATE UNIQUE INDEX unique_enterprise_login ON enterprise USING btree ("login"); The "--" lines just eats up screen real estate. -- Adam Tauno Williams, Network & Systems Administrator Consultant - http://www.whitemiceconsulting.com Developer - http://www.opengroupware.org ---(end of broadcast)--- TIP 4: Have you searched our list archives? http://archives.postgresql.org
Re: [SQL] Informix Schema -> PostgreSQL ?
On 7/3/07, Adam Tauno Williams <[EMAIL PROTECTED]> wrote: > pg_dump dbname -s -t tableorindexname [Also an Informix DBA] Is there a way to tweak the output of pg_dump when used in this manner to omit the verbose commentary. no AFAIK. dbexport and dbschema doesn't have that either, or they have? -- regards, Jaime Casanova "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the universe trying to produce bigger and better idiots. So far, the universe is winning." Richard Cook ---(end of broadcast)--- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
Re: [SQL] Informix Schema -> PostgreSQL ?
Adam Tauno Williams skrev: >> pg_dump dbname -s -t tableorindexname > > [Also an Informix DBA] Is there a way to tweak the output of pg_dump > when used in this manner to omit the verbose commentary. > > $ pg_dump OGo -s -t enterprise pg_dump OGo -s -t enterprise | grep -v '^--$' Nis ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
Re: [SQL] Query Problem from FoxPro???
Dear All, It turns out that, column "is_pilih" was written with "Is_Pilih". PostgreSQL column name is case sensitive! After I change the column name to "is_pilih" everything's showed up! Another NEW Question arose: "Why the is_pilih data type become Character when it displayed in VFP?" Anybody can help? T.I.A. Regards, djDevX Query Problem from FoxPro??? - From: dBHS Jakarta To: pgsql-sql ( at ) postgresql ( dot ) org Subject: Query Problem from FoxPro??? Date: Sat, 30 Jun 2007 20:47:04 -0700 (PDT) - Dear all, I am try to migrate from VFP back-end database to PostgreSQL. Table: mst_lang Columns: lang_id Char (2) lang_nm Char (20) is_pil Boolean I try to query from FoxPro via ADODB, Recordset using this SelectCmd: "SELECT * FROM mst_lang" Everything is showed. When I try to query using: "SELECT lang_id, lang_nm, is_pil FROM mst_lang" No Results showed... Does anybody know what's the problem is? Regards, djDevX - Luggage? GPS? Comic books? Check out fitting gifts for grads at Yahoo! Search.