在 sql 服务器 "names in each view or function must be unique" 中创建视图

Create view in sql server "names in each view or function must be unique"

我正在尝试在 sql 服务器 2012 中创建视图,但我收到此错误消息

Column names in each view or function must be unique. Column name 'Lastname' in view or function 'vFileImportDPERManagers' is specified more than once.

这是我的视图代码:

    GO



CREATE VIEW [dbo].[vFileImportDPERManagers]
    AS
SELECT M.*,
Managers.*

FROM            dbo.vFileImportDPERDebtors M WITH (NOLOCK)
CROSS APPLY 
(
    SELECT
        M1.Man.value('@j11','varchar(20)') as ManagerKey,
        M1.Man.value('@j12','varchar(50)') as Lastname,
        M1.Man.value('@j13','varchar(50)') as Firstname,
        M1.Man.value('@j14','numeric') as ManagerFunctionId


    FROM DebtorXml.nodes('d1/d36/t324/j1') as M1(Man)
) as Managers
GO

在您的情况下,table dbo.vFileImportDPERDebtors 将有一个名为 'Lastname' 的列。如果您 select 按名称列出所有列(即将 * 扩展到名称中),您可以为一个或两个姓氏列添加别名,SELECT M.Lastname as Lastname1, Managers.Lastname as Lastname2...。通常认为最好不要使用 'SELECT *'.