[英]Creating a column for a View from two different tables (SQL Server)
I have three tables A, B and C in a SQL Server, B and C share the same foreign key ( AgencyID
) from A , and they also share one same column name: AgencyName
我在SQL Server中有三个表A,B和C, B和C共享来自A的相同外键( AgencyID
),并且它们还共享一个相同的列名: AgencyName
Table A : AgencyID
, AgencyDate
, 表A : AgencyID
, AgencyDate
,
Table B : AgencyID
, AgencyNumber
, AgencyName 表B : AgencyID
, AgencyNumber
, AgencyName
Table C : AgencyID
, AgencyInvoiceNumber
, AgencyName 表C : AgencyID
, AgencyInvoiceNumber
, AgencyName
To help you understand the relationship, basically these 3 tables are generated with Entity Framework , where B
and C
each extends A
. 为了帮助您理解这种关系,基本上这3个表是使用Entity Framework生成的,其中B
和C
分别扩展了A
Now I am trying to create one View from these three tables in the following way: 现在,我尝试通过以下方式从这三个表中创建一个视图:
CREATE VIEW [rdo].[Agencies] AS
SELECT
A_1.AgencyID, A_1.AgencyDate,
dbo.B.AgencyNumber, dbo.B.AgencyName,
dbo.C.AgencyInvoiceNumber, dbo.C.AgencyName
FROM dbo.A AS A_1 LEFT OUTER JOIN
dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
dbo.C ON A_1.AgencyID = dbo.C.AgencyID
Now, apparently the script errors about AgencyName
that column name in a view should be unique :( How would I go about this so that both AgencyName
values from B and C are merged into one view? 现在,显然有关AgencyName
的脚本错误是AgencyName
中的列名应该是唯一的:(我将如何处理,以便将B和C中的两个AgencyName
值合并到一个视图中?
Note: I know the example isn't perfect as one could just move the column to table A, but it's just a simplified example, in reality I really cannot simply move the column :( 注意:我知道这个例子并不完美,因为可以将列移动到表A,但这只是一个简化的例子,实际上我真的不能简单地将列:(
Reason for having to have duplicated columns in B and C: 必须在B和C中复制列的原因:
Here's the relationships: 这里是关系:
A
/ \
A1 A2
/ \
B C
A1 and A2 extends A, B and C extends A1 and A2 respectively. A1和A2分别延伸A,B和C分别延伸A1和A2。 AgencyName
is an unique attribute that only B and C has. AgencyName
是只有B和C具有的唯一属性。 I know a refactoring could be done here to try to get rid of this weird hierarchy, but this legacy project has already grown too big and I cannot afford the cost atm. 我知道可以在这里进行重构以摆脱这种怪异的层次结构,但是这个旧项目已经变得太大了,我负担不起atm的费用。
Give different names to those columns in the view whose name are same in your tables like below 为视图中与表中名称相同的那些列指定不同的名称,如下所示
CREATE VIEW [rdo].[Agencies] AS
SELECT
A_1.AgencyID, A_1.AgencyDate,
dbo.B.AgencyNumber, dbo.B.AgencyName AS AgencyName_B,
dbo.C.AgencyInvoiceNumber, dbo.C.AgencyName AS AgencyName_C
FROM dbo.A AS A_1 LEFT OUTER JOIN
dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
dbo.C ON A_1.AgencyID = dbo.C.AgencyID
CREATE VIEW [rdo].[Agencies] AS
SELECT
A_1.AgencyID, A_1.AgencyDate,
dbo.B.AgencyNumber, dbo.B.AgencyName as AgencyNameB,
dbo.C.AgencyInvoiceNumber, dbo.C.AgencyName as AgencyNameC,
FROM dbo.A AS A_1 LEFT OUTER JOIN
dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
dbo.C ON A_1.AgencyID = dbo.C.AgencyID
give alice name
to AgencyName
column. 将alice name
为AgencyName
列。 In your case the table dbo.table.AgencyName
will have a column called 'AgencyName' for Tables B
and C
. 在您的情况下,表dbo.table.AgencyName
将具有表B
和C
名为“ AgencyName”的列。 That is the problem. 那就是问题所在。 Give alicename to any one of the column or both. 将alicename赋予列中的任一者或同时赋予两者。
Try using COALESCE to pick the first non null AgencyName: 尝试使用COALESCE选择第一个非null的AgencyName:
CREATE VIEW [rdo].[Agencies] AS
SELECT
A_1.AgencyID, A_1.AgencyDate,
dbo.B.AgencyNumber,
COALESCE(dbo.B.AgencyName, dbo.C.AgencyName) As AgencyName,
dbo.C.AgencyInvoiceNumber
FROM dbo.A AS A_1 LEFT OUTER JOIN
dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
dbo.C ON A_1.AgencyID = dbo.C.AgencyID
You may face the issue where AgencyName is different in B & C in which case you will need a set of rules to follow. 您可能会遇到B&C中的AgencyName不同的问题,在这种情况下,您需要遵循一系列规则。 My recommendation is to only have AgencyName in one of the tables (Table A?) otherwise you face potential update discrepancies. 我的建议是仅在其中一个表(表A?)中使用AgencyName,否则您将面临潜在的更新差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.