当表具有相同的结构时,如何使用视图跨越多个表
How to span mulitiple tables with a view, when the tables have same structure
所以我们有一个接收常量输入的数据库,然后我们将它在 x 天后作为 archive/reporting 类型的数据库移动到另一个数据库。所以问题是当我尝试创建一个合并两个表(每个数据库一个)的视图时:
CREATE VIEW Table_Full AS
SELECT *
FROM [Front].dbo.[DATA] AS A
Inner Join Back.dbo.DATA AS B
ON A.DATETIME=B.DATETIME
我收到以下错误:"Column names in each view or function must be unique. Column name 'PARTNO' in view or function 'Table_Full' is specified more than once."
那么,有没有更好的方法来完成我想做的事情?
您必须指定每个字段并为重复的字段名称提供别名,而不是星号:
CREATE VIEW Table_Full AS
SELECT
a.ID as A_ID,
b.ID as B_ID,
a.Thingy as A_Thingy,
a.Etcetera as Atcetera
FROM [Front].dbo.[DATA] AS A
Inner Join Back.dbo.DATA AS B
ON A.DATETIME=B.DATETIME
鉴于您的用例,我认为您需要联合。即
select * from table_a
Union all
Select * from table_b
所以我们有一个接收常量输入的数据库,然后我们将它在 x 天后作为 archive/reporting 类型的数据库移动到另一个数据库。所以问题是当我尝试创建一个合并两个表(每个数据库一个)的视图时:
CREATE VIEW Table_Full AS
SELECT *
FROM [Front].dbo.[DATA] AS A
Inner Join Back.dbo.DATA AS B
ON A.DATETIME=B.DATETIME
我收到以下错误:"Column names in each view or function must be unique. Column name 'PARTNO' in view or function 'Table_Full' is specified more than once."
那么,有没有更好的方法来完成我想做的事情?
您必须指定每个字段并为重复的字段名称提供别名,而不是星号:
CREATE VIEW Table_Full AS
SELECT
a.ID as A_ID,
b.ID as B_ID,
a.Thingy as A_Thingy,
a.Etcetera as Atcetera
FROM [Front].dbo.[DATA] AS A
Inner Join Back.dbo.DATA AS B
ON A.DATETIME=B.DATETIME
鉴于您的用例,我认为您需要联合。即
select * from table_a
Union all
Select * from table_b