Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am very new to PostgreSQL and i am trying to make a function in this but it's showing syntax error
SQL
CREATE OR REPLACE FUNCTION master.test(tehsil text, district text, state text,flag text)
    RETURNS SETOF refcursor AS
    $BODY$DECLARE
    recordSet  refcursor;  
    BEGIN

        OPEN  recordset FOR
        IF flag = 'A' THEN
            SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07';
	[ ELSIF flag = 'B' THEN
	    SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07' AND districtcode = '';
        [ ELSIF flag = 'C' THEN
	    SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07' AND districtcode = '' AND tehsilcode = '';
		]
	]
        [ ELSE
           SELECT Count(*) FROM population WHERE incomein(1,2); ]
        END IF;
        RETURN NEXT recordSet;
        RETURN;
    END; $BODY$
    LANGUAGE plpgsql
Posted
Updated 21-May-13 0:34am
v2

1 solution

visit link...
http://stackoverflow.com/questions/13122862/yet-another-stored-procedure-syntax-error-if-then-else[^]

Try this...
SQL
IF flag = 'A' THEN
            SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07';
ELSEIF flag = 'B' THEN
	    SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07' AND districtcode = '';
ELSEIF flag = 'C' THEN
	    SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07' AND districtcode = '' AND tehsilcode = '';
ELSE
           SELECT Count(*) FROM population WHERE incomein(1,2); 
END IF;

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Anusha Srivastava 21-May-13 6:55am    
My language is already 'plpgsql'
Aarti Meswania 21-May-13 7:00am    
see updated solution :)
Anusha Srivastava 21-May-13 7:15am    
i had also tried this

CREATE OR REPLACE FUNCTION master.test(tehsil text, district text, state text,flag text)
RETURNS void AS
$$
BEGIN


IF flag='A' THEN
select Count(*) from population where income in(1,2)and statecode='07';
ELSIF flag='B' THEN
select Count(*) from population where incomesource_urban in(1,2) and statecode='07' and districtcode='' ;
ELSIF flag='C' THEN
select Count(*) from population where income in(1,2) and statecode='07'and districtcode='' and tehsilcode='';


ELSE
select Count(*) from population where incomesource_urban in(1,2);
END IF;


$$
LANGUAGE plpgsql


and this also

CREATE OR REPLACE FUNCTION master.test(tehsil text, district text, state text,flag text)
RETURNS SETOF refcursor AS
$BODY$DECLARE
recordSet refcursor;
BEGIN

OPEN recordset FOR


IF flag='A' THEN
select Count(*) from population where incomesource_urban in(1,2)and statecode='07';
[ ELSIF flag='B' THEN
select Count(*) from population where income in(1,2) and statecode='07' and districtcode='' ;
[ ELSIF flag='C' THEN
select Count(*) from population where incomesource_urban in(1,2) and statecode='07'and districtcode='' and tehsilcode='';
]
]
[ ELSE
select Count(*) from population where income in(1,2); ]
END IF;


RETURN NEXT recordSet;
RETURN;
END; $BODY$
LANGUAGE plpgsql


I think there is some syntax error when the query is returning some value
Aarti Meswania 21-May-13 7:26am    
if it's postgres version 8 then...
there is bug there visit link...
http://www.postgresql.org/message-id/20041126111429.105AA7388F4@www.postgresql.com

you can go with nested if...
If flag='a' then
SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07';
else
If flag='b' then
SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07' AND districtcode = '';
else
If flag='c' then
SELECT Count(*) FROM population WHERE incomein(1,2) AND statecode='07' AND districtcode = '' AND tehsilcode = '';
else
SELECT Count(*) FROM population WHERE incomein(1,2);
end if;
end if;
end if;
Anusha Srivastava 21-May-13 7:27am    
yaa got it thks :)

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