473,385 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PL/PGSQL help for getting number of rows matched.


Hi,

We need to implement following logic efficiently.

SELECT * from some_table where .... [ Query 1 ]

IF rows_matched = 1 THEN

use the single row that matched.

ELSIF

loop thru the results of [Query 1]

END IF;
Currently i am doing select count(*) for getting rows_matched
in the top and repeating the same query in both branches of IF
to get the data of matching rows.

I have tried GET DIAGNOSTICS ROW_COUNT but for
"SELECTS" if returns 0 or 1 based on matching

I am sure there exists better methods. Kindly post a link
to better documentation of pl/pgsql or point out section in
the standard docs that discuss this issue.

Regds
Mallah.


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #1
4 9147
Hello,

it isn't problem. You can write

SELECT INTO ....
IF FOUND THEN
...
END IF

or

SELECT INTO ..
GET DIAGNOSTICS variable = ROW_COUNT;
IF variable > 0 THEN
...
END IF

You can see on
http://developer.postgresql.org/docs...QL-SELECT-INTO

Regards
Pavel
On Mon, 10 Nov 2003, Rajesh Kumar Mallah wrote:

Hi,

We need to implement following logic efficiently.

SELECT * from some_table where .... [ Query 1 ]

IF rows_matched = 1 THEN

use the single row that matched.

ELSIF

loop thru the results of [Query 1]

END IF;
Currently i am doing select count(*) for getting rows_matched
in the top and repeating the same query in both branches of IF
to get the data of matching rows.

I have tried GET DIAGNOSTICS ROW_COUNT but for
"SELECTS" if returns 0 or 1 based on matching

I am sure there exists better methods. Kindly post a link
to better documentation of pl/pgsql or point out section in
the standard docs that discuss this issue.

Regds
Mallah.


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2

On Mon, 10 Nov 2003, Pavel Stehule wrote:
Hello,

it isn't problem. You can write

SELECT INTO ....
IF FOUND THEN
...
END IF

or

SELECT INTO ..
GET DIAGNOSTICS variable = ROW_COUNT;
IF variable > 0 THEN
...
END IF

You can see on
http://developer.postgresql.org/docs...QL-SELECT-INTO
Probably sectino 37.7.4 of the docs ( in
http://developer.postgresql.org/docs...tructures.html
) is a better source as shown lower.

On Mon, 10 Nov 2003, Rajesh Kumar Mallah wrote:

Hi,

We need to implement following logic efficiently.

SELECT * from some_table where .... [ Query 1 ]

IF rows_matched = 1 THEN

use the single row that matched.

ELSIF

loop thru the results of [Query 1]

END IF;
Currently i am doing select count(*) for getting rows_matched
in the top and repeating the same query in both branches of IF
to get the data of matching rows.

I have tried GET DIAGNOSTICS ROW_COUNT but for
"SELECTS" if returns 0 or 1 based on matching

I am sure there exists better methods. Kindly post a link
to better documentation of pl/pgsql or point out section in
the standard docs that discuss this issue.


DECLARE
tup RECORD;
BEGIN
FOR tup IN select * from mytable
LOOP
Do the required action
END LOOP;
END

Indeed, I'm not even sure how to loop through the results of the query using
the scheme you show above. What do you assign the results of the select to?
--
Nigel Andrews
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #3
On Monday 10 Nov 2003 5:38 pm, Pavel Stehule wrote:
Hello,

it isn't problem. You can write

SELECT INTO ....
IF FOUND THEN
...
END IF
I have *different* logic for match=1
and for match > 1 , so FOUND is not a
solution as manual says.
There is a special variable named FOUND of type boolean.
FOUND starts out false within each PL/pgSQL function.
It is set by each of the following types of statements:

Section 19.5.5
http://www.postgresql.org/docs/7.3/s...tatements.html

or

SELECT INTO ..
GET DIAGNOSTICS variable = ROW_COUNT;
IF variable > 0 THEN
...
END IF

Even this does not solve my problem.
See my actual code and the output.

-- ***************** CODE *******************
CREATE OR REPLACE FUNCTION general.copy_accounts() returns integer AS
'
DECLARE
users_c CURSOR FOR SELECT userid FROM general.user_accounts where userid=46 ;
userid_v int;
i int;
matched int;
rec RECORD;
BEGIN
OPEN users_c;

i := 1;
LOOP
FETCH users_c INTO userid_v;
EXIT WHEN NOT FOUND ;

SELECT INTO rec profile_id from general.profile_master where userid=userid_v;
GET DIAGNOSTICS matched = ROW_COUNT;
RAISE INFO ''matched = % '' , matched;

SELECT INTO matched count(*) from general.profile_master where userid=userid_v;
RAISE INFO ''matched = % '' , matched;

EXIT;

END LOOP;
CLOSE users_c;
RETURN 1;
END
' LANGUAGE 'plpgsql';

-- ------ CODE ENDS
RESULTS BELOW:

tradein_clients=# SELECT copy_accounts();
INFO: matched = 1
INFO: matched = 3

Note that matched was 3 but in first place it did not come.

+---------------+
| copy_accounts |
+---------------+
| 1 |
+---------------+
(1 row)

Time: 386.76 ms
tradein_clients=#
Regds
Mallah.

You can see on
http://developer.postgresql.org/docs...nts.html#PLPGS
QL-SELECT-INTO

Regards
Pavel

On Mon, 10 Nov 2003, Rajesh Kumar Mallah wrote:
Hi,

We need to implement following logic efficiently.

SELECT * from some_table where .... [ Query 1 ]

IF rows_matched = 1 THEN

use the single row that matched.

ELSIF

loop thru the results of [Query 1]

END IF;
Currently i am doing select count(*) for getting rows_matched
in the top and repeating the same query in both branches of IF
to get the data of matching rows.

I have tried GET DIAGNOSTICS ROW_COUNT but for
"SELECTS" if returns 0 or 1 based on matching

I am sure there exists better methods. Kindly post a link
to better documentation of pl/pgsql or point out section in
the standard docs that discuss this issue.

Regds
Mallah.


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #4
On Monday 10 Nov 2003 6:05 pm, Nigel J. Andrews wrote:
DECLARE
tup RECORD;
BEGIN
FOR tup IN select * from mytable
LOOP
Do the required action
END LOOP;
END
Indeed, I'm not even sure how to loop through the results of the query
using the scheme you show above. What do you assign the results of the
select to?

My working code which i think can be improved is below
note that i treat match=1 and match>1 differently.

I hope it will answer both of your question.

Thanks everyone for the responses though :)

Pl/Pgsql itself seems to be the most mature of all PL
hence we have decided to shift our business logic from
perl layer to DataBase for obvious gains.

-- Actual Code-------

CREATE OR REPLACE FUNCTION general.copy_accounts() returns integer AS

'

DECLARE

users_c CURSOR FOR SELECT userid FROM general.user_accounts ;
userid_v int;
i int;
matched int;
rec RECORD;

BEGIN

OPEN users_c;

i := 1;

LOOP
FETCH users_c INTO userid_v;
EXIT WHEN NOT FOUND ;
SELECT INTO matched count(*) from general.profile_master where userid=userid_v;
IF matched = 1 THEN
SELECT INTO rec email,title1 , fname1 , mname1 , lname1 , desg1 , mobile from general.profile_master where userid=userid_v;

ELSIF matched > 1 THEN

-- multiple profiles then get the profile that
-- has highest score.

SELECT INTO rec email,title1 , fname1 , mname1 , lname1 , desg1 , mobile,source
from general.profile_master join
general.temp_source_priority using(source) where userid=userid_v
order by profile_score(email,title1 , fname1 , mname1 , lname1 , desg1 , mobile) desc limit 1;

END IF;

IF matched >= 1 THEN
i := i + 1;
UPDATE general.user_accounts set
email= rec.email,
title= rec.title1 ,
fname= rec.fname1 ,
mname= rec.mname1 ,
lname= rec.lname1 ,
desg = rec.desg1 ,
mobile= rec.mobile where userid = userid_v;
END IF;

IF i % 100 = 0 THEN
RAISE INFO '' copied % accounts '' , i;
END IF;
END LOOP;

CLOSE users_c;
RAISE INFO '' Successfully finished with % accounts '' , i;

RETURN 1;

END

' LANGUAGE 'plpgsql';
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #5

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

Similar topics

3
by: Xizor | last post by:
Ok, what I want to do is find out the number of rows in a table. The most obvious solution is to do something like the following: $sql = "SELECT blah FROM blah WHERE 1"; $result =...
0
by: t_pet422 | last post by:
Hi, I've been scouring the net and reading the PostgreSQL docs for a while now trying to learn how to create a recursive function in PL/pgSQL that will return a whole subtree given a starting...
1
by: Diogo Biazus | last post by:
From time to time I'm getting this error message: WARNING: Error occurred while executing PL/pgSQL function atualizabusca WARNING: line 25 at for over select rows ERROR: unknown cmdtype...
1
by: Jenny Zhang | last post by:
I have stored procedure written in pl/pgsql which takes about 13 seconds to finish. I was able to identify that the slowness is caused by one update SQL: UPDATE shopping_cart SET...
7
by: Jim Crate | last post by:
I have a couple queries in a PL/PGSQL function which execute very slowly (around one minute each) which execute in .5 second when not executed from within the function. Is there any way to...
34
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my...
2
by: Tim Vadnais | last post by:
Hi, My boss wants to add a special type of logging to some of our tables on update/delete/insert. I need to log who, when, table_name, field name, original value and new value for each record,...
11
by: UDBDBA | last post by:
Hi: This is a merge questions which has been posted and answered... in my case need more clairification when target table (tableB) matched multiple rows to be updated based on the ON condition...
7
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.