sql-merge

Migrating an Oracle MERGE statement to a PostgreSQL UPSERT statement

社会主义新天地 提交于 2021-02-18 15:22:42
问题 Can you please help me converting the following Oracle MERGE statement into a valid UPSERT statement for use in a PostgreSQL 9.3 database? MERGE INTO my_table a USING (SELECT v_c1 key, v_c2 AS pkey, v_c3 AS wcount, v_c4 AS dcount FROM DUAL) b ON ( a.key = b.key AND a.pkey = b.pkey WHEN MATCHED THEN UPDATE SET wcount = b.wcount, dcount = b.dcount WHEN NOT MATCHED THEN INSERT (key, pkey, wcount, dcount) VALUES(b.key,b.pkey,b.wcount,b.dcount); 回答1: I don't think there's UPSERT statement in

ORA-38104: Columns referenced in the ON Clause cannot be updated c.emp_id

佐手、 提交于 2021-02-16 21:32:36
问题 i have 2 simple table CREATE TABLE employee ( emp_id INT PRIMARY KEY, first_name VARCHAR(40), last_name VARCHAR(40), birth_day DATE, sex VARCHAR(1), salary INT, super_id INT, branch_id INT ); CREATE TABLE biodata ( emp_id INT PRIMARY KEY, first_name VARCHAR(40), last_name VARCHAR(40), birth_day DATE, sex VARCHAR(1) ); i want to merge it merge into biodata c using employee e on (c.emp_id = e.emp_id) when matched then update set c.emp_id=e.emp_id, c.first_name=e.first_name, c.last_name=e.last

ORA-38104: Columns referenced in the ON Clause cannot be updated c.emp_id

假装没事ソ 提交于 2021-02-16 21:32:08
问题 i have 2 simple table CREATE TABLE employee ( emp_id INT PRIMARY KEY, first_name VARCHAR(40), last_name VARCHAR(40), birth_day DATE, sex VARCHAR(1), salary INT, super_id INT, branch_id INT ); CREATE TABLE biodata ( emp_id INT PRIMARY KEY, first_name VARCHAR(40), last_name VARCHAR(40), birth_day DATE, sex VARCHAR(1) ); i want to merge it merge into biodata c using employee e on (c.emp_id = e.emp_id) when matched then update set c.emp_id=e.emp_id, c.first_name=e.first_name, c.last_name=e.last

Data Integrity bug query fix to rewrite the sql

人盡茶涼 提交于 2020-05-16 22:34:30
问题 I have a table and in this table i have data which has data integrity issue, since this is a dimension table we need to maintain the effective_dt_from and effective_dt_to and version correctly. This is the table and sample data: create table TEST ( LOC_SID NUMBER(38,0), POSTAL_CD VARCHAR2(15 BYTE), COUNTRY_CD_2CHAR VARCHAR2(2 BYTE), CITY VARCHAR2(180 BYTE), DISTRICT_CD VARCHAR2(120 BYTE), POPULATION_APPROX VARCHAR2(15 BYTE), EFFECTIVE_DT_FROM DATE, EFFECTIVE_DT_TO DATE, VERSION NUMBER(38,0) )

How do I convert these SQL statements into more efficient statements

て烟熏妆下的殇ゞ 提交于 2020-04-17 22:45:57
问题 I have the following code: CURSOR Q1 IS SELECT COL1, COL2, COL3, COL4, COL5, COL6 FROM TABLE1; CURSOR Q2 IS SELECT COL11, COL22, COL33, COL44, COL55, COL66 FROM TABLE2; PRESENT BOOLEAN; FOR X IN Q1 LOOP PRESENT := FALSE; FOR Y IN Q2 LOOP IF (X.COL3 = Y.COL33) AND (X.COL4 = Y.COL44) THEN PRESENT := TRUE; EXIT; END IF; END LOOP; IF NOT PRESENT THEN UPDATE TABLE_X SET COL_A = 'Y'; COMMIT; END IF; END LOOP; I am not sure if I can convert this code to MERGE statement or anything more efficient, if

Oracle merge statement and by source/target condition

删除回忆录丶 提交于 2020-02-05 07:37:48
问题 I need to do a MERGE in Oracle, but I'm stuck. In SQL Server, I always use the BY SOURCE and BY TARGET condition to check where record exists, and then take an action. I'm a little confused because I don't know how to achieve the same in PL/SQL. I need to do a MERGE on two tables ( customers and customers_stage ). If the record does not exists in the customer table - then insert. If the record exits in both - then update. If the record does not exists in customers_stage - then delete. In SQL

Need to convert Oracle “merge into” query to PostgreSQL

限于喜欢 提交于 2020-01-25 06:53:45
问题 I'm trying to convert the following Oracle query to PostgreSQL: MERGE into feepay.TRPT_W2_REPORTS TRPT1 USING( WITH RWS AS (SELECT PROG.BINCLIENT, TRPT.PUT_DIRECTORY FROM feepay.program2 PROG INNER JOIN feepay.TRPT_W2_PROGRAMS TRPT ON (PROG.BINCLIENT = TRPT.BINCLIENT OR PROG.ISSUER_ID = TRPT.ISSUER_ID)) SELECT TCI.CUSTOMERNAME AS ACCOUNT, TC.CUSTOMER_ID AS urn, TC.LAST_NAME, TC.FIRST_NAME, TC.DOB, TCA.ADDRESS FROM feepay.TAU_CARDNUMBERS TCN INNER JOIN feepay.TAU_CUSTOMER_CARDNUMBER TCCN ON

ORA-30926: unable to get a stable set of rows in the source tables

為{幸葍}努か 提交于 2020-01-06 19:59:06
问题 I am getting ORA-30926: unable to get a stable set of rows in the source tables in the following query: MERGE INTO table_1 a USING (SELECT a.ROWID row_id, 'Y' FROM table_1 a ,table_2 b ,table_3 c WHERE a.mbr = c.mbr AND b.head = c.head AND b.type_of_action <> '6') src ON ( a.ROWID = src.row_id ) WHEN MATCHED THEN UPDATE SET in_correct = 'Y'; I've ran table_1 it has data and also I've ran the inside query ( src ) which also has data. Why would this error come and how can it be resolved? 回答1:

ORA-30926: unable to get a stable set of rows in the source tables

。_饼干妹妹 提交于 2020-01-06 19:58:19
问题 I am getting ORA-30926: unable to get a stable set of rows in the source tables in the following query: MERGE INTO table_1 a USING (SELECT a.ROWID row_id, 'Y' FROM table_1 a ,table_2 b ,table_3 c WHERE a.mbr = c.mbr AND b.head = c.head AND b.type_of_action <> '6') src ON ( a.ROWID = src.row_id ) WHEN MATCHED THEN UPDATE SET in_correct = 'Y'; I've ran table_1 it has data and also I've ran the inside query ( src ) which also has data. Why would this error come and how can it be resolved? 回答1:

How to merge 2 or more columns into one?

99封情书 提交于 2020-01-05 03:07:11
问题 I have a real problem with one task in SQL concerning merging data from 2 and more columns into 1 column in the most effective way. id column1 column2 column3 1 ok notOK 2 3 abraka dabrra 4 miew haf and I need to merge 3 comments into 1 comment column like this id comments 1 ok 1 notOK 2 3 abraka 3 dabrra 4 miew 4 haf Now I do it manually through insert into table where I have id and comments columns and I have to sort out data from the primary table. It is really time-consuming, especially