Insert record if id is missing [duplicate] - sql

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)

Related

SQL - Insert only explicit values and not repeated ones [duplicate]

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.

Multi Row insert Query in Oracle (selecting multiple rows from one table & insert in to other table [duplicate]

This question already has answers here:
INSERT SELECT statement in Oracle 11G
(5 answers)
Closed 3 years ago.
Dear StackOverflow Community,
i have two tables users and USERQueries
]
My requirement is to write a query in oracle
"which fetch userids from users table & insert into USERQueries table"
I am able to insert one userid as below
insert into USERSQUERIES
(APP,CLAUSENAME,USERID,DEFAULTQUERYID,OWNER)
values
('SR','Assgined_SRs_To_Me',(select userid from USERS where groupname='IBMSDL2S' and userid='1249
),DEFAULTQUERYSEQ.NEXTVAL,'MAXADMIN')
but didn't understand how it will work for all userids ,
and userids shouldnt repeat.
kindly help
That depends on tables' description, but - generally speaking, you'd
insert into userqueries
(app,
clausename,
userid)
select app,
clausename,
userid
from users
where ...

PostgresSql error: more than one row returned by a subquery used as an expression [duplicate]

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.

PostgreSQL database - only insert if the record doesn't exist [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cannot SELECT from UPDATE RETURNING clause in postgres
I am using a PostgreSQL database which has a "company" table and a "country" table which each have a uniqueID.
I want a setup where if I try to add a company to the database, it only adds the company to the database with a new unique ID if it doesn't already exist.
insert into company (unique_id, company_name)
select 42, 'New Company Name'
from company
where not exists (select 1 from company where unique_id = 42);
See also here for a more general solution: Insert, on duplicate update in PostgreSQL?

Getting Identity Column Value [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get the primary key of the last row inserted into the table
In SQL Server 2008, I have a stored proc that inserts in a table which includes identity column for ID. I need to return the ID of record to my application so that I can use it for related tables.
How can I get generated ID by SQL?
Use select Scope_Identity() to get the ID
A separate SQL statement
SELECT SCOPE_IDENTITY()
Or the OUTPUT clause
INSERT MyTable (...=
OUTPUT INSERTED.KeyCol
VALUES (...) --Or SELECT ... FROM Another table)
There are multiple options that are a bit different:
SCOPE_IDENTITY() - that's what I would use
IDENT_CURRENT( 'table_name' )
##IDENTITY