[SQL] Making a prepared statement in a stored procedure

2004-12-30 Thread Nathan Pickett
Hi,
I am trying to convert the following function below:
---START FUNCTION
create or replace function 
update_tmp_sales_report_from_archive_with_prospects() returns integer as '
declare
row_data record;
begin
for row_data in select partner_id, count(*) as prospects from 
prospects_2004_09_01
group by partner_id loop
update tmp_sales_report_from_archive set prospects = row_data.prospects
where partner_id = row_data.partner_id;
end loop;

return 1;
end;
' language 'plpgsql';
--- END FUNCTION
to be able to pass in the table named prospects_2004_09_01, to be prospects_ 
concatanated with the date, so prospects_2004_08_01, prospects_2004_07_01, 
etc.

I tried the following:
-- START TEST FUCTION
drop function update_tmp_sales_report_from_archive_with_prospects(text);
create or replace function 
update_tmp_sales_report_from_archive_with_prospects(text) returns integer as 
'
declare
in_t ALIAS FOR $1;
row_data record;
begin
for row_data in select partner_id, count(*) as prospects in_t
group by partner_id loop
update tmp_sales_report_from_archive set prospects = row_data.prospects
where partner_id = row_data.partner_id;
end loop;

return 1;
end;
' language 'plpgsql';
-- END TEST FUNCTION
but go the following errors:
DROP FUNCTION
CREATE FUNCTION
You are now connected as new user bp_sales_match_user.
psql:scratch.postgresql:36: WARNING: Error occurred while executing PL/pgSQL 
function update_tmp_sales_report_from_archive_with_prospects
psql:scratch.postgresql:36: WARNING: line 5 at for over select rows
psql:scratch.postgresql:36: ERROR: parser: parse error at or near "$1" at 
character 44

Any suggestions? Thanks! -Nate

---(end of broadcast)---
TIP 8: explain analyze is your friend


[SQL] Problem with subquery containg GROUP BY

2004-12-30 Thread Ishay Pomerantz








Hi,

I’m using postgres 7.4.

 

I have two queries:

(1)

SELECT  a,b

FROM table1

WHERE a=99

 

(2)

SELECT  a,b,sum (o)  as sum_o

FROM table2

GROUP BY a,b

 

Both Runs very fast.

 

But when I try to make (2) as a subquery
of (1):

 

SELECT  a,b,sum_o

FROM table1

LEFT JOIN  (

SELECT  a,b,sum (o)  as sum_o

        FROM
table2

GROUP BY a,b

) sub_query

USING (a,b)

WHERE a=99

 

 

It runs 100 times slower.

I guess it has to do with the postgres unable
to pass the a=99 inside the subquery

 

Any ideas ?

 

ishay








Re: [SQL] [GENERAL] Get current trasanction id

2004-12-30 Thread Martijn van Oosterhout
I'm sure there's many tricky ways, but one simple way would be to
insert a row into a table and then grab its XMIN value...

Hope this helps,

On Mon, Dec 27, 2004 at 09:52:57AM +0100, Marek Lewczuk wrote:
> Hello,
> is there any way to get current transaction id using plpgsql or sql ?
> 
> Thanks in advance for any help.
> 
> ML
> 
> 
> 
> ---(end of broadcast)---
> TIP 2: you can get off all lists at once with the unregister command
>(send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

-- 
Martijn van Oosterhout  http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.


pgprLEwiPD9Tu.pgp
Description: PGP signature


[SQL] Making a prepared statement in a stored procedure

2004-12-30 Thread Nathan Pickett
Hi,
I am trying to convert the following function below:
---START FUNCTION
create or replace function 
update_tmp_sales_report_from_archive_with_prospects() returns integer as '
declare
   row_data record;
begin
   for row_data in select partner_id, count(*) as prospects from 
prospects_2004_09_01
   group by partner_id loop
   update tmp_sales_report_from_archive set prospects = 
row_data.prospects
   where partner_id = row_data.partner_id;
   end loop;

   return 1;
end;
' language 'plpgsql';
--- END FUNCTION
to be able to pass in the table named prospects_2004_09_01, to be prospects_ 
concatanated with the date, so prospects_2004_08_01, prospects_2004_07_01, 
etc.

I tried the following:
-- START TEST FUCTION
drop function update_tmp_sales_report_from_archive_with_prospects(text);
create or replace function 
update_tmp_sales_report_from_archive_with_prospects(text) returns integer as 
'
declare
   in_t ALIAS FOR $1;
   row_data record;
begin
   for row_data in select partner_id, count(*) as prospects in_t
   group by partner_id loop
   update tmp_sales_report_from_archive set prospects = 
row_data.prospects
   where partner_id = row_data.partner_id;
   end loop;

   return 1;
end;
' language 'plpgsql';
-- END TEST FUNCTION
but go the following errors:
DROP FUNCTION
CREATE FUNCTION
You are now connected as new user bp_sales_match_user.
psql:scratch.postgresql:36: WARNING:  Error occurred while executing 
PL/pgSQL function update_tmp_sales_report_from_archive_with_prospects
psql:scratch.postgresql:36: WARNING:  line 5 at for over select rows
psql:scratch.postgresql:36: ERROR:  parser: parse error at or near "$1" at 
character 44

Any suggestions?  Thanks! -Nate

---(end of broadcast)---
TIP 4: Don't 'kill -9' the postmaster


[SQL] Function in C++

2004-12-30 Thread Jose Mendoza








 

I would like to have, only if it is possible, a small
example of how can be that done. I have this problem now because I have a very
light interface using the ODBC and with the same code I can connect to
Postgresql (servers Linux) and SQLserver (servers Windows). And the libpq++
works in C++ if I reject the idea of use ODBC.

 

Any help will be appreciated.

 

Thans

 

Jose Mendoza

[EMAIL PROTECTED]