Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:54:56+00:00 2026-05-26T22:54:56+00:00

Something like this: CREATE OR REPLACE FUNCTION get(param_id integer) RETURNS integer AS $BODY$ BEGIN

  • 0

Something like this:

CREATE OR REPLACE FUNCTION get(param_id integer)
  RETURNS integer AS
$BODY$
BEGIN
SELECT col1 FROM TABLE WHERE id = param_id;
END;
$BODY$
  LANGUAGE plpgsql;

I would like to avoid a DECLARE just for this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:54:56+00:00Added an answer on May 26, 2026 at 10:54 pm

    Yes you can. There is a number of ways.

    1) RETURN (SELECT ...)

    CREATE OR REPLACE FUNCTION get_1(_param_id integer)
      RETURNS integer
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       RETURN _param_id;
    -- Or:
    -- RETURN (SELECT col1 FROM tbl WHERE id = _param_id);
    END
    $func$;
    

    2) Use an OUT or INOUT parameter

    CREATE OR REPLACE FUNCTION get_2(_param_id integer, OUT _col1 integer)
    -- RETURNS integer -- is optional noise in this case
      LANGUAGE plpgsql AS  
    $func$
    BEGIN
       SELECT INTO _col1  col1 FROM tbl WHERE id = _param_id;
    
       -- also possible (currently), but discouraged:
       -- _col1 := col1 FROM tbl WHERE id = _param_id;
    END
    $func$;
    

    More in the manual here.

    3) (Ab)use IN parameter

    Since Postgres 9.0 you can also use input parameters as variables. The release notes for 9.0:

    An input parameter now acts like a local variable initialized to the passed-in value.

    CREATE OR REPLACE FUNCTION get_3(_param_id integer)
      RETURNS integer
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       SELECT INTO _param_id  col1 FROM tbl WHERE id = _param_id;
       RETURN _param_id;
    
       -- also possible (currently), but discouraged:
       -- $1 := col1 FROM tbl WHERE id = $1;
       -- RETURN $1;
    END
    $func$;
    

    Variants 2) and 3) do use a variable implicitly, but you don’t have to DECLARE one explicitly (as requested).

    4) Use a DEFAULT value with an INOUT parameter

    This is a bit of a special case. The function body can be empty.

    CREATE OR REPLACE FUNCTION get_4(_param_id integer, INOUT _col1 integer = 123)
      RETURNS integer
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       -- You can assign some (other) value to _col1:
       -- SELECT INTO _col1  col1 FROM tbl WHERE id = _param_id;
       -- If you don't, the DEFAULT 123 will be returned.
    END
    $func$;
    

    INOUT _col1 integer = 123 is short notation for INOUT _col1 integer DEFAULT 123. See:

    • The forgotten assignment operator "=" and the commonplace ":="

    5) Use a plain SQL function instead

    CREATE OR REPLACE FUNCTION get_5(_param_id integer)
      RETURNS integer
      LANGUAGE sql AS
    'SELECT col1 FROM tbl WHERE id = _param_id';
    

    Or use use param reference $1 instead of param name.

    Variant 5) one uses plain single quotes for the function body. All the same. See:

    • What are '$$' used for in PL/pgSQL
    • Insert text with single quotes in PostgreSQL

    fiddle – demonstrating all (incl. call)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure how to achieve something like the following: CREATE OR REPLACE FUNCTION
I want to do something like this: create table app_users ( app_user_id smallint(6) not
I want to do something like this create type Item as object ( id
I have something like this: create table account ( id int identity(1,1) primary key,
is it possible to create something like this i ASP.NET MVC beta 1 i
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create a wpf treeviewlist to look something like this: AAAA BBBB
I'm creating a table that looks something like this. CREATE TABLE packages ( productCode
when i create an aspx page, the header includes something like this:- <%@ Page
In PHP, to create a new object you would do something like this, $dog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.