如何使用if或case来汇总这些陈述​​?

时间:2015-07-30 12:55:26

标签: sql sql-server sql-server-2008

我有这些陈述,我不知道如何整理,使用if或case

insert into tlp (given_name, namel, email)
   select 
       first_name, namel, mail
   from 
       glob
   where 
       (not exists (select email from tlp where (glob.mail = tlp.email)))
       and glob.mail is not null;

insert into tlp (given_name, namel, email)
   select 
       first_name, namel, mail
   from 
       glob
   where 
       (not exists (select namel from tlp where (glob.namel = tlp.namel)))
       and glob.mail is null;

2 个答案:

答案 0 :(得分:0)

如果要将两个插入转换为一个插入,请尝试这样的操作。

INSERT INTO tlp 
            (given_name, 
             namel, 
             email) 
SELECT first_name, 
       namel, 
       mail 
FROM   glob 
WHERE  ( NOT EXISTS (SELECT email 
                     FROM   tlp 
                     WHERE  glob.mail = tlp.email ) 
         AND glob.mail IS NOT NULL ) 
        OR ( NOT EXISTS (SELECT namel 
                         FROM   tlp 
                         WHERE  glob.namel = tlp.namel ) 
             AND glob.mail IS NULL ) 

答案 1 :(得分:0)

您可以像这样组合插页:

INSERT tlp (given_name, namel, email)
SELECT
    first_name, namel, mail
FROM
    glob
WHERE 
    NOT EXISTS
      (SELECT null 
       FROM tlp 
       WHERE
         (glob.mail = tlp.email or glob.mail is null)
         AND (glob.namel = tlp.namel or glob.mail is not null))