May 7, 2013 at 4:49 pm
Hi Professionals.
I have two table which are identical apart from the data in them
lookuptable with columns
raw_sw_manufacturer, amended_sw_manufacturer,
raw_product_name, amended_product_name,
raw_product_version, amended_product_version
lookuptable1
raw_sw_manufacturer, amended_sw_manufacturer,
raw_product_name, amended_product_name,
raw_product_version, amended_product_version
I am looking to insert rows into lookuptable based on
if
lookuptable1's
raw_sw_manufacturer,raw_product_name, raw_product_version exist in the lookuptable then
do nothing
else
insert the row
end if
is this possible
thanks in advance
May 8, 2013 at 12:15 am
Hi Alan,
insert into lookuptable
select * from lookuptable1
where raw_sw_manufacturer not in (select raw_sw_manufacturer from lookuptable ) and
raw_product_name not in (select raw_product_name from lookuptable ) and
raw_product_version not in (select raw_product_version from lookuptable )
May 8, 2013 at 12:53 am
INSERT INTO lookuptable (
raw_sw_manufacturer, amended_sw_manufacturer,
raw_product_name, amended_product_name,
raw_product_version, amended_product_version)
SELECT
raw_sw_manufacturer, amended_sw_manufacturer,
raw_product_name, amended_product_name,
raw_product_version, amended_product_version
FROM lookuptable1 l1
WHERE NOT EXISTS (
SELECT 1
FROM lookuptable l
WHERE l.raw_sw_manufacturer = l1.raw_sw_manufacturer
AND l.raw_product_name = l1.raw_product_name
AND l.raw_product_version = l1.raw_product_version)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
May 8, 2013 at 4:04 am
Also, you could use MERGE, which can be extended to update non-matched records or/and delete some:
MERGE lookuptable AS dest
FROM lookuptable1 AS src
ON ( dest.raw_sw_manufacturer = src.raw_sw_manufacturer
AND dest.raw_product_name = src.raw_product_name
AND dest.raw_product_version = src.raw_product_version )
WHEN NOT MATCHED THEN
INSERT (raw_sw_manufacturer, amended_sw_manufacturer,
raw_product_name, amended_product_name,
raw_product_version, amended_product_version)
VALUES (src.raw_sw_manufacturer, src.amended_sw_manufacturer,
src.raw_product_name, src.amended_product_name,
src.raw_product_version, src.amended_product_version)
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply