CakePHP during bulk insert if duplicate record found update that - cakephp

I am using CakePHP Version-4 and using saveMany function for bulk
insert now bulk insert would not work if there is any duplicate record
exist due to primary key in database. So point is can I update (or
ignore duplicate if update not possible) by using saveMany method .
$entities = $model->newEntities($data);
$model->saveMany($entities);

Related

POSTGRES - INSERT INTO FOREIGN TABLE

I created a table into an external server
CREATE FOREIGN TABLE external_table (
field_1 varchar(15) NULL,
field_2 int4 NULL
)
SERVER server_name
OPTIONS(compression 'pglz', stripe_row_count '500000');
Now I want to insert into external_table, but if I run this query
INSERT INTO external_table (field_1, field_2) VALUES ('test',1);
It return this error
ERROR: operation is not supported
How can I add record into a foreign table?
I've tried with the following insert
INSERT INTO external_table (field_1, field_2) select 'test',1;
It works, but I can't use a INSERT INTO with SELECT statment.
Looks like the extension you are using supports "insert into ... select .." but not direct inserts.
you can use you should probably ask this question while specifying the extension.
PS: It looks like the extension you use is cstore_fdw. It does not support direct inserts, because it completely cancels benefits of using columnar storage and create some extra overhead. If you are using cstore_fdw, try to use bulk inserts instead of single row ones. Inserting into a regular table and moving data into cstore_fdw table when data reaches certain size (i.e. stripe_row_count number of rows) is much better option.

Get Information about Inserted Rows during a BULK INSERT

I insert information into my table using a Bulk Insert to speed things up. Now I want to add a trigger to my table. But this trigger is run once with every Bulk Insert whereas I need to know what the rows that were inserted are during the latest bulk insert.
So, is there a query to know what the inserted rows during BULK INSERT were?
If you have an ID IDENTITY column, you could make a note of the ID before the BULK INSERT and then all rows with an ID higher than this value you wrote down have been bulk inserted.
You cannot have triggers on a per-row basis in SQL Server, nor can you do anything else (like using an OUTPUT statement) to capture the inserted rows, really. You just have to look at what's in the database before and after the BULK INSERT

Error in inserting data from one table to another

While I am inserting data from Sample_User_Bak to Sample_User by using the query below:
INSERT INTO Sample_User (Sample_UserID, Sample_Username, Sample_Password)
SELECT Sample_UserID, Sample_Username, Sample_Password
FROM Sample_User_Bak;
I receive the following error message in SQL Server.
Violation of PRIMARY KEY constraint 'PK_Sample_User'. Cannot insert duplicate key in object 'dbo.Sample_User'.
Both tables have the same column names. I am just trying to insert the values in normal procedure. What is wrong with my query? Any help will be much appreciated.
If you ever come into a situation where you can’t delete existing data because of other constraints you can do something like this.
INSERT INTO Sample_User
(Sample_UserID, Sample_Username, Sample_Password)
SELECT Sample_UserID, Sample_Username, Sample_Password
FROM Sample_User_Bak
WHERE Sample_User_Bak.Sample_UserID not in
(SELECT Sample_UserID FROM Sample_User)
Original data are existing in Sample_User. That's why I cannot insert the new values from Sample_User_Bak.
So, I deleted the values from Sample_User first.
DELETE * FROM Sample_User
and then executed the above INSERT query.

update null primary key in a trigger using SQL Server

I'm migrating our system from Oracle to SQL SERVER. In Oracle we have insert triggers that are resposnible for setting primary key if not set. Below you will find code from PL/SQL.
create or replace trigger trigg1
before insert on table1
for each row
when (new.ID_T1 is null) -- if primary key is null
begin
select OUR_SEQ.nextval into :new.ID_T1 from dual;
end trigg1;
Now I have to do something similar in T-SQL. I found the solution, but unfortunatelly I have to list all the columns for the table trigger is created. This is something I want to avoid (model for the system is still very dynamic).
Is it possible to implement such trigger without listing all the columns in trigger?
Marcin

What should be the table contain updated row for update trigger

A while ago i read the article for Trigger in SQL Server, and it said that i can use the Logical Table "Updated" for updated rows... And i got error:
System.Data.SqlClient.SqlException: Invalid object name 'Updated'.
After a while of google, i found out some more post that said only 2 logical tables available are: Inserted and Deleted...
I'm confused... What should i use since my Trigger rely on the Updated table that contain the updated row, and use it to insert to another table or the same table with new PK...
Thank you very much
The two dummy tables are called Inserted (available in INSERT and UPDATE triggers) and Deleted (available in DELETE and UPDATE triggers).
There is no Updated dummy table in SQL Server triggers.
For an FOR UPDATE trigger, the Deleted table contains the old values, while the Inserted table contains the new ones.
Marc

Resources