PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 23rd, 2012, 08:02 AM
negroscuro negroscuro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 negroscuro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 35 sec
Reputation Power: 0
Error while doing insert, something happens with my triggered function, please help

This is my table:
Code:
CREATE TABLE location
(
  id serial NOT NULL,
  district character varying(100),
  county character varying(100),
  subcounty character varying(100),
  village character varying(100),
  gid integer,
  CONSTRAINT location_pkey PRIMARY KEY (id )
)


But I also have this other table:
Code:
CREATE TABLE villages
(
  gid serial NOT NULL,
  dname character varying(32),
  cname character varying(32),
  sname character varying(32),
  pname character varying(32),
  vname character varying(32),
  the_geom geometry,
  the_geom2 geometry,
  CONSTRAINT villages_pkey PRIMARY KEY (gid ),
)


This is my function and trigger:
Code:
CREATE OR REPLACE FUNCTION getvillagegidfunction() RETURNS TRIGGER AS $$
  DECLARE
  register location;
  BEGIN
  
   SELECT * INTO register 
   FROM villages v
   WHERE NEW.village::TEXT=v.vname
   AND NEW.subcounty::TEXT=v.sname
   AND NEW.county::TEXT=v.cname
   AND NEW.district::TEXT=v.dname;
   
   new.gid := register.gid;

   RETURN NEW;
  END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER getvillagegidtrigger BEFORE INSERT
ON location FOR EACH ROW EXECUTE PROCEDURE getvillagegidfunction();


And this is my insert:
Code:
INSERT INTO location 
(id,district,county,subcounty,village, gid)
VALUES (2333,'KASESE', 'KASESE MUNICIPALITY', 'NYAMWAMBA DIVISION', 'SEBWE',  113);


So here is my error:
Code:
ERROR:  invalid input syntax for integer: "SEBWE"
CONTEXT:  PL/pgSQL function "getvillagegidfunction" line 5 at SQL statement

********** Error **********

ERROR: invalid input syntax for integer: "SEBWE"
SQL state: 22P02
Context: PL/pgSQL function "getvillagegidfunction" line 5 at SQL statement


The aim of this function is to retrieve the GID from villages table and add it to the gid column when any new row is inserted in location table. I do this because locations have the same fields as a village, so I serach with a select for the village matching the new location.

I have no idea about what I am doing wrong, so any help will be much appreciated.

Reply With Quote
  #2  
Old November 23rd, 2012, 08:29 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,351 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 8 h 2 m 22 sec
Reputation Power: 390
Code:
CREATE OR REPLACE FUNCTION getvillagegidfunction() RETURNS TRIGGER AS $$
  DECLARE
  register location;
  BEGIN


shouldn't that be

Code:
CREATE OR REPLACE FUNCTION getvillagegidfunction() RETURNS TRIGGER AS $$
  DECLARE
  register villages;
  BEGIN


instead?

Reply With Quote
  #3  
Old November 23rd, 2012, 08:30 AM
negroscuro negroscuro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 negroscuro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 35 sec
Reputation Power: 0
I think it maybe related to the recent update I did to the table location.
I added the last column 'GID'... I dont know if it may affect...


--edit---

Yes, thank you for your reply, I didnt realice, but thats it, now works. Simple things usually are the worst to find out.
Thank you!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Error while doing insert, something happens with my triggered function, please help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap