Need help to get an insert statement in SQL Server - sql

I would need to insert the non existing values to a Table.
Table name BarcodeSubgroup (single column table)
Column (nvarchar)
Sample data in the table
ProductSubGroupID
-----------------
F11WD
F77AH
G36CN
G37HJ
H11AA
H11AD
Now I need to insert the non existing values in the table.
Values want to be checked and inserted.
H11AA
H11AD
G78DE
G76DK
G41JA
B45JC
Query written
insert into BarcodeSubgroup
select
productsubgroupid
where
not exists ('G78DE', 'G76DK', 'G41JA',
'B45JC', 'H11AA', 'H11AD')
Now it should insert only the 4 non existing values.

You can do this with select . . . not exists:
insert into BarcodeSubgroup(productsubgroupid)
select productsubgroupid
from (values ('G78DE'), ('G76DK'), ('G41JA'), ('B45JC'), ('H11AA'), ('H11AD') ) v(productsubgroupid)
where not exists (select 1
from BarcodeSubgroup bs
where bs.productsubgroupid = v.productsubgroupid
);

Related

Insert only new records that are added to TABLE A into TABLE B

Presently I have 2 tables in a SQL database: Table A and Table B
I am using the syntax
Insert Into TABLE B
Select id, col, col, col......
From TABLE A
Where id NOT IN (SELECT id from TABLE B)
End;
This syntax works great. However, if I delete a record that is in TABLE B, the above code would in return, insert the deleted record back into TABLE B. I do not want this to happen. Is there another way to insert a record that is NOT IN TABLE B "only once". It should basically ignore all other previously inserted records which were inserted into TABLE B. If it was deleted it should not be inserted a second time.
I want it to only insert only new records added to TABLE A.
If the ID is consecutive integer then you can use:
Insert Into TABLE B
Select id, col, col, col......
From TABLE A
where ID > select(max(id) from table B)

How can I make this an insert statement

I am new to SQL, So I have a table called 'DealerShip' and I have a many different car ID's . I have a dealership called 'Hondo' and another one called 'Mitch' . I would like to insert about 80 records into 'Mitch' that 'Hondo' has . For instance with this Query
select * from DealerShips where name='Hondo' and CarType=63
That query above contains about 70 records, How can I create an insert statement that will insert all the returned records from that query above ? The insert will go into the same table above except that the name will be 'Mitch' . I am using MSSQL 2012
INSERT INTO SELECT
INSERT INTO yourtable (FIELDS...) ---- fields here should match the select fields
SELECT FIELDS... FROM
DealerShips where name = 'Hondo' and CarType=63
Just make sure you list the columns in the insert and the select in the same order.
INSERT INTO DealerShips (name, cartype, more columns)
SELECT
'Mitch'
, cartype
, more columns
from DealerShips where name='Hondo' and CarType=63

Using ignore_duplicate on non primary key

I've got table:
ID (identity, PK), TaskNr, OfferNr
I want to do insert ignore statement but sadly it's not working on MSSQL, so there's a IGNORE_DUP switch. But I need to check duplicates using TaskNr column. Is there any chance to do that?
Edit:
Sample data:
ID (identity, PK), TaskNr, OfferNr
1 BP1234 XAS
2 BD123 JFRT
3 1122AH JDA33
4 22345_a MD_3
Trying to do:
insert ignore into Sample_table (TaskNr, OfferNr) values (BP1234, DFD,)
Should ignore that row and go to next value of insert statement. ID is autoincremented but unique value should be checked using TaskNr column.
SQL Server does not support insert ignore. That is MySQL functionality.
You can do what you want as:
insert ignore into Sample_table (TaskNr, OfferNr)
select x.TaskNr, x.OfferNr
from (select 'BP1234' as TaskNr, 'DFD' as OfferNr) x
where not exists (select 1
from Sample_Table st
where st.TaskNr = x.TaskNr and st.OfferNr = x.OfferNr
);
You can try two options:
insert into ... where not exists ()
t-sql merge statement (https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql)

DB2 Using with statement to keep last id

In my project I need to create a script that insert data with auto generate value for the primary key and then to reuse this number for foreign on other tables.
I'm trying to use the WITH statement in order to keep that value.
For instance, I'm trying to do this:
WITH tmp as (SELECT ID FROM (INSERT INTO A ... VALUES ...))
INSERT INTO B ... VALUES tmp.ID ...
But I can't make it work.
Is it at least possible to do it or am I completely wrong???
Thank you
Yes, it is possible, if your DB2-server version supports the syntax.
For example:
create table xemp(id bigint generated always as identity, other_stuff varchar(20));
create table othertab(xemp_id bigint);
SELECT id FROM FINAL TABLE
(INSERT INTO xemp(other_stuff)
values ('a'), ('b'), ('c'), ('d')
) ;
The above snippet of code gives the result below:
ID
--------------------
1
2
3
4
4 record(s) selected.
If you want to re-use the ID to populate another table:
with tmp1(id) as ( SELECT id FROM new TABLE (INSERT INTO xemp(other_stuff) values ('a1'), ('b1'), ('c1'), ('d1') ) tmp3 )
, tmp2 as (select * from new table (insert into othertab(xemp_id) select id from tmp1 ) tmp4 )
select * from othertab;
As per my understanding
You will have to create an auto-increment field with the sequence object (this object generates a number sequence).
You can CREATE SEQUENCE to achieve the auto increment value :
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10

Inserting the data at a time in 3 tables using an array into Postgresql database

I have to insert the data at a time in 3 tables into PostgreSQL database.
I have to insert data into first and Second table is directly.
But The third table which i received data as array and inserted into 3rd table as same.
Is data inserting as array into PostgreSQL available or possible?
If it possible how can I insert can some correct me
How can i do the same mechanism with Wso2 DSS 3.0.1.
My query is
with first_insert as (insert into sample(name,age)
values(?,?)
RETURNING id
),
second_insert as (insert into sample1(empid,desig)
values((select id from first_insert),?)
RETURNING userid
)
insert into sample2(offid,details)
values((select userid from second_insert),?)
Not sure I understood your question exactly, but at least you could use insert into ... select:
with cte_first_insert as
(
insert into sample1(name, age)
values('John', 25)
returning id
), cte_second_insert as (
insert into sample2(empid, desig)
select id, 1 from cte_first_insert
returning userid
)
insert into sample3(offid, details)
select userid, 'test'
from cte_second_insert;
sql fiddle demo