Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have query, help me rewrite it for SQL Server:

insert into swi (co, na, ci, ac, id, version, add) 
    select co, na, ci, acc, id, ?, address 
    from swi_tmp 
    where co||me not in (select co||me from swi)

Now, I have, but it still not working

insert into swi (co, na, ci, ac, id, version, add) 
    select co, na, ci, acc, id, ?, address 
    from swi_tmp 
    where not exists (select 1 
                      from swi_tmp
                      where swi_tmp.co = swi.co and swi_tmp.na = swi.na)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
278 views
Welcome To Ask or Share your Answers For Others

1 Answer

The FROM clause in the NOT EXISTS subquery is using the source table instead of the target table. Change the table name in the NOT EXISTS subquery to the target table name:

INSERT INTO swi (co, na, ci, ac, id, version, add) 
    SELECT co, na, ci, acc, id, ?, address 
    FROM swi_tmp 
    WHERE NOT EXISTS (SELECT 1 
                      FROM swi
                      WHERE
                          swi_tmp.co = swi.co
                          AND swi_tmp.na = swi.na
                     );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...