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

  • Home
  • SEARCH
  • 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 6906967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:22:38+00:00 2026-05-27T08:22:38+00:00

I have two tables like this: CREATE TABLE table1_lang ( id serial NOT NULL,

  • 0

I have two tables like this:

CREATE TABLE table1_lang
(
  id serial NOT NULL,
  lang1 character varying,
  lang2 character varying,
  ...
)

CREATE TABLE table2_lang
(
  id serial NOT NULL,
  table1_id NOT NULL,
  lang1 character varying,
  lang2 character varying,
  ...
)

Each of the lang columns contains a name in different languages.
I want to create a function that returns the selected lang from table1_lang if it exist.
If the name does not exist in table1_lang then the function should return the name from table2_lang.

So far I have solved my problem with this function in plpgsql:

CREATE OR REPLACE FUNCTION get_name(integer, text)
  RETURNS character varying AS
$BODY$
DECLARE
    in_id ALIAS FOR $1;
    lang ALIAS FOR $2;
    table1_name varchar;
    table2_name varchar;
BEGIN
    IF lang IS NOT NULL AND lang = 'lang1' THEN
        SELECT a.lang1 INTO table1_name
            FROM table1_lang a
            WHERE a.id = in_id;

            IF table1_name IS NOT NULL AND length(table1_name) > 0 THEN
            RETURN table1_name;
        ELSE
            SELECT a.lang1 INTO table2_name
            FROM table2_lang a
            WHERE a.table1_id = in_id;

            IF table2_name IS NOT NULL AND length(table2_name) > 0 THEN
                RETURN table2_name;
            END IF;
        END IF; 
    END IF;

    IF lang IS NOT NULL AND lang = 'lang2' THEN
        SELECT a.lang2 INTO table1_name
            FROM table1_lang a
            WHERE a.id = in_id;

            IF table1_name IS NOT NULL AND length(table1_name) > 0 THEN
            RETURN table1_name;
        ELSE
            SELECT a.lang2 INTO table2_name
            FROM table2_lang a
            WHERE a.table1_id = in_id;

            IF table2_name IS NOT NULL AND length(table2_name) > 0 THEN
                RETURN table2_name;
            END IF;
        END IF;             
    END IF;

    ...

    IF table2_name IS NULL OR length(table2_name) = 0 THEN
        RETURN NULL;
    END IF;

END
$BODY$
  LANGUAGE plpgsql VOLATILE STRICT
  COST 100;
ALTER FUNCTION get_name(integer, text) OWNER TO postgres;

But I would like to know if is it possible to create a function in plpgsql or other languages available in PostgreSQL that takes a column as input parameter?

  • 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-27T08:22:38+00:00Added an answer on May 27, 2026 at 8:22 am

    It is possible in a plpgsql function. But if identifiers (including column names) are parametrized you have to build dynamic SQL and execute it with EXECUTE. Read about that in the manual here. Pay attention not to allow SQL injection by doing that. Make use of quote_ident() or format().

    I have written a couple of similar answers with links and explanation. Try this one for a start or this one.

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

Sidebar

Related Questions

I have two tables like these: CREATE TABLE people ( id INT NOT NULL,
I have two tables: CREATE TABLE items ( root_id integer NOT NULL, id serial
i have two tables: CREATE TABLE public.auctions ( id VARCHAR(255) NOT NULL, auction_value_key VARCHAR(255)
i have two tables: 1. CREATE TABLE [dbo].[HotelSourceMap]( [hotelsourcemapid] [bigint] IDENTITY(1,1) NOT NULL, [dspartnerid]
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have two tables like this create table A_DUMMY ( TRADE_ID VARCHAR2(16) TRADE_DATA VARCHAR2(500)
I have create two tables with reference with another table: I like this: Table1:
I have two tables in my database that look like this CREATE TABLE IF
I have three tables like this CREATE TABLE `money`.`categories` ( `ID` bigint(20) unsigned NOT
Say I have two tables: create table parent ( number not null, constraint parent_pk

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.