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

Function - sequence - cast

I am trying to use a sequence value in a function but I keep getting an error message:

WARNING: Error occurred while executing PL/pgSQL function correctaddress

WARNING: line 8 at SQL statement

ERROR: column "addressid" is of type integer but expression is of type character varying

You will need to rewrite or cast the expression
And the function looks like:

CREATE FUNCTION correctAddress(INT) RETURNS INT AS '

DECLARE

user_id ALIAS FOR $1;

old_addr INT;

new_addr INT;

BEGIN

PERFORM nextval(''public.address_addressid_seq'');

INSERT INTO address (SELECT strProvince, strAddress FROM address WHERE addressID = (SELECT addressID FROM companies WHERE companyID = (SELECT companyID FROM users WHERE userID=user_id)));

UPDATE users SET adressID = CAST(currval(''public.tbladdress_addressid_seq'') AS INTEGER) WHERE userID=user_id;
-- ---> ^ ^ ^ ^ ^ ^ = ?

RETURN 1;

END ' LANGUAGE 'plpgsql';
It's probably something simple that I'm doing wrong. Can anyone help?

Postgres 7.4.1

Thanks

Ron

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #1
2 2174
Ron St-Pierre wrote:
I am trying to use a sequence value in a function but I keep getting an
error message:

WARNING: Error occurred while executing PL/pgSQL function
correctaddress

WARNING: line 8 at SQL statement

ERROR: column "addressid" is of type integer but expression is of
type character varying
You will need to rewrite or cast the expression
And the function looks like:

CREATE FUNCTION correctAddress(INT) RETURNS INT AS '
DECLARE
user_id ALIAS FOR $1;

old_addr INT;
new_addr INT;
BEGIN

PERFORM nextval(''public.address_addressid_seq'');
If you've set up addressID as a SERIAL then this nextval() isn't necessary.
INSERT INTO address (SELECT strProvince, strAddress FROM address
WHERE addressID = (SELECT addressID FROM companies WHERE companyID =
(SELECT companyID FROM users WHERE userID=user_id)));
I'm using the force here, but the problem might be here instead. What
are the columns on the address table, and if addressID is the first one
is strProvince a varchar?
UPDATE users SET adressID =
CAST(currval(''public.tbladdress_addressid_seq'') AS INTEGER) WHERE
userID=user_id; --
---> ^ ^ ^
^ ^ ^ = ?


--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
Richard Huxton wrote:
Ron St-Pierre wrote:
I am trying to use a sequence value in a function but I keep getting
an error message:

WARNING: Error occurred while executing PL/pgSQL function
correctaddress

WARNING: line 8 at SQL statement

ERROR: column "addressid" is of type integer but expression is of
type character varying
You will need to rewrite or cast the expression
And the function looks like:

CREATE FUNCTION correctAddress(INT) RETURNS INT AS '
DECLARE
user_id ALIAS FOR $1;

old_addr INT; new_addr INT; BEGIN

PERFORM nextval(''public.address_addressid_seq'');

If you've set up addressID as a SERIAL then this nextval() isn't
necessary.
INSERT INTO address (SELECT strProvince, strAddress FROM
address WHERE addressID = (SELECT addressID FROM companies WHERE
companyID = (SELECT companyID FROM users WHERE userID=user_id)));

I'm using the force here, but the problem might be here instead. What
are the columns on the address table, and if addressID is the first
one is strProvince a varchar?


WOW! Amazing use of the force, strProvince is a text field :-) You're
correct, the first column is an in and strProvince is text. Thanks for
pointing out the obvious, my 'real' function has about 15 more fields
and I was too busy looking at other things to notice.
UPDATE users SET adressID =
CAST(currval(''public.tbladdress_addressid_seq'') AS INTEGER) WHERE
userID=user_id;
-- ---> ^ ^
^ ^ ^ ^ = ?

Thanks Richard.

Ron

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

Nov 23 '05 #3

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

Similar topics

1
by: Tomas Mudrak | last post by:
Hi, I need little help with Ms SQL Server 2000. I would like to know how to create sequence or something like that. I want to have an automatic counter for each row in one entity, so then I can...
5
by: Glenn | last post by:
Hi! Server info - Win2K3 Server +SP1 with 1 GB Memory and 1.5 GB Virtual Memory SQL Server 2000 Enterprise Edition + SP3 running on this. Required result - Create a SQL Script that will...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
6
by: yamu_rhyme | last post by:
Is there any split function in DB2? How to split a field value separated with delimiter ","(comma)?
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
0
by: Dmitry Bond. | last post by:
Hi. Please help me. I need to port one stored function from Oracle to DB2. In Oracle there is a sequence used, like this: SELECT seqTF.nextval INTO seq_nextval FROM dual; I have lost 2...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.