1

This is my code:

create function dept_count (dept_name varchar(20))
returns integer as $$
begin
declare d_count integer;
select count(*) into d_count
from instructor;
return d_count; end; 
$$ language plpgsql;

It gives an error:

ERROR: syntax error at or near "*"
LINE 5: select count(*) into d_count
 ^
CONTEXT: invalid type name "count(*) into d_count from instructor"

What is the reason for this?

Erwin Brandstetter
669k160 gold badges1.2k silver badges1.3k bronze badges
asked Aug 24, 2014 at 10:54

1 Answer 1

3

DECLARE must come before BEGIN.

Apart from that, your function would work - except that it doesn't make much sense to declare a parameter that is not referenced, but that's probably just a simplification for the purpose of this question.

answered Aug 27, 2014 at 7:00
Sign up to request clarification or add additional context in comments.

1 Comment

you're epic for this, Erwin. Postgres freaks me out, though.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.