This question already has answers here:
Sql insert multiple rows if not exists
(7 answers)
Closed 10 months ago.
I have a SQL script with about 250 entries that are being inserted. The script on its own runs fine however, I run into problems in case of duplicate entries. I could try doing something like this:
IF NOT EXISTS (SELECT * FROM [TS].[Configs] WHERE [Id] = 15)
INSERT . . .
However, I cannot write this repeated statement over and over for the 250 entries. I have tried setting Identity_Insert to ON but I still get this error:
Exception Message: Violation of PRIMARY KEY constraint 'Tbl_LangFiles'. Cannot insert duplicate key in object '[TS].[Configs]'
How can I go about only inserting unique entries in this table?
Have you tried the UPSERT statement?
UPSERT INTO employees (id, name, email)
VALUES (2, ‘Dennis’, ‘dennisp#weyland.corp’);
This will attempt to insert a new record on the database, but if it already exists, it will take the parameters and update.
L.
Related
This question already has answers here:
Postgres: INSERT if does not exist already
(19 answers)
Closed last year.
I have this SQL query which I need to run on different databases. In some databases it's missing. In other this record is present.
INSERT INTO common.pair_aliases (pair_id, alias)
VALUES (356, 'parisrome');
Is it possible to implement a logic first to check is this record present before executing the SQL query?
If I understand you correctly, maybe WHERE NOT EXISTS could help you:
INSERT INTO common.pair_aliases (pair_id, alias)
SELECT R.*
FROM (VALUES (356, 'pairsrome')) R (pair_id, alias)
WHERE NOT EXISTS (SELECT 1 FROM common.pair_aliases PR WHERE PR.pair_id = R.pair_id)
This question already has answers here:
SQL Server Trigger - Insert the deleted record into another table with deletetime
(2 answers)
Closed last year.
create trigger sampletrigger
on SunilkarDB.dbo.salesorder_table
for delete
as
insert into salesorderdel_table
I have a table called salesorder, I am creating a trigger such that when I delete a row from the salesorder table, the row here gets deleted and gets added to new table called salesorderdel table.
I have written the following code and its not working, please help!
You need to specify the values that you want to insert in the table. You can use the deleted logical table.
e.g.
CREATE TRIGGER sampletrigger ON SunilkarDB.dbo.salesorder_table
FOR DELETE
AS
INSERT INTO SunilkarDB.dbo.salesorder_table ( [Name])
SELECT [Name]
FROM deleted
This question already has answers here:
Insert into ... values ( SELECT ... FROM ... )
(27 answers)
Closed 3 years ago.
i am running this query where in rel_branding there are more than 1 rows and i want to insert brandingid of every record in rel_branding_permission.
This is for PostgresSQL.
insert into rel_branding_permission (brandingid,permissionid)
values((select brandingid from rel_branding), 404);
I want to insert this query in my DB as for all branding ids
You should be using an INSERT INTO ... SELECT here, something like this:
INSERT INTO rel_branding_permission (brandingid)
SELECT brandingid
FROM rel_branding
WHERE <some condition>
You would probably want to include more than one column in the insert into the rel_branding_permission table, and you may modify the above to fit your need by adding the additional columns to both the INSERT and the SELECT.
This question already has answers here:
PostgreSQL function for last inserted ID
(14 answers)
Closed 4 years ago.
I have:
Insert Into(name)
Value('Anton')
How do I get the newly created id?
Insert a single row into table distributors, returning the sequence
number generated by the DEFAULT clause:
Code
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
RETURNING did;
The optional RETURNING clause causes INSERT to compute and return
value(s) based on each row actually inserted. This is primarily useful
for obtaining values that were supplied by defaults, such as a serial
sequence number. However, any expression using the table's columns is
allowed. The syntax of the RETURNING list is identical to that of the
output list of SELECT.
Reference:
https://www.postgresql.org/docs/9.1/static/sql-insert.html
https://stackoverflow.com/a/2944481/7793817
You use the RETURNING keyword
https://www.postgresql.org/docs/9.5/static/dml-returning.html
From the doc :
INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inserting rows into a table with one IDENTITY column only
I have a SQL table with just one column. Column is an autoincrement field like:
Id INT IDENTITY
I need to insert a rows to this table (this is a bulk/dummy table temporarily):
INSERT INTO my_table VALUES ()
But I'm got a syntax error.
How can I do this?
INSERT INTO my_table DEFAULT VALUES
Try to insert explicit null values:
INSERT INTO my_table VALUES (NULL, NULL);