473,395 Members | 1,977 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,395 software developers and data experts.

Obtain boolean value of expression in PLPGSQL

Hello

My task is to validate expression and get value of it in boolean variable.
I try to do following:
CREATE OR REPLACE FUNCTION get_value(integer) RETURNS boolean AS'
DECLARE
Ret boolean;
Op TEXT = ''='';
BEGIN

Ret := 3 || Op || $1;
RETURN Ret;
END;
' language 'plpgsql';

when I try to execute this function I get such result:

bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'
bill=#

So, how can I explain that resulting type must be boolean ? Also, I try to
play with SELECT INTO with same result.

--
.... All opinions expressed are mine and not those of my employer.

Yours, Max [Msg N 2419]
-------------------------------------------
mailto: mx@lucky.net phone: +380-44-2054455

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #1
3 4364
On Tuesday 04 November 2003 10:06, Max Speransky wrote:
Hello

My task is to validate expression and get value of it in boolean variable.
I try to do following: Ret := 3 || Op || $1;
RETURN Ret; bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'


The problem is that your expression is being evaluated as a string. In the
absence of an eval() function, you probably should do something like:

my_stmt := ''SELECT '' || 3 || Op || $1;
EXECUTE my_stmt;
FOR EACH ...

So - build a simple query, execute it and read the result. Should do what you
want.

--
Richard Huxton
Archonet Ltd

---------------------------(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 #2
Hello

You are on wrong way.

Try like

DECLARE
b RECORD;
s TEXT;
op TEXT;
BEGIN op := ''='';
s := ''SELECT 3 ''|| op || ''|| $1 || ''AS b'';
FOR b IN EXECUTE s LOOP
RETURN b.b;
END LOOP;
END;

Regards
Pavel
On Tue, 4 Nov 2003, Max Speransky wrote:
Hello

My task is to validate expression and get value of it in boolean variable.
I try to do following:
CREATE OR REPLACE FUNCTION get_value(integer) RETURNS boolean AS'
DECLARE
Ret boolean;
Op TEXT = ''='';
BEGIN

Ret := 3 || Op || $1;
RETURN Ret;
END;
' language 'plpgsql';

when I try to execute this function I get such result:

bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'
bill=#

So, how can I explain that resulting type must be boolean ? Also, I try to
play with SELECT INTO with same result.

---------------------------(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 #3
Max Speransky said:
Hello

My task is to validate expression and get value of it in boolean variable. [snip] when I try to execute this function I get such result:

bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'


|| is the string concatenation operator - so your expression creates a
string "3=3" and then tries to return that as a boolean hence the error.

if you cannot just do "return 3 = $1;" perhaps you could try using EXECUTE?

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

http://archives.postgresql.org

Nov 12 '05 #4

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

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
0
by: Frank van Vugt | last post by:
Hi, FWIW, I walked into this one when changing an int into a numeric(5,1) : IF ( 0::int ) => interpreted as false IF ( 0.0::numeric(5,1) ) => interpreted as true
1
by: Philippe Lang | last post by:
Hello, Imagine we have the following kind of table, with two values (a and b), and a varchar (f) representing an expression. ---------------------------------- CREATE TABLE public.test ( id...
6
by: anonymous | last post by:
hi all, I'm trying to use the capCreateCaptureWindow (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_capcreatecapturewindow.asp) in a Windows...
9
by: HS1 | last post by:
Hello Could you please help for a simple boolean expression If a is not equal to Null I tried If (a != null) Then
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
6
by: GHUM | last post by:
I need to split a text at every ; (Semikolon), but not at semikolons which are "escaped" within a pair of $$ or $_$ signs. My guess was that something along this should happen withing csv.py;...
7
by: rguarnieri | last post by:
Hi! I'm trying to create a query with a boolean expression like this: select (4 and 1) as Value from Table1 this query return always -1, but when I make the same calculation in visual...
4
by: Greg Corradini | last post by:
Hello all, I'm having trouble understanding why the following code evaluates as it does: True -1 In the 2.4 Python Reference Manual, I get the following explanation for the 'and' operator...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.