Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have database with field called schcode for state.when user wants to enter new code it give auto incremented code.How can i achieve it

What I have tried:

CREATE FUNCTION fngetlastcodeofvillage( IN districtname text,
IN talukname text,
IN villagenam text) RETURNS setof text AS
$BODY$
BEGIN
SELECT schcd FROM villagerural_school_form where districtna=$1 and talukname=$2 and villagenam=$3 order by id desc limit 1
IF schcd IS NULL THEN
RETURN census2011||01 from villagerural where districtna=$1 and talukname=$2 and villagenam=$3
else
return schcd+01 from villagerural_school_form where districtna=$1 and talukname=$2 and villagenam=$3
END IF;

END;

$BODY$
LANGUAGE sql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION fngetlastcodeofvillage(text, text, text)
OWNER TO postgres;

I want to do some thing like above can you tell whats wrong in the above function and make it wotkout
Posted
Updated 4-Apr-16 1:46am
v2

An alternative to using SERIAL or SMALLSERIAL columns in the table would be to create a sequence. For example both these tables below end up with identical data
SQL
create table t1
(
  id SERIAL,
  someData varchar
);

CREATE SEQUENCE t3_id_seq;
CREATE TABLE t3 (
  id smallint NOT NULL DEFAULT nextval('t3_id_seq'),
  someData varchar
);
ALTER SEQUENCE t3_id_seq OWNED BY t3.id;

insert into t1 (someData) values ('one');
insert into t1 (someData) values ('two');
insert into t1 (someData) values ('three');

insert into t3 (someData) values ('one');
insert into t3 (someData) values ('two');
insert into t3 (someData) values ('three');

The disadvantage of this method is that you would require a sequence of each table that had such an identity column (which is why I included the table name in the sequence name)

One advantage of using sequence over SERIAL could be that you can use the LastVal() function against the sequence to find out which was the last id inserted (if you wanted to update your GUI immediately)

But there is an even neater and more efficient way to do that (from version 8.2) using the RETURNING version of the insert statement (ref: PostgreSQL: Documentation: 9.1: INSERT[^])
E.g.
SQL
insert into t1 (someData) values ("four") RETURNING id
 
Share this answer
 
Comments
Veena Hosur 4-Apr-16 1:31am    
REATE TABLE villagerural_school_form
(
id serial NOT NULL,
villagenam character varying(50),
talukname character varying(30),
districtna character varying(30),
schcd character varying(25),
schname character varying(50)
)

i have table like above. every school has unique code.when go with admin hierarchy there will b code till village after village for school it should give incremented value to user
Veena Hosur 4-Apr-16 2:36am    
295570545259951701 this is the school code.1st 2 digits represents state next 3 digit represent district,next 5 digit represent taluk,next 6 digit represents village and last 2 digit represent school.till village we have code for school it should generate adumatically to respective village.for example if one village doesnt have any school that last digit should increment by 01 else if village already has 2 school means it should generate 03
CHill60 5-Apr-16 5:47am    
This is not a normalized schema, but what you are looking for is RANK - see PostgreSQL: Documentation: 9.1: Window Functions[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900