Pregunta

I want to write a query to insert dynamic values if they or NOT present therein already. So far I have tried this.

INSERT MTB_AML..tb_aml_codes (aml_code, aml_desc)

    SELECT 'NRA', 'Non-Resident Alien'
    UNION
    SELECT 'DOM', 'Resident Alien'
    FROM MTB_AML..tb_aml_codes t1
    WHERE t1.aml_code NOT IN (SELECT t2.aml_code from MTB_AML..tb_aml_codes t2)

But this is returning only upper select (which is already present in the table). What am I doing wrong?

¿Fue útil?

Solución

I think that the problem you're having is that in your version the WHERE clause was only applying to the last Select in the UNION clause

TAke the records you want

 SELECT 'NRA' AS aml_code, 'Non-Resident Alien' AS aml_desc
    UNION 
 SELECT 'DOM' AS aml_code, 'Resident Alien' AS aml_desc

And then wrap them up as a subquery (aliased as [src] in the example) you can then check which ones don't have matching keys in the destination table (aliased as [dst])

INSERT MTB_AML..tb_aml_codes (aml_code, aml_desc)
SELECT src.aml_code, src.aml_desc
(
    SELECT 'NRA' AS aml_code, 'Non-Resident Alien' AS aml_desc
    UNION 
    SELECT 'DOM' AS aml_code, 'Resident Alien' AS aml_desc
) src
WHERE src.aml_code NOT IN (SELECT dst.aml_code from MTB_AML..tb_aml_codes dst)

Personally I'd do it with a left join like this but it's up to you

  INSERT MTB_AML..tb_aml_codes (aml_code, aml_desc)
SELECT src.aml_code, src.aml_desc
FROM
(
    SELECT 'NRA' AS aml_code, 'Non-Resident Alien' AS aml_desc
    UNION 
    SELECT 'DOM' AS aml_code, 'Resident Alien' AS aml_desc
) src
LEFT JOIN MTB_AML..tb_aml_codes dst
ON dst.aml_code = src.aml_code
WHERE dst.aml_code IS NULL

both would work but if you had to match on multi-column key you'd need to use the join method

Otros consejos

You are HARDCODING the values of the resultset, wrapping quotes means a literal value, you must want the COLUMN name.

SELECT 'DOM', 'Resident Alien'

You need to have your SELECT like this:

SELECT T1.ColName, T1.AndAnotherColName
....

You are inserting static data which have no relation to the tables MTB_AML..tb_aml_codes. So your insert query can be simplified like below

INSERT MTB_AML..tb_aml_codes (aml_code, aml_desc)
VALUES ('NRA', 'Non-Resident Alien'),('DOM', 'Resident Alien')

EDIT:

Then you need to check whether those values do exist or not and accordingly do the insertion as shown below

IF NOT EXISTS 
(select 1 from MTB_AML..tb_aml_codes where aml_code in('NRA','DOM') 
and aml_desc in ('Non-Resident Alien','Resident Alien'))
BEGIN
INSERT INTO MTB_AML..tb_aml_codes (aml_code, aml_desc)
VALUES ('NRA', 'Non-Resident Alien'),('DOM', 'Resident Alien')
END

I think the MERGE statement can be used for your needs. It succinctly does what you're trying to do.

MERGE MTB_AML..tb_aml_codes AS dest
USING (
    -- place here whatever you want to insert if not already present
    -- I'm using something like what you have in your question since 
    -- that's your example
    SELECT 'NRA', 'Non-Resident Alien'
    UNION
    SELECT 'DOM', 'Resident Alien'
) AS src
ON dest.aml_code = src.aml_code
WHEN NOT MATCHED BY TARGET THEN
    INSERT (aml_code, aml_desc)
    VALUES (src.aml_code, src.aml_desc)
;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top