获取唯一的列名以允许创建视图?

Getting unique column names to allow a creation of a view?

我目前正在尝试通过连接从两个表创建视图,我在这里使用了一些方法,但我仍然收到此错误:

Msg 4506, Level 16, State >1, Procedure vw_Employee, Line 3 [Batch Start Line 19]
Column names in each view or function must be unique. Column name 'empNumber' in view or function 'vw_Employee' is specified more than once.

我的看法如下:

create view vw_Employee as
(
    select * 
    from Employees e1 
    inner join EmpInfo e2 on e2.empNumber = e1.empNumber
)

这是因为两个表都有 empNumber 列,而您正在使用 * 到 select。 select 中的列名中不应有任何 ambiguity/collisions。假设唯一的公共列名是 empNumber.

create view vw_Employee as
(
select * from Employees e1 inner join EmpInfo e2 
using (empNumber)
)

我建议您明确列出所需的所有列名称。

create view vw_Employee as
(
select e1.empNumber, . . .
from Employees e1 inner join EmpInfo e2 
on e2.empNumber = e1.empNumber
)

编辑:

根据您的评论,尝试:

create view vw_Employee
as
(
        select e1.empNumber,
            e1.firstName,
            e1.lastName,
            e1.ssn,
            e1.job,
            e1.projectId,
            e2.[address],
            e2.[state],
            e2.zip,
        from Employees e1
        inner join EmpInfo e2 on e2.empNumber = e1.empNumber
        )

您必须在 select 查询中写入列名并且只取 ​​e1.empNumber