将具有相同字段的 3 table 中的数据插入到 1 table 中

insert data from 3 tables with the same fields into 1 table

我有 3 个 table(accntjobcostservic15)。全部具有相同的字段(codejnovendate)。我需要在一个查询中将这些 table 中的数据插入到另一个具有相同字段的名为 dummy 的 table 中。我该怎么做?

我已经使用了 3 个单独的 insert into 语句,如下所示:但我需要在一个语句中使用它:

INSERT INTO dbo.dummy
SELECT
dbo.jobcost.code,
dbo.jobcost.ven,
dbo.jobcost.date,
dbo.jobcost.jno,
FROM 
dbo.jobcost
WHERE
dbo.jobcost.jno > '40000'

INSERT INTO dbo.dummy
SELECT
dbo.servic15.code,
dbo.servic15.ven,
dbo.servic15.date,
dbo.servic15.jno
FROM 
dbo.servic15
WHERE

dbo.servic15.jno > '40000'

INSERT INTO dbo.dummy
SELECT
dbo.accnt.code,
dbo.accnt.ven,
dbo.accnt.date,
dbo.accnt.jno
FROM 
dbo.accnt
WHERE
dbo.jobcost.jno > '40000'

你需要INSERT ... SELECT声明

INSERT INTO dummy  (code, jno, ven, date)
  SELECT code, jno, ven, date FROM accnt jno > '40000'
  UNION ALL
  SELECT code, jno, ven, date FROM jobcost jno > '40000'
  UNION ALL
  SELECT code, jno, ven, date FROM servic15 jno > '40000';

如果尚未创建 table,请使用 CREATE TABLE ... SELECT

INSERT INTO dummy  (code, invno, ven, invdate, amon, accno, amnt, jno, saccno, ckno)
  SELECT code, invno, ven, invdate, amon, accno, amnt, jno, saccno, ckno FROM accnt 
  WHERE jno > '40000'
  UNION ALL
  SELECT code, invno, ven, invdate, amon, accno, amnt, jno, saccno, ckno FROM jobcost 
  WHERE jno > '40000'
  UNION ALL
  SELECT code, invno, ven, invdate, amon, accno, amnt, jno, saccno, ckno FROM servic15 
  WHERE jno > '40000';

这有效