Skip site navigation (1) Skip section navigation (2)

Re: PLPGSQL problem with SELECT INTO

From: "Jay O'Connor" <joconnor(at)cybermesa(dot)com>
To: "Jay O'Connor" <joconnor(at)cybermesa(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PLPGSQL problem with SELECT INTO
Date: 2003-05-29 22:16:52
Message-ID: 20030529151652.C1144@altaica (view raw or flat)
Thread:
Lists: pgsql-general
> OK, I'm trying to count the number of records based on two criteria, this
> works at the psql prompt but not in a plpgsql function
> 
> 	SELECT count(*) FROM mytable WHERE fieldone = 'val1' AND fieldtwo
> =
> 'val2';
> 
> This gives me back '4' which is what I expect (trust me :)
> 
> but if I try to put this in a PLPGSQL function, it doesn't work.
> 
> 	CREATE FUNCTION countRows (varchar, varchar) RETURNS int AS
> '	
> 	DECLARE
> 		val1 ALIAS FOR $1;
> 		val2 ALIAS FOR $2;
> 		total int;
> 	BEGIN
> 		SELECT INTO total count(*) FROM mytable WHERE fieldone =
> val1 AND fieldtwo = val2;
> 		RETURN total;
> 	END;
> 	' LANGUAGE PLPGSQL;


After some furtehr research I determined that it was ignoring the fieldTwo
check completely.  I finally narrowerd it ddown because my code read

	fieldTwo ALIAS FOR $2;

and the query read
	
	SELECT .....fieldtwo = fieldTwo

And plpgsql was not being case sensitive and thought it was comparing it to
itself.

Take care,
Jay
	

In response to

pgsql-general by date

Next:From: Jim C. NasbyDate: 2003-05-29 22:23:17
Subject: Re: Moving a table to a different schema
Previous:From: nolanDate: 2003-05-29 22:09:18
Subject: Re: How to deny user changing his own password?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group