sql-returning

Efficiently duplicate some rows in PostgreSQL table

我怕爱的太早我们不能终老 提交于 2019-12-20 23:31:14
问题 I have PostgreSQL 9 database that uses auto-incrementing integers as primary keys. I want to duplicate some of the rows in a table (based on some filter criteria), while changing one or two values, i.e. copy all column values, except for the ID (which is auto-generated) and possibly another column. However, I also want to get the mapping from old to new IDs. Is there a better way to do it then just querying for the rows to copy first and then inserting new rows one at a time? Essentially I

INSERT or SELECT strategy to always return a row?

被刻印的时光 ゝ 提交于 2019-12-20 03:52:31
问题 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.

PL/SQL How return all attributes in ROW

谁都会走 提交于 2019-12-20 02:42:33
问题 I don't know how I can return all attributes with the RETURNING clause I want something like this: DECLARE v_user USER%ROWTYPE BEGIN INSERT INTO User VALUES (1,'Bill','QWERTY') RETURNING * INTO v_user; END; RETURNING * INTO gets an error , how can I replace * ? 回答1: It would be neat if we could do something like that but alas: SQL> declare 2 v_row t23%rowtype; 3 begin 4 insert into t23 5 values (my_seq.nextval, 'Daisy Head Maisy') 6 returning * into v_row; 7 end; 8 / returning * into v_row; *

Returning the value of identity column after insertion in Oracle

血红的双手。 提交于 2019-12-18 06:17:12
问题 How do I return the value of an identity column (id) in Oracle 12c after insertion? Seems like most of the approaches out there uses sequence to get back the id of the inserted item. 回答1: Simply use the RETURNING clause. For example - RETURNING identity_id INTO variable_id; Test case - SQL> set serveroutput on SQL> CREATE TABLE t 2 (ID NUMBER GENERATED ALWAYS AS IDENTITY, text VARCHAR2(50) 3 ); Table created. SQL> SQL> DECLARE 2 var_id NUMBER; 3 BEGIN 4 INSERT INTO t 5 (text 6 ) VALUES 7 (

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

僤鯓⒐⒋嵵緔 提交于 2019-12-18 05:34:10
问题 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? 回答1: 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

Insert data in 3 tables at a time using Postgres

拜拜、爱过 提交于 2019-12-17 02:58:29
问题 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

How to SELECT the result of RETURNING

别等时光非礼了梦想. 提交于 2019-12-11 15:38:58
问题 How to put the result of a RETURNING clause into a SELECT in PostgreSQL? I.e., I would like the SELECT statement to return whatever RETURNING returns. I have tried the following two syntaxes, none of them seem to work: (1) SELECT (INSERT INTO ... RETURNING *) (2) SELECT * FROM (INSERT INTO ... RETURNING *) ... You might ask, why I'm trying to do this, in spite of RETURNING already giving me the desired result? It's because I have a structure using two WITH clauses, and the insertion would be

Update a table and display updated rows with a single SQL command

无人久伴 提交于 2019-12-11 09:03:23
问题 I need your help with PostgreSQL. I have a homework in which I have to update a column of a table and display all the information that has been updated, but I have to use a single command to do all that. Even worse, I just can use basic clauses like SELECT, UPDATE, SET, WHERE etc. Is this possible? I didn't find any example. I've tried several combinations like: SELECT * FROM customer (UPDATE custumer SET bithdate = bithdate + INTERVAL '1 DAY' WHERE bithcity = 'New York'); This didn't work!

PHP, Postgres help using RETURNING

风流意气都作罢 提交于 2019-12-09 10:11:53
问题 I think I understand how PostgreSQL and RETURNING works - I've found many, many resources. If I'm catching on, it would look something like "INSERT INTO table (column2, column3) VALUES ('value1', 'value2') RETURNING id;" However, I can't find anything that helps me access this via PHP. When I thought I figured it out, I tried $new_id = pg_query($dbc, "INSERT INTO table (column2, column3) ". "VALUES ('value1', 'value2') RETURNING id;"); return $new_id; But it returns NULL. I also tried

Oracle - RETURNING combined with aggregate functions

邮差的信 提交于 2019-12-03 17:44:17
问题 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);