sql-returning

Oracle - RETURNING combined with aggregate functions

随声附和 提交于 2019-12-03 06:32:19
Oracle supports RETURNING clause which could be very useful. For example for data: CREATE TABLE t(Id INT, Val varchar2(50)); INSERT INTO t(Id, Val) SELECT 10,'a' FROM dual UNION ALL SELECT 20,'b' FROM dual UNION ALL SELECT 30,'a' FROM dual UNION ALL SELECT 40,'b' FROM dual; Query: DECLARE l_cnt INT; BEGIN DELETE FROM t RETURNING COUNT(*) INTO l_cnt; DBMS_OUTPUT.put_line('l_cnt: ' || l_cnt); END; l_cnt: 4 It supports MIN/MAX/AVG/SUM/LISTAGG: DECLARE l_max INT; l_min INT; l_str VARCHAR2(100); BEGIN DELETE FROM t RETURNING MAX(id), MIN(id), LISTAGG(id, ',') WITHIN GROUP(ORDER BY id) INTO l_max, l

INSERT or SELECT strategy to always return a row?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 00:54:34
Using Postgres 9.6, I have followed the strategy recommended in https://stackoverflow.com/a/40325406/435563 to do an INSERT or SELECT and return the resulting id: with ins as ( insert into prop (prop_type, norm, hash, symbols) values ( $1, $2, $3, $4 ) on conflict (hash) do update set prop_type = 'jargon' where false returning id) select id from ins union all select id from prop where hash = $3 However, sometimes this returns nothing. I would have expected it to return a row no matter what. How can I fix it to insure it always returns an id? NB, despite not returning a row, the row does seem

Postgres Insert Into View Rule with Returning Clause

流过昼夜 提交于 2019-12-01 03:24:39
问题 I am attempting to allow insert statements with a returning clause into a view in Postgres v9.4, but am struggling with the syntax. This is how I want to call the insert statement: CREATE VIEW MyView AS SELECT a.*, b.someCol1 FROM tableA a JOIN tableB b USING(aPrimaryKey); INSERT INTO MyView (time, someCol) VALUES (someTime, someValue) RETURNING *; INSERT INTO MyView (someCol) VALUES (someValue) RETURNING *; Note that the default for time is NOW(). This is what I have so far: CREATE RULE

Is INSERT RETURNING guaranteed to return things in the “right” order?

落花浮王杯 提交于 2019-11-29 09:12:19
Example: create table foo( id serial, txt text ); insert into foo(txt) values ('a'),('b'),('c') returning id; Returns: id ---- 1 2 3 (3 rows) It seems that the first id in the return value will always be the id for 'a' , the second for 'b' and so on, but is this defined behaviour of insert into , or is it a coincidence that may fail under odd circumstances? I don't see anything in the documentation that guarantees an order for RETURNING so I don't think you can depend on it. Odds are that the RETURNING order will match the VALUES order but I don't see any guarantees about what order the VALUES

INSERT INTO … FROM SELECT … RETURNING id mappings

只愿长相守 提交于 2019-11-29 06:49:54
I'm using PostgreSQL 9.3. I want to duplicate some of the db records. Since I'm using an auto-increment pk id for the table, I want to get back the id mappings from the generated ids of duplicated records to the original ones. For example, say I have a table posts with 2 records in it: [{'id': 1, 'title': 'first'} , {'id': 2. 'title': 'second'}] With SQL: INSERT INTO posts (title) SELECT title FROM posts RETURNING id, ?? I expect to see mappings like: [{'id': 3, 'from_id': 1} , {'id': 4, 'from_id': 2}] Any idea on how to fill in the question marks above to make it work? Thanks a lot! This

Get default serial value after INSERT inside PL/pgSQL

烈酒焚心 提交于 2019-11-28 11:49:16
问题 I have a table. I wrote a function in plpgsql that inserts a row into this table: INSERT INTO simpleTalbe (name,money) values('momo',1000) ; This table has serial field called id . I want in the function after I insert the row to know the id that the new row received. I thought to use: select nextval('serial'); before the insert, is there a better solution? 回答1: Use the RETURNING clause. You need to save the result somewhere inside PL/pgSQL - with an appended INTO .. INSERT INTO simpleTalbe

Can I use return value of INSERT…RETURNING in another INSERT?

ⅰ亾dé卋堺 提交于 2019-11-27 11:11:05
Is something like this possible? INSERT INTO Table2 (val) VALUES ((INSERT INTO Table1 (name) VALUES ('a_title') RETURNING id)); like using the return value as value to insert a row in a second table with a reference to the first table? Denis de Bernardy You can do so starting with Postgres 9.1: with rows as ( INSERT INTO Table1 (name) VALUES ('a_title') RETURNING id ) INSERT INTO Table2 (val) SELECT id FROM rows In the meanwhile, if you're only interested in the id, you can do so with a trigger: create function t1_ins_into_t2() returns trigger as $$ begin insert into table2 (val) values (new

How to include excluded rows in RETURNING from INSERT … ON CONFLICT

孤人 提交于 2019-11-27 08:25:00
I've got this table (generated by Django): CREATE TABLE feeds_person ( id serial PRIMARY KEY, created timestamp with time zone NOT NULL, modified timestamp with time zone NOT NULL, name character varying(4000) NOT NULL, url character varying(1000) NOT NULL, email character varying(254) NOT NULL, CONSTRAINT feeds_person_name_ad8c7469_uniq UNIQUE (name, url, email) ); I'm trying to bulk insert a lot of data using INSERT with an ON CONFLICT clause. The wrinkle is that I need to get the id back for all of the rows, whether they're already existing or not. In other cases, I would do something like:

Insert data in 3 tables at a time using Postgres

允我心安 提交于 2019-11-26 15:06:41
I want to insert data into 3 tables with a single query. My tables looks like below: CREATE TABLE sample ( id bigserial PRIMARY KEY, lastname varchar(20), firstname varchar(20) ); CREATE TABLE sample1( user_id bigserial PRIMARY KEY, sample_id bigint REFERENCES sample, adddetails varchar(20) ); CREATE TABLE sample2( id bigserial PRIMARY KEY, user_id bigint REFERENCES sample1, value varchar(10) ); I will get a key in return for every insertion and I need to insert that key in the next table. My query is: insert into sample(firstname,lastname) values('fai55','shaggk') RETURNING id; insert into

How to include excluded rows in RETURNING from INSERT … ON CONFLICT

旧时模样 提交于 2019-11-26 14:08:11
问题 I've got this table (generated by Django): CREATE TABLE feeds_person ( id serial PRIMARY KEY, created timestamp with time zone NOT NULL, modified timestamp with time zone NOT NULL, name character varying(4000) NOT NULL, url character varying(1000) NOT NULL, email character varying(254) NOT NULL, CONSTRAINT feeds_person_name_ad8c7469_uniq UNIQUE (name, url, email) ); I'm trying to bulk insert a lot of data using INSERT with an ON CONFLICT clause. The wrinkle is that I need to get the id back