Answers for "insert or update"

SQL
3

mysql insert exists update

INSERT INTO table (column_list)
VALUES (value_list)
ON DUPLICATE KEY UPDATE
   c1 = v1, 
   c2 = v2,
   ...;
Posted by: Guest on August-03-2020
7

update after insert sql

-- SQL Server (update my_table2 after insert on my_table1)
CREATE TRIGGER trigger_name ON my_table1 FOR INSERT AS
BEGIN
    UPDATE my_table2 SET my_col_date = getdate() FROM my_table1 
END
-- Oracle (insert into log table after update on my_table)
CREATE OR REPLACE TRIGGER trigger_name AFTER INSERT ON my_table
FOR EACH ROW
BEGIN
    INSERT INTO my_log_table (LOG_DATE, ACTION) VALUES (SYSDATE, 'Changed');
END;
Posted by: Guest on July-10-2021
1

sql insert or update

-- Oracle: Example for Insert or update in t1 from t2 values 
MERGE INTO table1 t1
USING table2 t2
ON (t1.CODE = t2.ID)
WHEN MATCHED THEN
    UPDATE SET t1.COL1 = t2.VALUE1
WHEN NOT MATCHED THEN
    INSERT (CODE, COL1)  VALUES (t2.ID, t2.VALUE1);

-- MySql (makes a INSERT + DELETE if existing)
REPLACE INTO table1 (pk_id, col1) VALUES (5, 'aaaa');
--
INSERT INTO table1 VALUES (key, generation)
ON DUPLICATE KEY UPDATE (key = key, generation = generation + 1);
Posted by: Guest on January-24-2021
0

insert or update sql query

INSERT INTO devices(id,name) 
VALUES 
   (4,'Central Printer') 
ON DUPLICATE KEY UPDATE name = 'Central Printer';
Posted by: Guest on October-15-2021

Code answers related to "SQL"

Browse Popular Code Answers by Language