SQL问题 - 如何投入表格?

时间:2009-12-04 07:26:49

标签: sql-server-2005

如何将此结果导入新表?

SELECT DISTINCT h.CustomerCode, h.BillName, h.BillAddress1
FROM hist2 h
WHERE NOT EXISTS
       (SELECT CustomerCode FROM tblCustomer c WHERE c.CustomerCode = h.CustomerCode)

4 个答案:

答案 0 :(得分:1)

像这样:

SELECT DISTINCT h.CustomerCode, h.BillName, h.BillAddress1
INTO NewTable
FROM hist2 h
WHERE NOT EXISTS
   (SELECT CustomerCode FROM tblCustomer c WHERE c.CustomerCode = h.CustomerCode)

答案 1 :(得分:1)

如果存在与您的字段匹配的表:

insert into mytable
   select Distinct h.CustomerCode, h.BillName, h.BillAddress1 From hist2 h where not exists (select CustomerCode From tblCustomer c Where c.CustomerCode=h.CustomerCode)

如果它与您的字段不匹配,则必须指定字段,如在所有插入中一样:

insert into mytable (customercode, billname...)
    select Distinct h.CustomerCode, h.BillName, h.BillAddress1 From hist2 h where not exists (select CustomerCode From tblCustomer c Where c.CustomerCode=h.CustomerCode)

如果表不存在,您想使用Select Into

答案 2 :(得分:0)

这适用于大多数SQL - 我不确定MS:

create table tablename
  select Distinct h.CustomerCode, h.BillName, h.BillAddress1
  From hist2 h
  where not exists (select CustomerCode
                     From tblCustomer c
                     Where c.CustomerCode=h.CustomerCode)

答案 3 :(得分:0)

如果您还没有桌子,例如如果要将其存储到全新的表中,可以在此处使用以下语法:

SELECT DISTINCT h.CustomerCode, h.BillName, h.BillAddress1
INTO dbo.NewTable
FROM hist2 h
WHERE NOT EXISTS
       (SELECT CustomerCode FROM tblCustomer c WHERE c.CustomerCode = h.CustomerCode)

只需添加“INTO(tablename)”子句 - 这就是全部!