- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsBasically I want to pass a schema name as a parameter to a stored procedure, and make a select statement based on that schema name. This is what I've got:
CREATE OR REPLACE FUNCTION myFunction( schemaName "varchar" )
RETURNS "numeric" AS
$BODY$
DECLARE
temp numeric;
BEGIN
SELECT count(*) INTO temp
FROM schemaName.myTable
RETURN temp;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
The problem is that the query tries to look for a schema named "schemaName" instead of using the value of the variable schemaName.
I've also tried the following:
CREATE OR REPLACE FUNCTION myFunction( schemaName "varchar" )
RETURNS "numeric" AS
$BODY$
DECLARE
temp numeric;
BEGIN
EXECUTE
'SELECT count(*) INTO temp
FROM ' || schemaName || '.myTable'
RETURN temp;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
This would work, except I'm using EnterpriseDB version 8.1, so an EXECUTE...INTO statement hasn't been implemented yet.
The problem is that I'm going to be creating many different schemas with the same table structure that use this function, and I could create a stored procedure for each one, but that just seems like a lot of duplicate code to me. (The real stored procedure is much larger)
Any suggestions would be appreciated.
Thanks,
B.J.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: cminearPosted on 2009-09-11 at 13:40:50ID: 25313296
Actually, you just about had it. But you missed it with the attempt to use SELECT ... INTO; you only need to save to a variable with EXECUTE ... INTO so just use a normal SELECT to return the value. I got the following tweak of your code to work with my database (other than changing "myTable" for a table in my actual schema). This worked with PostgreSQL 8.1, which I assume is the basis for EnterpriseDB 8.1.
Select allOpen in new window