i try update data from another table using PostgreSQL 9.6 following the document and stackoverflow advice with this query
1. this query is for finding the id_vertex of geom that close to lokasi_esb.geom. You can ignore this one, it works properly
CREATE TEMP TABLE temp1 AS
WITH kuery2 as(
SELECT id_esb, id_vertex, distant, rank() OVER (PARTITION BY id_esb ORDER BY distant asc) as ranked FROM table vertex)
select id_esb, id_vertex, distant, ranked
from kuery2
where ranked=1;
2. this query to update the lokasi_esb table with id_vertex_nearest column without excluded table. ////i already know it's wrong and i update on the number 3
INSERT INTO lokasi_esb(id_esb, id_vertex_nearest)
select id_esb,id_vertex
from temp1
ON CONFLICT (id_esb) DO UPDATE
SET lokasi_esb.id_vertex_nearest = temp1.id_vertex;
i got this error
ERROR: missing FROM-clause entry for table « temp1 »
SQL state: 42P01
Character: 634
3. this query to update the lokasi_esb table with id_vertex_nearest column with excluded table
INSERT INTO lokasi_esb(id_esb, id_vertex_nearest)
select id_esb,id_vertex
from temp1
ON CONFLICT (id_esb) DO UPDATE
SET lokasi_esb.id_vertex_nearest = excluded.id_vertex;
igot this error(transleted from indonesia)
ERROR: column excluded.id_vertex not yet exist
SQL state: 42703
Character: 634
So can anybody can help me figure out what happened here?
The column names from the "excluded" record refer to the columns of the target table. And the target column in the SET expression must not be prefixed with the table name (because you can't update a different table anyway)
So you need to use:
SET id_vertex_nearest = excluded.id_vertex_nearest
Related
I have a simple table called source with 3 columns (source_id, name, tenant_id) and some data in it.
I a trying to insert a data into the table by checking its existence in the table. But I am getting this error ..any idea how to resolve
The Query -
INSERT INTO moa.source(source_id, name, tenant_id)
SELECT ((select max(source_id)+1 from moa.source), 'GE OWS - LHR', 1)
WHERE NOT EXISTS(SELECT 1 FROM moa.source where name = 'GE OWS - LHR');
The Error :
ERROR: INSERT has more target columns than expressions
LINE 1: INSERT INTO moa.source(source_id, name, tenant_id)
^
HINT: The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?
SQL state: 42
The Table:
Sorry..figured it out..there should be no paranthesis after Select
I have the following sql code:
UPDATE google_calendar_accounts SET google_refresh_token="d",google_org_token="d" WHERE userID=5;
IF ROW_COUNT()=0 THEN
INSERT INTO google_calendar_accounts (userID,google_refresh_token,google_org_token) VALUES (5,"d","d"); END IF
and I am getting the error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2
I am using mariadb 10.1.14
In spite the comment suggesting to do INSERT ... ON DUPLICATE KEY UPDATE ..., There may be a reason to do the update first, and insert just if there was no row affected, like the OP tried: this way auto increment won't be increased in vain.
So, a possible solution to the question may be using insert from select with a condition where row_count()=0
For example:
--first query
UPDATE google_calendar_accounts
SET google_refresh_token='d',google_org_token='d'
WHERE userID=5;
--second query use the affected rows of the previous query
INSERT IGNORE INTO google_calendar_accounts (userID,google_refresh_token,google_org_token)
SELECT 5,'d','d' WHERE ROW_COUNT()=0
BTW: I've added IGNORE to the insert query for a case there was a row match to the update condition but it wasn't updated since all columns was identical to the updated, like in the case before the update there was already row 5,'d','d'.
In such case, if the 5 is primary or unique key the query won't fail.
I have a simple table (id and name column, both unique), which I am importing a tab delimited CSV file.
I am running psql 9.5, and wanted to try out the new ON CONFLICT feature to update the name column if the ID already exists.
CREATE TEMP TABLE tmp_x AS SELECT * FROM repos LIMIT 0;
COPY tmp_x FROM '/Users/George/git-parser/repo_file' (format csv, delimiter E'\t');
INSERT INTO repos SELECT * FROM tmp_x
ON CONFLICT(name) DO UPDATE SET name = tmp_x.name;
DROP TABLE tmp_x;
I am getting this error:
SELECT 0
COPY 1
ERROR: missing FROM-clause entry for table "tmp_x"
LINE 4: ON CONFLICT(name) DO UPDATE SET name = tmp_x.name;
^
Query failed
PostgreSQL said: missing FROM-clause entry for table "tmp_x"
Not too sure whats going wrong here.
If you look at the documentation of the ON CONFLICT clause, it says this about the "conflict action":
The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table's name (or an alias)
In your query, the target table is repos.
tmp_x, on the other hand, is the source of the data you are trying to insert, but the ON CONFLICT clause cannot "see" that - it is looking at a particular row that has been calculated and failed. Consider if you'd written something like this:
INSERT INTO repos SELECT max(foo_id) FROM tmp_x
Clearly, it wouldn't make sense for a row which failed to insert into repos to have access to any one row from tmp_x.
If there was no way of seeing the rejected data, the whole feature would be pretty useless, but if we read on:
... and to rows proposed for insertion using the special excluded table.
So instead, you need to access the magic table alias excluded, which contains the values which you tried to insert but got a conflict on, giving you this:
INSERT INTO repos SELECT * FROM tmp_x
ON CONFLICT(name) DO UPDATE SET name = excluded.name;
If it seems weird that an imaginary table name pops up for this purpose, consider that a similar thing happens when writing triggers, where you get OLD and NEW (depending on the kind of trigger you're writing).
There are some similar questions on StackOverflow, but they don't seem to exactly match my case. I am trying to bulk insert into a PostgreSQL table with composite unique constraints. I created a temporary table (temptable) without any constraints, and loaded the data (with possible some duplicate values) in it. So far, so good.
Now, I am trying to transfer the data to the actual table (realtable) with unique index. For this, I used an INSERT statement with a subquery:
INSERT INTO realtable
SELECT * FROM temptable WHERE NOT EXISTS (
SELECT 1 FROM realtable WHERE temptable.added_date = realtable.added_date
AND temptable.product_name = realtable.product_name
);
However, I am getting duplicate key errors:
ERROR: duplicate key value violates unique constraint "realtable_added_date_product_name_key"
SQL state: 23505
Detail: Key (added_date, product_name)=(20000103, TEST) already exists.
My question is, shouldn't the WHERE NOT EXISTS clause prevent this from happening? How can I fix it?
The NOT EXISTS clause only prevents rows from temptable conflicting with existing rows from realtable; it will not prevent multiple rows from temptable from conflicting with each other. This is because the SELECT is calculated once based on the initial state of realtable, not re-calculated after each row is inserted.
One solution would be to use a GROUP BY or DISTINCT ON in the SELECT query, to omit duplicates, e.g.
INSERT INTO realtable
SELECT DISTINCT ON (added_date, product_name) *
FROM temptable WHERE NOT EXISTS (
SELECT 1 FROM realtable WHERE temptable.added_date = realtable.added_date
AND temptable.product_name = realtable.product_name
)
ORDER BY ???; -- this ORDER BY will determine which of a set of duplicates is kept by the DISTINCT ON
I have the following Oracle 10g sql which to me looks about right:
update ( select OLD1.TC_CUSTOMER_NUMBER,NEW1.PRD_CUST_NUMBER
FROM TBYC84_PROFILE_ACCOUNT OLD1,
TMP_PRD_KEP NEW1
WHERE
OLD1.TC_CUSTOMER_NUMBER = NEW1.KEP_CUST_NUMBER )
SET
TC_CUSTOMER_NUMBER = PRD_CUST_NUMBER
But i am getting this error when i run the script:
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.
I have done done some research on this error but not quite sure how to remedy.
So my question is, how can i fix this or is there a better way to write the update sql?
Any help would be appreciated.
many thanks
UPDATE
I have changed the update sql to this:
update
TBYC84_PROFILE_ACCOUNT PA
set
(
PA.TC_CUSTOMER_NUMBER
) = (
select
TPK.PRD_CUST_NUMBER
from
TMP_PRD_KEP TPK
where
TPK.KEP_CUST_NUMBER = PA.TC_CUSTOMER_NUMBER
)
Now this has updated the TBYC84_PROFILE_ACCOUNT table AND nulled out the TC_CUSTOMER_NUMBER
column.
Why did it do this?
There may be more than one row in the TBYC84_PROFILE_ACCOUNT.TC_CUSTOMER_NUMBER
that has the same account number but for different user_id's.
Please can anyone assist in helping me resolve this.
All I need to to is update the TBYC84_PROFILE_ACCOUNT.TC_CUSTOMER_NUMBER to the one that is xrefed in the TMP_PRD_KEP, surely this is not impossible.
many thanks
For an UPDATE statement, all the columns that are updated must be extracted from a key-preserved table.
Also:
A key-preserved table is one for which every primary key or unique key value in the base table is also unique in the join view.
Here.
In this case, TBYC84_PROFILE_ACCOUNT is being updated. So, it must be key-preserved in the view's subquery. Currently it is not. It must be changed in a way that it becomes key-preserved by involving primary or unique columns in the where clause. If not possible, you should try to update the base table instead.
UPDATE
In case of the table update problem, assuming the subquery returns at most one distinct value for the TC_CUSTOMER_NUMBER column, the reason you get NULLs is that all records are being updated even if they do not have any matching records in the TMP_PRD_KEP table. So, the parent update statement needs to be fitted with a where clause:
update
TBYC84_PROFILE_ACCOUNT PA
set
(
PA.TC_CUSTOMER_NUMBER
) = (
select
TPK.PRD_CUST_NUMBER
from
TMP_PRD_KEP TPK
where
TPK.KEP_CUST_NUMBER = PA.TC_CUSTOMER_NUMBER
)
where exists(select *
from TMP_PRD_KEP TPK
where TPK.KEP_CUST_NUMBER = PA.TC_CUSTOMER_NUMBER)
;
Create a index on the columns used in your where clause predicates. That should solve your problem.