26
CREATE TABLE app_for_leave
(
  sno integer NOT NULL,
  eid integer,
  ename varchar(20),
  sd date,
  ed date,
  sid integer,
  status boolean DEFAULT false,
  CONSTRAINT pk_snoa PRIMARY KEY (sno)
);

Basic Insertion is ::

INSERT INTO app_for_leave(sno, eid, sd, ed, sid, status)
 VALUES(1,101,'2013-04-04','2013-04-04',2,'f' );

...

INSERT INTO app_for_leave(sno, eid, sd, ed, sid, status) VALUES (?, ?, ?, ?, ?, ?);

My Requirement:: How to insert data into a table using stored procedures?

6
  • I need to insert data ,by creating a funtion or a procedure to insert data into the table app_for_leave Commented Jul 9, 2013 at 4:03
  • Postgres doesn't have stored procedures, it does support functions though.. Commented Jul 9, 2013 at 4:04
  • so there is no other way to insert data except the DML language queries INSERT statement..or is there any other way to solve this @mike christensen Commented Jul 9, 2013 at 4:06
  • Sure, use a function.. Commented Jul 9, 2013 at 4:13
  • I'm not sure what you're asking here. You can write a function that does the INSERT I suppose. Commented Jul 9, 2013 at 4:13

4 Answers 4

43

PostgreSQL didn't support stored procedures until PG11. Prior to that, you could get the same result using a function. For example:

CREATE FUNCTION MyInsert(_sno integer, _eid integer, _sd date, _ed date, _sid integer, _status boolean)
  RETURNS void AS
  $BODY$
      BEGIN
        INSERT INTO app_for_leave(sno, eid, sd, ed, sid, status)
        VALUES(_sno, _eid, _sd, _ed, _sid, _status);
      END;
  $BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;

You can then call it like so:

select * from MyInsert(1,101,'2013-04-04','2013-04-04',2,'f' );

The main limitations on Pg's stored functions - as compared to true stored procedures - are:

  1. inability to return multiple result sets
  2. no support for autonomous transactions (BEGIN, COMMIT and ROLLBACK within a function)
  3. no support for the SQL-standard CALL syntax, though the ODBC and JDBC drivers will translate calls for you.

Example

Starting from PG11, the CREATE PROCEDURE syntax is introduced which provides support for transactions.

CREATE PROCEDURE MyInsert(_sno integer, _eid integer, _sd date, _ed date, _sid integer, _status boolean)
LANGUAGE SQL
AS $BODY$
    INSERT INTO app_for_leave(sno, eid, sd, ed, sid, status)
    VALUES(_sno, _eid, _sd, _ed, _sid, _status);   
$BODY$;

Which could be called with:

CALL MyInsert(1,101,'2013-04-04','2013-04-04',2,'f' );
Sign up to request clarification or add additional context in comments.

11 Comments

@user2561626 - You need to CREATE the FUNCTION first. Use my example above, I just verified it with Postgres 9.2 on my own server.
plpgsql does not support autonomous transactions, but other languages do, for example plperl. You can probably accomplish the same using dblink and plproxy.
what does the LANGUAGE 'plpsql' 'VOLATILE' and 'cost 100' mean in the query.. is it the name of the language or something which states different @mike
@user2561626 - Postgres supports various languages, such as PL/pgSQL, Perl, C, there's even a JavaScript extension. VOLATILE indicates the function must be run each time and the result cannot be cached (only VOLATILE functions may change the database). This is the default so my code is actually redundant. The COST provides an estimated execution cost for the planner. More Details
@lad2025 - Woah sweet! Man I miss working with PG. I'm all MS SQL Server now days, sigh..
|
11

Starting from PostgreSQL 11 you could create stored procedures and invoke them using CALL:

CREATE PROCEDURE MyInsert(_sno integer, _eid integer, _sd date,
                          _ed date, _sid integer, _status boolean)
LANGUAGE SQL
AS $$
    INSERT INTO app_for_leave(sno, eid, sd, ed, sid, status)
    VALUES(_sno, _eid, _sd, _ed, _sid, _status);   
$$;

CALL MyInsert(1,101,'2013-04-04','2013-04-04',2,'f' );

Plus it allows to handle transaction

SQL Stored Procedures

PostgreSQL 11 introduces SQL stored procedures that allow users to use embedded transactions (i.e. BEGIN, COMMIT/ROLLBACK) within a procedure. Procedures can be created using the CREATE PROCEDURE command and executed using the CALL command.

2 Comments

Nice @lukasz ... How to return the result set i.e tables in stored procedure?
@Atj You use a FUNCTION, not a stored PROCEDURE.
2

PostgreSQL doesn't support stored procedures, but you can get the same result using a function.

Whatever the data you want to insert in to the table are given as parameters to the function you are creating.

CREATE OR REPLACE represents if a function with the same name (you are using) is already present in the database, then it will be replaced or else if no function with the same name is not present then a new function will be created.

You have to write the insesrtion query inside the body of the function.

CREATE OR REPLACE FUNCTION Insert_into_table(_sno INTEGER, _eid INTEGER, _ename VARCHAR(20), _sd DATE, _ed DATE, _sid INTEGER)
      RETURNS void AS
      $BODY$
          BEGIN
            INSERT INTO app_for_leave(sno, eid, sd, ed, sid)
            VALUES(_sno, _eid, _sd, _ed, _sid);
          END;
      $BODY$
      LANGUAGE 'plpgsql' VOLATILE
      COST 100;

As you have already mentioned in the table a default value for the column Status, now it is no need to insert data into that column

Here is the SQLFiddle Link for your understanding

1 Comment

Time to update this answer. Stored procedures with transaction control are supported in PG11, which has been released now for several months.
1
CREATE OR REPLACE FUNCTION  new_bolshek(parent_id bigint, _key text, _value text, enabled boolean)
  RETURNS SETOF bolshekter AS
  $BODY$
  DECLARE
    new_id integer;
    returnrec bolshekter;
  BEGIN
        INSERT INTO bolshekter(parent_id, content_key, content_value, enabled)
        VALUES(parent_id, _key, _value, enabled) RETURNING id INTO new_id;
        FOR returnrec IN SELECT * FROM bolshekter where id=new_id LOOP
            RETURN NEXT returnrec;
        END LOOP;
  END;
  $BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;

Comments

Your Answer

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.