我有两个略有不同的表:
表1:
Sm Reads Oct
Install, Date, Con, MT
表2:
Sm Reads
Contract, Inst, Read T, Date
新表:
Sm Reads Master
Contract, Install, Date, Type
代码:
CREATE TABLE [Sm Reads Master]
(
[Contract] FLOAT,
[Install] FLOAT,
[Date] DateTime,
[Type] NVARCHAR (3)
)
GO
INSERT INTO [Sm Reads Master]
SELECT *
FROM (
SELECT * FROM [Sm Reads Oct]
UNION
SELECT * FROM [Sm Reads]
) LU
我想它不喜欢这两个表有不同的字段名和不同数据的事实,所以甚至可以这样做吗?
感谢您的帮助。
(第一个表中的MT与第二个表中的读取T的数据相同)
答案 0 :(得分:1)
您需要相同的列名(和类型):
INSERT INTO [Sm Reads Master]
SELECT * FROM
FROM (
SELECT Contract = Con,
Install = Inst,
Date,
Type = MT
FROM [Sm Reads Oct]
UNION
SELECT Contract,
Install,
Date,
Type
FROM [Sm Reads]
) LU
答案 1 :(得分:1)
您的联合查询不正确.Column Name必须相同且序列相同
尝试以下脚本:
INSERT INTO [Sm Reads Master] ([Contract],[Install],[Date],[Type])
SELECT [Contract] = Con,
[Install] = Inst,
[Date],
[Type] = MT
FROM [Sm Reads Oct]
UNION
SELECT [Contract],
[Install],
[Date],
[Type]
FROM [Sm Reads]