Unix Technical Forum

How to test/read a stored procedure that returns a boolean?

This is a discussion on How to test/read a stored procedure that returns a boolean? within the pgsql Sql forums, part of the PostgreSQL category; --> I wrote this function but I'm not sure how to test it in PG Admin III Query. CREATE LANGUAGE ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 06:00 PM
Kevin Jenkins
 
Posts: n/a
Default How to test/read a stored procedure that returns a boolean?

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!

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

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 06:00 PM
Tom Lane
 
Posts: n/a
Default Re: How to test/read a stored procedure that returns a boolean?

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

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 06:00 PM
codeWarrior
 
Posts: n/a
Default Re: How to test/read a stored procedure that returns a boolean?

Looks to me like you are declaring "begin.... "

I don't see ythat you need to declare anything in your SP... Also -- it is
wrong in postgreSQL to create "C-Style" functions declarations -- you only
list the "type" of parameter... parameters are mapped internally as $1, $2,
$3, etctera -- looking at

IsUsedHandle(h text)

sit SHOUD BE:

IsUsedHandle(text)

I would write it like this:

CREATE OR REPLACE FUNCTION sys_IsUsedHandle(text) RETURNS boolean AS
$BODY$

DECLARE a INTEGER;
DECLARE b BOOLEAN;

BEGIN

SELECT CASE WHEN (SELECT COUNT(*) FROM handles h WHERE h.handle = $1) > 0
THEN true ELSE false INTO B;
RETURN B;

END;

$BODY$
LANGUAGE 'plpgsql' VOLATILE;




"Kevin Jenkins" <gameprogrammer@rakkar.org> wrote in message
news:478E901D.7060701@rakkar.org...
>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!
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 06:00 PM
Kevin Jenkins
 
Posts: n/a
Default Re: How to test/read a stored procedure that returns a boolean?

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');

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

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:22 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com