File: procedure.sql

package info (click to toggle)
pljs 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,916 kB
  • sloc: ansic: 69,358; javascript: 5,408; sql: 880; makefile: 446; sh: 123
file content (25 lines) | stat: -rw-r--r-- 531 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- create a table to hold the data
CREATE TABLE test1 (a INTEGER);

-- and create a procedure that uses rollback and commit
-- note this will fail on versions older than 11
CREATE PROCEDURE transaction_test1()
LANGUAGE pljs
AS $$
for (let i = 0; i < 10; i++) {
  pljs.execute('INSERT INTO test1 (a) VALUES ($1)', [i]);
  if (i % 2 == 0)
    pljs.commit();
  else
    pljs.rollback();
}
$$;

call transaction_test1();

-- and get the results back
SELECT a FROM test1;

-- cleanup
DROP TABLE test1;
DROP PROCEDURE transaction_test1;