Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 September String Array as an input parameter in PostgreSQL

String Array as an input parameter in PostgreSQL

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration How to pass the string array as an input parameter in stored function of PostgreSQL.

We already used a different approach to pass multiple values as a single input parameter like comma separated, XML Parameter, Table Type.
You can also find one more type which is an array of PostgreSQL.

I created a stored function for this demonstration.
In the below, the stored function I declared one input parameter as a string array and using for..loop I am fetching record from this array and storing into one temporary table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CREATE OR REPLACE FUNCTION fn_PostgresArray
(
ArrayText character varying[]
)
RETURNS TABLE (outArrayTextData CHARACTER VARYING(250)) AS
$BODY$
DECLARE
Counter INT = 0 ;
BEGIN
CREATE TEMP TABLE ArrayData(ArrayTextData CHARACTER VARYING(250));
FOR Counter in array_lower(ArrayText, 1) .. array_upper(ArrayText, 1)
LOOP
INSERT INTO ArrayData VALUES(ArrayText[Counter]);
END LOOP;
 
RETURN QUERY
SELECT ArrayTextData
FROM ArrayData;
 
DISCARD TEMP;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Now let’s call the above function by passing a string array.
1
SELECT *FROM fn_PostgresArray(ARRAY['Apple', 'Orange', 'Mango']);
The result is:

PostgreSQL String Array

Sep 21, 2015Anvesh Patel
Import or Export a CSV File using PostgreSQL COPY CommandScript to find active sessions or connections in PostgreSQL
Comments: 5
  1. Pramod
    June 27, 2018 at 1:17 pm

    I have created a type which and trying to pass it as argument in procedure, but getting an error as ‘type XXX does not exists’

    • Anvesh Patel
      Anvesh Patel
      June 28, 2018 at 5:45 pm

      Did you create your own datatype?
      If yes then give permission to your user.

  2. suresh
    October 8, 2018 at 11:54 am

    HI Anvesh,

    I am new in postgresql. I am learning the through your blog,
    I want to implement the table values parameter in stored function in postgresql.
    In MS SQL , I can do the below script

    Create type tbl as table(sno int,name varchar(100))
    go
    declare @tt as tbl
    insert @tt(sno,name) values (1,’XXX’)
    insert @tt(sno,name) values (2,’YYY’)
    insert @tt(sno,name) values (3,’ZZZ’)
    create proc pgetlist (@tt tbl)
    as
    begin
    select * from @tt
    end

    Please provide the sample script how can implement the same in postgresql.
    Please help me.

    • Anvesh Patel
      Anvesh Patel
      October 9, 2018 at 7:29 pm

      I think, PostgreSQL doesn’t support table value parameter. But using array parameter, you can do similar like table value.

  3. Kiran
    March 23, 2019 at 5:25 am

    How to implement table value parameter in postgresql like sql server

Anvesh Patel
Anvesh Patel

Database Engineer

September 21, 2015 PostgreSQLAnvesh Patel, array, database, database research and development, dbrnd, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....