pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to test/read a stored procedure that returns a boolean?
3+ messages / 2 participants
[nested] [flat]

* How to test/read a stored procedure that returns a boolean?
@ 2008-01-16 23:15  Kevin Jenkins <gameprogrammer@rakkar.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Kevin Jenkins @ 2008-01-16 23:15 UTC (permalink / raw)
  To: pgsql-sql

I wrote this function but I'm not sure how to test it in PG Admin III 
Query.

CREATE LANGUAGE 'plpgsql' HANDLER plpgsql_call_handler
                            LANCOMPILER 'PL/pgSQL';

CREATE TABLE handles (
handleID_pk serial PRIMARY KEY UNIQUE NOT NULL,
userID_fk integer UNIQUE NOT NULL,
handle text UNIQUE NOT NULL);

CREATE TABLE disallowedHandles (
handle text UNIQUE NOT NULL);

create or replace function IsUsedHandle(h text) returns boolean as $$
declare
begin
select COUNT(*) as num_matches from handles where handles.handle = h;
return num_matches > 0;
end;
$$ LANGUAGE plpgsql;

INSERT INTO handles (handle, userid_fk) VALUES ('blah', 0);

select * from IsUsedHandle('k');

Instead of true or false, it says

ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Context: PL/pgSQL function "isusedhandle" line 3 at SQL statement

If I wanted to call this query and get the boolean result in C++, 
using PQgetvalue, how would I get this?

Thanks!



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to test/read a stored procedure that returns a boolean?
@ 2008-01-16 23:28  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Kevin Jenkins <gameprogrammer@rakkar.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2008-01-16 23:28 UTC (permalink / raw)
  To: Kevin Jenkins <gameprogrammer@rakkar.org>; +Cc: pgsql-sql

Kevin Jenkins <gameprogrammer@rakkar.org> writes:
> create or replace function IsUsedHandle(h text) returns boolean as $$
> declare
> begin
> select COUNT(*) as num_matches from handles where handles.handle = h;
> return num_matches > 0;
> end;
> $$ LANGUAGE plpgsql;

I think you've confused AS with INTO.

You forgot to declare num_matches as a local variable, too.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to test/read a stored procedure that returns a boolean?
@ 2008-01-16 23:50  Kevin Jenkins <gameprogrammer@rakkar.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Kevin Jenkins @ 2008-01-16 23:50 UTC (permalink / raw)
  To: ; +Cc: pgsql-sql

Thanks Tom!

Also, how do I check if a language is already created so I don't load 
it twice?

"ERROR: language "plpgsql" already exists
SQL state: 42710"

Here is the code fixed.

/*
CREATE LANGUAGE 'plpgsql' HANDLER plpgsql_call_handler
                          LANCOMPILER 'PL/pgSQL';

CREATE TABLE handles (
handleID_pk serial PRIMARY KEY UNIQUE NOT NULL,
userID_fk integer UNIQUE NOT NULL,
handle text UNIQUE NOT NULL);

CREATE TABLE disallowedHandles (
handle text UNIQUE NOT NULL);
*/

create or replace function IsUsedHandle(h text) returns boolean as $$
declare
	num_matches integer;
begin
	num_matches := COUNT(*) from handles where handles.handle = h;
	return num_matches > 0;
end;
$$ LANGUAGE plpgsql;

-- INSERT INTO handles (handle, userid_fk) VALUES ('blah', 0);

select * from IsUsedHandle('blah');



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2008-01-16 23:50 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-01-16 23:15 How to test/read a stored procedure that returns a boolean? Kevin Jenkins <gameprogrammer@rakkar.org>
2008-01-16 23:28 ` Tom Lane <tgl@sss.pgh.pa.us>
2008-01-16 23:50   ` Kevin Jenkins <gameprogrammer@rakkar.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox