Bulk update using MERGE without a temporary table [duplicate] - postgresql

This question already has answers here:
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
(7 answers)
Merge Into Statement Erroring out at "Merge"
(1 answer)
Closed 7 months ago.
In a preview version of Entity Framework 7, a .NET ORM, bulk inserts for SQL Server are designed to be executed in a single SQL command (one roundtrip to the db):
MERGE [Blogs] USING (
VALUES (#p0, 0),
(#p1, 1),
(#p2, 2),
(#p3, 3)) AS i ([Name], _Position) ON 1=0
WHEN NOT MATCHED THEN
INSERT ([Name])
VALUES (i.[Name])
OUTPUT INSERTED.[Id], i._Position;
This command inserts new rows with values in the Name column, in the Blogs table, and returns their Ids.
Is there a way to do bulk update like this in PostgreSQL? Without creating a temporary table and running select after the merge?

Related

I am having a syntax error in the MERGE statement [duplicate]

This question already has answers here:
Need to convert Oracle "merge into" query to PostgreSQL
(1 answer)
Migrating an Oracle MERGE statement to a PostgreSQL UPSERT statement
(3 answers)
Merge statement with two conditions in same CLAUSE alternative in Postgres
(1 answer)
PostgreSQL Upsert with a WHERE clause
(1 answer)
Closed 2 years ago.
I have postgres 12.3 and find an error in a simple MERGE statement like below:
MERGE INTO lkup_language a
USING (SELECT *
FROM dblc_stg.stg_lkup_home_language
WHERE home_lang_code NOT IN (SELECT home_lang_code
FROM dblc_stg.stg_lkup_home_language
GROUP BY home_lang_code
HAVING COUNT (*) > 1)) b
ON (a.language_cd = b.home_lang_code)
WHEN NOT MATCHED THEN
INSERT (a.language_key, a.language_cd, a.language_desc)
VALUES (NEXTVAL('SEQ_LKUP_LANGUAGE'),b.home_lang_code, b.home_lang_desc)
WHEN MATCHED THEN
UPDATE
SET a.language_desc = b.home_lang_desc ;
I hope I get some help
Thanks
Ajay

PostgreSQL equivalent of SQL merge trigger and INSERTED table

I am new to database triggers/PostgreSQL and trying to convert the following SQL trigger to PostgreSQL.
SQL script :
CREATE TRIGGER tr_EmpMerger ON Emp INSTEAD OF INSERT
AS
BEGIN
MERGE INTO Emp AS Target
USING ( SELECT * FROM INSERTED ) AS Source
ON
( Target.EmpId = Source.EmpId
)
WHEN MATCHED THEN UPDATE SET
EmpName = Source.EmpName,
Age = Source.Age
WHEN NOT MATCHED THEN INSERT VALUES
(
Source.EmpId,
Source.EmpName,
Source.Age
);
END
GO
Questions :
1) Is there any equivalent of SQL's INSERTED table in PostgreSQL? If not, what is the work around?
2) Does PostgreSQL support Merge triggers? If not, what is the work around?
3) What will be the equivalent PostgreSQL script for the above merge trigger?
EDIT :
Note - In this scenario the insertion of data into the Emp table (as well as other tables) is happening through Bulk Copy command of Postgres. So there is no direct INSERT INTO query available for this table

Postgres 11 INSERT ON CONFLICT SELECT * [duplicate]

This question already has an answer here:
Postgres 9.5 ON CONFLICT DO SELECT
(1 answer)
Closed 3 years ago.
I am trying to insert a row into a table, if that insert succeeds I would to return the newly inserted row. If that insert fails, I would to select a row using some of the values I already know to be on that row.
For example, table 'reports' has 3 columns, so far I have the following:
INSERT INTO reports (col2, col3) VALUES ($1, $2)
ON CONFLICT ON CONSTRAINT custom_index DO
SELECT * FROM reports WHERE col1=$1 AND col2=$2;
For those who will ask, custom_index was created using:
CREATE UNIQUE INDEX custom_index ON reports (col2) INCLUDE (col3);
The current error I am getting is
syntax error at or near "SELECT"
Select is not allowed in the conflict.
Check the documentation https://www.postgresql.org/docs/current/sql-insert.html.
Related question is already answered.. Postgres 9.5 ON CONFLICT DO SELECT

Insert or select and returning the key [duplicate]

This question already has answers here:
Get id from INSERT or SELECT
(3 answers)
Return rows from INSERT with ON CONFLICT without needing to update
(1 answer)
Is SELECT or INSERT in a function prone to race conditions?
(3 answers)
Closed 4 years ago.
What is the easiest way to insert a row in a database and returning its id if that row doesn't exist, otherwise return the ID of that word?
INSERT INTO mytable (name)
SELECT 'd'
WHERE NOT EXISTS (SELECT id FROM mytable WHERE name='d')
RETURNING id
This code works only if the row doesn't exist.
I either do that and then a SELECT, or try the INSERT with ON CONFLICT DO NOTHING and then a SELECT again.
Is there a better solution?

Insert script to auto populate numbers 1-1000? [duplicate]

This question already has an answer here:
Populate Postgres database with numbers 1-1000?
(1 answer)
Closed 5 years ago.
I have a single table with a single column called id as shown below. I need to populate the table with numbers 1-1000, how can I do that with the scripting?
Use generate_series to this:
--Without column declaration
INSERT INTO "MyTable" SELECT generate_series(1, 1000);
--With column declaration
INSERT INTO "MyTable"(id) SELECT generate_series(1, 1000);